Explain the implementation of static IP configuration in centos8

Time:2021-4-21

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

The error information is as follows:

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

Unable to restart the network service because it cannot be found network.service Network services.

On rhel8 (including centos8), you can’t see any script files in / etc / sysconfig / network scripts /. There is no traditional script file network.service . Therefore, network configuration can only be done through other methods, including nm command toolset. 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

Reload network configuration using nmcli

nmcli c reload

see


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

Complement nmcli command

How to use nmcli

The usage of nmcli is very similar to Linux IP command and Cisco switch command, and supports tab completion (see tips at the end of this article for details). You can also view help at the end of the command through – H, — help and 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

Two commands are most commonly used in nmcli

nmcli connection
Connection, which can be understood as configuration file, is equivalent to ifcfg ethx. It can be abbreviated as nmclic

nmcli device
It can be understood as the actual network card (including physical network card and virtual network card). It can be abbreviated as nmclid
In nm, there are two dimensions: connection and device, which are many to one. If you want to configure IP for a network card, first nm should be able to manage the network card. The network card in the device (that is, what nmclid can see) is the name of nm nanotube. Next, you can configure multiple connections for a device (as nmclic can see), and each connection can be understood as an ifcfg configuration file. At the same time, a device can only have one active connection. The connection can be switched through nmclic up.

There are two states of connection

  • Active (colored font): indicates that the current connection is in effect
  • Inactive (normal font): indicates that the current connection is invalid

Device has four common states:

  • Connected: nm nanotube has been used, and there is currently an active connection
  • Disconnected: the nm nanotube has been disconnected, but there is no active connection at present
  • Unmanaged: unmanaged by NM
  • Unavailable: not available. Nm cannot be used. It usually occurs when the network card link is down (for example, IP link set ens32 down)

List of common nmcli commands

#View IP (similar to ifconfig and IP addr)
nmcli
 
#Create connection and configure static IP (equivalent to configuring 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 ifcfg, where bootproto = DHCP, and ifup start)
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
#Overload all ifcfg or routes to connection (will not take effect immediately)
nmcli c reload
#Overload specifies ifcfg or route to connection (does not take effect immediately)
nmcli c load /etc/sysconfig/network-scripts/ifcfg-ens32
nmcli c load /etc/sysconfig/network-scripts/route-ens32
#There are three ways to make connection effective immediately
nmcli c up ens32
nmcli d reapply ens32
nmcli d connect ens32
#View the device list
nmcli d
#View all device details
nmcli d show
#View the details of the specified device
nmcli d show ens32
#Activate network card
nmcli d connect ens32
#Turn off wireless network (nm enables wireless network by default)
nmcli r all off
#View nm nanotube status
nmcli n
#Turn on nm nanotube
nmcli n on
#Shut down nm nanotube (with caution)
nmcli n off
#Monitoring events
nmcli m
#View the status of nm itself
nmcli
#Check whether nm is available online
nm-online

Ifcfg refers 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
  • The first column is the name of the connection, con for short – Name (note con – Name is not network card name)
  • The second column is the UUID of the connection
  • The last column is the name of the network card (called device name in the standard), which can be viewed through nmcil D

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

Con name of nmcli C

At the same time, it corresponds to the ifcfg file name and the name in the content. This parameter represents the name of the connection. It does not need to be the same as the network card name. It can create multiple connections for a device, but only one connection can take effect at the same time. When there are multiple connections, nmcli C delete will delete the current connection and automatically select other connections of the same device to take effect instead. You can use nmclic up to switch the specified connection into effect.
Note: modifying con name through nmcli C modify will only modify the name in the ifcfg file, but not the ifcfg file name.

Ipv4.method of nmcli C

For bootproto corresponding to ifcfg file content, the default value of ipv4.method is auto, and the corresponding value is bootproto = DHCP. In this case, if IP is specified, the network card may have both DHCP assigned IP and static IP. Setting to manual means bootproto = none, that is, only static IP.

Example: create a 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: when creating a connection, you must specify a type. There are many types, which can be seen through nmcli C add type – H. here you specify Ethernet.
  • Con name ens32 ifname ens32: the first ethx represents the name of the connection, which can be defined arbitrarily and does not need to be the same as the name of the network card; the second ethx represents the name of the network card, which must be visible in nmclid.
  • ipv4 . addresses ‘ one hundred and ninety-two . one hundred and sixty-eight . one . one hundred / twenty-four , one hundred and ninety-two . one hundred and sixty-eight . one . one hundred and one / thirty-two ‘ : configure two IP addresses, 192 . one hundred and sixty-eight . one . one hundred / 24 and 192 . one hundred and sixty-eight . one . one hundred and one / thirty-two
  • ipv4 . gateway 192 . one hundred and sixty-eight . one . 254: the gateway is 192 . one hundred and sixty-eight . one . two hundred and fifty-four
  • IPv4. DNS’ 8.8.8.8,4.4.4 ‘: DNS is 8.8.8.8 and 4.4.4.4
  • ipv4 . Method Manual: configure 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 a connection added through nmclic

Note: if this is the first connection created for ethx, it will take effect automatically; if there is a connection at this time, it 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
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 are connections but they are not active, a connection is automatically selected and activated; if there is no connection, a connection is automatically generated and activated.

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

nmcli d reapply ens32
It is 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 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 restart the system, it will automatically return to the connected and managed yes state. So the command is not very useful. Note: if managed is set to no, nmcli C reload will read the configuration file, but it will not take effect immediately. If nmcli C up ethx is executed, it will take effect immediately, and managed will automatically change to yes.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.