0

In Linux, I can change the hostname as follows:

sudo hostname <hostname>

Additionally, I can just change the hostname in the /etc/hostname and /etc/hosts file.

My question is: If I change the hostname in the files, do I still need to call sudo hostname <hostname> or not? What does this command do in addition to change the hostname in the /etc/hostname file?

1 Answer 1

3

The "active" hostname is actually stored in the kernel. Programs don't use /etc/hostname directly; the file is only read once at boot time to initialize the kernel hostname.

Specifically, on Linux, the 'hostname' command will set the sysctl parameter kernel.hostname (by writing to /proc/sys/kernel/hostname), and all programs will look there for the system's current hostname.

Most likely it works this way for historical reasons – Unix programs have used uname() for a long time to get the current hostname together with the OS version, and uname() has been a kernel syscall for a long time, so even though the kernel doesn't do much with the hostname it'll still store and return it.

1
  • 2
    I think to set hostname, the sethostname syscall is made, as seen by strace. Commented Feb 23, 2022 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .