18

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?

1
  • 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, 2012 at 20:51

8 Answers 8

12

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}"
3
  • 1
    I got host -TtA $(hostname -s)|grep "has address"|awk '{print $1}' working on my redhat servers
    – VonC
    Mar 8, 2012 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. Nov 22, 2016 at 20:56
  • Update; for mac using AD LDAP, easiest way is to parse: dsconfigad -show Nov 22, 2016 at 21:18
8

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?

7

Doesn't hostname --fqdn work for you?

2
  • 1
    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, 2012 at 14:58
  • Oh right, I didn't realise -f and --fqdn are the same thing. Mar 8, 2012 at 15:40
5

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).

3
  • 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, 2012 at 13:43
  • So parse the answer of host to get only the A-record (or make an according lookup).
    – Nils
    Mar 7, 2012 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, 2012 at 7:20
4

In Ubuntu, you can do hostname -A

3

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())'
0
host `hostname -I` | cut -d' ' -f5

hostname -I gives the raw ip address, then host looks up the details including the full address, then cut cleans up and gets the correct column (for me on debian/bash, your milage might vary!) - you can use awk -NF I think as a probably tidier alternative.

-1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .