Detailed explanation of Centos8 configuration static IP implementation

  • 2021-07-06 12:10:41
  • OfStack

After installing centos 8, the following error occurs when you restart the boot network

The error message is as follows:

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

The network service could not be restarted because the network. service network service could not be found.

On rhel8 (including centos8), you can't see any script files in/etc/sysconfig/network-scripts/, and there is no traditional network. service. Therefore, network configuration can only be done by other methods, including NM command toolset. In other words, on rhel8, NM must be turned on or 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

Reload network configuration using nmcli

nmcli c reload

View


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

Supplementary nmcli command

How to use nmcli

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


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 commands most commonly used in nmcli:

nmcli connection
Connection, can be understood as a configuration file, equivalent to ifcfg-ethX. It can be abbreviated as nmcli c

nmcli device
Equipment can be understood as actual network cards (including physical network cards and virtual network cards). It can be abbreviated as nmcli d
In NM, there are two dimensions: connection (connection) and device (device), which is a many-to-one relationship. If you want to match a network card with ip, first of all, NM should be able to accommodate this network card. The network card existing in the device (that is, as can be seen by nmcli d) is the NM nanotube. You can then configure multiple connections for one device (as seen by nmcli c), each of which can be understood as one ifcfg configuration file. At the same time, only one connection of one device can be active. You can switch connections through nmcli c up.

connection has two states:

Active (colored font): Indicates that the current connection is in effect Inactive (normal font): Indicates that the current connection is not effective

device has four common states:

connected: Has been transistorized by NM and currently has active connection disconnected: It has been installed by NM, but there is no active connection at present unmanaged: Uncovered by NM unavailable: Not available, NM is unable to accommodate tubes, which usually occurs when the network card link is down (such as ip link set ens32 down)

Overview of nmcli Common Commands 1


#  View ip (Similar to ifconfig , ip addr ) 
nmcli
 
#  Create connection Configure static ip (Equivalent to configuring ifcfg , of which BOOTPROTO=none , and ifup Startup) 
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 Configuration dynamic ip (Equivalent to configuring ifcfg , of which BOOTPROTO=dhcp , and ifup Startup) 
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 connection List 
nmcli c show

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

#  Overload all ifcfg Or route To connection (Will not take effect immediately) 
nmcli c reload

#  Overload specification 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

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

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

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

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

#  Activate the network card 
nmcli d connect ens32

#  Turn off the wireless network ( NM Wireless network enabled by default) 
nmcli r all off

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

#  Open NM Nanotube 
nmcli n on

#  Shut down NM Acceptance of tube (careful implementation) 
nmcli n off

#  Monitor events 
nmcli m

#  View NM Intrinsic state 
nmcli

#  Detection NM Is it available online 
nm-online

References to ifcfg refer to/etc/sysconfig/network-scripts/ifcfg-ens32 and/etc/sysconfig/network-scripts/route-ens32

nmcli connection Focus


nmcli c show
NAME UUID TYPE DEVICE
ens32 066b4926-b40c-4c28-a5b4-2310d2b96613 ethernet ens32
Column 1 is the name of connection, referred to as con-name for short. (Note that con-name is not a network card name.) Column 2 is UUID for connection The last column is the name of the network card (called device in standard terms), and device can be viewed through nmcil d

When operating on connection, you need to specify an identity, which can be con-name or UUID. If there is an ifcfg file, you can also use the complete path of ifcfg, 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

con-name of nmcli c

At the same time, it corresponds to the file name of ifcfg and NAME= in the content, which indicates the name of the connection (connection). It does not need to be the same as the network card name, and multiple connections can be created for one 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, and will automatically select other connections with one device to take effect instead. The specified connection handover can take effect through nmcli c up.
Note: Modifying con-name via nmcli c modify will only correspondingly modify NAME in the ifcfg file, not the ifcfg file name.

ipv4. method for nmcli c

BOOTPROTO and ipv4.method corresponding to the contents of ifcfg files default to auto, and correspond to BOOTPROTO=dhcp. If ip is specified at this time, it may cause the network card to have ip allocated by dhcp and static ip at the same time. Setting to manual means BOOTPROTO=none, which means 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
type ethernet: You must specify a type when creating a connection. There are many types, which can be seen through nmcli c add type-h, which is specified as ethernet here. con-name ens32 ifname ens32: The first ethX represents the name of the connection (connection), which can be defined arbitrarily and does not need to be the same as the network card name; The second ethX represents the name of the network card, and this ethX must be visible in nmcli d. ipv4. addresses '192.168. 1.100/24, 192.168. 1.101/32': Configure two ip addresses, 192.168. 1.100/24 and 192.168. 1.101/32, respectively ipv4. gateway 192.168. 1.254: Gateway is 192.168. 1.254 ipv4. dns '8.8. 8.8, 4.4. 4.4': dns 8.8. 8.8 and 4.4. 4.4 ipv 4. method manual: Configuring static IP

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 see an additional connection through nmcli c

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

nmcli device Focus

nmcli d connect ens32
The designated network card is managed by NM, and the active connection corresponding to the network card is refreshed at the same time (if the connection configuration has been modified before); If there are connection but they are all in inactive state, one connection is automatically selected and activated; If there is no connection, 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 status of the actual network card, but will only make the corresponding connection inactive. If you restart the system, it will automatically connect. In addition, if the connection of the network card is deleted manually, the status of the network card will automatically change to disconnected.

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

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


Related articles: