Why doesn't "hostname --fqdn" work on my Ubuntu computer?

Solution 1:

Can you provide the content of /etc/nsswitch.conf ?

It looks like /etc/nsswitch.conf has a bad value for the "hosts" line. Does it start with "files" ?

Otherwise the FQDN is set by editing /etc/hosts and putting the FQDN on the line where the hostname appears. Suppose you have an hostname "foo", and you find a line:

127.0.0.1 foo

You would edit it like this:

127.0.0.1 foo.localdomain foo

foo.localdomain would be your new FQDN.

Solution 2:

edit /etc/hosts to add your FQDN

Information on syntax located here: http://www.faqs.org/docs/securing/chap9sec95.html

update: reading over your question again it almost sounds like you either don't have your path set right, or there is something wrong with the hostname program.

do 'which hostname'

it should return with the path '/bin/hostname'

if that works try the command again like,

'/bin/hostname --fqdn'

Solution 3:

Unlike the simple hostname command invocation, the invocation hostname --fqdn will attempt to do a few more things, which will often result in some DNS lookups.

Take for example the following (successful) invocation (this is from a Red Hat 6 box which had 'hostname' from the 'net-tools' package; newer machines, such as RHEL7, have a different implementation which lacks the --verbose option):

# hostname --fqdn --verbose
gethostname()=`myserver.example.com'
Resolving `myserver.example.com' ...
Result: h_name=`myserver.example.com'
Result: h_addr_list=`10.1.2.3'
myserver.example.com

Note the very helpful --verbose option.

In short, anything other than a simple hostname is probably doing more than you expect. Here's another example:

# hostname --ip --verbose
gethostname()=`myserver.example.com'
Resolving `myserver.example.com' ...
Result: h_name=`myserver.example.com'
Result: h_addr_list=`10.1.2.3'
10.1.2.3

And to round it off:

# hostname --verbose
gethostname()=`myserver.example.com'
myserver.example.com

Note that the hostname of a system (as returned by gethostname) can me an unqualified hostname, such as just 'myserver'. This is why the program you are wanting to install is using the hostname --fqdn instead.

The error message hostname: Name or service not known comes from the resolver functions: these are the parts of the system library that translate between names and addresses (typically DNS names and IP addresses).

Actually, the resolver does more than just DNS (and more than just translating between hostnames and IP addresses); its behaviour is configured in part by the file /etc/nsswitch.conf, and typically it will consult the following, typically in this order:

  • 'hosts' (on Linux, the means /etc/hosts)
  • (sometimes) nscd (name-service caching daemon)
  • 'dns'

(note, you can also have a caching DNS server such as dnsmasqd --- for the point of the above, that is still under the 'dns' mechanism).

It is worth pointing out that tools such as dig, host and the venerable nslookup do not follow this order; they are explicitly DNS querying tools. This means that if you rely on them (in a script for example) you may end up getting a different result than what regular client programs would (that use the system resolver). For this reason, use the getent program in scripts, particularly if you have a caching component such as nscd running.

# getent hosts myserver.example.com
10.1.2.3    myserver.example.com

So the key takeaway here is that a) if you have /etc/hosts well configured with an entry for your own machine, and b) your /etc/nsswitch.conf has the usual configuration -- hosts: files dns in that order, then c) even if you don't have DNS well-configured in your environment, then hostname --fqdn should work.

In a well-configured DNS, you would be expected to have one 'reverse' address (a "PTR record") that gives the 'canonical' name of your server, and that name should also be able to be looked up (an "A record" for IPv4).

Short version: (for RHEL6 and similar ages) add --verbose; it will point you to what you are lacking. For newer platforms, you may need to just do the reverse and forward look-ups manually.

Hope that helps you to understand what is going on.