Linux network configuration (add network card, network card session configuration, network card binding, configure host name, configure routing)

Table of contents

Configure basic information about the network card

Configure the network card through nmcli command

Configure the network card through the configuration network card file

Configure the network card through the nmtui command

Configure the network card through the nm-connection-editor command

Network card advanced configuration

Configure network sessions

Configure network card binding (Bonding)

Configure network card binding through nmcli command

nm-connection-editor performs network card binding (graphical interface)

Network card binding is achieved by editing files

The route command displays and sets routing information

Configure device hostname


Configure basic information about the network card

Configure the network card through nmcli command

nmcli is a command line tool of NetworkManager. Before using this command, you need to ensure that NetworkManager is running.

1. Check NetworkManager status

systemctl status NetworkManager View NetworkManager status

systemctl start NetworkManager starts Net

systemctl restart NetworkManager Restart Net (PID value will change)

systemctl reload NetworkManager Restart Net (PID value will not change)

systemctl is-enabled NetworkManager Check whether Net starts automatically at boot

systemctl enable NetworkManager sets Net to start automatically at boot

2. Before using the nmcli command, you need to check whether NetworkManager has taken over the network settings.

nmcli network checks whether to take over the network configuration (or nmcli n )

nmcli n on set to take over network configuration

nmcli n off Set to cancel taking over network configuration

3. nmcli view command

nmcli network connectivity View network connection status (or nmcli nc )

full network connection is normal

       Limited network connection, but unable to access the Internet

       Portal network connection, but authentication is required before you can access the Internet

       none No network connection

       unknown Unable to identify the network

nmcli general status   to view detailed network connection status (or nmcli gs )

       STATE shows whether the network is connected (con means connected)

       CON Same as nmcli nc command

      WIFI-HW WIFI hardware switch is turned on?

       WIFI WIFI software switch is turned on?

       Is the WWAN-HW WWAN hardware switch turned on?

       WWAN Is the WWAN software switch turned on?

nmcli connection show  displays all network connection information (or nmcli cs )

 nmcli cs –a displays currently started connections (--active)

 nmcli cs network card name displays detailed connection information for a specific network card

nmcli device status    displays the devices recognized by Net and their status (or nmcli ds )

4. Use nmcli to configure basic information of the network card

c is connection, m is the abbreviation of modify

nmcli cm network card ipv4.address 192.168.10.1/24 configure IPv4 address

nmcli cm network card ipv4.method manual Set the IP address to static configuration (default is auto)

nmcli cm network card ipv4.getway 192.168.10.254 modify gateway

nmcli cm network card ipv4.dns 192.168.10.254 configure DNS server

nmcli cm network card+ipv4.dns 192.168.20.1 Add a DNS server

nmcli cm network card connection.autoconect yes set to start at boot

nmcli c up network card Enable network card connection

nmcli c down network card closes the network card connection

nmcli c reload network card Restart the network card connection

nmcli cm network card ipv4.method "" returns the option to the default value ("" indicates the default value)

nmcli c loda network card configuration file reload network card configuration file

Configure the network card through the configuration network card file

Network card configuration file/etc/sysconfig/network-scripts/

cd /etc/sysconfig/network-scripts/ to enter the network card configuration file

vim ifcfg-ens160 edits a certain network card information under the file

nmcli connection reload ens160 Re-enable this network card

nmcli connection up ens160 enable this network card

Configure the content of network card information

TYPE=Ethernet                   indicates the network card type (here it indicates Ethernet)

PROXY_METHOD=none          proxy mode (default is none, indicating no proxy)

BROWSER_ONLY=no             Whether it is only used for browsing the browser (the default is no, which means it is not)

BOOTPROTO=none              What protocol is used for IPv4 address configuration (none does not use the protocol, static assigns addresses statically, dhcp uses dhcp to assign addresses)

DEFROUTE=yes                    Whether to select the default route when the destination address of the data packet does not exist (the default is yes, which means the default route will be selected)

IPV4_FAILURE_FATAL=no          Whether to enable IPv4 fatal error detection (default is no)

IPV6INIT=yes                          whether to allow IPv6 addresses (default is no, indicating that IPv6 is prohibited)

