Ubuntu – How to change HOSTNAME on an Ubuntu server

hostnameUbuntu

I'm attempting to change the hostname on my shared server with Slicehost so I can setup Postfix as a null client.

I edited /etc/hosts
and after reboot, the hostname is still incorrect.
What am I doing wrong?

username@mail Fri Jul 01 13:01:32 ~   
$ sudo cat /etc/hostname  
mail.domain1.com  

username@mail Fri Jul 01 13:01:45 ~   
$ cat /etc/hosts  
127.0.0.1     localhost localhost.localdomain  
208.78.100.198  mail.domain1.com   

username@mail Fri Jul 01 13:02:13 ~   
$ hostname -f  
pop.where.secureserver.net  

I also intend to add another domain to this server, how do I configure this correctly.

Best Answer

Try setting the second line in hosts to 208.78.100.198 mail.domain1.com mail. Also, rebooting is not necessary: the FQDN isn't stored anywhere, it's looked up on-the-fly.

I believe that the FQDN is found by doing a DNS query on the server's IP address, then taking the first response, which is why this works. It should be possible to set the FQDN without modifying hosts by using only DNS (or NIS, or LDAP, or wherever else nsswitch.conf looks for hosts) but if it's in the hosts file there's no need for network traffic that might time out or otherwise cause problems in an emergency.

More information Here's everything that happens (this is Linux-only, other OSes may and will do things somewhat differently):

hostname uses the gethostname(2) function specified in unistd.h to obtain the hostname. This function is implemented as a call to uname(2), where the nodename field of the structure is extracted and returned. This will have been set earlier in the boot process with the corresponding sethostname(2) call, since the kernel has no other way of knowing the host's name. In your case, it will be mail. (By the way, this is what you should have in /etc/hostname).

This is then resolved using the standard system resolver, which will be either getaddrinfo(3) or gethostbyname(3) depending upon the vintage of your system utils. The field h_name, defined as "official name of host", gets returned. I believe this corresponds to the first name listed in a hosts file.

As for your multiple domains question, hostname is not able to deal properly with multiple names. Every interface (even virtual ones) can have its own IP and sometimes even multiple physical interfaces can share an IP.

You should ensure that each IP attached to your host resolves via reverse-IP lookups to the appropriate name and configure your mail server properly.