Ubuntu / Linux news and application reviews.

Ubuntu hostname

If you need to change the computer name (hostname) in Ubuntu for whatever reason, you won't find a System Settings option to do it so you must edit two configuration files. But don't worry, it's an easy task!

How to change the hostname (computer name) in Ubuntu

To change the hostname in Ubuntu, you must edit 2 files. So open a terminal and run the following commands:

1. Optional: make a backup of your existing 'hostname' and 'hosts' files (it's a good idea because if you don't change the hostname properly, you might get some gksu / sudo issues):
sudo cp /etc/hosts /etc/hosts.old
sudo cp /etc/hostname /etc/hostname.old

2. Edit the 'hosts' file:
gksu gedit /etc/hosts

The file should look like this:       localhost       some_hostname

Here, change "some_hostname" to the new hostname / computer name.

3. Edit the hostname file:
gksu gedit /etc/hostname

This file only contains your hostname so change it again to what you like, but make sure it's the same as in the hostname in the /etc/hosts file.

4. Now restart the hostname and networking services:
sudo service hostname restart
sudo service networking restart

Your new computer name / hostname should now show up instead of the old one. After this, log out and log back in to avoid some issues like "gksu" not working.

Instead of the last two commands above, you can also simply restart your computer.

Reverting the changes

Since we've made a backup, reverting the changes is easy: all you have to do is copy the backup files and replace the new ones:
sudo cp /etc/hosts.old /etc/hosts
sudo cp /etc/hostname.old /etc/hostname

In case you're having issues with "sudo", use "gksu" instead.