Skip to main content

Centos Network Configuration

Battambang, Cambodia

Change DNS Server Manually

Check current DNS Server:

cat /etc/resolv.conf

You can change the nameserver IP address by editing this file. Note that your edit is going to be overwritten by the NetworkManager again once you restart your server:

nano  /etc/resolv.conf

Edit/update nameserver entry as follows (you can set maximum 3 nameserver IP address):

# Generated by NetworkManager
search fritz.box
nameserver 192.168.2.1
nameserver 192.168.2.5

DNS Settings using the NetworkManager

Run the following nmcli command to view your network interfaces:

[root@CentOS8 ~]# nmcli connection

NAME UUID TYPE DEVICE
docker0 09eef622-55b4-4d6e-9ee1-a05c1e8d5e69 bridge docker0
enp2s0 280ed14d-7c8b-4586-853d-420df9f65412 ethernet enp2s0

To set up DNS IP address to 192.168.2.1 and 192.168.2.5, run:

nmcli con mod {interfaceNameHere} ipv4.dns '192.168.2.254 192.168.2.18'

For the case above this would be nmcli con mod enp2s0 ipv4.dns '192.168.2.1 192.168.2.5'

Reload new DNS settings by running any one of the following command:

systemctl restart NetworkManager.service

Default gateway on CentOS

On CentOS you can check the routing table with:

[root@CentOS8 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.2.5 0.0.0.0 UG 100 0 0 enp2s0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.2.0 0.0.0.0 255.255.255.0 U 100 0 0 enp2s0

The last line of the table indicates the default gateway of the machine. In this case the default gateway for the enp2s0 interface is 192.168.2.5. Or alternatively use:

[root@CentOS8 ~]# ip route show
default via 192.168.2.5 dev enp2s0 proto static metric 100
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.2.0/24 dev enp2s0 proto kernel scope link src 192.168.2.111 metric 100

You can control default gateway using route command - temporarely (will default back on your next rebood):

route del default gw <default_gateway_ip>
route add default gw <default_gateway_ip>

Or you can use the following command:

ip route replace default via <default_gateway_ip> dev enp2s0

To change default gateway permanently update /etc/sysconfig/network-scripts/ifcfg-{interfaceNameHere} accordingly.

[root@CentOS8 ~]# nano /etc/sysconfig/network-scripts/ifcfg-enp2s0
GATEWAY=192.168.2.5