Explain the implementation of Centos8 static IP configuration

After installing centos 8, when restarting the network, the following error will appear

The error message is as follows:

Failed to start network.service: Unit network.service not found.

It means that the network service cannot be restarted because the network.service network service cannot be found.

On rhel8 (including centos8), there is no script file in /etc/sysconfig/network-scripts/, there is no traditional network.service. Therefore, only through other methods for network configuration, including the NM command tool set. In other words, on rhel8, NM must be turned on, otherwise the network cannot be used.

Manually configure static IP

cat /etc/sysconfig/network-scripts/ifcfg-ens32
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens32
UUID=066b4926-b40c-4c28-a5b4-2310d2b96613
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.1.200
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
DNS1=223.5.5.5
PREFIX=24

Use nmcli to reload the network configuration

nmcli c reload

View

nmcli c
NAME UUID                 TYPE DEVICE
ens32 066b4926-b40c-4c28-a5b4-2310d2b96613 ethernet ens32  

Supplement nmcli command

How to use nmcli

The use of nmcli is very similar to the linux ip command and cisco switch command, and it supports tab completion (see Tips at the end of this article for details). You can also view the help through -h, -help, and help at the end of the command.

nmcli --help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }
 
OPTIONS
- o[verview] overview mode(hide default values)-t[erse] terse output
- p[retty] pretty output
- m[ode] tabular|multiline output mode
- c[olors] auto|yes|no whether to use colors in output
- f[ields]<field1,field2,...>|all|common specify fields to output
- g[et-values]<field1,field2,...>|all|common shortcut for-m tabular -t -f
- e[scape] yes|no escape columns separators in values
- a[sk] ask for missing parameters
- s[how-secrets] allow displaying passwords
- w[ait]<seconds>set timeout waiting for finishing operations
- v[ersion] show program version
- h[elp] print this help
 
OBJECT
g[eneral] NetworkManager's general status and operations
n[etworking] overall networking control
r[adio] NetworkManager radio switches
c[onnection] NetworkManager's connections
d[evice] devices managed by NetworkManager
a[gent] NetworkManager secret agent or polkit agent
m[onitor] monitor NetworkManager changes

There are two most commonly used commands in nmcli:

nmcli connection
Translated as a connection, it can be understood as a configuration file, equivalent to ifcfg-ethX. Can be abbreviated as nmcli c

nmcli device
Translated as equipment, it can be understood as the actual network card (including physical network card and virtual network card). Can be abbreviated as nmcli d
In NM, there are two dimensions: connection and device. This is a many-to-one relationship. If you want to configure ip for a network card, NM must be able to handle the network card first. The network card (that can be seen by nmcli d) in the device is managed by NM. Then, you can configure multiple connections for a device (that can be seen by nmcli c), and each connection can be understood as an ifcfg configuration file. At the same time, only one connection of a device can be active. You can switch the connection through nmcli c up.

The connection has 2 states:

There are 4 common states of device:

List of commonly used commands in nmcli

# View ip (similar to ifconfig, ip addr)
nmcli
 
# Create connection, configure static ip (equivalent to configure ifcfg, where BOOTPROTO=none, and ifup starts)
nmcli c add type ethernet con-name ens32 ifname ens32 ipv4.addr 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.method manual
 
# Create connection, configure dynamic ip (equivalent to configure ifcfg, where BOOTPROTO=dhcp, and ifup starts)
nmcli c add type ethernet con-name ens32 ifname ens32 ipv4.method auto
# Modify ip (non-interactive)
nmcli c modify ens32 ipv4.addr '192.168.1.200/24'
nmcli c up ens32
# Modify ip (interactive)
nmcli c edit ens32
nmcli> goto ipv4.addresses
nmcli ipv4.addresses> change
Edit 'addresses' value:192.168.1.200/24
Do you also want to set'ipv4.method' to 'manual'?[yes]: yes
nmcli ipv4> save
nmcli ipv4> activate
nmcli ipv4> quit
# Enable connection (equivalent to ifup)
nmcli c up ens32
# Stop connection (equivalent to ifdown)
nmcli c down
# Delete connection (similar to ifdown and delete ifcfg)
nmcli c delete ens32
# View the connection list
nmcli c show
# View connection details
nmcli c show ens32
# Reload all ifcfg or route to connection (not effective immediately)
nmcli c reload
# Reload the specified ifcfg or route to connection (will not take effect immediately)
nmcli c load /etc/sysconfig/network-scripts/ifcfg-ens32
nmcli c load /etc/sysconfig/network-scripts/route-ens32
# Immediately effective connection, there are 3 methods
nmcli c up ens32
nmcli d reapply ens32
nmcli d connect ens32 
# View device list
nmcli d
# View all device details
nmcli d show
# View detailed information of the specified device
nmcli d show ens32
# Activate the network card
nmcli d connect ens32
# Turn off wireless network (NM enables wireless network by default)
nmcli r all off
# View NM management status
nmcli n
# Turn on NM management
nmcli n on
# Close NM management (execute with caution)
nmcli n off
# Listen for events
nmcli m
# View the status of NM itself
nmcli
# Check if NM is available online
nm-online

The ifcfg mentioned refers to /etc/sysconfig/network-scripts/ifcfg-ens32 and /etc/sysconfig/network-scripts/route-ens32

nmcli connection key points

nmcli c show
NAME UUID TYPE DEVICE
ens32 066b4926-b40c-4c28-a5b4-2310d2b96613 ethernet ens32

