Changing the primary IP address of a CentOS

How to change the primary/main IP address of a CentOS host in no-time! Open up the config for your primary network interface, most commonly called eth0:

[root@daddylinux~]#nano -w /etc/sysconfig/network-scripts/ifcfg-eth0

In there you'll find something along the lines of this:

DEVICE=eth0                                                                   
BOOTPROTO=static                                                              
HWADDR=XX:ee:7c:6e:XX:XX                                                      
IPADDR=XX.XX.XXX.XXX                                                           
NETMASK=255.255.255.0                                                         
ONBOOT=yes

NB: I have obfuscated the MAC address (HWADDR) and the IP address in this example, replacing some of the real numbers/letters with X's instead. Make sure to keep the HWADDR intact.

Now, to change the IP address of the host - we simply change the value for IPADDR, then save our file. (CTRL + X, y for yes, enter to keep the same filename.)

Note: If you are changing to an IP on a different subnet and need to change your default gateway, this can be configured under /etc/sysconfig/network

Look over any changes you have made to these files and make sure the information is absolutely correct. WARNING: Typo's here could leave your machine/server unavailable if you are connecting to it remotely and apply the changes (in the next step) without double checking them.

Once you have made your changes, you can restart the network service to apply the new IP address:

[root@daddylinux~]# service network restart

source : centosforge.com

2 comments:

  1. In CentOS 6.4 (and perhaps others) it is important to put everything in quotes, otherwise things like 'hostname -i' command will stop retrieving the correct information.
    EX:
    BOOTPROTO="static"
    IPADDR="192.168.3.13"

    ReplyDelete