Wednesday, November 18, 2009

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.

Now, to the solution...
Changing the hostname on a Linux machine is a bit more complicated than it is in windows.




First off, open up a terminal window













Then type sudo nano /etc/hostname










The file that comes up should contain nothing but a hostname. In my case this is hal8000 (yes, laugh all you want). Change this to whatever hostname you want. Once you have input this, press Ctrl + x. From there press y and Enter. (Ctrl + x closes the file, y says to save the file before closing, Enter saves the file under the original filename).





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



***NOTE***
While the method I just described will change the actual hostname of the computer, its IP address will no longer resolve to its hostname.

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



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