How can I change the computer name on an Ubuntu desktop computer?

I already tried changing the /etc/hostname file but that only caused the wireless to stop working. I found an old guide that suggested changing the setting under networking, but that screen doesn't exist in 9.10 (Karmic Koala).


There is a tutorial on Ubuntu Forums: link

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:

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:

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:       localhost       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:

sudo hostname

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:

    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.

|improve this answer|||||

Change both /etc/hostname and /etc/hosts:

gksudo gedit /etc/hostname
gksudo gedit /etc/hosts
|improve this answer|||||
  • 1
    And type hostname "new name" so it's taken into consideration without rebooting. – ℝaphink Dec 17 '09 at 19:59

How about sudo hostname "new computer name"

|improve this answer|||||
  • 1
    It doesn't last after reboot. – ℝaphink Dec 17 '09 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.