Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Howto: Change the hostname of a system SAFELY

  1. #1
    Join Date
    Jan 2006
    Location
    Bristol, UK
    Beans
    275
    Distro
    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:

    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:

    Code:
    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:
      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
    Last edited by sammydee; May 2nd, 2008 at 07:05 PM.

  2. #2
    Join Date
    Dec 2006
    Location
    Manila, Philippines
    Beans
    1,886
    Distro
    Ubuntu Development Release

    Re: Howto: Change the hostname of a system SAFELY

    Very nice. I wonder why no one replied to this yet
    MacBook Air 3,1 128gb | Fujitsu Esprimo U9200 | Google Nexus One

    Pinoy ka ba? | Google Profile | Twitter: @AllanCaeg| Identi.ca: allancaeg

  3. #3
    Join Date
    Dec 2008
    Beans
    3

    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.

  4. #4
    Join Date
    Aug 2007
    Location
    Colorado
    Beans
    51
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Howto: Change the hostname of a system SAFELY

    Thanks man.

  5. #5
    Join Date
    Apr 2009
    Beans
    12

    Re: Howto: Change the hostname of a system SAFELY

    Just what I was looking for. Thanks!

  6. #6
    Join Date
    Jul 2009
    Beans
    7
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Howto: Change the hostname of a system SAFELY

    perfect instructions - thanks!

  7. #7
    Join Date
    Dec 2005
    Location
    Midlands, United Kingdom
    Beans
    22
    Distro
    Ubuntu 9.10 Karmic Koala

    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.

  8. #8
    Join Date
    Dec 2008
    Beans
    10

    Re: Howto: Change the hostname of a system SAFELY

    Works perfectly,

    Thanks

  9. #9
    Join Date
    Nov 2009
    Beans
    1

    Re: Howto: Change the hostname of a system SAFELY

    Worked like a charm!

    Thank you a lot!

  10. #10
    Join Date
    Jul 2007
    Beans
    2

    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!

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

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