#查看 root@ubuntu22:~# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:29:55:5d brd ff:ff:ff:ff:ff:ff altname enp2s1 inet 10.0.0.159/24 metric 100 brd 10.0.0.255 scope global dynamic ens33 valid_lft 1695sec preferred_lft 1695sec inet6 fe80::20c:29ff:fe29:555d/64 scope link valid_lft forever preferred_lft forever
#修改 root@ubuntu22:~# vim /etc/default/grub GRUB_CMDLINE_LINUX=" net.ifnames=0"
#重读配置文件并重启 root@ubuntu22:~# grub-mkconfig -o /boot/grub/grub.cfg;reboot Sourcing file `/etc/default/grub' Sourcing file `/etc/default/grub.d/init-select.cfg' Generating grub configuration file ... Found linux image: /boot/vmlinuz-5.15.0-43-generic Found initrd image: /boot/initrd.img-5.15.0-43-generic Found linux image: /boot/vmlinuz-5.15.0-41-generic Found initrd image: /boot/initrd.img-5.15.0-41-generic Warning: os-prober will not be executed to detect other bootable partitions. Systems on them will not be added to the GRUB boot configuration. Check GRUB_DISABLE_OS_PROBER documentation entry. done
#再次查看 root@ubuntu22:~# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:29:55:5d brd ff:ff:ff:ff:ff:ff altname enp2s1 altname ens33 inet 10.0.0.159/24 metric 100 brd 10.0.0.255 scope global dynamic eth0 valid_lft 1551sec preferred_lft 1551sec inet6 fe80::20c:29ff:fe29:555d/64 scope link valid_lft forever preferred_lft forever
临时修改网卡名
1 2 3
[root@Rocky86 ~]# ip link set ens160 down [root@Rocky86 ~]# ip link set ens160 name abc [root@Rocky86 ~]# ip link set abc up
# 配置文件位置改变了 [root@rocky ~]# head -2 /etc/sysconfig/network-scripts/readme-ifcfg-rh.txt NetworkManager stores new network profiles in keyfile format in the /etc/NetworkManager/system-connections/ directory.
[root@rocky ~]# cd /etc/NetworkManager/system-connections/ [root@rocky system-connections]# ls ens160.nmconnection
[root@rocky system-connections]# nmcli connection NAME UUID TYPE DEVICE ens160 f334ca6b-bf42-3ab3-ad1f-3aaea7da854c ethernet ens160 ens224 4242c0f6-b9ba-39f4-b11e-fb965a79d709 ethernet ens224 lo f4b4eedb-3433-44b0-a767-6ba41fef4294 loopback lo
root@ubuntu22:/etc/netplan# cat 00-installer-config.yaml # This is the network config written by 'subiquity' network: ethernets: ens33: dhcp4: true version: 2
#查看 root@ubuntu22:/etc/netplan# ip a show eth2 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:29:55:71 brd ff:ff:ff:ff:ff:ff altname enp2s6 altname ens38 inet 192.168.10.66/24 brd 192.168.10.255 scope global eth2 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe29:5571/64 scope link valid_lft forever preferred_lft forever
查看路由
1 2 3 4 5 6 7 8 9 10 11 12 13 14
root@ubuntu22:/etc/netplan# route -n Command 'route' not found, but can be installed with: apt install net-tools root@ubuntu22:/etc/netplan# apt install -y net-tools
root@ubuntu22:/etc/netplan# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 eth0 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0 10.0.0.2 0.0.0.0 255.255.255.255 UH 100 0 0 eth0 192.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 eth2
root@ubuntu22:~# resolvectl status Global Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported resolv.conf mode: stub
Link 2 (eth0) Current Scopes: DNS Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported Current DNS Server: 10.0.0.2 DNS Servers: 10.0.0.2 DNS Domain: localdomain Link 3 (eth1) Current Scopes: DNS Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported Current DNS Server: 10.0.0.2 DNS Servers: 10.0.0.2 180.76.76.76 DNS Domain: magedu.com magedu.org Link 4 (eth2) Current Scopes: none Protocols: -DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
网络配置命令
主机名
hostname
hostname是临时有效,重启后消失
1 2 3 4 5 6 7 8 9 10
hostname [-b] {hostname|-F file} set host name (from file) hostname [-a|-A|-d|-f|-i|-I|-s|-y] display formatted name hostname display host name
#桥接网卡 [root@rocky86 network-scripts]# vim ifcfg-ens192 DEVICE=ens192 NAME=con-ens192 IPADDR=172.16.1.254 PREFIX=16
#NAT网卡 [root@rocky86 network-scripts]# vim ifcfg-ens160 DEVICE=ens160 NAME=con-ens160 IPADDR=10.0.0.8 PREFIX=8
#启用 [root@rocky86 network-scripts]# nmcli conn reload;nmcli con up con-ens160;nmcli con up con-ens192; Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/34) Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/35)
#查看 [root@rocky86 network-scripts]# ip a show ens160 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:23:22:1e brd ff:ff:ff:ff:ff:ff inet 10.0.0.8/8 brd 10.255.255.255 scope global noprefixroute ens160 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe23:221e/64 scope link valid_lft forever preferred_lft forever
[root@rocky86 network-scripts]# ip a show ens192 3: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:23:22:28 brd ff:ff:ff:ff:ff:ff inet 172.16.1.254/16 brd 172.16.255.255 scope global noprefixroute ens192 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe23:2228/64 scope link valid_lft forever preferred_lft forever
#添加路由 [root@rocky86 network-scripts]# route add -net 10.0.0.0/8 dev ens160
[root@rocky86 network-scripts]# route add -net 172.16.0.0/16 dev ens192
#添加指向 192.168.10.0 网段的网关,其出口设备为本机NAT网卡,网关为主机2上的NAT网卡地址 [root@rocky86 network-scripts]# route add -net 192.168.10.0/24 gw 10.0.0.88 dev ens160 [root@rocky86 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 ens160 172.16.0.0 0.0.0.0 255.255.0.0 U 0 0 0 ens192 192.168.10.0 10.0.0.88 255.255.255.0 UG 0 0 0 ens160 #开启路由转发 [root@rocky86 network-scripts]# echo 1 > /proc/sys/net/ipv4/ip_forward
#NAT网卡 [root@rocky86 network-scripts]# vim ifcfg-eth0 DEVICE=eth0 NAME=con-eth0 IPADDR=10.0.0.88 PREFIX=8
#仅主机网卡 [root@rocky86 network-scripts]# vim ifcfg-eth1 DEVICE=eth1 NAME=con-eth1 IPADDR=192.168.10.254 PREFIX=24
#启用 [root@rocky86 network-scripts]# nmcli con reload;nmcli con up con-eth0;nmcli con up con-eth1; Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7) Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8)
#查看 [root@rocky86 network-scripts]# ip a show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:9a brd ff:ff:ff:ff:ff:ff inet 10.0.0.88/8 brd 10.255.255.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fef3:449a/64 scope link valid_lft forever preferred_lft forever
#查看 [root@rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff inet 192.168.10.254/24 brd 192.168.10.255 scope global noprefixroute eth1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fef3:44a4/64 scope link valid_lft forever preferred_lft forever #添加路由表 [root@rocky86 network-scripts]# route add -net 10.0.0.0/8 dev eth0 [root@rocky86 network-scripts]# route add -net 192.168.10.0/24 dev eth1
#添加指向 172.16.0.0 网段的网关,其出口设备为本机NAT网卡,网关为主机1上的NAT网卡地址 [root@rocky86 network-scripts]# route add -net 172.16.0.0/16 gw 10.0.0.8 dev eth0 [root@rocky86 network-scripts]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 eth0 172.16.0.0 10.0.0.8 255.255.0.0 UG 0 0 0 eth0 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
[root@Rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff #禁用网卡 [root@Rocky86 network-scripts]# ip link set eth1 down
#改名 [root@Rocky86 network-scripts]# ip link set eth1 name eth1-test
#启用 [root@Rocky86 network-scripts]# ip link set eth1-test up
#查看 [root@Rocky86 network-scripts]# ip link show eth1-test 3: eth1-test: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff
#查看网卡 [root@Rocky86 network-scripts]# ip address show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff #向设备添加IP地址 [root@Rocky86 network-scripts]# ip address add 10.0.0.110/24 dev eth1
#查看 [root@Rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff inet 10.0.0.110/24 scope global eth1 valid_lft forever preferred_lft forever
#继续添加IP [root@Rocky86 network-scripts]# ip address add 10.0.0.119/24 dev eth1
#查看,一个网卡上有两个IP [root@Rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff inet 10.0.0.110/24 scope global eth1 valid_lft forever preferred_lft forever inet 10.0.0.119/24 scope global secondary eth1 valid_lft forever preferred_lft forever
#查看 [root@Rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff inet 10.0.0.110/24 scope global eth1 valid_lft forever preferred_lft forever inet 10.0.0.119/24 scope global secondary eth1 valid_lft forever preferred_lft forever inet 10.0.0.114/24 scope global secondary eth1:114 valid_lft forever preferred_lft forever
#删除IP [root@Rocky86 network-scripts]# ip a del 10.0.0.119/24 dev eth1
#删除别名 [root@Rocky86 network-scripts]# ip a del 10.0.0.114/24 dev eth1 label eth1:114
#再次查看 [root@Rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff inet 10.0.0.110/24 scope global eth1 valid_lft forever preferred_lft forever
#添加IP,30S生命周期 [root@Rocky86 network-scripts]# ip a change 10.0.0.137/24 dev eth1 preferred_lft 30 valid_lft 30
#查看 [root@Rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff inet 10.0.0.110/24 scope global eth1 valid_lft forever preferred_lft forever inet 10.0.0.137/24 scope global secondary dynamic eth1 valid_lft 25sec preferred_lft 25sec #查看 [root@Rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff inet 10.0.0.110/24 scope global eth1 valid_lft forever preferred_lft forever inet 10.0.0.137/24 scope global secondary dynamic eth1 valid_lft 12sec preferred_lft 12sec #查看 [root@Rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff inet 10.0.0.110/24 scope global eth1 valid_lft forever preferred_lft forever
范例:
1 2 3 4 5 6
#清除网卡上所有IP [root@Rocky86 network-scripts]# ip a flush dev eth1 [root@Rocky86 network-scripts]# ip a show eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:f3:44:a4 brd ff:ff:ff:ff:ff:ff
#重载配置 [root@localhost network-scripts]#nmcli connection reload;nmcli connection up ens224
管理路由
ip route 用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#添加路由 ip route add TARGET via GW dev IFACE src SOURCE_IP
#添加网关: ip route add default via GW dev IFACE
#删除路由: ip route del TARGET
#显示路由: ip route show|list
#清空路由表: ip route flush [dev IFACE] [via PREFIX]
#查看路由过程 ip route get IP
1 2 3 4 5
ip route add 192.168.0.0/24 via 172.16.0.1 ip route add 192.168.1.100 via 172.16.0.1 ip route add default via 172.16.0.1 ip route flush dev eth0 ip route get 8.8.8.8
#查看 [root@Rocky86 network-scripts]# ls ifcfg-con-eth1 ifcfg-eth0
#查看 [root@Rocky86 network-scripts]# nmcli con NAME UUID TYPE DEVICE eth0 5c093cad-84c9-4cfc-8b6f-e1041db357df ethernet eth0 virbr0 e47f77ad-00bf-4e5e-9db5-3ee490e63b2b bridge virbr0 eth1 52203ca3-742c-4ad4-9162-9482d6f2bbef ethernet eth1 con-eth1 6b2c0337-7fca-4e61-854a-7d7fa916c6b0 ethernet --
#让新配置生效 [root@Rocky86 network-scripts]# nmcli co reload;nmcli con up con-eth1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
#再次查看 [root@Rocky86 network-scripts]# nmcli con NAME UUID TYPE DEVICE eth0 5c093cad-84c9-4cfc-8b6f-e1041db357df ethernet eth0 con-eth1 6b2c0337-7fca-4e61-854a-7d7fa916c6b0 ethernet eth1 virbr0 e47f77ad-00bf-4e5e-9db5-3ee490e63b2b bridge virbr0 eth1 52203ca3-742c-4ad4-9162-9482d6f2bbef ethernet --
[root@rocky86 ~]# nmcli con down eth0 Connection 'eth0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
www.jd.com is an aliasfor www.jd.com.gslb.qianxun.com. www.jd.com.gslb.qianxun.com is an aliasfor www.jdcdn.com. www.jdcdn.com is an aliasfor img20.360buyimg.com.s.galileo.jcloud-cdn.com. img20.360buyimg.com.s.galileo.jcloud-cdn.com is an aliasfor img20.jcloudcdn.com. img20.jcloud-cdn.com has address 27.36.125.3
www.jd.com is an aliasfor www.jd.com.gslb.qianxun.com. www.jd.com.gslb.qianxun.com is an aliasfor www.jdcdn.com. www.jdcdn.com is an aliasfor img20.360buyimg.com.s.galileo.jcloud-cdn.com. img20.360buyimg.com.s.galileo.jcloud-cdn.com is an aliasfor img20.jcloudcdn.com. img20.jcloud-cdn.com has address 27.36.125.3
#ifconfig 命令 ifconfig eth0:0 192.168.1.100/24 up ifconfig eth0:0 down
#ip 命令 ip addr add 172.16.1.1/16 dev eth0 ip addr add 172.16.1.2/16 dev eth0 label eth0:0 ip addr flush dev eth0 label eth0:0
为每个设备别名生成独立的接口配置文件,格式为:ifcfg-ethX:xxx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
cat ifcfg-lo:1
DEVICE=lo:1 IPADDR=137.0.0.1 NETMASK=255.0.0.0 NETWORK=137.0.0.0 # If you're having problems with gated making 127.0.0.0/8 a martian, # you can change this to something else (255.255.255.255, for example) BROADCAST=137.255.255.255 ONBOOT=yes NAME=loopback1
cat /etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE=eth0:1 IPADDR=10.0.0.100 PREFIX=8
注意:
建议 CentOS 6 关闭 NetworkManager 服务
网卡别名必须使用静态地址
多网卡 bonding
将多块网卡绑定同一IP地址对外提供服务,可以实现高可用或者负载均衡。直接给两块网卡设置同一 IP 地址是不可以的。通过 bonding,虚拟一块网卡对外提供连接,物理网卡的被修改为相同的MAC地址
容错能力:只有一个slave是激活的(active)。也就是说同一时刻只有一个网卡处于工作状态,其他的slave都处于备份状态,只有在当前激活的slave故障后才有可能会变为激活的(active)。无负载均衡:此算法的优点是可以提供高网络连接的可用性,但是它的资源利用率较低,只有一个接口处于工 作状态,在有 N 个网络接口的情况下,资源利用率为1/N。
#查看 [root@rocky86 network-scripts]# ls ifcfg-bond0 ifcfg-eth0 ifcfg-eth1 ifcfg-eth2
[root@rocky86 network-scripts]# nmcli conn reload;nmcli conn NAME UUID TYPE DEVICE eth0 5c093cad-84c9-4cfc-8b6f-e1041db357df ethernet eth0 bond0 ad33d8b0-1f7b-cab9-9447-ba07f855b143 bond bond0 virbr0 4350d6a9-a2de-45d6-89a2-1a1116f0073c bridge virbr0 eth1 9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04 ethernet eth1 eth2 3a73717e-65ab-93e8-b518-24f5af32dc0d ethernet eth2
[root@rocky86 network-scripts]# ifconfig ......
[root@rocky86 network-scripts]# ip a ......
[root@rocky86 network-scripts]# cat /proc/net/bonding/bond0 Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011) Bonding Mode: fault-tolerance (active-backup) (fail_over_mac active) Primary Slave: None Currently Active Slave: eth1 MII Status: up MII Polling Interval (ms): 100 Up Delay (ms): 0 Down Delay (ms): 0 Peer Notification Delay (ms): 0 Slave Interface: eth1 MII Status: up Speed: 10000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 00:0c:29:f3:44:a4 Slave queue ID: 0 Slave Interface: eth2 MII Status: up Speed: 10000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 00:0c:29:f3:44:ae Slave queue ID: 0
测试
1 2 3 4 5 6
root@ubuntu22:~# ping 192.168.10.100 PING 192.168.10.100 (192.168.10.100) 56(84) bytes of data. 64 bytes from 192.168.10.100: icmp_seq=1 ttl=64 time=0.571 ms 64 bytes from 192.168.10.100: icmp_seq=2 ttl=64 time=1.24 ms 64 bytes from 192.168.10.100: icmp_seq=3 ttl=64 time=1.28 ms ....
#添加bonding接口 [root@rocky86 network-scripts]# nmcli con add type bond con-name bond0 ifname bond0 mode active-backup ipv4.method manual ipv4.addresses 192.168.10.100/8 Connection 'bond0' (d7f8a6d5-1236-43f6-a39a-5996afada9d5) successfully added.
#添加从属接口 [root@rocky86 network-scripts]# nmcli con add type bond-slave ifname eth1 master bond0 Connection 'bond-slave-eth1' (57e21024-104f-4167-8952-4caa90737934) successfully added.
[root@rocky86 network-scripts]# nmcli con add type bond-slave ifname eth2 master bond0 Connection 'bond-slave-eth2' (d50db0ba-30f5-484b-8e47-803fd566f663) successfully added.
#查看 [root@rocky86 network-scripts]# ls ifcfg-bond0 ifcfg-bond-slave-eth1 ifcfg-bond-slave-eth2 ifcfg-eth0
#启动 [root@rocky86 network-scripts]# nmcli con up bond-slave-eth1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/12)
[root@rocky86 network-scripts]# nmcli con up bond-slave-eth2 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)
[root@rocky86 network-scripts]# nmcli con up bond0 Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/14)
Bonding Mode: fault-tolerance (active-backup) Primary Slave: None Currently Active Slave: eth1 MII Status: up MII Polling Interval (ms): 100 Up Delay (ms): 0 Down Delay (ms): 0 Peer Notification Delay (ms): 0 Slave Interface: eth1 MII Status: up Speed: 10000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 00:0c:29:f3:44:a4 Slave queue ID: 0
Slave Interface: eth2 MII Status: up Speed: 10000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 00:0c:29:f3:44:ae Slave queue ID: 0
网络组 Network Teaming
网络组工作模式
网络组:是将多个网卡聚合在一起方法,从而实现冗错和提高吞吐量
网络组不同于旧版中bonding技术,提供更好的性能和扩展性
网络组由内核驱动和teamd守护进程实现
网络组特点
启动网络组接口不会自动启动网络组中的port接口
启动网络组接口中的port接口总会自动启动网络组接口
禁用网络组接口会自动禁用网络组中的port接口
没有port接口的网络组接口可以启动静态IP连接
启用DHCP连接时,没有port接口的网络组会等待port接口的加入
常用工作模式
broadcast
roundrobin
random
activebackup
loadbalance
lacp (implements the 802.3ad Link Aggregation Control Protocol)
#创建port配置文件 [root@rocky86 network-scripts]# vim ifcfg-team0-eth1 DEVICE=eth1 DEVICETYPE=TeamPort TEAM_MASTER=team0 NAME=team0-eth1 ONBOOT=yes
#创建port配置文件 [root@rocky86 network-scripts]# vim ifcfg-team0-eth2 DEVICE=eth2 DEVICETYPE=TeamPort TEAM_MASTER=team0 NAME=team0-eth2 ONBOOT=yes
#启用 [root@rocky86 network-scripts]# nmcli con reload;nmcli con up team0 Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/50)
nmcli 命令实现
格式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#创建网络组接口 nmcli con add type team con-name CON-NAME ifname TEAM-NAME config 'CONFIG-JSON-STRING'
#添加team [root@rocky86 network-scripts]# nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name":"loadbalance"}}' ipv4.addresses 192.168.10.100/8 ipv4.method manual Connection 'team0' (e781a0cc-566a-4b96-8eb8-d1a10f4850a2) successfully added.
#添加网卡 [root@rocky86 network-scripts]# nmcli con add con-name team0-eth1 type team-slave ifname eth1 master team0 Connection 'team0-eth1' (7a2a28a0-2b2e-4f76-a430-831d706a579e) successfully added.
#添加网卡 [root@rocky86 network-scripts]# nmcli con add con-name team0-eth2 type team-slave ifname eth2 master team0 Connection 'team0-eth2' (9e1df88f-018e-46e2-bde5-e09230fb181e) successfully added.
#查看 [root@rocky86 network-scripts]# ls ifcfg-eth0 ifcfg-team0 ifcfg-team0-eth1 ifcfg-team0-eth2
[root@rocky86 network-scripts]# nmcli con reload;nmcli con NAME UUID TYPE DEVICE eth0 5c093cad-84c9-4cfc-8b6f-e1041db357df ethernet eth0 team0 e781a0cc-566a-4b96-8eb8-d1a10f4850a2 team team0 virbr0 4350d6a9-a2de-45d6-89a2-1a1116f0073c bridge virbr0 team0-eth1 7a2a28a0-2b2e-4f76-a430-831d706a579e ethernet eth1 team0-eth2 9e1df88f-018e-46e2-bde5-e09230fb181e ethernet eth2
#启用 [root@rocky86 network-scripts]# nmcli con up team0 Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/29)
[root@rocky86 network-scripts]# nmcli con up team0-eth1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/32)
[root@rocky86 network-scripts]# nmcli con up team0-eth2 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/33)
#查看 [root@rocky86 network-scripts]# teamdctl team0 state setup: runner: roundrobin ports: eth1 link watches: link summary: up instance[link_watch_0]: name: ethtool link: up down count: 0 eth2 link watches: link summary: up instance[link_watch_0]: name: ethtool link: up down count: 0 #断开一块网卡,再看 [root@rocky86 network-scripts]# teamdctl team0 state setup: runner: roundrobin ports: eth1 link watches: link summary: down instance[link_watch_0]: name: ethtool link: down down count: 1 eth2 link watches: link summary: up instance[link_watch_0]: name: ethtool link: up down count: 0
删除
1 2 3 4 5 6 7 8 9 10 11 12
[root@rocky86 network-scripts]# nmcli con down team0 Connection 'team0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/43)
[root@rocky86 network-scripts]# nmcli con del team0 Connection 'team0' (702de3eb-2e80-897c-fd52-cd0494dd8123) successfully deleted.
[root@rocky86 network-scripts]# nmcli con del team0-eth1 Connection 'team0-eth1' (7465825a-f775-d608-7222-8f2fb493423f) successfully deleted.
[root@rocky86 network-scripts]# nmcli con del team0-eth2 Connection 'team0-eth2' (7b86983f-9f30-5ef5-4e97-269d4af8e492) successfully deleted.
#添加网桥 [root@rocky86 network-scripts]# nmcli con add type bridge con-name br0 ifname br0 Connection 'br0' (deedb558-72d9-4cd9-b136-08917304d777) successfully added.
#启用 [root@rocky86 network-scripts]# nmcli con up br0 Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
#加网卡 [root@rocky86 network-scripts]# nmcli con add type bridge-slave con-name br0-port0 ifname ens160 master br0 Connection 'br0-port0' (0ec95062-a194-423c-b120-662f8c9f7bb3) successfully added.
#加网卡 [root@rocky86 network-scripts]# nmcli con add type bridge-slave con-name br0-port1 ifname ens192 master br0 Connection 'br0-port1' (49eba769-25e6-4794-945e-6190e5795cd6) successfully added.
#启用网卡 [root@rocky86 network-scripts]# nmcli con up br0-port0 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
#启用网止 [root@rocky86 network-scripts]# nmcli con up br0-port1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8)
#查看 [root@rocky86 network-scripts]# nmcli con NAME UUID TYPE DEVICE br0 deedb558-72d9-4cd9-b136-08917304d777 bridge br0 virbr0 68d52b85-639b-4019-9c1b-d192e17b1c0a bridge virbr0 br0-port0 0ec95062-a194-423c-b120-662f8c9f7bb3 ethernet ens160 br0-port1 49eba769-25e6-4794-945e-6190e5795cd6 ethernet ens192
[root@rocky86 network-scripts]# bridge link show 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 100 3: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 100
[root@rocky86 network-scripts]# ip link show master br0 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:23:22:1e brd ff:ff:ff:ff:ff:ff 3: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:23:22:28 brd ff:ff:ff:ff:ff:ff
再次测试
1 2 3 4 5 6 7 8 9 10 11 12 13
#client2 ping client3 root@ubuntu22:/etc/netplan# ping 172.16.1.114 Ping 172.16.1.114 (172.16.1.114) 56(84) bytes of data. From 172.16.1.110 icmp_Seq=1 ttl=64 time=2.05ms From 172.16.1.110 icmp_Seq=2 ttl=64 time=2.01ms ......
#client3 ping client2 root@ubuntu22:/etc/netplan# ping 172.16.1.110 Ping 172.16.1.110 (172.16.1.110) 56(84) bytes of data. From 172.16.1.114 icmp_Seq=1 ttl=64 time=2.23ms From 172.16.1.114 icmp_Seq=2 ttl=64 time=1.12ms ......
删除禁用
1 2
[root@rocky86 network-scripts]# nmcli con down br0 [root@rocky86 network-scripts]# nmcli con del br0
[root@centos8 ~]# fping 10.0.0.7 10.0.0.7 is unreachable
[root@centos8 ~]# fping 10.0.0.7 10.0.0.7 is unreachable
[root@centos8 ~]# fping 10.0.0.7 10.0.0.8 10.0.0.8 is alive 10.0.0.7 is unreachable
#-g 选项可以指定网段或地址范围 [root@centos8 ~]# fping -g 10.0.0.0/24 10.0.0.1 is alive 10.0.0.2 is alive 10.0.0.8 is alive 10.0.0.100 is alive ICMP Host Unreachable from 10.0.0.8 for ICMP Echo sent to 10.0.0.3 ICMP Host Unreachable from 10.0.0.8 for ICMP Echo sent to 10.0.0.3 ICMP Host Unreachable from 10.0.0.8 for ICMP Echo sent to 10.0.0.6 ICMP Host Unreachable from 10.0.0.8 for ICMP Echo sent to 10.0.0.6 ......
[root@centos8 ~]# fping -g 10.0.0.5 10.0.0.10 10.0.0.8 is alive ICMP Host Unreachable from 10.0.0.8 for ICMP Echo sent to 10.0.0.6 ICMP Host Unreachable from 10.0.0.8 for ICMP Echo sent to 10.0.0.6 ICMP Host Unreachable from 10.0.0.8 for ICMP Echo sent to 10.0.0.6 10.0.0.5 is unreachable 10.0.0.6 is unreachable 10.0.0.7 is unreachable
#仅列出指定网段上的每台主机,不发送任何报文到目标主机. [root@centos8 ~]#nmap -sL 10.0.0.0/24 Starting Nmap 7.70 ( https://nmap.org ) at 2020-04-23 12:28 CST Nmap scan report for 10.0.0.0 Nmap scan report for 10.0.0.1 ...... Nmap scan report for 10.0.0.254 Nmap scan report for 10.0.0.255 Nmap done: 256 IP addresses (0 hosts up) scanned in 1.04 seconds
#可以指定一个IP地址范围 [root@centos8 ~]#nmap -sP 10.0.0.1-10 Starting Nmap 7.70 ( https://nmap.org ) at 2020-04-23 12:30 CST Nmap scan report for 10.0.0.1 Host is up (0.000081s latency). MAC Address: 00:50:56:C0:00:08 (VMware) Nmap scan report for 10.0.0.2 Host is up (0.00034s latency). MAC Address: 00:50:56:F0:1E:25 (VMware) Nmap scan report for 10.0.0.6 Host is up (0.00031s latency). MAC Address: 00:0C:29:4D:EF:2C (VMware) Nmap scan report for 10.0.0.7 Host is up (0.00036s latency). MAC Address: 00:0C:29:29:F9:26 (VMware) Nmap scan report for 10.0.0.8 Host is up. Nmap done: 10 IP addresses (5 hosts up) scanned in 2.89 seconds
#一次性扫描多台目标主机 [root@centos8 ~]#nmap 10.0.0.6 10.0.0.7 Starting Nmap 7.70 ( https://nmap.org ) at 2020-04-23 12:39 CST Nmap scan report for 10.0.0.6 Host is up (0.00055s latency). Not shown: 998 closed ports PORT STATE SERVICE 22/tcp open ssh 111/tcp open rpcbind MAC Address: 00:0C:29:4D:EF:2C (VMware) Nmap scan report for 10.0.0.7 Host is up (0.00050s latency). Not shown: 999 closed ports PORT STATE SERVICE 22/tcp open ssh MAC Address: 00:0C:29:29:F9:26 (VMware) Nmap done: 2 IP addresses (2 hosts up) scanned in 101.01 seconds
[root@centos8 ~]#nmap -iL hosts.txt Starting Nmap 7.70 ( https://nmap.org ) at 2020-04-23 12:43 CST Nmap scan report for 10.0.0.7 Host is up (0.0024s latency). Not shown: 999 closed ports PORT STATE SERVICE 22/tcp open ssh MAC Address: 00:0C:29:29:F9:26 (VMware) Nmap scan report for 10.0.0.6 Host is up (0.0032s latency). Not shown: 998 closed ports PORT STATE SERVICE 22/tcp open ssh 111/tcp open rpcbind MAC Address: 00:0C:29:4D:EF:2C (VMware) Nmap scan report for 58.87.87.99 Host is up (0.016s latency). Not shown: 998 filtered ports PORT STATE SERVICE 80/tcp open http 3306/tcp open mysql Nmap done: 3 IP addresses (3 hosts up) scanned in 120.33 seconds
#探测目标主机开放的端口,可指定一个以逗号分隔的端口列表(如-PS22,443,80) [root@centos8 ~]#nmap -PS22,80,443 10.0.0.1 Starting Nmap 7.70 ( https://nmap.org ) at 2020-04-23 12:31 CST Nmap scan report for 10.0.0.1 Host is up (0.00042s latency). Not shown: 996 filtered ports PORT STATE SERVICE 135/tcp open msrpc 139/tcp open netbios-ssn 445/tcp open microsoft-ds 8082/tcp open blackice-alerts MAC Address: 00:50:56:C0:00:08 (VMware) Nmap done: 1 IP address (1 host up) scanned in 12.65 seconds
#使用SYN半开放扫描 [root@centos8 ~]#nmap -sS 10.0.0.1 Starting Nmap 7.70 ( https://nmap.org ) at 2020-04-23 12:33 CST Nmap scan report for 10.0.0.1 Host is up (-0.052s latency). Not shown: 996 filtered ports PORT STATE SERVICE 135/tcp open msrpc 139/tcp open netbios-ssn 445/tcp open microsoft-ds 8082/tcp open blackice-alerts MAC Address: 00:50:56:C0:00:08 (VMware) Nmap done: 1 IP address (1 host up) scanned in 10.07 seconds
#扫描开放了TCP端口的设备 [root@centos8 ~]#nmap -sT 10.0.0.1 Starting Nmap 7.70 ( https://nmap.org ) at 2020-04-23 12:34 CST Nmap scan report for 10.0.0.1 Host is up (0.00040s latency). Not shown: 996 filtered ports PORT STATE SERVICE 135/tcp open msrpc 139/tcp open netbios-ssn 445/tcp open microsoft-ds 8082/tcp open blackice-alerts MAC Address: 00:50:56:C0:00:08 (VMware) Nmap done: 1 IP address (1 host up) scanned in 4.52 seconds
#扫描开放了UDP端口的设备 [root@centos8 ~]#nmap -sU 10.0.0.1 Starting Nmap 7.70 ( https://nmap.org ) at 2020-04-23 12:34 CST Nmap scan report for 10.0.0.1 Host is up (0.00046s latency). Not shown: 999 open|filtered ports PORT STATE SERVICE 137/udp open netbios-ns MAC Address: 00:50:56:C0:00:08 (VMware) Nmap done: 1 IP address (1 host up) scanned in 18.52 seconds
#只扫描UDP端口 nmap –e eth1 -sU -O 10.0.0.1
#扫描TCP和UDP端口 nmap -sTU -O 10.0.0.1
#用于扫描目标主机服务版本号 [root@centos8 ~]#nmap -sV 10.0.0.7 Starting Nmap 7.70 ( https://nmap.org ) at 2020-04-23 12:37 CST Nmap scan report for 10.0.0.7 Host is up (0.0011s latency). Not shown: 999 closed ports PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 7.4 (protocol 2.0) MAC Address: 00:0C:29:29:F9:26 (VMware) Service detection performed. Please report any incorrect results at https://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 1.97 seconds