in Linux

How to Change HostName and IP-Address in Fedora / CentOS / RedHat Linux

You can use any one of the following methods to change the hostname and/or ip-address on RedHat related distributions.

If you want to change only the hostname you can either do it from command line, or from GUI as explained below.

To change the ip-address along with the hostname, follow the steps shown below.

I. Change HostName From Command Line

1. Use hostname command to Change Hostname

In this example, we’ll change the hostname from dev-server to prod-server.

hostname command by default will display the current hostname as shown below:

# hostname
dev-server

The following will change the hostname to prod-server.

# hostname prod-server

Once the hostname is changed, verify that it has changed the hostname successfully. As you see below, it has changed the hostname to prod-server

# hostname
prod-server

2. Modify the /etc/hosts file

If you have entries in the /etc/hosts file with the old hostname, you should modify it.

For example, the entry for 127.0.0.1 line in the /etc/hosts file will still show the old hostname. In this example, it shows as dev-server.

$ cat /etc/hosts
127.0.0.1  dev-server localhost.localdomain localhost

Modify this file, and set the new hostname here. For example, change dev-server to prod-server as shown below.

$ cat /etc/hosts
127.0.0.1  prod-server localhost.localdomain localhost

3. Modify the /etc/sysconfig/network file

The /etc/sysconfig/network file also has an entry for HOSTNAME. Change the value here as shown below.

# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=prod-server

4. Restart the Network

Restart the network service, if you want any other services that are using the hostname to pickup the changes.

# service network restart
Shutting down interface eth0:        [  OK  ]
Shutting down loopback interface:   [  OK  ]
Bringing up loopback interface:     [  OK  ]
Bringing up interface eth0:          [  OK  ]

If this is not a production system, you can also reboot the system to make sure the hostname is changed properly, and the system is picking it up properly during startup.