Change hostname on ubuntu

Changing the hostname on an ubuntu machine

Bibliography

Ubuntu forum: http://askubuntu.com/a/9614

Stuff to know

The known hosts file

The file with the known hosts for the machine it’s /etc/hosts. It’s used in conjunction with DNS to resolve names into IP address.

The machine name it’s used to look back to 127.0.0.1 as well as localhost.

The hostname file

The file with the hostname it’s /etc/hostname. It contains just the hostname and it’s used around the system.

Wrapping it all up

We need to change all the occurrences of the current hostname with the hostname we want in the two files mentioned before.

I want to change a clone of my ubuntu base VM image name from ubuntu-base-vm to ubuntu-docker.

Known hosts

The original /etc/hosts file:

Must be edited to look like this:

Hostname

The /etc/hostname file it’s pretty easy to edit because it contains just one line whith the hostname. So the original file:

Must be edited like this:

Checking everything is fine

Reboot the machine entering the following command:

sudo reboot

You should see the login prompt reflect the changes made like this:

Ubuntu 16.04.1 LTS ubuntu-docker tty1

IP Address: 192.168.1.5
ubuntu-docker login:

Login onto the machine and ping it by name using the following command:

ping ubuntu-docker -c 2

The output should be nice with 0% packet lost and look like this:

PING ubuntu-docker (127.0.1.1) 56(84) bytes of data.
64 bytes from ubuntu-docker (127.0.1.1): icmp_seq=1 ttl=64 time=0.013 ms
64 bytes from ubuntu-docker (127.0.1.1): icmp_seq=2 ttl=64 time=0.020 ms

--- ubuntu-docker ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.013/0.016/0.020/0.005 ms

That’s it.