IPV6_AUTOCONF=no                Whether the IPv6 address is automatically configured (default is yes)

I PV6_DEFROUTE=yes                  IPv6 default route (default is yes)

IPV6_FAILURE_FATAL=no            Whether to enable IPv6 fatal error detection (default is no)

I PV6_ADDR_GEN_MODE=stable-privacy   IPv6 address generation model (default is stable-privacy)

NAME=ens160The                          name of this network card configuration file

UUID=f63b96d7-cf29-4afd-890c-5263076313eb    network card unique identification code

DEVICE=ens160                         indicates the name of this network card

ONBOOT=no                            Whether to automatically enable the network card when booting (default is no, indicating not activated)

IPADDR=192.168.20.1                IPv4 address

PREFIX=24                                 IPv4 address prefix

GATEWAY=192.168.20.254          IPv4 gateway

DNS1=192.168.20.254                DNS server for IPv4 address

IPV6ADDR=2001::1/64               IPv6 address and its prefix

IPV6_DEFAULTGW=2001::2        IPv6 Gateway

DNS2=2001::2                            DNS server for IPv6 address

The simplest configuration information

TYPE=Ethernet      

BOOTPROTO=none

NAME=ens160    

DEVICE=ens160   

ONBOOT=no       

IPADDR=192.168.20.1        

PREFIX=24    

GATEWAY=192.168.20.254

DNS1=192.168.20.254 

Configure the network card through the nmtui command

Nmtui configures the network card through a graphical interface (RHEL 7 8 is nmtui, RHEL 5 6 is setup)

nmcli connection up ens160 After the configuration is completed, enable the network card

Configure the network card through the nm-connection-editor command

nm-connection-editor also configures the network card through a graphical interface

nmcli connection up ens160 After successful configuration, enable the network card


Network card advanced configuration

Configure network sessions

The function of network session (can be understood as a network card template )

Since users need to manually modify the IP address or manually specify it as DHCP in different scenarios, which is more cumbersome, the network session function needs to be configured at this time.

By configuring the network session function, users can switch between multiple network card configuration files to obtain different IP addresses (these network card configuration files can share a physical network card), realizing free switching of network configuration information.

Configure network sessions

nmcli connection add con-name home type ethernet ifname ens160

Configure the network session: the name is home, the type is ethernet, the IP address is obtained automatically, and the physical network card used is ens160

nmcli connection add con-name work ifname ens160 autoconnect no type ethernet ip4 192.168.30.1/24 gw4 192.168.30.254

Configure the network session: the name is work, the type is ethernet, the IP address and gateway are manually specified, and the physical network card used is ens160

Enable corresponding network sessions based on scenarios

nmcli cu home    enables home network session (at this time the network card ens160 address is automatically obtained)

nmcli cu work     enables the work network session (at this time the network card ens160 address is manually specified)

nmcli c delete work delete work network session

After configuring a network session, you will find the network card configuration file ifcfg-network session name under the network card configuration file.

After enabling the work network session, it was found that the ens160 network card has been used by the work network card template, and the IP address at this time is also the IP address configured by work.

Configure network card binding (Bonding)

Network card binding, binding multiple network cards to the same IP address to provide services to the outside world (that is, virtualizing multiple network cards into one virtual network card), can achieve load balancing and high availability.

Add a network card to the system (can be understood as adding a network session)

If you find that there is no corresponding network card configuration information after adding a network card, you can use nmcli to generate the network card configuration information.

nmcli c add con-name ens1921 type ethernet ifname ens192

      con-name specifies the network card configuration file name

      ifname specifies the network card name

 Generate a configuration file of type ethernet for the network card ens192. The configuration file is named ifconf-ens1921.

Network card bonding mode

There are seven network card bonding modes (bond0~bond6), and there are three commonly used ones.

mode=0 (balance-rr) balance polling working mode

All network cards/links are in load state, requiring link aggregation to be configured on the switch.

mode=1 (active-backup) active and backup working mode

Only one network card is active. When the working network card fails, the other network card becomes the main network card.

mode=6 (balance-alb) adapter adaptive load mode

All network cards/links are in load status. There is no need to configure link aggregation on the switch side. This is achieved through ARP negotiation.

