Linux Network Management

1, Modify network card name

Centos6 network card names are eth0, eth1
Centos7 network card names are ens32, ens33

Because this irregular naming method brings difficulties to later maintenance, it is necessary to modify the network card name to eth0, eth1

1. After installing the operating system, modify the naming rule of the network card to eth0 eth1

1. Modify the network card configuration file

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# mv ifcfg-ens32 ifcfg-eth0
[root@localhost network-scripts]# vim ifcfg-eth0  # Amend as follows
NAME=eth0
DEVICE=eth0

2. Modify the kernel startup parameters, disable the prediction naming rule scheme, and set net Ifnames = 0 biosdevname = 0 parameter off

[root@localhost ~]vim /etc/sysconfig/grub
GRUB_CMDLINE_LINUX="...net.ifnames=0 biosdevname=0 quiet"

3. Run the command GRUB2 mkconfig - O / boot / GRUB2 / GRUB CFG to regenerate GRUB configuration and update kernel parameters

Default kernel parameters(biosdevname=0,net.ifnames=1):  adapter name "ens32"

biosdevname=1,net.ifnames=0: adapter name "em1"

biosdevname=0,net.ifnames=0: adapter name "eth0" (The most traditional way,eth0 eth1 )

4. Restart the system, and then check whether the network card name is successfully modified

[root@localhost ~]# reboot
[root@localhost ~]# ifconfig

2. When the system is newly installed, modify the network card name rule

1. Select Install Centos7 in the installation system and press Tab to set kernel parameters;

2. Add kernel parameter: net ifnames=0 biosdevname=0

3. Check whether the modification is successful. After that, continue to install the system

2, Query network information

[root@localhost ~]# yum install net-tools -y

1. Use the network interface that ifconfig is currently active

