Top Menu

Thursday, October 18, 2012

How to change hostname without reboot Linux machine.

In this article I am going to demonstrate how to change hostname in RedHat Linux without reboot the server. I've tested this on RHEL 5.5, 6.0 Fedora 15, and CentOS 5. It should work for all the versions in between since they all closely follow the same RedHat configuration.
Make sure you are logged in as root and move to /etc/sysconfig and open the network file in vi.

[root@redhat1 ~]# cd /etc/sysconfig/
[root@redhat1 sysconfig]# vi network

Look for the HOSTNAME line and replace it with the new hostname you want to use. In this example I want to replace redhat1 with redhat2.


When you are done, save your changes and exit vi. Next we will edit the /etc/hosts file and set the new hostname.
In hosts, edit the line that has the old hostname and replace it with your new one.

[root@redhat1 sysconfig]# vim /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0 redhat2 redhat2
#::1            localhost6.localdomain6 localhost6

Save your changes and exit vi. The changes to /etc/hosts and /etc/sysconfig/network are necessary to make your changes persistent (in the event of an unscheduled reboot).
Now we use the hostname program to change the hostname that is currently set.

[root@redhat1 sysconfig]# hostname redhat02
[root@redhat1 sysconfig]# hostname
[root@redhat1 sysconfig]#

Finally we will restart the network to apply the changes we made to /etc/hosts and /etc/sysconfig/network.

[root@redhat1 sysconfig]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  error in ifcfg-eth0:0: didn't specify device or ipaddr
                                                           [  OK  ]
Bringing up interface eth0.bkp:  error in ifcfg-eth0:0: didn't specify device or ipaddr
                                                           [  OK  ]
[root@redhat1 sysconfig]#

To verify the hostname has been fully changed, logout of your system and you should see your new hostname being used at the login prompt and after you've logged back in.

login as: root
root@'s password:
Last login: Wed Sep 26 03:09:48 2012 from
[root@redhat2 ~]#

No comments:

Post a Comment