Configure network card binding through nmcli command

Create a bonded network card and use the physical network card as a slave network card of the bonded network card

nmcli c add type bond con-name bond0 ifname bond0 bond.options "mode=balance-rr"

Create a network card of type bond, the network card name is bond0, the configuration file is named bond0, and the network card binding mode is balance-rr.

nmcli c add type ethernet slave-type bond con-name bond0-port1 ifname ens160 master bond0

Use the ens160 network card as the slave network card of the bound network card band0. After binding, the ens160 network card is renamed bond0-port1.

nmcli c add type ethernet slave-type bond con-name bond0-port2 ifname ens192 master bond0

Use the ens192 network card as the slave network card of the bound network card band0. After binding, the ens192 network card is renamed bond0-port2.

nmcli connection delete bond0-port1 delete bonded network card

Then configure the bound network card (that is, configure the basic information of the network card)

Choose one of the above methods and select the file editing method here.

vim /etc/sysconfig/network-scripts/ifconfig-bond0

TYPE=Bond

BOOTPROTO=none

NAME=bond0

DEVICE=bond0

ONBOOT=yes

IPADDR=192.168.100.1

PREFIX=24

GATEWAY=192.168.100.254

DNS1=192.168.100.254

nmcli cd bond0   close this network card

nmcli cu bond0  enable this network card

View bound network card information

cat /proc/net/bonding/bond0 View detailed information about the bonded network card bond0

nm-connection-editor performs network card binding (graphical interface)

nm-connection-editor enters the graphical interface configuration

 Create a bond network card and add a physical network card to the bond network card

Network card binding is achieved by editing files

  At this time, there is no need to configure the network card information on each individual network card. You only need to configure the information on the bond network card.

Configure bond0 network card information

vim /etc/sysconfig/network-scripts/ifconfig-bond0

TYPE=Bond

BOOTPROTO=none

NAME=bond0

DEVICE=bond0

ONBOOT=yes

IPADDR=192.168.100.1

PREFIX=24

GATEWAY=192.168.100.254

DNS1=192.168.100.254

BONDING_OPTS=”mode=0” Configure the binding mode to 0

Add the physical network card to the bonded network card

vim /etc/sysconfig/network-scripts/ifcfg-bond0-port1

TYPE=Ethernet

NAME=bond0-port1

UUID=51025336-777b-463b-a1ac-8a7bac314a27

DEVICE=ens160 physical network card ens160

ONBOOT=yes

MASTER=bond0 The bound network card is named bond0

SLAVE=yes

vim /etc/sysconfig/network-scripts/ifcfg-bond0-port2

TYPE=Ethernet

NAME=bond0-port2

UUID=a7c6067d-12b2-4abc-a3dc-124a2360009e

DEVICE=ens192 physical network card ens192

ONBOOT=yes

MASTER=bond0 The bound network card is named bond0

SLAVE=yes


The route command displays and sets routing information

route displays current routing table information

route add -net 192.168.10.0 netmask 255.255.255.0 dev ens160

 The route to the 192.168.10.0/24 network segment goes out through the ens160 network card.

 add adds a route (del means delete a route)

 -A sets the address type (default ipv4 when configuring ipv6 address: -A inet6)

 -net Route to a network segment (to the destination network segment)

 -host Route to a host (to the destination host)

 gw sets default gateway

 dev is the outbound interface to this destination address.

 metric sets routing hop count


Configure device hostname

1. Directly modify the configuration file vim /etc/hostname

 systemctl restart systemd-hostnamed.service

Note: After modifying the configuration, you need to restart the hostnamed service.

2. Use nmtui to modify the host name (graphical interface)

systemctl restart systemd-hostnamed.service

3.hostnamectl changes the host name and permanently changes the host name.

hostnamectl set-hostname 123 changes the current hostname to 123

Note: This method needs to be distinguished from hostname 

The hostname command is to temporarily modify the hostnamectl to permanently modify the host name.

4. nmcli command to modify the host name

nmcli general hostname server1

systemctl restart systemd-hostnamed.service

Guess you like

Origin blog.csdn.net/m0_49864110/article/details/129142779