This tutorial is for users who already have a server running the Ubuntu operating system.

The hostname is a name given to your system to identify it on a network. It should be unique on your network, and it is customizable. Setting a custom hostname allows you to create a memorable or descriptive name, to make it easier to find different servers on the network.

This guide is for Ubuntu 18.04, but the same steps also work for Ubuntu 16.04. Now, let’s learn how to change a hostname on Ubuntu.

header for article on changing hostname in ubuntu

Prerequisites

  • Server running Ubuntu 16.04 or 18.04
  • Access to an Ubuntu user with sudo privileges
  • Access to a command prompt (Ctrl-Alt-T)
  • A text editor, such as nano, included by default

Changing Hostname in Ubuntu

Step: 1 Find the Current Hostname

To view the current hostname, open a terminal window, and enter the following command:

hostnamectl

The system will return the static hostname, plus additional information. The static hostname is the hostname of the system you’re working on.

Step 2: Change Ubuntu Hostname

Change the hostname with hostnamectl command

You can easily change the hostname using options with the hostnamectl command:

sudo hostnamectl set-hostname file_server

This will rename your server file_server. You can change it to any name you like. You can verify the change by re-running the hostnamectl command.

Change hostname by editing hosts file

The /etc/hosts file is an older solution from when servers needed a configuration file to translate a memorable name to an IP address.

You can change your hostname by editing the configuration files, but there are three files to change.

First, open the /etc/cloud/cloud.cfg file:

sudo nano /etc/cloud/cloud.cfg

Look for a line that says:

preserve_hostname: true

If it’s already set to true, leave it alone. If it’s set to false, change it to true. Save the file and exit.

Note: Your system may or may not have the /etc/cloud/cloud.cfg file. You can check your system by entering cd /etc/cloud > then type ls. The system will give you an error if the directory doesn’t exist. If the file is present, the ls command will show it in the file list.

Next, edit the /etc/hosts file. Before you make the change, you’ll want to know your IP address. If you don’t, you can find it quickly by entering:

ip a

The inet value is your IP address. Edit your hosts file as follows:

sudo nano /etc/hosts

You should see a line at the very top that says:

127.0.0.1 localhost

Add a second line just below it, as follows (replace this IP address with yours):

10.0.2.15 file_server

Save the file and exit.

Finally, edit the /etc/hostname file:

sudo nano /etc/hostname

This file will display the current hostname. Replace the current entry with the hostname of your choice:

file_server

Then save the file and exit.

Step 3: Verify Changes

To verify that the hostname was successfully changed, restart your system.

To verify the changes, run the command: hostnamectl

You should now see your new server name on the console.

Conclusion

This guide provides two different methods to change Ubuntu server hostname without restarting.

Hostnames make networking a lot easier, by giving servers easier names to remember.

Knowing more than one way to perform a task is helpful if you ever find yourself working on an older system or one with a different operating system.