How to Change HostName and IP-Address in CentOS (2024)

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:

# hostnamedev-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

# hostnameprod-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/hosts127.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/hosts127.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/networkNETWORKING=yesHOSTNAME=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 restartShutting 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.

II. Change Hostname from UI

If you have desktop related utilities installed on your system, you can change the hostname from the GUI.

Execute system-config-network from the command line.

# system-config-network

This will display the “Network Configuration” GUI. Go to the “DNS” tab and change the hostname from here as shown below.

How to Change HostName and IP-Address in CentOS (1)

III. Change the IP-Address

1. Change ip-address Temporarily Using ifconfig

You can change the ip-address of the server using ifconfig command as we discussed earlier. For example, the following changes the ip-address of the server on eth0 interface to 192.168.1.2

# ifconfig eth0 192.168.1.2

2. Change ip-address Permanently

Under the /etc/sysconfig/network-scripts directory, you’ll see file for every network interface on your system. For example, if your interface is “eth0”, you’ll see ifcfg-eth0 file under this directory.

Modify the ifcfg-eth0 file and change the IPADDR field accordingly as shown below to change the ip-address.

# vi /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE="eth0"BOOTPROTO=noneNM_CONTROLLED="yes"ONBOOT=yesTYPE="Ethernet"UUID="11111-2222-3333-4444"IPADDR=192.168.1.2PREFIX=24GATEWAY=192.168.1.1DEFROUTE=yesIPV4_FAILURE_FATAL=yesIPV6INIT=noNAME="System em1"HWADDR=DD:BB:DD:AA:11:55

3. Modify /etc/hosts file

If you’ve defined the ip-address in the /etc/hosts file, make sure to change those also. For example, if you have a FQDN that was pointing to the old ip-address in the /etc/hosts file, change it to the new ip-address. Depending on how you’ve configured your system, you might not have to do this step.

$ vi /etc/hosts127.0.0.1 prod-server localhost.localdomain localhost192.168.1.2 prod-server.mydomain.com

4. Restart the Network

Finally, restart the network service, for the system to pick-up the changes.

# service network restartShutting 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 and ip-address is changed properly, and the system is picking it up properly during startup.

How to Change HostName and IP-Address in CentOS (2024)
Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5539

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.