So. Yesterday I told you how to change your hostname. Unfortunately I did forget one more file, and GNOME reminded me as much earlier this evening: /etc/hosts. As long as I’m explaining it, I may as well explain the rest of the file.

I changed the name of my home desktop PC via SSH (admittedly because I was too busy to walk to the living room). I normally go for KDE, and it never gave me any trouble about my hostname. Tonight I needed to burn a CD, however (more on that fiasco later). I fired up GNOME to try Nautilus’s CD burner, and suddenly got warnings about being unable to find the hostname in the hosts file.

Strangely, it only gave me the option to “Try Again” or to “Login Anyway.” Cancel might have been a nice option, but at lest Login Anyway got me into the GUI, followed by another error or two probably related to the same problem. To get rid of the error, I need to make the correction in my /etc/hosts file.

The /etc/hosts file contains IP addresses for systems on your network. It predates DNS and is nice to have for small networks without dedicated DNS servers, especially on a network with private addresses. They syntax is simple: you have an IP address, followed by the hostname(s).

Every /etc/hosts file has the following entry:

127.0.0.1         localhost

This simply says “localhost” points to 127.0.0.1. So, if you have a web server running on your computer, you can point your browser to http://localhost and see your site. (127.0.0.1, for those who aren’t aware, is the loopback interface — every system has this as an internal IP address for itself).

Often you will also see your hostname with the same local address:

127.0.0.1        myhostname    myhostname.example.com

As such, you’ll want to change myhostname to reflect your current hostname and domain name. Incidentally, you can also add other hosts on your network, and their IP addresses here. Say you have a Linksys router as your gateway, a desktop called hamster, and a laptop called gerbil, with the IP addresses ending in .1, .2 and .3 respectively. Your /etc/hosts file would look like this:

127.0.0.1          localhost
192.168.1.1        linksys
192.168.1.2        hamster
192.168.1.3        gerbil

With this setup on all three hosts, you could run ping linksys and receive pings from your Linksys router. You could do ssh hamster to access your desktop from your laptop. Think of it as easy DNS.