I have tested hostname on several servers (RedHat, Ubuntu), and hostname -f has proven unreliable, returning sometime the short name only (as described in this question).

I can see the fqn in the aliases: hostname -a (one of the aliases is the fqn I look for), but the order of the aliases is not fixed.

Is there another way to get the fully qualified name reliably, and store it in a bash variable?

  • If sometimes the CNAME is what you are looking for and not the A-record, how do you know which is the "right" FQN? – Nils Mar 7 '12 at 20:51

I tried this on CentOS5:

host -TtA $SERVERNAME|grep "has address"|awk '{print $1}'

I have to query my DNS in TCP-Mode. If UDP works in your environment leave away the "T" option.

Note: on an Ubuntu guest (VirtualBox) it won't work:

git@aHostname:~/$ host -TtA $(hostname -s)
Host aHostname not found: 3(NXDOMAIN)

So to cover all the cases:

fqn=$(host -TtA $(hostname -s)|grep "has address"|awk '{print $1}') ; \
if [[ "${fqn}" == "" ]] ; then fqn=$(hostname -s) ; fi ; \
echo "${fqn}"
  • 1
    I got host -TtA $(hostname -s)|grep "has address"|awk '{print $1}' working on my redhat servers – VonC Mar 8 '12 at 16:08
  • This works on mac osx 10.11.6, though giving one line per IP, but not on osx 10.12.1. I'm not sure if it fails on v12 due to my machine's setup or inherently. – AnneTheAgile Nov 22 '16 at 20:56
  • Update; for mac using AD LDAP, easiest way is to parse: dsconfigad -show – AnneTheAgile Nov 22 '16 at 21:18

Here is one way I found the fqn:

fqn=$(nslookup $(hostname -i)) ; fqn=${fqn##*name = } ; fqn=${fqn%.*} ; echo $fqn

In other words, I had to nslookup the IP address, which gives me something like:

Server:         128.xxx.yyy.zzz
Address:        128.xxx.yyy.zzz#ww

aa.bb.cc.dd.in-addr.arpa        name = myserver.fully.qualified.name.

From there, it was just a question of removing what I didn't want, through bash string manipulations.

Maybe there is a simpler way?


Your method works only if you have correctly configured your hostname, and have the correct dns settings, etc. If you have all that, then you already know your fqdn.

There is no reliable way to get the fqdn. A single IP address can have several fqdn, and a single fqdn can have several IP addresses... and lots of IP don't have any fqdn at all.

If your machine is directly connected to the internet, just use the IP address and make a reverse DNS request: host
But this very often don't give you the desired answer (just try with google for example).

  • True, but in my context (world-wide enterprise), all my servers have a fqn, and have correct dns settings. The servers I need should have only one fqdn. – VonC Mar 7 '12 at 13:43
  • So parse the answer of host to get only the A-record (or make an according lookup). – Nils Mar 7 '12 at 20:52
  • @Nils: that seems a good suggestion. Could you make it an answer, for me to upvote and (after test) accept? – VonC Mar 8 '12 at 7:20

Doesn't hostname --fqdn work for you?

  • No, as clearly stated in the question. On the servers I have to get the FQDN information, hostname --fqdn returns the short name, with the fqdn defined as one of the hostname aliases. – VonC Mar 7 '12 at 14:58
  • Oh right, I didn't realise -f and --fqdn are the same thing. – ThatGraemeGuy Mar 8 '12 at 15:40

For cross-platform alternative, if you don't mind using python (runs at least on python 2.7 and python 3)

python -c 'import socket; print(socket.getfqdn())'

In Ubuntu, you can do hostname -A


An OS X safe bash only method, which likely won't work other *nix flavors due to variations in command output:

fqn=$(nslookup $(hostname) | sed -n s/Name:.//p);

Python is likely your best bet for broad compatibility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.