Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

4 Answers

up vote 1 down vote accepted

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}"
share|improve this answer
    
I got host -TtA $(hostname -s)|grep "has address"|awk '{print $1}' working on my redhat servers –  VonC Mar 8 '12 at 16:08
    
@VonC thanks for your Ubuntu addition. –  Nils Mar 31 '12 at 20:10
add comment

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 1.2.3.4
But this very often don't give you the desired answer (just try with google for example).

share|improve this answer
    
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
add comment

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?

share|improve this answer
add comment

Doesn't hostname --fqdn work for you?

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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