Should one use FQDN in /etc/hostname instead of hostname?
Revised Answer:
The host itself does not handle the actual FQDN. That is handled by the DNS. FQDN (Fully Qualified Domain Name) is handled by DNS translating names into IP addresses. Using the /etc/hosts
file, you are essentially overriding the DNS server. The computer looks to the /etc/hosts
file first to see if an entry is defined for a hostname to IP address. The entries in the /etc/hosts
looks like the following:
127.0.0.1 localhost
127.0.1.1 terrance-ubuntu.local terrance-ubuntu
These entries are not distro specific. All OSes use the same format for these lines. Just the location of the hosts
file changes. Linux, typically it is located in the /etc/
folder, where in Windows it is typically located in C:\Windows\System32\drivers\etc\
folder.
Breaking that line up, you can see that I am assigning both terrance-ubuntu.local
, which is my FQDN itself to override DNS so that applications know not to leave my computer, and terrance-ubuntu
, which is the hostname, again so that applications know not to leave my computer or 127.0.0.1 (localhost). Assigning my hostname of my system to 127.0.1.1
has no effect on the rest of the computers finding my host on the network. If DNS is working properly, they will see my hostname as 10.0.0.100
. The reason for using 127.0.1.1
is for my applications to find my system quicker since it will know that my system is not out somewhere else on my network. My actual hostname with the .local
being my FQDN, the .local
is actually my Domain that I setup via my router which is also another DNS server on my network.
Now, let's say that the DNS services on the local network are not assigning hostnames or FQDNs to IP addresses, but yet you know what the IP address of the host on the local network is. You would then assign that host in your /etc/hosts
file so that you don't need to type in the IP address of the host every time you want to access it. The host might be a tool server, printer, or some other network connected system. Add the entry like you would normally to the /etc/hosts
file.
I am going to use my network connected printer for example. It has a static IP of 10.0.0.253. I don't know the name of it though. For this, I want to call it hp_printer
. I will ping the IP and the hostname for it, then add to /etc/hosts
.
terrance@terrance-ubuntu:~$ ping -c 2 10.0.0.253
PING 10.0.0.253 (10.0.0.253) 56(84) bytes of data.
64 bytes from 10.0.0.253: icmp_seq=1 ttl=255 time=0.326 ms
64 bytes from 10.0.0.253: icmp_seq=2 ttl=255 time=0.334 ms
terrance@terrance-ubuntu:~$ ping -c 2 hp_printer
ping: unknown host hp_printer
terrance@terrance-ubuntu:~$ sudo vi /etc/hosts
10.0.0.253 hp_printer.local hp_printer
terrance@terrance-ubuntu:~$ ping -c 2 hp_printer
PING hp_printer.local (10.0.0.253) 56(84) bytes of data.
64 bytes from hp_printer.local (10.0.0.253): icmp_seq=1 ttl=255 time=0.334 ms
64 bytes from hp_printer.local (10.0.0.253): icmp_seq=2 ttl=255 time=0.303 ms
Now, I can also access the webpage for my printer setup at the name I gave it instead of the IP address which could be easier to remember:
Your /etc/resolv.conf file is also used by DNS to help find hostnames. It is the configuration file for the resolver. It provides the search domain so that you don't have to specify your FQDN all the time when you're looking for a host. It also supplies the IP address for the DNS or nameserver of your local network. The search
line below shows the name local
which is my domain name.
terrance@terrance-ubuntu:~$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.0.0.1
search local
Hopefully this helps give a better understanding of how DNS and FQDNs work.
In the /etc/hostname file you use only the hostname. The FQDN you can set on: /etc/hosts that might look like:
127.0.1.1 thishost.mydomain.org thishost
According to manual of the hosts file.
http://man7.org/linux/man-pages/man5/hosts.5.html
The manpage of hostname(1)
discusses this (the paragraph in bold is emphasised by me):
THE FQDN The FQDN (Fully Qualified Domain Name) of the system is the name that the resolver(3) returns for the host name, such as, ursula.example.com. It is usually the hostname followed by the DNS domain name (the part after the first dot). You can check the FQDN using hostname --fqdn or the domain name using dnsdomainname. You cannot change the FQDN with hostname or dnsdomainname. The recommended method of setting the FQDN is to make the hostname be an alias for the fully qualified name using /etc/hosts, DNS, or NIS. For example, if the hostname was "ursula", one might have a line in /etc/hosts which reads 127.0.1.1 ursula.example.com ursula Technically: The FQDN is the name getaddrinfo(3) returns for the host name returned by gethostname(2). The DNS domain name is the part after the first dot. Therefore it depends on the configuration of the resolver (usually in /etc/host.conf) how you can change it. Usually the hosts file is parsed before DNS or NIS, so it is most common to change the FQDN in /etc/hosts.
And if you give a FQDN as the hostname during installation, it writes only the fist component to /etc/hostname
and keeps the FQDN in /etc/hosts
.
So, in this case, just let the installer do what it does.