This article objective is provide you the way to assign the IP address to the network interface from the command line.
You can see the available network interfaces on your machine by using the simple command ifconfig
You can find the interfaces and their names and you can identify the name which is preceded by colon(:) in the left side. Something like eth0, lo and wlan0 etc. The network interface lo interface is the special interface where we called it as localhost and special IP assigned to it 127.0.0.1 also called loopback IP.
Do you know? you can assign multiple IPs to single network interface. This is pretty useful when you need multiple IP addresses but you have only one network card.
ifconfig eht0:1 192.168.2.9 up
We created new interface alias with IP assigned. We can do it simply by giving colon(:) and alias number.
Assign IP using ifconfig
ifconfig eth0 down ifconfig eth0 192.168.2.3 up
This command will assign the specified IP to the give network interface. It is not a persistent change. The would change after reboot.
ifconfig eth0 192.168.2.14 netmask 255.255.255.0 up
Get IP using dhclient
The program dhclient will get you the IP to the given interface using the Dynamic Host Configuration Protocol (DHCP). The IP which will be assigned to the given network interface which will be provided by gateway or router. If you won’t have any preference of having specific IP assigned to the network card, this can be used
Assign static IP
The IP we assigned above is not a persistent or static IP. Means you will lose that IP and will get an other different IP assigned after reboot. To make this IP permanent we have to edit the configuration files.
# Your primary public IP address. iface eth0 inet static address 198.51.100.5/24 gateway 198.51.100.1 # To add a second public IP address: iface eth0 inet static address 198.51.100.10/24 # To add a private IP address: iface eth0 inet static address 192.0.2.6/17
Unlike the distribution Debian, CentOS maintains the configuration in a separate file for each interface. The file path would be something like /etc/sysconfig/network-scripts/ifcfg-<interface_name>.
# eth0 DEVICE=eth0 BOOTPROTO=none ONBOOT=yes # Your primary public IP address. IPADDR=126.96.36.199 NETMASK=255.255.255.0 GATEWAY=188.8.131.52