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>


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 and contain your previous hostname.

For example, on Debian this looks like: previoushostname previoushostname localhost

Some other distro use: localhost previoushostname

others use localhost 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)