Linux Change Hostname

by on January 7, 2009 · 10 comments· LAST UPDATED November 7, 2009

in , ,

How do I change hostname under Linux operating system?

You need to use the hostname command. It is used to either set or display the current host or domain name of the system. The hostname is used by many of the networking programs (such as sendmail, Apache servers) to identify the machine.

Display Hostname

Type the following command:

hostname

Sample ouputs:

server.nixcraft.net.in

Step # 1: Change Hostname

You need to update two files:

  1. Linux Distribution specific file. Edit appropriate file as per your distribution as follows.
  2. /etc/hosts

Redhat / CentOS / Fedora: Change Hostname

Edit /etc/sysconfig/network, enter:

vi /etc/sysconfig/network

Set HOSTNAME=newhost.example.com, enter:

HOSTNAME=server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

Now, proceed to step # 2.

Debian / Ubuntu: Change Hostname

Edit, /etc/hostname file, enter:

vi /etc/hostname

Delete old name and add new name:

server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

Now, proceed to step # 2.

Slackware: Change Hostname

Edit /etc/HOSTNAME, enter:

vi /etc/HOSTNAME

Delete old name and add new one:

server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

Now, proceed to step # 2.

Suse / OpenSuse: Change Hostname

Edit /etc/HOSTNAME, enter:
Delete old name and add new one:

server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

Now, proceed to step # 2.

Step #2: Update /etc/hosts

Now, you need to edit /etc/hosts file, enteR:

vi /etc/hosts

Change all old hostname with newer one.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 10 comments… read them below or add one }

1 Iris November 13, 2012 at 3:37 am

echo “servername” > /proc/sys/kernel/hostname

Reply

2 Yeah March 16, 2013 at 7:01 am

None of this does anything to change the prompt which still reads: [root@localhost /] in CentOS6. After years of Linux mods, why is it still so damn complicated to change a single prompt ?!

Reply

3 Kimmo March 18, 2013 at 7:45 am

Sure it does, but you have to re-connect ssh to get it visible

Reply

4 smudger March 31, 2013 at 4:34 pm

For my debian-based xubuntu

after editing /etc/hostname with sudo

>> cd /etc/init.d
>> sudo start hostname

Reply

5 vishnu May 23, 2013 at 3:31 pm

*In Fedora Simply follow this:

# whereis hostname

output: hostname: /bin/hostname /usr/bin/hostname /etc/hostname /usr/share/man/man1/hostname.1.gz /usr/share/man/man5/hostname.5.gz

*edit
vi /etc/hostname

Reply

6 noname001 August 22, 2013 at 12:18 pm

well you do not vi hostname file you pipe stuff there

echo “hostname_1″ >/etc/hostsname
just to be clear

Reply

7 Pablosky January 29, 2014 at 8:49 pm

Thank you for help on how to find the file. It was crazy for me because it wasn’t hostname “xP

Reply

8 sabarimurugan S March 24, 2014 at 5:07 pm

I configure my rpm& yum server but the following error occurs please anyone tell me how to rectify the error

file:///var/ftp/pup/repo/Server/repodata/repomd.xml: [Errno 5] OSError: [Errno 2] No such file or directory: ‘/var/ftp/pup/repo/Server/repodata/repomd.xml’
Trying other mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: Server. Please verify its path and try again

Reply

9 Logash July 16, 2014 at 5:24 am

How do i check which version of linux am running in and bit version?

Reply

10 mathews October 8, 2014 at 3:35 pm

cat /proc/version – provides the bit information
cat /etc/issue – Release

The above Commands should help.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: