How to configure network interface to manually assign IP address on Linux: CentOS

This article will help to configure network interface on Linux manually

Prerequisites to configure network interface

To configure network interface following things are required

Terminal or SSHAccess to shell via terminal emulator or via SSH
IP AddressIP Address to assign to interface
NetmaskNetwork mask. It depends on in which network system is placed.
intefaceName of the interface. This can be obtained from ifconfig
Default GatewayIP of default gateway is necessary to access the internet. Which is also called gateway of last resort
Command line toolsifconfig, route and ping

Steps involved in successfully configuring network interface

  1. Gather the information: interface name, IP, netmask and default gateway
  2. Assign IP to interface using command ifconfig
  3. Update Routing table using route command
  4. Update nameserver in /etc/resolv.conf to resolve domain names
  5. Verify network connectivity

For the sake of this tutorial assume details as follows

  • IP Address: 192.168.1.3
  • Netmask: 255.255.255.0
  • Default Gateway: 192.168.1.1

To list all available network interfaces with configuration use the command ifconfig

ifconfig

Following command will help you to only the names of network interfaces

ifconfig -a | sed 's/[ \t].*//;/^$/d'

eth0
lo

Or use the command “nmcli” to get interface summary

nmcli -p dev   #or  nmcli device 

Assuming you know IP address, netmask, run ifconfig with those details to assign IP address as follows

ifconfig eth0 192.168.1.3 netmask 255.255.255.0 up

To connect to the rest of the word need default gateway. It is typically the IP address of the router that connects internal network and public internet. Use route command to add the default gateway

route add default gw 192.168.1.1

Default route is also gateway of last resort. Which is typically the LAN IP address of router which is in between WAN and LAN

At this point you might be able to access internet and communicate with your peers if IP and netmask are valid. But, system may not be able to resolve domain names unless nameserver is configured to use for domain name resolution. Use DNS of ISP or local DNS server, otherwise use the public google DNS 8.8.8.8 . Add this setting to file /etc/resolv.conf so networking service can use

echo 'nameserver 8.8.8.8' > /etc/resolv.conf

Verify the connectivity and DNS using the following commands

Use the well known ping tool to ping any valid website to test both name resolution and internet access

ping getkt.com 

Make these changes permanent by adding them into the device specific file at /etc/sysconfig/network-scripts/<inet> on CentOS

If you want to make these change permanent across reboots, add them into file /etc/sysconfig/network-scripts/<interface>

Configure static IP address on CentOS editing network-scripts

Take the backup of existing file and update with relevant defaults that would suite your need as follows

TYPE=Ethernet
PROXY_METHOD=none
BOOTPROTO=none
IPADDR=10.0.2.15
PREFIX=24
GATEWAY=10.0.2.2
DNS1=8.8.8.8
DNS2=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
#It is specific to device
UUID=07a7c069-03a0-4cbe-aed1-6fce29273f06
DEVICE=enp0s3
NAME=enp0s3
ONBOOT=yes

Options IPADDR, GATEWAY and PREFIX may be different according to your network. Contact admin if you are not sure

After file is updated with desired configuration restart network service. Use systemctl to restart on older CentOS distributions as follows

systemctl restart network 

Restart network service on CentOS8 or later as follows

systemctl restart NetworkManager

Or, if nmcli tool is available

nmcli networking off && nmcli networking on 
Step by Step process to assign IP address manually to network interface on Linux (CentOS)
Linux (CentOS) – Steps to assign IP Address manually to network interface

Troubleshooting

  • Make sure both IP address and netmask are correct other wise you may get error “SIOADDRT: Network is unreachable”
  • If you are able to ping IP address but not the domain name. It means DNS is configured. Configure DNS in /etc/resolv.conf file
  • Find how long packets are able to travel using command “tracepath” to determine if the issue is with local network or remote network
Default image
neotam
Naveen T aka neotam. Programming language agnostic, Software architect, Python expert, Networking & DevOps engineer & consultant with 7+ years of experience in creating serious web applications, real time event-driven non blocking applications and database driven applications ranging from small scale to enterprise grade. website
Leave a Reply