Linux entry series 11 — centos7 network service management

Time:2020-7-30

Through the study of the previous article, we have mastered the knowledge of Linux system configuration management. This paper explains centos7 network configuration knowledge.

In order to provide external services, Linux needs to ensure the normal network communication, so it needs to configure the network parameters correctly. This article will explain how to use network manager to configure network parameters, manage network session services, and how to manually bind mode6 mode dual network card to realize network load balancing.

1、 Centos7 Network Overview

1.1 centos7 network management

For network functions, centos7 has changed a lot from the previous version.

In RHEL / CentOS 6 and earlier versions, network functions are implemented through a series of network related script files (such as / etc / init.d/network file and / SBIN / if * file, etc.).

Starting from RHEL / CentOS 7, network functions are provided by network manager as a service by default. Network manager is a daemons that can dynamically control and configure the network, manage network services and network connections, and NetworkManager.service Service (its configuration file / etc / NetworkManager/ NetworkManager.conf , the default is empty, no configuration is required)

Although network related script files in RHEL / CentOS 6 still use network.service Is supported, but recommended NetworkManager.service To configure and manage.And you can only choose one of them, otherwise there will be conflicts.

1.2 network.service

etwork.service Is one of the services provided by the system to be compatible with legacy network functions. The operation of this service is also included in the management of SYSTEMd.

Management command format

systemctl start|stop|restart|status network

Set boot up

systemctl enable network

The above command settings network.service It is loaded automatically when the system starts. Note that when the system starts, / etc / init.d/network will read the ifcfg file and check whether the network manager has started the device. If network manager has started the device, the / etc / init.d/network script does nothing; otherwise / etc / init.d/network starts the device.

1.3 NetworkManager.service

Start network manager (pay attention to case, Linux is case sensitive)

***systemctl start NetworkManager***

Network manager does not execute any scripts by default, and will only be installed in alphabetical order when the following conditions are met.

(1) It is located in the directory / etc / NetworkManager / dispatcher. D /

(2) Have the executable rights of the root user

1.4 network profile

No matter what network.service , or NetworkManager.service Can use the following network related configuration files.

(1) Global profile

File name / etc / sysconfig / network

(2) Network card related configuration files

Folder of configuration files / etc / sysconfig / network scripts/

Note: after modifying the network configuration file, the network connection needs to be reloaded. If the network.service Then use the command: systemctl restart network NetworkManager.service Then use the nmcli command: nmcli connection reload.

2、 Network parameter configuration

The essence of configuration service is configuration file. The following two ways are demonstrated

2.1 configure network by editing profile

In previous versions of rhel7, the prefix of the network card configuration file was eth, the first network card was eth0, the second network card was eth1; and so on. In rhel7, the prefix of network card configuration file begins with ifcfg, and the name of network card forms the name of network card configuration file, such as ifcfg-eno16777736.

Now there is a network card device named ifcfg eno16777736. We configure it to boot automatically, and the IP address, subnet, gateway and other information are manually specified. The steps should be as follows.

(1) Find the network card configuration file

The network card configuration file is stored in the / etc / sysconfig / network scripts directory, such as ifcfg-eno16777736.

[[email protected] ~]# ls /etc/sysconfig/network-scripts/
ifcfg-eno16777736
... omit part of the content

As shown in the figure is the network card configuration file.

(2) Edit network card profile

Since the first article describes the installation of Linux, you have already configured the network parameters and can normally access the Internet, so first check the existing configuration file content.

