Detailed Linux network configuration

One: related network configuration files

1. Network card name configuration related files

Network card name naming rule file:

/etc/udev/rules.d/70-persistent-net.rules

# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:db:c9:5c", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:db:c9:66", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

Modify the network card naming example:

1. Check the network card driver and uninstall the network card driver

[root@rhel6 ~]# ethtool -i eth0
driver: e1000 #Network card driver 
[root@rhel6 ~]# modprobe -r e1000 #Uninstall network card driver

2. Modify the 70-persistent-net.rules file

3. Reload the network card driver and restart the network service

[root@rhel6 ~]# modprobe e1000 #Reload the network card driver
[root@rhel6 ~]# /etc/rc.d/init.d/network restart #Restart the network service

Change the name of the CentOS7.x network card to the traditional naming method:

1. Modify the /etc/default/grbu file

# sed -i.bak -r 's/(GRUB_CMDLINE_LINUX=.*)"/\1 net.ifnames=0"/' /etc/default/grub

2. Generate a new grub configuration file and restart to take effect

grub2-mkconfig -o /etc/grub2.cfg

2. Network configuration related files

Network configuration reference file: /usr/share/doc/initscripts-9.03.53/sysconfig.txt

The configuration of the network card is under: /etc/sysconfig/network-scripts/, the configuration file: ifcfg-network card name

Configuration file example:

