Linux change my hostname / computer system name

by on July 26, 2007 · 12 comments· LAST UPDATED July 26, 2007


Q. I’m using Ubuntu Linux. How do I change my hostname or the machine name?

A.. You need to use hostname command, which is used to either set or display the current host or domain name of the system. This name is used by many of the networking programs to identify the machine.

Following instructions should work on both Debian and Ubuntu Linux systems.

Task: Display current hostname

Just type hostname command print the name of the system:
$ hostname

Task: Change hostname

Set hostname to
# hostname
# hostname


Task: Change hostname permanently

You need to edit a file called /etc/hostname:
# vi /etc/hostname
Set new hostname:

Save and close the file.

A note for Redhat / CentOS Linux users

If you are using CentOS or Fedora or Redhat (RHEL) Linux, see this FAQ.

Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 12 comments… read them below or add one }

1 Michael Stelly August 2, 2009 at 4:33 am

Thanks. That’s exactly what I needed!


2 papa February 10, 2011 at 7:34 pm

hostname —temp changing the hostname
For Permanent
vim /etc/hosts
vim /etc/sysconfig/network


3 tong November 8, 2011 at 4:23 am

i did as you tell but can’t ping to hostname. My hostname is linuxserver, when i use command ping it appear connect: Network is unreachable.


4 root May 31, 2011 at 2:21 pm

after changing, type “bash” and the prompt will change to the newest hostname your just changed.


5 name June 4, 2011 at 12:00 am

this just doesn’t work, it seems like it does but you wont be able to use “sudo” anymore…..


6 Deviram yaduwanshi June 7, 2011 at 4:53 am

hostname change command


7 Bob December 31, 2011 at 10:14 am

Thanks for pointing me to the right direction.
As for me and perhaps ppl using certain distro, may have to edit the /etc/hosts file as well.
sudo gedit /etc/hosts localhost newhostnamehere
Look for the line that says: originalname
and change it to the newhostname

PS: until I fix this, sudo took a long time trying to but fail to “resolve” the name, before asking for password and the GUI ‘update’ taskbar icon and GUI package manager stop working.


8 arik1 November 30, 2014 at 3:30 pm

The same thing happened to me, thanks for the comment


9 haji March 2, 2012 at 3:08 pm

my need change host name in centos 5.7 ??


10 martin April 27, 2012 at 11:16 am

Really from my own point of view it is so amazing to use it please i need some e-book how to use it.


11 Kitana August 21, 2012 at 11:11 pm

Setting up a CentOS 6 box today and was able to use the hostname command successfully without errors. Not sure why using the other FAQ is suggested.


12 ashkan March 10, 2015 at 7:46 am

so useful, thanks a lot!!!


Leave a Comment

Tagged as: , , ,

Previous Faq:

Next Faq: