CentOS: How to edit your NIC via the command line

There are several ways you can edit the settings of your NIC via the command line. All changes take effect right away. You can pass new configuration data directly by the command prompt or by editing the actual file responsible for the active NIC. The first method is temporary because in a case of a reboot the machine may get a new IP from the DHCP server. The second method ensure that the changes are permanent.

1st) Change the IP address directly on the command prompt:

As root you can use the ifconfig command and give the new IP address and/or the new netmask. Here is an example.

#ifconfig eth0 10.0.0.2 netmask 255.255.255.0

The change will be read instantly by the kernel. However this change is not permanent.

 

2nd) Change the IP address by editing the config file for your NIC:

The file that stores the configuration for your NIC  is

/etc/sysconfig/network-script/ifcfg-eth0

When you originally view this file most likely you will see the following parameters in it

DEVICE=eth0
BOOTPROTO=dhcp
HWADDR=00:14:5E:C2:1E:40
ONBOOT=yes

The parameters you need in the file are listed below

DEVICE=”eth0″ 
IPADDR=10.0.0.5
NETMASK=255.255.255.0
GATEWAY=10.0.0.1
TYPE=Ethernet
NM_CONTROLLED=”yes”
ONBOOT=”yes”
HWADDR=00:0C:29:2E:E8:E3
BOOTPROTO=none
DNS1=10.0.0.1

DEVICE refers to the NIC you are working on.

IPADDR refers to the IP address assigned to the NIC

NETMASK, HWADDR DNS and Gateway are obvious.

ONBOOT is responsible for turning on or off the NIC during bootup.

BOOTPROTO refers to the protocol. As you can see in the original state of the file it had DHCP. However since we want this machine to have a static IP address we opted to enter “none”.

NM_CONTROLLED this will either allow or not for eth0 to be controlled by the NetworkManager service. For automation purposes it would be a good idea to set it to yes.

 

 

 

Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.