[root@rhel6 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static|dhcp|none
IPADDR=192.168.0.6
NETMASK=255.255.255.0
#PREFIX=24 #子网掩码
GATEWAY=192.168.0.1
DNS1=114.114.114.114
DNS2=8.8.8.8
DNS3=1.1.1.1
TYPE=Ethernet
ONBOOT=yes
HWADDR=00:0C:29:DB:C9:5C
#MACADDR=00:0C:29:DB:C9:5A #修改MAC地址
UUID=38d329c5-b1bb-491b-a669-47422cfda764
NM_CONTROLLED=no

Detailed explanation of common configuration parameters of network configuration files:

  • DEVICE: The device to which this profile applies
  • HWADDR: MAC address of the corresponding device
  • BOOTPROTO: The address configuration protocol used when activating this device, commonly used dhcp, static, none, bootp
  • NM_CONTROLLED: NM is the abbreviation of NetworkManager, whether this network card is controlled by NM; it is recommended to be "no" (NetworkManager: a network configuration tool for graphical interface, does not support bridging, it is strongly recommended to close)
  • ONBOOT: Whether to activate this device at system boot
  • TYPE: Interface type, common Ethernet, Bridge
  • UUID: The unique identifier of the device
  • IPADDR: Specifies the IP address
  • NETMASK: subnet mask
  • GATEWAY: default gateway
  • DNS1: The first DNS server points to
  • DNS2: The second DNS server points to
  • USERCTL: Can normal users control this device
  • PEERDNS: If the value of BOOTPROTO is "dhcp", whether to allow the dns server pointing information allocated by the dhcp server to be directly overwritten in the /etc/resolv.conf file

3. Other related configuration files

Routing configuration file: /etc/sysconfig/network-scripts/route-interface

  • NETWOEK/NETMASK via GATEWAY

DNS configuration file: /etc/resolv.conf

  • nameserver DNS_IP

Local network parsing configuration file: /etc/hosts

  • IP  hostname alias

Hostname configuration file:

  • centos6.x:/etc/sysconfig/network
  • centos7.x:/etc/hostname

Two: About the configuration tools of the network

ifconfig

  • -a: View enabled and disabled network card information
  • interface {up|down}: enable or disable the network card
  • interface IP/NETMASK: temporarily set IP
  • interface [-]promisc: Set the network card to work in promiscuous mode
  • -s interface: View the traffic information of the specified network card

route

  • -n: display numerically, do not parse, improve efficiency
  • add {-net | -host} NETWORK/NETMASK gw GATEWAY dev DEVICE Add route
  • {add | del} default gw GATEWAY Add or delete default route
  • del {-net | -host} NETWORK/NETMASK gw GATEWAY delete route
route add -net 10.0.0.0/8 gw 172.20.0.1 dev eth1 #Add a route to the 10.0.0.0 network segment 
route del -net 10.0.0.0/8 gw 172.20.0.1 #Delete the route to the 10.0.0.0 network segment

netstat

  • -n: display numerically, do not parse, improve efficiency
  • -r: View routing table
  • -t: TCP related
  • -u: UDP related
  • -w: bare socket
  • -l: View the ports in the listening state
  • -a: view ports in all states
  • -e: show more detailed information
  • -p: View related process PIDs
  • -i: show network card traffic
  • -Iinterface: View the traffic information of the specified network card == ifconfig -s interface
[root@centos7 ~]# netstat -tnulp #Display TCP, UDP listening status and ports of related processes
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      975/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1073/master         
tcp        0      0 127.0.0.1:2601          0.0.0.0:*               LISTEN      10945/zebra         
tcp6       0      0 :::80                   :::*                    LISTEN      11641/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      975/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1073/master         
udp        0      0 0.0.0.0:68              0.0.0.0:*                           749/dhclient        
udp        0      0 0.0.0.0:17101           0.0.0.0:*                           749/dhclient        
udp6       0      0 :::37053                :::*                                749/dhclient        

ip

  • link
    • set interface {up|down}: enable or disable the network card
    • show interface: Display the specified network card information
  • addr
    • add IP/NETMASK [label interface:#] [scope {global | link | host}] [broadcast IP] dev interface: add configuration temporary address
      • label: specify an alias
      • scope: scope
        • global: the scope is global
        • link: Only the network connected to this network card takes effect
        • host: only the host is available
      • broadcast: set the broadcast address
    • del dev interface [label interface:#]:删除IP
    • flush dev interface [label interface:#]:清空IP
  • route
    • add IP/NETMASK via GATEWAY dev interface: add a route
    • add default via GATEWAY dev interface: add default route
    • del IP/NETMASK via GATEWAY dev interface: delete route
    • flush: clear the routing table
    • show: View routing table

ss

  • -n: display numerically, do not parse, improve efficiency
  • -t: TCP related
  • -u: UDP related
  • -w: bare socket
  • -x: Display unix sock related information
  • -l: View the ports in the listening state
  • -a: view ports in all states
  • -e: show more detailed information
  • -p: View related process PIDs
  • -m: memory usage
  • -o: timer information
  • -s: Display current socket details
  • state TCP_STATE '( dport = :ssh or sport = :ssh )'
    • established
    • listen
    • fin_wait_1
    • fin_wait_2
    • syn_sent
    • syn_recv

nmcli: address configuration tool (CentOS7.x)

Subcommand completion function: yum install bash-completion, depends on epel source

1. View information

[root@centos7 ~]# nmcli device status
[root@centos7 ~]# nmcli connection show

2. Delete the configuration

[root@centos7 ~]# nmcli connection delete ens33

3. Add configuration

[root@centos7 ~]# nmcli connection add con-name ens33 ifname ens33 type ethernet ipv4.method auto connection.autoconnect yes
  • con-name ens33: Configuration file name
  • ifname ens33: Specify the network card device
  • type ethernet: network type ethernet
  • ipv4.method auto: get IP automatically
  • connection.autoconnect yes: auto-start at boot

 4. Switch configuration

[root@centos7 ~]# nmcli connection up ens33

 5. Modify the configuration file name ens33 --> ens33-static

[root@centos7 ~]# nmcli connection modify ens33 con-name ens33-static

 6. Modify the configuration IP

[root@centos7 ~]# nmcli connection modify ens33-static ipv4.addresses 192.168.0.100/24 ipv4.gateway 192.168.0.1 ipv4.method manual
  • ipv4.addresses 192.168.0.100/24: IP address
  • ipv4.gateway 192.168.0.1: gateway
  • ipv4.method manual: manual acquisition, static address must be configured as manual, otherwise the default dynamic

7. Reread the configuration file

[root@centos7 ~]# nmcli connection reload

8. Disconnect and connect network connections

[root@centos7 ~]# nmcli device disconnect ens33
[root@centos7 ~]# nmcli device connect ens33

9. View network configuration details

[root@centos7 ~]# nmcli connection show ens33

10. Add another address to the configuration

[root@centos7 ~]# nmcli connection modify ens33-static +ipv4.addresses 10.0.0.2/8

Three: other network related tools

ifup

ifdown

setup

system-config-network-tui

hostnamectl

  • status
  • set-hostname HOSTNAME

mm-connection-editor

nmtui

nmtui-connect

nmtui-edit

nmtui-hostname

Four: Bonding and Team

++Bonding++

Binding: Bind multiple network cards to the same IP address to provide external services, which can achieve high availability or load balancing.

Operating mode:

mode 0: balance-rr rotation strategy: multiple network cards can send data packets in turn to achieve the function of load balancing

mode 1: active-backup active-backup strategy: the active network card sends data packets, and the other standby

mode 3: broadcast broadcast strategy: each network card will send a packet

Configuration example:

1. Create a bonding device configuration file

# cat >/etc/sysconfig/network-scripts/ifcfg-bond0 <<EOF
DEVICE=bond0
BOOTPROTO=none
BONDING_OPTS="miimon=100 mode=1"
IPADDR=192.168.0.6
PREFIX=24
EOF

miimon=100: link detection every 100ms

2. Configure the slave network card for bonding

[root@rhel6 ~]# cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE=eth0
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
EOF
[root@rhel6 ~]# cat >/etc/sysconfig/network-scripts/ifcfg-eth1 <<EOF
DEVICE=eth1
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
EOF

3. Restart the network service and check the bonding status

# /etc/rc.d/init.d/network restart
[root@rhel6 ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth0 #Now eth0 is working
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:db:c9:5c
Slave queue ID: 0

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:db:c9:66
Slave queue ID: 0

Delete bonding configuration example:

1. Disable bond0 and uninstall the bonding module

# ip link set bond0 down
# modprobe -r bonding

2. Restore the configuration file and restart the network

Attachment: link to official documentation

++Team++

Netgroup: It is a method of grouping together multiple network cards to achieve redundancy and improve throughput.

Working mode: runner

  • broadcast: broadcast
  • roundrobin: rotation
  • activebackup:主备

1. Create a network group interface

[root@centos7 ~]# nmcli connection add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}'

2. Configure team0

[root@centos7 ~]# nmcli connection modify team0 ipv4.addresses 172.20.108.244/16 ipv4.method manual ipv4.gateway 172.20.0.1

3. Create port interface

[root@centos7 ~]# nmcli connection add con-name team0-eth1 type team-slave ifname eth1 master team0
[root@centos7 ~]# nmcli connection add con-name team0-eth2 type team-slave ifname eth2 master team0

 4. Start team0 and its slave interfaces

[root@centos7 ~]# nmcli connection up team0
[root@centos7 ~]# nmcli connection up team0-eth1
[root@centos7 ~]# nmcli connection up team0-eth2

5. Check the working status

[root@centos7 ~]# teamdctl team0 state

6. Configuration file example:

[root@centos7 network-scripts]# cat ifcfg-team0
DEVICE=team0
TEAM_CONFIG="{\"runner\":{\"name\":\"activebackup\"}}"
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
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=team0
UUID=6c68702b-2ec5-4ca3-b7a2-0b609316c75c
ONBOOT=yes
DEVICETYPE=Team
#IPADDR=172.20.108.244
#PREFIX=16
#GATEWAY=172.20.0.1

[root@centos7 network-scripts]# cat ifcfg-team0-eth1
NAME=team0-eth1
UUID=9be1b6bf-3f40-4d93-b686-aff1b6c32ec8
DEVICE=eth1
ONBOOT=yes
TEAM_MASTER=team0
DEVICETYPE=TeamPort

[root@centos7 network-scripts]# cat ifcfg-team0-eth2
NAME=team0-eth2
UUID=5f26b005-4f7d-4c13-af42-2fe98f7ec545
DEVICE=eth2
ONBOOT=yes
TEAM_MASTER=team0
DEVICETYPE=TeamPort
ifcfg-team0 configuration file

7. Delete team0

[root@centos7 ~]# nmcli connection down team0
[root@centos7 ~]# nmcli connectioni delete team0-eth0
[root@centos7 ~]# nmcli connectioni delete team0-eth1

Five: bridge

Bridging: "connecting" several network interfaces on a machine. As a result, the packets received by one of the network ports will be copied to the other network ports and sent out. This enables packets between network ports to be forwarded to each other. A network bridge is such a device, it has several network ports, and these network ports are bridged. The hosts connected to the bridge can communicate with each other through the packet forwarding of the switch.

The packet sent by host A is sent to the eth0 port of switch S1. Since eth0 is bridged with eth1 and eth2, the packet is copied to eth1 and eth2, sent out, and then received by host B and switch S2. S2, in turn, forwards the packet to hosts C and D.

1. Create a bridge

[root@centos7 ~]# nmcli connection add type bridge con-name br0 ifname br0

 

2. Configure the bridge

[root@centos7 ~]# nmcli connection modify br0 ipv4.addresses 192.168.0.7/24 ipv4.method manual

 

3. Add the slave network card to the bridge

[root@centos7 ~]# nmcli connection add type bridge-slave con-name br0-eth0 ifname eth0 master br0

 

4. Enable the bridge and view the status

[root@centos7 ~]# nmcli connection up br0
[root@centos7 ~]# nmcli connection up br0-eth0
[root@centos7 ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.000c295df21e       yes             eth0

 

5. Configuration file example:

[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
STP=yes
BRIDGING_OPTS=priority=32768
TYPE=Bridge
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
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=br0
UUID=94582afc-01a1-406d-a25a-91be7c350c23
ONBOOT=yes
IPADDR=192.168.0.7
PREFIX=24

[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0-eth0
TYPE=Ethernet
NAME=br0-eth0
UUID=9dd2a7fc-f143-4243-89ca-85f223e34348
DEVICE=eth0
ONBOOT=yes
BRIDGE=br0

 

 

 

+Personal study notes+ Recorded at 2018.5.3 16:10

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325249703&siteId=291194637