How-To: Change Hostname

How-To: Change Hostname

I swear, i always forget how to do this even though i’ve done it for the nth time. For my sake (and maybe a few others out there), here are several ways on how to do it.

But first check your hostname. It’s normally set to localhost.localdomain. To do that, use uname -n or simply hostname.

Now to change to your desired hostname, the easiest way is to use the hostname command.

> hostname

Normally it does the trick. But if it doesn’t, edit /etc/hosts and add the ff line. server1

Then edit /etc/hostname and add your hostname. Don’t worry if the file doesn’t exist, just create it.

Next edit /etc/sysconfig/network and add


Finally, this last trick is very useful if all else fails.

echo > /proc/sys/kernel/hostname

There! Now i don’t have to use google when i have to do this.

  • talk about overkill… anyway, just some things to consider:

    1. even if you edit /etc/hosts, the “hostname” command will never, ever show the host name you placed in that file… /etc/hosts function as a local DNS… you can execute “ping server1” and it will ping… but “hostname” command will never output (unless you used other means to set it as your hostname)…

    this step, however, is necessary for some services that check /etc/hosts for the hostname and ip address pair to be able to run.

    2. executing the “hostname” will indeed set your hostname to… but only for that session… once you reboot, it will revert to the original one… so go with the /etc/hostname (not sure if this is legit), the /etc/sysconfig/network, or the /proc/sys/kernel/hostname…

  • now that’s a better explanation… straight from the linux guru himself! i actually just listed all possible command as reference (and use them at random) w/o actually thinking which one works with what. thanks master stalker. 😀

Comments are closed.