Ubuntu: How to Change the Computer Name

Posted January 7, 2008 by Rob Rogers in Linux

You might run into a situation that requires you to change your computer name, either because you need it to meet a naming scheme or you’re just bored with it and want something better. By following these steps, you’ll give your computer a new identity in no time.

1. Open a terminal window.

2. Input the following command and hit Enter:
gksudo gedit /etc/hostname

3. When prompted, enter the administrator password and click the OK button.

4. The hostname file will open, displaying the current computer name. Replace the current computer name with the desired new name.

5. Click Save.

6. Close all open windows and restart your system.

After your system has restarted, it will have the new computer name.


About Rob Rogers

Once a prolific author here on Tech-Recipes, Rob has moved on to greener pastures.
View more articles by Rob Rogers

The Conversation

Follow the reactions below and share your own thoughts.

  • ubu-fan

    hey thnx for the tutorial, worked like a charm 😉

    • kumareshane1986

      its working pretty cool…

  • Rahul


  • Lurker

    I would change the /etc/hosts first if you don’t the machine can not create a window. Otherwise works great on Ubuntu 9.04

  • Greg

    You may (will) want to edit your /etc/hosts file too. So that your system isn’t confused by the change: (sudo gedit /etc/hosts) look at the second line typically.

  • Anonymous

    So simple? I can”t believe it. I always wanted my computer to bare a good name but, like in any other operating system, I’ve been using Administrator or other general words.
    Mathew Farney – Web Hosting

  • Name

    As mentioned before, you’ll need to change your /etc/hosts file as well. If you change the /etc/hostname first, Ubuntu probably won’t let you open the /etc/hosts file with the gedit command. You can get around this with:
    sudo vi /etc/hosts

  • Lars

    Very helpful, thank you. What is the character limit?

  • ralph


  • Nick Djinn


  • Yashwant108


  • Anonymous

    That’s very interesting, I will bone up on the subject and then express myself regarding the problem

  • Anonymous

    There are simple commands on Linux:)) Never thought I would see that soon enough. Thanks for the piece of advice. Very useful for non-Linux users that have to deal with Linux.
    computer checkup

  • Tiburon19

    this is awesome. best decision was to go linux. Thanks for the advice.

    • vasvi gupta

      You can use other editor also I use
      sudo vi /etc/hostname . replace the name that you liked and save this file.thats all…. but you will see new hostname after restarting your system.

  • Salman

    thnks it work 4 me thnks again———————————————————

  • nyomz

    thanks. it’s so helpful..

  • Rodrigo Primo

    Please update the post and mention that you need to edit /etc/hosts as well as others mentioned before. Thanks.

  • olof nord

    thank you!

  • Aniruddh


  • Anonymous

    Is there an official way to do this instead of using a hack on the command line? Obviously this solution is not complete, as it does not change the hosts file. There should be an official GUI way to change the hostname that changes all relevant files at the same time.

    • Greg

      Well, this is the “official” way, Junior…at least for Debian and derivatives such as the *buntus, etc. There might be a GUI thingy out there somewhere to do it, but it’s just simple enough as it is:

      Change the hosts file first and then change the hostname file. Then restart your networking and you’re all set.

      1) From a command line: sudo gedit /etc/hosts – will give you something like: localhost.localdomain “computername”.”domain_name” “computername”

      ..change the “computername” parts to whatever you want and “domain_name” part if you have to/want to to make it complete….Then save it.

      2) Run (also from command line): sudo gedit /etc/hostname
      put your new hostname in where the old one was…and save this file, too.

      3) Restart your computer or restart networking from the command line.

      If you’re running RedHat or another flavor of Linux the process is pretty similar, just the filenames and details are different.

      • http://pulse.yahoo.com/_SCYWCXRIK2PW5OTKJQROCCV7CA gahh

        You’re forgetting something.

        Yet another reason why I switched from Ubuntu to Windows 7. Stuff Just Works.

        • Maximiliano Guzman

          so windows 7 guesses you want to change the hostname? What part of this doesnt “just work”. Maybe you prefer a gui, but that’s another thing.

  • Dahc

    so whats the complete process? Do I change (sudo gedit /etc/hosts) this first or what? Sorry I’m new to Linux. I have to put this in the Terminal command line right. My IT guy did all of my Ubuntu stuff for me and he”s gone now and so am I. I need some training on Linux in general, any suggestions as to where to get it?

  • Anon


  • http://removevirusguide.com/how-to-uninstall-blueflare-antivirus-scam.html remove Blueflare Antivirus

    Change the computer name is easy.

  • http://removevirusguide.com/how-to-remove-windows-xp-recovery-virus.html windows xp recovery virus

    You saved my alot of time thank you so much for sharing your experience.

  • Gil

    Very simple and easy.


  • kalpesh

    thnx buddy

  • Rajesh


  • Sathishkumar

    Thanks for the tip !

  • Daegon

    Thank’s for the tips, I nearly did a re-install to achieve the same result.

  • SonnyE

    in freshly installed Lubuntu 12.04 the method given doesn’t work

    (there is a gksudo, but there is no gedit installed)

    steps to change hostname in Lubuntu 12.04, as a new user:

    (this is my first week using Linux)

    1. open Leafpad and open /etc/hosts

    2. edit your hostname in it to what you want it to be

    (don’t change anything else, every tab and space is important)

    (if you don’t see your hostname in it, something’s wrong, stop)

    3. save it in your Documents folder

    (the system won’t permit saving it directly to /etc/hosts)

    4. open a console (Ctrl-Alt-f1)

    5. use the command:

    sudo cp hosts /etc/hosts

    (after this command you will start getting error messages:)

    “sudo: unable to resolve host [your old hostname here]”

    (ignore the messages, it’s because the system is half changed)

    6. check that you altered hosts correctly by the command:

    cat /etc/hosts

    (it should show the edited file you put in)

    7. return to desktop using Alt-f7

    8-14. repeat all of the above steps for the file: hostname

    15. logout or exit each console or terminal you have open, to refresh the prompts

    16. open a new LXTerminal, the prompt should now include your new hostname

    (if that didn’t work, put everything back the way it was)

    • Joeb

      > You can simplify the Lubuntu method by simply opening a terminal and sudo leafpad /etc/hosts and change the computer name and save and exit from leafpad and then sudo leafpad /etc/hostmane and change the computer name. After changing them both, reboot.

      Basically, the same steps as for Ubuntu but substituting leafpad for gedit.

  • Arek

    You can change it easily, still, thank you 🙂

    sudo hostname {new hostname here}

  • Nabil

    Thank you 🙂

  • Sirisha

    Thank you Rob 🙂

  • Gustav


  • ccspro

    Here is a one liner and in the end it just opens a new bash shell to show you that next connection via ssh, telnet, whatever should show/use the new hostname – its just that without using hostname -F the system doesn’t know about the hostname change and thus you force it to load.

    Do note that this will not work on redhat systems since they use variables in /etc/hostname file, I’m sure you can figure it out on your own 😛

    sudo su

    newhn=’mynewhostname’;sudo sed -i s/`hostname`/$newhn/g /etc/hosts;sed -i s/`hostname`/$newhn/g /etc/hostname;hostname -F /etc/hostname;bash

    FYI this is untested, it should work otheriwse just edit /etc/hosts – change hostname, edit /etc/hostname – change; hostname -F /etc/hostname; and run bash or ssh to your box again… hostname will be changed throughout

    If your mysql (as an example) uses hostnames for anything (say for the PID file) on command line (or any other binary you use) you can and will have some problems stopping/starting them on running systems, be warned! (rebooting should almost always be avoided)

    You can stop those services that may not start/stop properly before hand, stop them before any changes, make the changes and start them up…

    Good times.

  • Manigandan

    nice man…Thanks 🙂

  • HowieFields

    This is really a dumb way to do it. Every help article I see with regards to Ubuntu has you running a desktop application. This is cumbersome to do when you are ssh’d into a box and cumbersome to do if you are not.

    just freaking: $vi /etc/hostname

    • David Kirk

      Thanks for adding to the discussion, Howie. Most people that coming new into Ubuntu want the “windows” way of doing things until they get comfortable with the terminal.

  • daniel

    work great, thanks a lot !

  • Drake Mandin

    This is only half of it. If we only do this, we will get an error stating unable to find host… to resolve it, type the following command in terminal and change the name there as well…

    gksudo gedit /etc/hosts

    and save it and restart your system. Have fun