How to Setup Basic Networking on CentOS or RedHat Linux

One of the most essential items on modern servers is networking. Networking on CentOS7 and RHEL7 is managed by service called NetworkManager. We will show steps required to configure and validate networking. In our examples we will use two utilities: ip and nmcli.

Linux networking diagram

Runtime Configuration

IP utility

Show basic configuration:

[[email protected] ~]# ip addr
[[email protected] ~]# ip link

Add IP address to the network device:

[[email protected] ~]# ip addr add 10.0.0.10/24 dev ens33

Verify the new address:

[[email protected] ~]# ip addr
[. . .]
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:60:e6:9d brd ff:ff:ff:ff:ff:ff
inet 192.168.0.15/24 brd 192.168.0.255 scope global dynamic ens33
valid_lft 84877sec preferred_lft 84877sec
inet 10.0.0.10/24 scope global ens33
valid_lft forever preferred_lft forever
[. . .]

Verify new ip address:

[[email protected] ~]# ping 10.0.0.10
PING 10.0.0.10 (10.0.0.10) 56(84) bytes of data.
64 bytes from 10.0.0.10: icmp_seq=1 ttl=64 time=0.083 ms
64 bytes from 10.0.0.10: icmp_seq=2 ttl=64 time=0.062 ms
[. . .]

Persistent Configuration

 

 

 

 

 

 

 

 

nmcli utility

Show nmcli examples:

[[email protected] ~]# man nmcli-examples

Bash completion will help us navigate long commands with many parameters and options:

[[email protected] ~]# rpm -qa | grep bash-completion
bash-completion-2.1-6.el7.noarch

Show which connection is active:

[[email protected] ~]# nmcli device status
DEVICE TYPE STATE CONNECTION
ens33 ethernet connected ens33
lo loopback unmanaged --

Show connection and device status:

[[email protected] ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens33 b7a37611-f0d6-4262-837d-2a457ca37c56 802-3-ethernet ens33
[. . .]

[[email protected] ~]# nmcli device status
DEVICE TYPE STATE CONNECTION
ens33 ethernet connected ens33
lo loopback unmanaged --
[[email protected] ~]#

Add and apply new connection

Add connection:

[[email protected] ~]# nmcli connection add ifname ens33 type ethernet ipv4.addresses 192.168.0.200/24 ipv4.gateway 192.168.0.2
Connection 'ethernet-ens33' (e400e0c7-ed8e-4096-a185-a5df7c0d5008) successfully added.

Verify connection is added:

[[email protected] ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens33 b7a37611-f0d6-4262-837d-2a457ca37c56 802-3-ethernet ens33
ethernet-ens33 e400e0c7-ed8e-4096-a185-a5df7c0d5008 802-3-ethernet --

Bring connection up:

[[email protected] ~]# nmcli connection up ethernet-ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/20)

Verify connection is applied to the device:

[[email protected] ~]# nmcli connection show
NAME UUID TYPE DEVICE
ethernet-ens33 e400e0c7-ed8e-4096-a185-a5df7c0d5008 802-3-ethernet ens33
ens33 b7a37611-f0d6-4262-837d-2a457ca37c56 802-3-ethernet --

Verify device status:

[[email protected] ~]# nmcli device status
DEVICE TYPE STATE CONNECTION
ens33 ethernet connected ethernet-ens33
lo loopback unmanaged --

As we forgot to add dns option we have to modify the connection:

[[email protected] ~]# nmcli connection modify ethernet-ens33 ipv4.dns 8.8.8.8

Activate the connection after the update:

[[email protected] ~]# nmcli connection up ethernet-ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/21)

Show the connection again:

[[email protected] ~]# nmcli connection show
NAME UUID TYPE DEVICE
ethernet-ens33 e400e0c7-ed8e-4096-a185-a5df7c0d5008 802-3-ethernet ens33
ens33 b7a37611-f0d6-4262-837d-2a457ca37c56 802-3-ethernet --

Verify the dns param is added to /etc/resolv.conf:

[[email protected] ~]# cat /etc/resolv.conf
# Generated by NetworkManager
[. . .]
nameserver 8.8.8.8

Ping a host to check if dns works:

[[email protected] ~]# ping home.pl
PING home.pl (212.85.96.1) 56(84) bytes of data.
64 bytes from home.pl (212.85.96.1): icmp_seq=1 ttl=56 time=69.5 ms
[. . .]

Let’s apply the original configuration:

[[email protected] ~]# nmcli connection show
NAME UUID TYPE DEVICE
ethernet-ens33 e400e0c7-ed8e-4096-a185-a5df7c0d5008 802-3-ethernet ens33
ens33 b7a37611-f0d6-4262-837d-2a457ca37c56 802-3-ethernet --

Apply original config:

[[email protected] ~]# nmcli connection up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/22)

Verify the connection:

[[email protected] ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens33 b7a37611-f0d6-4262-837d-2a457ca37c56 802-3-ethernet ens33
ethernet-ens33 e400e0c7-ed8e-4096-a185-a5df7c0d5008 802-3-ethernet --

Verify resolv.conf file:

[email protected] ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.0.1
nameserver 192.168.0.2
[. . .]

Leave a Reply