How to modify network configuration by editing configuration files in CentOS/RHEL 7 and 8
DESCRIBING CONNECTION CONFIGURATION FILES
By default, changes made with nmcli con mod name are automatically saved to /etc/ sysconfig/network-scripts/ifcfg-name. That file can also be manually edited with a text editor. After doing so, run nmcli con reload so that NetworkManager reads the configuration changes.
For backward-compatibility reasons, the directives saved in that file have different names and syntax than the nm-settings(5) names. The following table maps some of the key setting names to ifcfg-* directives.
Comparison of nm-settings and ifcfg-* Directive
NMCLI CON MOD | IFCFG-* FILE | EFFECT |
---|---|---|
ipv4.method manual | BOOTPROTO=none | IPv4 addresses configured statically. |
ipv4.method auto | BOOTPROTO=dhcp | Looks for configuration settings from a DHCPv4 server. If static addresses are also set, will not bring those up until we have information from DHCPv4. |
ipv4.addresses “192.0.2.1/24 192.0.2.254” | IPADDR0=192.0.2.1 PREFIX0=24 GATEWAY0=192.0.2.254 | Sets static IPv4 address, network prefix, and default gateway. If more than one is set for the connection, then instead of 0, the ifcfg-* directives end with 1, 2, 3 and so on. |
ipv4.dns 8.8.8.8 | DNS0=8.8.8.8 | Modify /etc/resolv.conf to use this nameserver. |
ipv4.dns-search example.com | DOMAIN=example.com | Modify /etc/resolv.conf to use this domain in the search directive. |
ipv4.ignore-auto-dns true | PEERDNS=no | Ignore DNS server information from the DHCP server. |
ipv6.method manual | IPV6_AUTOCONF=no | IPv6 addresses configured statically. |
ipv6.method auto | IPV6_AUTOCONF=yes | Configures network settings using SLAAC from router advertisements. |
ipv6.method dhcp | IPV6_AUTOCONF=no DHCPV6C=yes | Configures network settings by using DHCPv6, but not SLAAC. |
ipv6.addresses “2001:db8::a/64 2001:db8::1” | IPV6ADDR=2001:db8::a/64 IPV6_DEFAULTGW=2001:db8 | Sets static IPv6 address, ::n1etwork prefix, and default gateway. If more than one address is set for the connection, IPV6_SECONDARIES takes a double-quoted list of space- delimited address/prefix definitions. |
ipv6.dns … | DNS0= … | Modify /etc/resolv.conf to use this nameserver. Exactly the same as IPv4. |
ipv6.dns-search example.com | DOMAIN=example.com | Modify /etc/resolv.conf to use this domain in the search directive. Exactly the same as IPv4. |
ipv6.ignore-auto-dns true | IPV6_PEERDNS=no | Ignore DNS server information from the DHCP server. |
connection.autoconnect yes | ONBOOT=yes | Automatically activate this connection at boot. |
connection.id ens3 | NAME=ens3 | The name of this connection. |
connection.interface- name ens3 | DEVICE=ens3 | The connection is bound to the network interface with this name. |
802-3-ethernet.mac- address . . . | HWADDR= … | The connection is bound to the network interface with this MAC address. |
Modifying Network Configuration
It is also possible to configure the network by directly editing the connection configuration files. Connection configuration files control the software interfaces for individual network devices. These files are usually named /etc/sysconfig/network-scripts/ifcfg-name, where name refers to the name of the device or connection that the configuration file controls. The following are standard variables found in the file used for static or dynamic IPv4 configuration.
IPv4 Configuration Options for ifcfg File
1. Static:
BOOTPROTO=none
IPADDR0=172.25.250.10
PREFIX0=24
GATEWAY0=172.25.250.254
DEFROUTE=yes
DNS1=172.25.254.254
2. Dynamic:
BOOTPROTO=dhcp
3. Either:
DEVICE=ens3
NAME="static-ens3"
ONBOOT=yes
UUID=f3e8(...)ad3e
USERCTL=yes
In the static settings, variables for IP address, prefix, and gateway have a number at the end. This allows multiple sets of values to be assigned to the interface. The DNS variable also has a number used to specify the order of lookup when multiple servers are specified.
After modifying the configuration files, run nmcli con reload to make NetworkManager read the configuration changes. The interface still needs to be restarted for changes to take effect.
[root@host ~]# nmcli con reload
[root@host ~]# nmcli con down "static-ens3"
[root@host ~]# nmcli con up "static-ens3"