Debian Hostname refusing to stick

Solution 1:

Roughly speaking, hostname shows what your computer thinks it's called, whereas hostname -f shows what the world thinks your computer is called. Changing /etc/hostname affects what hostname displays. To change what hostname -f display, you should look into your network setup, specifically at your DNS setup.

Here's a relevant extract from the hostname(1) man page:

You can't change the FQDN (as returned by hostname --fqdn) or the DNS domain name (as returned by dnsdomainname) with this command. The FQDN of the system is the name that the resolver(3) returns for the host name.

Technically: The FQDN is the name gethostbyname(2) 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 (usually in /etc/host.conf) how you can change it. Usually (if the hosts file is parsed before DNS or NIS) you can change it in /etc/hosts.

Depending on what you want to use $(hostname -f) for, you should either edit /etc/hosts or configure the DNS server running on your host machine or network to know about ayil. Editing /etc/hosts is the simplest path to having hostname -f show ayil, but if there is a purpose to running hostname -f as opposed to hostname, it's likely that you want the information to be consistent with some information stored outside this machine.

Solution 2:

Your current changes won't apply until you reboot the system since kernel itself has hostname and domain name in mind. To make things properly I would advise to perform these simple steps (some of them you have already done) on the host example.domain.com:

  • Edit /etc/hostname to contain your hostname (w/o domain name) example
  • Edit /etc/hosts to match your new hostname. Otherwise you'll get errors for host lookup during sudo or other system operations. It's normally to map your hostname to loopback. Remember to put FQDN first and then just the hostname. Part of this file can look like:
127.0.0.1 example.domain.com example localhost.localdomain localhost

This step can be omitted if DNS lookup will point on this server when prompting for example.domain.com record.

  • Update the kernel hostname and domainname records:
sudo sysctl -e kernel.hostname=example
sudo sysctl -e kernel.domainname=domain.com

The next time you log in your shell prompt will show you the new example.domain.com hostname.

Solution 3:

Have you checked to make sure DHCP isn't setting your hostname? Do 'ps aux | grep dhclient' to see if a DHCP client is running. If it is, you can doublecheck if the DHCP server is assigning the hostname by looking at the DHCP lease in /var/lib/dhcp/. Find the lease file there of your interface (probably dhclient.eth0.leases if you're using eth0). cat the file, and you might find that the host-name and domain-name dhcp options are set.

For example, my hostname at the prompt is 'dhcpuser-82', with the full domain name being "dhcpuser82.dh.example.com". My /var/lib/dhcp/dhclient.eth0.leases file looks like this:

...blah blah
option host-name "dhcpuser-82";
option domain-name "dh.example.com";
... more blah blah

If so, this is the origin of your frustrations. Either stop using dhcp and go static (which, if you do, don't forget to issue a kill to the running dhclient since a network restart won't automatically kill it, and you'll find things back the way they were the following day), or check google for ways to bypass the host-name and domain-name DHCP options. BTW, don't try to edit the leases file to correct the problem. It'll just get overwritten the next time dhcp updates the lease, or worse it wont be able to renew your lease and your host will lose it's ip.