how to change hostname in centos using command line


Sometime we need to change server hostname , It can be possible to change host name by using SSH command line ;

1st we can check the current hostname by command :


outputs example :

Now , we change the hostname by editing network and hosts files . we can edit file by using nano or vi command ;

nano /etc/sysconfig/network

look in outputs and find  HOSTNAME value & change with new hostname ;
for example : HOSTNAME=””
Save and close the file.

Now we need to edit hosts file with host name with your IP  :

nano /etc/hosts localhost

Save and close the file.

Set hostname manually without rebooting the box

Type the following command:
# hostname

Restart the CentOS networking and other services:

service network restart
/etc/init.d/network restart

Verify new hostnames

Simply type the following commands:
# hostname


thats all.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.