Results 1 to 10 of 21

Thread: Howto: Change the hostname of a system SAFELY

Threaded View

  1. #1
    Join Date
    Jan 2006
    Bristol, UK
    Ubuntu 9.10 Karmic Koala

    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:

    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.

    That's it! If anybody has problems, post here and I'll try to help.

    Last edited by sammydee; May 2nd, 2008 at 07:05 PM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts