Network Configuration
In the early Linux system, the network cards were named eth0, eth1, eth2, etc., but they often did not necessarily correspond to the physical order of the network card interfaces.
- Ethernet interface names start with en, WLAN interface names start with wl, and WWAN interface names start with ww.
- The next character indicates the type of adapter, where o indicates on the motherboard, s indicates a hot-swap slot, and p indicates a PCI interface device.
- The third character is x for merging MAC addresses, it is not used by default, it is available for administrators.
- Finally use the number n for index, ID or port.
- If the name cannot be determined, a traditional name such as ethn is used.
concept
The network has two dimensions: connection and device, which is a many-to-one relationship. If you want to assign an ip to a certain network card, first the NM must be able to manage the network card. The NICs in the device (that is, those that can be seen by nmcli d) are managed by NM. Then, multiple connections can be configured for a device (ie nmcli c can see), each connection can be understood as an ifcfg configuration file. A device can only have one active connection at a time. Connections can be switched via nmcli c up. Networks can be visually edited with the nmuti command.
Connection
Connection, which can be understood as a configuration file, is equivalent to ifcfg-ethX. Can be abbreviated as nmcli c
two states
- Active (with colored font): indicates that the connection is currently in effect
- Inactive (normal font): indicates that the current connection is not in effect
Device
Devices can be understood as actual network cards (including physical network cards and virtual network cards). Can be abbreviated as nmcli d
four states
- connected: has been managed by NM, and there is currently an active connection
- disconnected: has been managed by NM, but there is currently no active connection
- unmanaged: Not managed by NM
- unavailable: Unavailable, the NM cannot manage it, usually when the link of the network card is down (such as ip link set ethX down)
command reference
NetworkManager provides nmcli, nmtui and nm-connection-editor management tools. beat
copy# basic format nmcli [OPTIONS...] [COMMAND] [ARGUMENTS...]
copy# View ip (similar to ifconfig, ip addr) nmcli # Create connection, configure static ip (equivalent to configuring ifcfg, where BOOTPROTO=none, and start ifup) nmcli c add type ethernet con-name ethX ifname ethX ipv4.addr 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.method manual # Create connection, configure dynamic ip (equivalent to configuring ifcfg, where BOOTPROTO=dhcp, and start ifup) nmcli c add type ethernet con-name ethX ifname ethX ipv4.method auto # Modify ip (non-interactive) nmcli c modify ethX ipv4.addr '192.168.1.200/24' nmcli c up ethX # modify ip (interactive) nmcli c edit ethX 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 ethX # Stop connection (equivalent to ifdown) nmcli c down # Delete connection (similar to ifdown and delete ifcfg) nmcli c delete ethX # View connection list nmcli c show # View connection details nmcli c show ethX # Reload all ifcfg or route to connection (will not take effect immediately) nmcli c reload # Overload specified ifcfg or route to connection (will not take effect immediately) nmcli c load /etc/sysconfig/network-scripts/ifcfg-ethX nmcli c load /etc/sysconfig/network-scripts/route-ethX # Effective immediately connection, there are 3 methods nmcli c up ethX nmcli d reapply ethX nmcli d connect ethX # view device list nmcli d # View all device details nmcli d show # View the detailed information of the specified device nmcli d show ethX # Activate network card nmcli d connect ethX # Turn off wireless network (NM enables wireless network by default) nmcli r all off # View NM management status nmcli n # Enable NM management nmcli n on # Close NM management (executed with caution) nmcli n off # listen event nmcli m # View the status of NM itself nmcli # Detect if NM is available online nm-online
When operating on the connection, you need to specify the identifier. The identifier can be con-name, UUID, and if there is an ifcfg file, you can also use the full path of ifcfg, that is, /etc/sysconfig/network-scripts/ifcfg-ethX
create a connection
First of all, we need to clarify a few concepts
device as interface
A connection is a configuration for use by a device, consisting of a set of settings.
Multiple connections may exist to the same device, but only one can be kept active at a time
copynmcli c add type ethernet con-name ethX-test ifname ethX 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 nmcli connection add con-name link2 ifname eno33554960 type ethernet ip4 192.168.12.109/24 gw4 192.168.12.254
type ethernet: The type must be specified when creating a connection. There are many types, which can be seen through nmcli c add type-h, here specified as ethernet.
con-name ethX: 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.
ifname ethX: ethX indicates the network card name, this ethX must be visible in nmcli d.
ipv4.addresses '192.168.1.100/24,192.168.1.101/32': Configure 2 ip addresses, 192.168.1.100/24 and 192.168.1.101/32
ipv4.gateway 192.168.1.254: the gateway is 192.168.1.254
ipv4.dns '8.8.8.8,4.4.4.4': dns are 8.8.8.8 and 4.4.4.4
ipv4.method manual: configure static IP
Example:
View device information
copy[root@server101 ~]# nmcli d status DEVICE TYPE STATE CONNECTION br0 bridge connected Bridge eno16777736 eno16777736 ethernet connected System eno16777736 eno33554960 ethernet disconnected -- eno50332184 ethernet disconnected -- lo loopback unmanaged --
View the information of interface eno33554960, the IP address has not been set
copy[root@server101 ~]# ip addr show eno33554960 3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:33:56:64 brd ff:ff:ff:ff:ff:ff
Create new connection with device eno33554960
copy[root@server101 ~]# nmcli connection add con-name link2 ifname eno33554960 type ethernet ip4 192.168.12.109/24 gw4 192.168.12.254 Connection 'link2' (b1a27f59-b39a-4485-94d8-c48cabde073d) successfully added.
Start the newly created connection link2
copy[root@server101 ~]# nmcli connection up link2 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
View the information of interface eno33554960 again
copy[root@server101 ~]# ip addr show eno33554960 3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:33:56:64 brd ff:ff:ff:ff:ff:ff inet 192.168.12.109/24 brd 192.168.12.255 scope global eno33554960 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe33:5664/64 scope link valid_lft forever preferred_lft forever
Remotely try to ping the newly created network connection,
copyC:\Users\IH1407>ping 192.168.12.109 is Ping 192.168.12.109 has 32 bytes of data: from 192.168.12.109 reply from: byte=32 time<1ms TTL=64 from 192.168.12.109 reply from: byte=32 time<1ms TTL=64 from 192.168.12.109 reply from: byte=32 time<1ms TTL=64 from 192.168.12.109 reply from: byte=32 time<1ms TTL=64 192.168.12.109 of Ping Statistics: data pack: Has been sent = 4,Received = 4,lost = 0 (0% lost), Estimated time for round trip(in milliseconds): the shortest = 0ms,longest = 0ms,average = 0ms
modify connection
copynmcli c modify "con-name" ipv4.addr 10.10.10.1/24 ipv4.gate 10.10.10.254 nmcli c modify "con-name" connection.autoconnect on ## boot self-start nmcli c mod "old_name" con-name "new_name" # Modify connection name