[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="ea487965-c5bc-4b43-9eab-36445d996179"
ONBOOT="yes"
HWADDR="00:0C:29:BC:5E:EF"
IPADDR0="192.168.78.100"
PREFIX0="24"
GATEWAY0="192.168.78.2"
DNS1="114.114.114.114"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"

You can see that the configuration is generated according to the configuration at that time, and you can also access the Internet normally. But in fact, there are some contents that can be simplified. We first backup the original files, and then manually configure them

[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# ls
ifcfg-eno16777736
... omit part of the content
[[email protected] network-scripts]# cp ifcfg-eno16777736 ifcfg-eno16777736-autobackup
[[email protected] network-scripts]# vi ifcfg-eno16777736

Enter the following and save to exit.

Note: the purpose of backup is to facilitate recovery in case of configuration errors. The backup file is ifcfg-eno16777736-autobackup. Although the prefix is the same, Linux system will not regard it as a network card device.

TYPE="Ethernet"
BOOTPROTO="static"
NAME="eno16777736"
ONBOOT="yes"
IPADDR="192.168.78.100"
NETMASK=255.255.255.0
GATEWAY="192.168.78.2"
DNS1="114.114.114.114"

Configuration Item Description:

Device type: type = Ethernet

Address allocation mode: bootproto = static means to set static IP address

Name of network card: name = eno16777736

Whether to start: onboot = yes

IP address: IPADDR = 192.168.78.100

Subnet mask: netmask = 255.255.255.0 can not be configured

Gateway address: gateway = 192.168.78.2

DNS address: dns1 = 114.114.114.114

The specific parameter values are configured according to the actual situation.

(3) Restart the network card device

[[email protected] ~]# systemctl restart network
[[email protected] ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38: icmp_seq=1 ttl=128 time=41.4 ms

After restarting the network card service, you can ping to indicate that the configuration is successful.

2.2 configure network through nmtui command

Run the network configuration tool through the nmtui command

[[email protected] network-scripts]# nmtui

After execution, enter the main interface of configuration tool

file

Select the operation item through the keyboard direction key, select “edit a connection” here, and press enter to enter the network card selection interface

file

Select the network card, and then select edit through the keyboard direction key (Note: after selecting the network card in this interface, if you select Delete, the corresponding network card configuration file under the configuration file will be deleted)

file

Press enter to enter the editing interface, and then select manual at IPv4 to specify IP address manually

file

If it is in the collapsed state, select show to display the configuration interface. If it is expanded by default, you can configure it directly

file

Configure IP address, gateway and DNS information

file

After configuration, select “OK”

file

Select “quit” to exit

file

At this point, the configuration is complete.

After the configuration is completed, restart the network service manually and test whether it takes effect.

[[email protected] network-scripts]# systemctl restart network
[[email protected] network-scripts]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38: icmp_seq=1 ttl=128 time=41.4 ms
 ... omit part of the content

3、 Network session management

Centos7 uses network manager to provide network services by default. It is a kind of daemons for dynamic management of network configuration, which can keep network devices connected.

Nmcli is a command line based network configuration tool with rich functions. You can use nmcli commands to manage network manager services.

3.1 view network information or network status

[[email protected] network-scripts]# nmcli connection show 
NAME         UUID                                  TYPE            DEVICE      
eno16777736  13756690-ac77-b776-4fc1-f5535cee6f16  802-3-ethernet  eno16777736 
[[email protected] network-scripts]# nmcli con show eno16777736 
connection.id:                          eno16777736
connection.uuid:                        13756690-ac77-b776-4fc1-f5535cee6f16
connection.interface-name:              --
connection.type:                        802-3-ethernet
... omit part of the content

3.2 network session management

Centosl7 system supports network session function and allows users to quickly switch among multiple configuration files, which is very similar to the regional technology in firewalld firewall service.

If we need to specify the IP address of the network manually when we use the notebook computer in the company network, we use DHCP to automatically assign the IP address at home. This requires frequent modification of IP address, but after using the network session function, everything is much simpler – you only need to activate the corresponding network session in different use environment, and then you can realize the automatic switching of network configuration information.

The main implementation principle is to create two different network sessions and activate the corresponding network sessions as required. The format of nmcli command used is: connection add session name type ifname. But generally only for the use of personal computers will have this demand, and our main line is mainly based on the server to explain, so we will not demonstrate the specific usage, interested in self-study can refer to the relevant information.

4、 Binding dual network card

The server requirement of production environment is to provide services 24 hours a day. With the help of network card binding technology, it can not only improve the network transmission speed, but also ensure that when one of the network cards fails, it can still provide network services normally.

There are two ways for centos7 to bind dual network cards: binding or team. Due to the limited space, this paper only uses the bonding mode to demonstrate.

We still add network card to the virtual machine for simulation. The specific steps are as follows:

4.1 shut down the virtual machine

4.2 add network card equipment

file

Note that the mode of the added network card device must be the same. This example uses NAT mode.

3.4 network card configuration

It is necessary to set the network card devices involved in the binding one by one, and configure these originally independent network card devices as a “slave” network card to serve the “master” network card, and should not have its own IP address and other information. After initial setup, they can support network card binding.

Open the virtual machine and enter the network card configuration file to check whether there is no configuration information of the new network card or only the original network card configuration file autobackup-ifcfg-eno16777736.

[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# ls
autobackup-ifcfg-eno16777736  ifdown-sit       ifup-plusb
ifcfg-eno16777736             ifdown-Team      ifup-post
ifcfg-lo

At this time, you can view the newly added network card device through the IP command

[[email protected] network-scripts]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:bc:5e:ef brd ff:ff:ff:ff:ff:ff
    inet 192.168.78.100/24 brd 192.168.78.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:febc:5eef/64 scope link 
       valid_lft forever preferred_lft forever
3: eno33554976:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:bc:5e:f9 brd ff:ff:ff:ff:ff:ff

Configure the parameters of the first network card

[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# vim ifcfg-eno16777736

Enter the following and save

TYPE="Ethernet"
BOOTPROTO=none
NAME="eno16777736"
ONBOOT="yes"
USERCTL="no"
MASTER=bond0
SLAVE=yes

Configure the second network card parameters

[[email protected] network-scripts]# vi ifcfg-eno33554976

Enter the following and save

TYPE="Ethernet"
BOOTPROTO=none
NAME="eno33554976"
ONBOOT="yes"
USERCTL="no"
MASTER=bond0
SLAVE=yes

Note: the name of the network card device must be configured according to its own computer, and it should be changed to the network card name of your computer.

Configure main network card parameters

[[email protected] network-scripts]# vim ifcfg-bond0

Enter the following and save

BOOTPROTO=none
ONBOOT=yes
USERCTL=no
DEVICE=bond0
IPADDR=192.168.78.100
PREFIX=24
GATEWAY=192.168.78.2
DNS1="114.114.114.114"
NM_CONTROLLED=no

4.4 create network card driver file

Linux kernel supports network card binding driver binding. There are seven modes, mode0 to mode6. Among them, mode0, mode1 and mode6 are commonly used, and mode0 is the default mode.

Mode 0 (load balancing mode): normally, both network cards work and are automatically backed up, but port aggregation is required on the switch equipment connected with the local network card of the server to support the binding technology.

Mode 1 (automatic backup mode): usually only one network card works. When it fails, it will automatically replace with another network card.

Mode 6 (balanced load mode): at ordinary times, both network cards work, and automatic backup, without the auxiliary support of switch equipment.

Create a driver file for network card binding to enable the bound bond0 network card device to support binding technology. At the same time, it defines that the network card is bound in mode 6 mode, and the automatic switching time in case of failure is 100 ms.

[[email protected] network-scripts]# vim /etc/modprobe.d/bond.conf

Fill in the following and save

alias bond0 bonding
options bond0 miimon=100 mode=6

4.5 restart network services

After restarting the network service (system restart), the network card binding operation can succeed. Under normal circumstances, only bond0 network card equipment will have IP address and other information.

[[email protected] network-scripts]# systemctl restart network
[[email protected] network-scripts]# ifconfig
bond0: flags=5187  mtu 1500
        inet 192.168.78.100  netmask 255.255.255.0  broadcast 192.168.78.255
        inet6 fe80::20c:29ff:febc:5ef9  prefixlen 64  scopeid 0x20
        ether 00:0c:29:bc:5e:f9  txqueuelen 0  (Ethernet)
        RX packets 28  bytes 2856 (2.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 83  bytes 8273 (8.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
eno16777736: flags=4163  mtu 1500
        ether 00:0c:29:bc:5e:ef  txqueuelen 1000  (Ethernet)
        RX packets 1054  bytes 112121 (109.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 731  bytes 137795 (134.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
eno33554976: flags=6211  mtu 1500
        ether 00:0c:29:bc:5e:f9  txqueuelen 1000  (Ethernet)
        RX packets 28  bytes 2856 (2.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 83  bytes 8273 (8.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 0  (Local Loopback)
        RX packets 12  bytes 1088 (1.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1088 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Ping the IP of the primary network card in the host computer. Ping indicates that the dual network card configuration is successful

file

Note: occasionally, after restarting the network, you can ping, but you can’t Ping Baidu. However, after restarting the virtual machine directly, it is normal again.

4.6 verify the automatic backup function of dual network card

Idea: open another CentOS (the purpose is that Ping will not stop automatically in CentOS, unless it is terminated by pressing Ctrl + C, which can simulate the network card failure and test the automatic backup function), Ping the IP address of the main network card, and turn off the network cards one by one until the network card is closed.

(1) In the other CentOS, Ping the IP address of the primary network card set in the front

[[email protected] ~]# ping 192.168.78.100
PING 192.168.78.100 (192.168.78.100) 56(84) bytes of data.
64 bytes from 192.168.78.100: icmp_seq=1 ttl=64 time=0.844 ms
64 bytes from 192.168.78.100: icmp_seq=2 ttl=64 time=0.405 ms

(2) Simulate the network card failure, stop the network card one by one, and observe the data in step 1

When the virtual machine is on, disconnect the network cards one by one and observe the data receiving and sending in step 1

file

It will be found that when a network card is stopped, data will still be received in step 1, but there is a short terminal in the way. If you continue to stop the second network card, no data will be received. Data will not be received until the network card is opened again.

It can be seen that multiple network cards do have automatic backup function to avoid network disconnection caused by single network card failure.

This paper discusses the network configuration, the next article will explain the disk and storage structure related knowledge.