How to Change Your Hostname (Computer Name) on Ubuntu Linux

ubuntu-linux-change-hostname-or-computer-name-shown-in-terminal

Ubuntu asks you to choose a name for your computer — known as a “hostname” — when you install it. You can change this hostname later, but Ubuntu doesn’t provide a graphical interface for doing so.

The process here will also work on Linux Mint and other Debian-based Linux distributions. Other Linux distributions — for example, Fedora and other Red Hat-based distributions — use different methods for specifying a hostname.

Choosing a Hostname

A hostname is a name assigned to a “host” — a computer on a network. The hostname is basically just your computer’s name. It’s used to identify your computer on the network. It’s also shown very visibly in the terminal.

You can change the hostname to anything that you want. However, you shouldn’t use the same hostname on two different computers on a network, as this can cause problems. The hostname is supposed to uniquely identify a computer on a network.

Don’t get too fancy with your hostname.  Host names can only contain letters (a through z), digits (0 through 9), and the hyphen character ( – ), and the period character ( . ). A hostname must begin and end with a letter or number — not a hyphen or period. Letters are also case-insensitive, so “COMPUTER” is equivalent to “computer.” The hostname must be between 2 and 63 characters long, although you’ll probably find shorter hostnames more convenient.

ubuntu-change-hostname-or-computer's-name

Edit Your /etc/hostname File

Open a terminal window to get started. In Ubuntu’s Unity desktop, click the Ubuntu button to pull up the dash, search for Terminal, and press Enter.

open-terminal-on-ubuntu-linux

You’ll need to edit your /etc/hostname file, which is where Ubuntu and other Debian-based distributions store the hostname. Run the following command in the terminal to open the /etc/hostname file in the graphical “gedit” text editor for editing:

sudo gedit /etc/hostname

(You could also use terminal-based text editors like nano or vi for this — you’d run the sudo nano /etc/hostname or sudo vi /etc/hostname commands to open the file for editing. Consult our guide to using nano or introduction to using vi for help using these text editors.)

open-etc-hostname-file-for-editing

The /etc/hostname file is very simple. It’s contains only one thing — your computer’s hostname. To change it, just delete the existing hostname from the file. Replace it with your new desired hostname and then save the text file.

etc-hostname-file-on-ubuntu,-mint,-or-debian

Ubuntu and other Debian-based distribution read the /etc/hostname file while booting up and set your computer’s hostname to the one contained in the file. Your change won’t take effect immediately — you’ll have to reboot or use the hostname command to change it immediately.

set-new-hostname-on-ubuntu-linux

Edit Your /etc/hosts File

The hostname is also stored in the /etc/hosts file, where it’s set to redirect to your local computer — localhost. You’ll need to change the hostname in your hosts file, too.

Open the /etc/hosts file for editing with a command like the following one:

sudo gedit /etc/hosts

(Once again, you could use any other text editor you want — nano, vi, or another graphical text editor of your choice.)

open-hosts-file-for-editing-on-ubuntu

Locate your old hostname in the hosts file. It’ll be on a line that looks something like:

127.0.1.1 your-old-hostname

add-new-hostname-to-hosts-file-on-ubuntu

Once again, replace the old hostname with your new hostname. You can then save the /etc/hosts file.

new-hostname-added-to-etc-hosts-file-on-ubuntu-linux

Change Your Hostname Immediately

The change above will take effect when you reboot, but you can use the hostname command in a terminal to change your hostname immediately. This command only changes the hostname until the next time you reboot, so you do have to change the /etc/hostname file to change it permanently.

Run the hostname command in a terminal window to specify a new hostname. If you chose an invalid hostname, this command will tell you and you can try again. Run the command like so:

sudo hostname your-new-hostname

change-hostname-without-rebooting-on-ubuntu

Your computer’s hostname will be changed immediately, although the change won’t appear in the terminal immediately. The terminal will notice when you re-open it — or if you just open a new one.

change-hostname-immediately-on-ubuntu


That’s it; you should be done. On Linux distributions not based on Debian, there’s a different process. Check your Linux distribution’s documentation for more information. Some Linux distributions may even provide a graphical utility you can use to quickly change your hostname, so you don’t have to use the terminal — unless you want to.

Chris Hoffman is a technology writer and all-around computer geek. He's as at home using the Linux terminal as he is digging into the Windows registry. Connect with him on Twitter.