When operating on the connection, you need to specify the identifier. The identifier can be con-name, UUID, or the full path of ifcfg if there is an ifcfg file, that is, /etc/sysconfig/network-scripts/ifcfg-ens32

nmcli c show ens32
nmcli c show cae3f1ef-e79a-46c3-8e0c-946b91a65e11
nmcli c show /etc/sysconfig/network-scripts/ifcfg-ens32

the con-name of nmcli c

At the same time, it corresponds to the file name of ifcfg and the NAME= in the content. This parameter indicates the name of the connection. It does not need to be the same as the network card name. Multiple connections can be created for a device (device), but only one connection can take effect at the same time . When there are multiple connections, nmcli c delete deletes the current connection, it will automatically select other connections of the same device to take effect. You can use nmcli c up to make the specified connection switch take effect.
Note: Modifying con-name through nmcli c modify will only modify the NAME in the ifcfg file instead of the ifcfg file name.

ipv4.method of nmcli c

Corresponding to the BOOTPROTO of the ifcfg file content, ipv4.method defaults to auto, and corresponds to BOOTPROTO=dhcp. At this time, if ip is specified, it may cause the network card to have both dhcp assigned ip and static ip. Setting to manual means BOOTPROTO=none, that is, only static ip.

Example: Create a connection (connection)

nmcli c add type ethernet con-name ens32-test ifname ens32 ipv4.addresses '192.168.1.100/24,192.168.1.101/32' ipv4.routes '10.0.0.0/8 192.168.1.10,192.168.0.0/16 192.168.1.11' ipv4.gateway 192.168.1.254 ipv4.dns '8.8.8.8,4.4.4.4' ipv4.method manual

The corresponding ifcfg and dns are

# /etc/sysconfig/network-scripts/ifcfg-ens32-test
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.1.100
PREFIX=24
IPADDR1=192.168.1.101
PREFIX1=32
GATEWAY=192.168.1.254
DNS1=8.8.8.8
DNS2=4.4.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens32-test
UUID=9a10ad89-437c-4caa-949c-a394a6d28c8d
DEVICE=ens32
ONBOOT=yes
 
# /etc/resolv.conf
nameserver 8.8.8.8
nameserver 4.4.4.4

At this point, you should be able to see an additional connection through nmcli c

Note: If this is the first connection created for ethX, it will take effect automatically; if there is already a connection at this time, the connection will not take effect automatically. You can execute nmcli c up ethX-test to switch to take effect

nmcli device focus

nmcli d connect ens32
The NM manages the specified network card and refreshes the active connection corresponding to the network card (if the connection configuration has been modified before); if there is a connection but are in an inactive state, a connection is automatically selected and activated; if there is no connection, Then one is automatically generated and activated.

nmcli d disconnect ens32
Let NM temporarily not manage the specified network card. This operation will not change the link state of the actual network card, but will only make the corresponding connection inactive. If you restart the system, it will automatically connect again. In addition, if the connection of the network card is deleted manually, the status of the network card will automatically become disconnected.

nmcli d reapply ens32
Specially used to refresh the connection, provided that the device of the network card is in the connected state, otherwise an error will be reported.

nmcli d set ens32 autoconnect yes|no managed yes|no
You can set whether to automatically connect and whether to automatically manage, but it can only be used in the current boot state after testing. If these two parameters are set to no, and then restart the system, it will automatically return to the connected and managed yes state. Therefore, the command is of little use. Note: If managed is set to no, then nmcli c reload will read the configuration file, but it will not take effect immediately, and then if you execute nmcli c up ethX, it will take effect immediately, and managed automatically becomes yes.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Explain the implementation of Centos8 static IP configuration
Implementation of CentOS8.0 Network Configuration
Summary of CentOS method to set static IP
Set static IP under Centos
Centos7 installation and configuration of Jenkins
Detailed examples of Centos6 network configuration
Simple implementation of creating centos rootfs
Installation and configuration of redis under centos7
Python|Interestingly explain the mystery of ID number
The implementation of the Ubuntu18.04 installation Pycharm tutorial
Centos 8.1.1911 solves the problem of yum reinstallation
Set static IP for CentOS in VMware
The fourth installment of Zabbix under CentOs7
CentOS8 yum/dnf configuration method of domestic sources
[Centos8] The bumpy process of installing docker
Centos7 configuration host name and IP address
Installation and configuration of JDK in CentOS 7 system
Summarize the knowledge points of Centos7 system reinforcement
The basic configuration and interface beautification of Ubuntu
Explain how to set static IP on ubuntu14.04
The actual combat of rpm packaging under CentOS6
Installation and configuration of rsync server under CentOS 6.5
Installation and configuration of CentOS 7 in VMware Workstation
IP and network settings of CentOS Linux 7 VMware
Modify the default encoding of mysql5.6 under CentOS7
Distributed deployment of Apollo configuration center under CentOS8
Python writes the game implementation of fishing master
CentOS7.0 network configuration
CentOS 7.0 network configuration
CentOS7 basic configuration
Centos MySQL8 configuration
Detailed explanation of static DNS configuration method in Ubuntu
Centos7 adjusts the size of the root directory mount partition
Detailed explanation of the implementation steps of Python interface development
Tencent cloud centos 8.0 single network card configuration multiple IP
Environment configuration of JDK, mysql and tomcat under Centos7