Skip to content

Change a machine's hostname

Actually change the hostname

Using hostnamectl is the best way to change it, editing /etc/hostname directly is also possible, but not recommended.

hostnamectl set-hostname <newhostname>

Example:

hostnamectl set-hostname thisismyserver

Update the network "pointer"

Even though the hostname is changed now, the networking still uses the old hostname.

So you need to edit the hosts file:

sudo nano /etc/hosts

Then find the 2 lines that starts with 127.0.1.1 and contain your previous hostname.

For example, on Debian this looks like:

127.0.1.1 previoushostname previoushostname
127.0.0.1 localhost

Some other distro use:

127.0.0.1 localhost previoushostname

others use

127.0.0.1 localhost
127.0.1.1 previoushostname

What's used really doesn't matter, what matters is that you just have to replace the previous hostname with the new one.

The end

It is recommended that you reboot after that, however it isn't mandatory.

Your shell might still display your old hostname so you will have to restart it (so either reconnect the SSH connection, or close the terminal)