Changing the Hostname on a Linux Box

I recently had need to change a server name due to a change in our server naming scheme (local ski resorts to breweries). For the simple comedy of the naming scheme switch, here’s how it currently looks (without server prefixes for security purposes of course). If you just want the solution, skip down a paragraph.

Our current environment is mostly virtualized. The hosts are named after breweries and their virtual guests are named after the beers that each brewery produces. Clever, yeah? I can already feel my morale rising.

First off, open up a terminal window

terminal001

Then type

terminal002b

The file that comes up should contain nothing but a hostname. In my case this is . (Ctrl + x closes the file, y says to save the file before closing, Enter saves the file under the original filename).

terminal003

Once you’ve done this, all you need to do is restart your computer and you should be golden.

Here’s how we fix the aforementioned issue.

If you’ve closed your terminal for the restart, open it up again. Type sudo nano /etc/hosts

terminal004

At the top you should see 127.0.0.1, 127.0.1.1 and their associated hostnames. The one to the right of 127.0.1.1 should show your old hostname. Change that to the new hostname and save the file (Press Ctrl + x → y → Enter). Now your computer’s IP address should resolve to its new hostname.

Enjoy!

Now for a cup of joe…​

Category:Linux