10

As I read in man page, it is recommended to put hostname(without domain name) to /etc/hostname. For example, host instead of host.domainsub.domaintld. But, as I know, some software relies on having FQDN in /etc/hostname.

What should one specify in /etc/hostname on Debian/Ubuntu machines?

If not FQDN, where FQDN should be specified instead?

I know, question is a bit old, but didn't found clear answer. Some people say hostname, some - FQDN. Thanks.

  • 1
    Where are you finding people that are saying that you put the FQDN in the /etc/hostname file? That file is only for the hostname of the system. – Terrance Dec 21 '16 at 17:47
  • When reading material about it on the internet met somewhere. And where one should specify his FQDN? – Bulat M. Dec 22 '16 at 6:02
  • @Terrance, actually I want to get answers from experienced people in the field so they show how to do it "right" from the beginning. Write as an answer. And what to do in distributions that don't have /etc/hostname? – Bulat M. Dec 22 '16 at 11:14
6

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

  • 1
    FIX: Thanks, however it would be to know about pros and cons of specify/not specifying of FQDN in /etc/hostname. I know about your solution: it is a stub for programs that want to get FQDN resolved to something(does not matter to what address) to work. – Bulat M. Dec 21 '16 at 17:35
10

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:

enter image description here

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.

  • why do you map FQDN in /etc/hosts to 127.0.1.1? – Bulat M. Dec 23 '16 at 13:42
  • 1
    @BulatM. Please see my revised answer. – Terrance Dec 23 '16 at 15:08
  • could you please explain difference between search and domain keywords in /etc/resolv.conf? I am reading man page of resolv.conf, still not very clear. – Bulat M. Dec 24 '16 at 15:59
  • And could one include them both? Could you present useful valid case in answer? Thanks. – Bulat M. Dec 24 '16 at 16:00
  • 1
    @BulatM. Click on the links in my answer, and they take you to more information about things. Like en.wikipedia.org/wiki/Resolv.conf gives much more information. I am here to help, not to teach you. I don't get paid, as this is all volunteer, so please do some studying of your own. – Terrance Dec 24 '16 at 18:13
4

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.

  • Is it(inclusion of 127.0.1.1 ursula.example.com ursula in /etc/hosts) Debian\Ubuntu specific or could be applied equally well to any other distr? – Bulat M. Dec 23 '16 at 13:43
  • 1
    Dunno about other distros. You can do the same in Arch and Cent OS, though. Questions about other distros should be asked at Unix & Linux. – muru Dec 23 '16 at 13:56

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.