[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::2f8d:caad:2f7f:d26b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d2:8f:37  txqueuelen 1000  (Ethernet)
        RX packets 8667  bytes 815250 (796.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2392  bytes 191042 (186.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 568  bytes 47120 (46.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 568  bytes 47120 (46.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


2. View only the status information of the specified network card
For example, eth0

[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::2f8d:caad:2f7f:d26b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d2:8f:37  txqueuelen 1000  (Ethernet)
        RX packets 9368  bytes 883349 (862.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2613  bytes 214395 (209.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3. View the status information of all network cards, including disabled and enabled

[root@localhost ~]# ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::2f8d:caad:2f7f:d26b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d2:8f:37  txqueuelen 1000  (Ethernet)
        RX packets 9394  bytes 885507 (864.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2629  bytes 216497 (211.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 588  bytes 48780 (47.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 588  bytes 48780 (47.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Explanation of terms:

UP: The network card is active 
BROADCAST: Support broadcasting 
RUNNING: Network cable connected
MULTICAST: Support multicast 
MTU: Maximum transmission unit(byte),The maximum packet that the interface can transmit at one time
inet: display IPv4 Address line
inet6: display IPv6 Address line
enther: Hardware(MAC)address
 txqueuelen: Transmission buffer length size
RX packets: Received packets
TX packets: Packets sent
errors: Total number of errors received   
dropped: An error occurred in the copy and was discarded
collisions: Network signal conflict, If the value is not 0, there may be a network failure

4. Use the ip command to view the current address

[root@localhost ~]# ip a
[root@localhost ~]# ip addr

View specific network card information

[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP①,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    ②link/ether 00:0c:29:d2:8f:37 brd ff:ff:ff:ff:ff:ff
    ③inet 10.0.0.100/24 brd④ 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    ⑤inet6 fe80::2f8d:caad:2f7f:d26b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

①: Active interface is UP
②: Link Row specifies the name of the device MAC address
③: inet Line display IPv4 Address and prefix
④: The broadcast address, scope, and device name are on this line
⑤: inet6 Line display IPv6 information

5. Use the ip -s link show eth0 command to view network performance statistics, such as sent and transmitted packets, errors, and discards

[root@localhost ~]# ip -s link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:d2:8f:37 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    907407     9654     0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    238179     2786     0       0       0       0  

3, Configure network address

By default, the CentOS7 system uses NetworkManager to provide network services. This is a daemon that dynamically manages network configuration and can keep network devices connected. The command line and graphical configuration tools provided by NetworkManager set the network. The configuration files saved are in / etc / sysconfig / network scripts directory. The tools include nmcli and nmtui

NetworkManager has the following two concepts to understand:

  • Device physical device, such as eth0, eth1, etc. (device)
  • Connection connection settings, specific network configuration scheme (NAME)
  • A physical device can have multiple sets of logical connection configurations, but only one connection configuration can be used at the same time

1 use the nmcli device command to view the device status

[root@localhost network-scripts]# nmcli device
DEVICE  TYPE      STATE      CONNECTION 
eth0    ethernet  connected  eth0       
lo      loopback  unmanaged  --  

# Specify to view the detailed status of a device
[root@localhost network-scripts]# nmcli dev show eth0

2. Use the nmcli connection command to view the connection status

[root@localhost network-scripts]# nmcli connection
NAME  UUID                                  TYPE      DEVICE 
eth0  e0b1c794-feeb-4010-bf8d-67f549901050  ethernet  eth0   


# View the status of all active connections
[root@localhost network-scripts]# nmcli con show --active
NAME  UUID                                  TYPE      DEVICE 
eth0  e0b1c794-feeb-4010-bf8d-67f549901050  ethernet  eth0  

# View the specified connection status
[root@localhost network-scripts]# nmcli con show eth0

3.nmcli configure IP address
Use nmcli to create a static connection and configure IP, mask, gateway, etc

  1. Add a connection configuration and specify the connection configuration name
  2. Bind the connection configuration to the physical network card device
  3. Configure the network card type, and whether the network card is powered on
  4. What mode does the network card use to configure IP addresses (static, dhcp)
  5. Configure the ip address, mask, gateway, DNS, etc. of the network card
[root@localhost network-scripts]# nmcli connection add \
> con-name eth0-static \  
> ifname eth0 \
> type ethernet \
> autoconnect yes \
> ipv4.addresses 10.0.0.222/24 \
> ipv4.gateway 10.0.0.2 \
> ipv4.dns 223.5.5.5 \
> ipv4.method manual 
Connection 'eth0-static' (3f9559ab-ea8f-4016-be76-7ce946b90689) successfully added.

activation eht0-static At this time, the middle end will be offline
[root@localhost network-scripts]# nmcli connection up eth0-static

# Reopen a middle end and connect 10.0.0.222
[root@localhost ~]# nmcli connection show 
NAME                UUID                                  TYPE      DEVICE  
eth0-static         3f9559ab-ea8f-4016-be76-7ce946b90689  ethernet  eth0   
eth0                e0b1c794-feeb-4010-bf8d-67f549901050  ethernet  --   

If you want to discard the previous configuration
[root@localhost ~]# nmcli connection delete eth0-static
 Will put/etc/sysconfig/network-scripts/ifcfg-eth0-static File deletion

4.nmcli modify IP address

  • Cancel eht0-static connection and activate the network automatically after startup
[root@localhost ~] nmcli connection modify eht0-static autoconnect no
  • Modify dns configuration of eht1 static connection
[root@localhost ~]# nmcli connection modify eht0-static ipv4.dns 8.8.8.8
  • Add dns to the connection. Some settings can be added or removed by + / - 0
[root@localhost ~]# nmcli connection modify eht0-static \+ipv4.dns 8.8.8.8
  • Replace the static IP and default gateway of the connection
[root@localhost ~]# nmcli connection modify eht0-static ipv4.addresses 10.0.0.111/24 ipv4.gateway 10.0.0.254
  • nmlci only modified and saved the configuration. To activate the changes, you need to reactivate the connection
[root@localhost ~]# nmcli connection down eth0-static && nmcli connection up eht0-static
  • Delete self built connection
[root@localhost ~]# nmcli connection delete eht0-static

5.nmcli management configuration file
Using nmcli to manage the / etc / sysconfig / network scripts / configuration file is actually to customize the configuration file of a network card, and then add it to the NetworkManager service for management;

  1. Add physical network card
  2. Copy the configuration file (can be consistent with the device name)
  3. Modify the configuration, such as UUID, connection name, device name, IP address, etc
  4. Reload network configuration
  5. Enable connection and check

1. Add a physical device and copy a network card configuration file

[root@localhost network-scripts]# cp ifcfg-eth0-static ifcfg-eth1
# Or create vim ifcfg-eth1 manually

2. Modify the network card configuration file as follows:

[root@localhost network-scripts]# cat ifcig-eth1
TYPE=Ethernet
BOOTPROTO=static # Network card type none; static; dhcp;  Static, virtual machine and docker container instances are selected for hardware servers, and DHCP is generally used
IPADDR=10.0.0.111 # IP address
PREFIX=24 # Subnet mask or write NETMASK=255.255.255.0
DEFROUTE=yes # Default route (tomorrow's route)
NAME=eth1 # Linked configuration name
DEVICE=eth1 # Equipment name
ONBOOT=yes # Start up or not
DNS1=223.5.5.5
DNS2=114.114.114.114

3. Reload the connection connection so that the NetworkManager service can recognize and add custom network card configuration;

[root@localhost network-scripts]# nmcli connection reload

View connection status
[root@localhost ~]# nmcli connection
NAME                UUID                                  TYPE      DEVICE 
eth0                e0b1c794-feeb-4010-bf8d-67f549901050  ethernet  eth0   
eth1                9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04  ethernet  eth1   
eth0-static         3f9559ab-ea8f-4016-be76-7ce946b90689  ethernet  -- -     

You can see that it is already a dual network card. The eth0 configuration file is being used by the eth0 network card and the eth1 configuration file is being used by the eth1 network card. Re open the middle end and connect it with address 10.0.0.111. It is found that it can be connected.

4. The eth1 connection configuration file has been associated with the eth1 physical device. If you want to modify the IP address, you can use the following two methods;

# Mode 1: modify and then reload the configuration in nmcli modify mode
[root@localhost ~]# nmcli modify eth1-static ipv4.address 10.0.0.233/24
[root@localhost ~]# nmcli down eth1 && nmcli up eth1

# Mode 2: modify vim, reload first, and then reload
[root@oldxu network-scripts]# cat ifcfg-eth1-static
 ...
 IPADDR=10.0.0.234
 ...
[root@localhost ~]# nmcli connection reload
[root@localhost ~]# nmcli connection down eth1 && nmcli connection up eth1

Of course, the systemctl restart network command is still reserved in CentOS 7 to reload the network card information, but it is not recommended. It is still recommended to use network manager for management

Keywords: Linux Operation & Maintenance socket

Added by newbtophp on Sat, 01 Jan 2022 14:06:30 +0200