How to Change the Hostname on Ubuntu

Whenever you log into your server, you’ll notice a command language interface (CLI) prompt starting with user@hostname. This tutorial shows you how to change that hostname.

Matthias Hagemann
2 min readSep 15, 2018

This tutorial was originally published on Gatemill:

Type the following command to change the hostname with immediate effect:

$ sudo hostname <new-hostname>

If you re-login to the CLI, you’ll be greeted with that new hostname you have set.

Additional Configuration Files to Edit

There are additional configuration files to ensure that the hostname is changed across the board. It has been saved in /etc/hostname and /etc/hosts, as well.

$ sudo nano /etc/hostname

In the text editor, update the hostname to the one above and save by existing and confirming with Yes.

Let’s proceed to the /etc/hosts file:

$ sudo nano /etc/hosts

Change 127.0.1.1 <current-hostname> to 127.0.1.1 <new-hostname> and save the file.

FQDN Not Written Out by Default

The CLI prompt only shows up to the first ‘.’ using the default setup. If you change your hostname to a fully qualified domain name (FQDN) and expect it to show on the CLI prompt, it won’t without a change.

In case you want the FQDN shown, change the prompt to \H in .bashrc. To find out how to do it exactly, check out this tutorial.

--

--

No responses yet