Ubuntu: How to Change the Computer Name
Posted 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
View more articles by Rob Rogers
The Conversation
Follow the reactions below and share your own thoughts.
January 25, 2009 at 3:38 pm, ubu-fan said:
hey thnx for the tutorial, worked like a charm
May 05, 2009 at 11:13 am, kumareshane1986 said:
its working pretty cool…
June 05, 2009 at 2:06 pm, Rahul said:
Thanks
June 17, 2009 at 11:01 am, Lurker said:
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
December 05, 2009 at 4:54 pm, Greg said:
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.
December 08, 2009 at 10:26 am, Anonymous said:
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
January 17, 2010 at 7:34 am, Name said:
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
February 11, 2010 at 9:55 pm, Lars said:
Very helpful, thank you. What is the character limit?
April 23, 2010 at 8:58 pm, ralph said:
thanks
May 16, 2010 at 10:47 pm, Nick Djinn said:
Thanks!
July 17, 2010 at 10:35 pm, Yashwant108 said:
Thanks
July 21, 2010 at 1:38 pm, Anonymous said:
That’s very interesting, I will bone up on the subject and then express myself regarding the problem
July 30, 2010 at 7:42 am, Anonymous said:
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
August 12, 2010 at 6:04 pm, Tiburon19 said:
this is awesome. best decision was to go linux. Thanks for the advice.
August 27, 2012 at 6:17 am, vasvi gupta said:
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.
September 02, 2010 at 9:00 am, Salman said:
thnks it work 4 me thnks again———————————————————
September 04, 2010 at 7:58 am, nyomz said:
thanks. it’s so helpful..
September 18, 2010 at 2:43 pm, Rodrigo Primo said:
Please update the post and mention that you need to edit /etc/hosts as well as others mentioned before. Thanks.
September 30, 2010 at 4:57 pm, olof nord said:
thank you!
December 09, 2010 at 11:34 am, Aniruddh said:
Thanks
February 21, 2011 at 2:53 pm, Anonymous said:
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.
March 24, 2011 at 12:27 am, Greg said:
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:
127.0.0.1 localhost.localdomain
127.0.1.1 “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.
March 24, 2011 at 1:24 am, gahh said:
You’re forgetting something.
Yet another reason why I switched from Ubuntu to Windows 7. Stuff Just Works.
June 21, 2012 at 7:27 pm, Maximiliano Guzman said:
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.
March 03, 2011 at 4:06 pm, Dahc said:
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?
March 16, 2011 at 7:19 pm, Anon said:
Thanks!!
May 24, 2011 at 2:25 pm, remove Blueflare Antivirus said:
Change the computer name is easy.
May 25, 2011 at 8:35 am, windows xp recovery virus said:
You saved my alot of time thank you so much for sharing your experience.
December 30, 2011 at 1:20 am, Gil said:
Very simple and easy.
Thanks.
January 08, 2012 at 10:34 am, kalpesh said:
thnx buddy
May 07, 2012 at 4:02 pm, Rajesh said:
Thanks………………
June 16, 2012 at 9:15 am, Sathishkumar said:
Thanks for the tip !
June 28, 2012 at 1:31 am, Daegon said:
Thank’s for the tips, I nearly did a re-install to achieve the same result.
June 29, 2012 at 9:52 pm, SonnyE said:
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)
July 04, 2012 at 10:30 pm, Joeb said:
> 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.
August 10, 2012 at 11:43 am, Arek said:
You can change it easily, still, thank you
sudo hostname {new hostname here}
December 11, 2012 at 11:43 am, Nabil said:
Thank you
December 26, 2012 at 11:41 am, Sirisha said:
Thank you Rob
February 09, 2013 at 5:36 am, Gustav said:
Thanks!
February 26, 2013 at 4:55 am, ccspro said:
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.