An IP (Internet Protocol) Address is a unique numerical representation of a computer on a network. Every computer connected to the Internet is identified by an IP Address.
Usually, IP addresses are dynamically assigned to a computer by a dedicated server called DHCP Server (Dynamic Host Control Protocol), and hence change from time to time as and when the connection is lost and reestablished.
However, there are scenarios where a static IP address is more preferable; Eg. In large corporations, where it removes the load of using DHCP for each computer in the organization.
Today, we will learn how to set a static IP address on a local network in CentOS.
List Network Interface Name
A computer can be connected to one or more network interfaces, for example to a WiFi device and a LAN device, which has different IP addresses for each interface.
Run the following command to show the interface names.
# ifconfig OR # ip addr
The interface ‘enp0s3‘ is the LAN device connected to my computer and the IP Address is ‘10.0.2.15‘. The other interface ‘lo‘ (Loopback) which is nothing but the local network of the computer within itself. Thus my computer is only connected to one interface, ‘enp0s3‘.
Configuring Static IP Address in CentOS
Go to directory ‘/etc/sysconfig/network-scripts‘ and list the files; you should see a file corresponding to your network interface.
$ cd /etc/sysconfig/network-scripts $ ls
Open the file ‘ifcfg-enp0s3’ using ‘Vim‘ or any editor of your choice.
Set the following values for the variables. Change the values according to the IP address and subnet that you need to set.
Save and exit the file. Restart the networking service with the following commands:
# nmcli networking off # nmcli networking on
Finally, run ‘ifconfig‘ again to verify if static IP has been set.
# ifconfig
Conclusion
In this article, we saw an easy way to set an IP address in CentOS. The example, in this case, is a static IP on the local network, i.e., it is not a static public IP over the Internet.
To set a static public IP address over the Internet, you need to purchase the IP Address and configure it in the file as shown above, along with other details like DNS server, network prefix, which will be provided by your Internet Service Provider.
Thanks a lot for reading and let us know your thoughts in the comments below!