Posted by tony on November 22, 2017
How to Change Hostname in Ubuntu 16.04
For those who pay attention to the little details, the default hostname in Ubuntu 16.04 can be a constant source of irritation. Something like 'dell-laptop-3344' just won't do for a hostname when you're used to more sensible and exotic stuff. So, how do you get back control (and sanity)?
Changing the hostname is one of the simplest things you can do in Ubuntu 16.04. The hostname is simply looked up from a file called /etc/hosts, and in order to change it, you guessed it, we just need to change that file.
With that in mind, go to the terminal and do this:
$ sudo gedit /etc/hosts
This command opens the /etc/hosts file in the default text editor (which is called Gedit in Ubuntu). We add 'sudo' here because it's a system-level file and is not in the reach of regular users for change.
After you provide your root password, you'll see the file having entries like these:
See the culprit there? The name 'dell-laptop-3344' is linked to the IP address 127.0.0.1, which is the same machine you're working on. Just change this name to the one you want (how about 'overlord'?!) and save it. Close this file, restart your system, and you're good to go!
A small aside, by the way: Notice the line '127.0.0.1 localhost' in the file /etc/hosts? That maps the name 'localhost' to the machine you're on. That's what allows you to type 'http://localhost' and be able to visit the web server of the machine you're on.
Changing the Hostname in Ubuntu 16.04 Without Restarting
Well, what if you don't want to restart your system and yet change the hostname? Is there a command for that? There is, but unfortunately, it remains effective only in the current session. You can do:
$ sudo hostname overlord
to change your hostname to 'overlord' instantly. And once you do the same in the /etc/hosts file, you'll end up with the best of both worlds: your hostname will be changed instantly, and the new (same) hostname will be read from /etc/hosts the next time you start up.