Howto: Change the hostname of a system SAFELY
This is a short and sweet howto that is nonetheless very important. It is based on my experiences when changing the hostname of a system. The hostname is effectively just the name of the system. You do not usually need to change it, but if you do have to for whatever reason, it isn't very difficult at all.
Whilst changing the hostname is not a difficult task in itself, it can be dangerous because if the system cannot resolve it's own hostname, it is impossible to sudo. Thus, if done improperly, changing a hostname can lock you out of your own system. Firstly, and most importantly, we make backups. Go to accessories/terminal and paste this command in, then press enter:
Code:
sudo cp /etc/hosts /etc/hosts.bak && sudo cp /etc/hostname /etc/hostname.bak
You MUST make backups, just in case. If you can't boot to a usable system after finishing this howto, or sudo doesn't work, see the instructions at the end of this guide. If you aren't completely familiar with the linux terminal, it might be a good idea to write these down or print them off.
Ok, now we have made backups, we can edit /etc/hosts:
Code:
sudo nano /etc/hosts
You should see what you are looking for fairly quickly, at the top of the file it should look something like:
Code:
127.0.0.1 localhost
127.0.1.1 old_hostname
Change old_hostname to a new hostname of your choice. Now save and exit.
Second file to edit is /etc/hostname. This does what it says on the tin, the file will have one line in it and that line is your hostname. Change it to your new hostname, save and exit.
Now reboot, you should have a perfectly working system in which sudo works. You can test both sudo and your new hostname by doing, simply:
NOTE: This command does not usually need to be run sudo, we are just testing both the changed hostname AND sudo at the same time.
If it doesn't work, see below. You DID take backups, didn't you?
- Get into the grub menu by pressing esc on bootup, or allowing it to display automatically if you have set it up like this
- Choose recovery mode
- If prompted, select "drop to single user mode" or similar (note: this applies only on hardy I think)
- When you are at a root shell, type the following command:
Code:
cp /etc/hosts.bak /etc/hosts && cp /etc/hostname.bak /etc/hostname
- Then press ctrl-alt-del and you should reboot back to a perfectly working system.
That's it! If anybody has problems, post here and I'll try to help.
Sam
Re: Howto: Change the hostname of a system SAFELY
Very nice. I wonder why no one replied to this yet
Re: Howto: Change the hostname of a system SAFELY
I just wanted to say that this helped me out a lot today. It worked without a hitch.
Thank you for sharing this.
Re: Howto: Change the hostname of a system SAFELY
Re: Howto: Change the hostname of a system SAFELY
Just what I was looking for. Thanks!
Re: Howto: Change the hostname of a system SAFELY
perfect instructions - thanks!
Re: Howto: Change the hostname of a system SAFELY
You do need to consider updating certificates (stuff like webmin, ssh) too, otherwise you'll get security certificate mismatches (especially if you install IMAPS on your system for instance).
Not entirely sure how to do that myself, but its a consideration to extend this howto.
Re: Howto: Change the hostname of a system SAFELY
Re: Howto: Change the hostname of a system SAFELY
Worked like a charm!
Thank you a lot!
Re: Howto: Change the hostname of a system SAFELY
Cool, thanks for that! I just had a very curious bug when I tried to change the hostname. My first action was just to change the /etc/hostname file, when I was not aware that one must also change the /etc/hosts file. After rebooting the Ubuntu (2.6.31-20-PAE) refused to boot due to a kernal panic of unknown reason.
Many attempts concluded that the kernel panic was caused by inconsistent hostnames in the /etc/hostname and /etc/hosts files - my box boots now without any problem.
Therefore take care guys when you want to change the hostname from the command line, and be aware of what to do if a strange kernel panic appears!