Saturday, November 1, 2014

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





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.

Edit Your /etc/hostname File

v 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.



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.



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.



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.



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.)



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



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



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



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.



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.