UBUNTU网卡配置
主机名修改
hostnamectl set-hostname ubuntu1804
cat /etc/hostname
网卡改名
#修改配置文件为下面形式
vi /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"
#生效新的grub.cfg文件
grub-mkconfig -o /boot/grub/grub.cfg
#或者
update-grub
#重启生效
reboot
修改名称后需要修改配置文件否则没有地址(一般两个缩进)
cat /etc/netplan/01-netcfg.yaml #自动获取
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
修改后执行
netplan apply命令生效
添加网卡后 配置静态地址
yuml格式尽量用空格缩进
[root@ubuntu1804||15|netplan]#cd /etc/netplan/
[root@ubuntu1804||16|netplan]#ls
01-netcfg.yaml
[root@ubuntu1804||17|netplan]#cp 01-netcfg.yaml 02-netcfg.yaml
[root@ubuntu1804||18|netplan]#vim 02-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth1:
dhcp4: no #自动获取
addresses: [192.168.8.10/24,10.0.0.10/8] #两个地址
gateway4: 10.0.0.3 #网关
nameservers:
search: [magedu.com, magedu.org] #自动补全
addresses: [180.76.76.76, 8.8.8.8, 1.1.1.1] #DNS
百度
netplan apply #重启网络服务
查看ip和gateway
root@ubuntu1804:~#ip addr
root@ubuntu1804:~#route -n
查看DNS
root@ubuntu1804:~#ls -l /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Dec 12 11:36 /etc/resolv.conf ->
…/run/systemd/resolve/stub-resolv.conf
root@ubuntu1804:~#systemd-resolve --status #观察DNS信息
配置多卡静态IP #可以用一个文件代替多个
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
addresses: [10.0.0.18/16]
gateway4: 10.0.0.2 网关
nameservers:
addresses: [223.6.6.6] DNS
eth1:
dhcp4: no
dhcp6: no
addresses: [10.20.0.18/16]
routes: #配置路由
- to: 10.30.0.0/16 #到达哪个网段
via: 10.20.0.1 #网关(指向)
- to: 10.40.0.0/16
via: 10.20.0.1
- to: 10.50.0.0/16
via: 10.20.0.1
- to: 10.60.0.0/16
via: 10.20.0.1
netplan aply 生效
route -n 查看
单网卡桥接
3 4 网卡桥接
[root@ubuntu1804||34|netplan]#cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth1:
dhcp4: no
dhcp6: no
eth2:
dhcp4: no
bridges: #单独列出
br0: #设备名
dhcp4: no
dhcp6: no
addresses: [10.0.0.18/16]
gateway4: 10.0.0.1
nameservers:
addresses: [223.6.6.6]
interfaces:
- eth1 #要桥接的网卡
- eth2
netplan apply #生效
brctl show
ifconfig br0
四块网卡桥接
两个桥接
network:
version: 2
renderer: networkd
ethernets:
eth1:
dhcp4: no
dhcp6: no
eth2:
dhcp4: no
eth3:
dhcp4: no
dhcp6: no
eth4:
dhcp4: no
bridges:
br0:
dhcp4: no
dhcp6: no
addresses: [10.0.0.18/16]
gateway4: 10.0.0.1
nameservers:
addresses: [223.6.6.6]
interfaces:
- eth1 #要桥接的网卡
- eth2
br1:
dhcp4: no
dhcp6: no
addresses: [10.0.0.18/16]
gateway4: 10.0.0.1
nameservers:
addresses: [223.6.6.6]
interfaces:
- eth3 #要桥接的网卡
- eth4
brctl stp br1 off #生成树协议
ifconfig br1 down
brctl delbr br1 #删除
多网卡绑定
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
eth1:
dhcp4: no
dhcp6: no
bonds:
bond0:
interfaces:
- eth3
- eth4
addresses: [172.160.18/16]
gateway4: 172.16.0.1
nameservers:
addresses: [223.6.6.6,223.5.5.5]
parameters:
mode: active-backup
mii-monitor-interval: 100
ifconfig br0 down 关闭
brctl delbr br1 删除
brctl show
cat /proc/net/bonding/bond0 查看活跃的网卡
[root@ubuntu1804||34|netplan]#cat /proc/net/bonding/bond0
双卡绑定加桥接
root@ubuntu1804:~# cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
eth1:
dhcp4: no
dhcp6: no
bonds:
bond0:
interfaces:
- eth0
- eth1
parameters:
mode: active-backup
mii-monitor-interval: 100
bridges:
br0:
dhcp4: no
dhcp6: no
addresses: [10.0.0.18/16]
gateway4: 10.0.0.1
nameservers:
addresses: [223.6.6.6,223.5.5.5]
interfaces:
- bond0
root@ubuntu1804:~# netplan apply
root@ubuntu1804:~# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.96dbd15c1daf no bond0
root@ubuntu1804:~# ifconfig br0
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.18 netmask 255.255.0.0 broadcast 10.0.255.255
inet6 fe80::94db:d1ff:fe5c:1daf prefixlen 64 scopeid 0x20<link>
ether 96:db:d1:5c:1d:af txqueuelen 1000 (Ethernet)
RX packets 97 bytes 6634 (6.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 83 bytes 8286 (8.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@ubuntu1804:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.1 0.0.0.0 UG 0 0 0 br0
10.0.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br0
root@ubuntu1804:~# ifconfig
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST> mtu 1500
ether 2a:20:b7:a8:58:37 txqueuelen 1000 (Ethernet)
RX packets 2592 bytes 230049 (230.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2826 bytes 318282 (318.2 KB)
TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.18 netmask 255.255.0.0 broadcast 10.0.255.255
inet6 fe80::94db:d1ff:fe5c:1daf prefixlen 64 scopeid 0x20<link>
ether 96:db:d1:5c:1d:af txqueuelen 1000 (Ethernet)
RX packets 144 bytes 9890 (9.8 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 117 bytes 12554 (12.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether 2a:20:b7:a8:58:37 txqueuelen 1000 (Ethernet)
RX packets 27 bytes 1990 (1.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether 2a:20:b7:a8:58:37 txqueuelen 1000 (Ethernet)
RX packets 2565 bytes 228059 (228.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2826 bytes 318282 (318.2 KB)
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 1131 bytes 75592 (75.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1131 bytes 75592 (75.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
网卡的多组绑定
可以实现网卡的多组绑定,比如:eth0,eth1绑定至bond0,eth2和eth3绑定bond1
root@ubuntu1804:~#cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
eth1:
dhcp4: no
dhcp6: no
eth2:
dhcp4: no
dhcp6: no
eth3:
dhcp4: no
dhcp6: no
bonds:
bond0:
interfaces:
- eth0
- eth1
addresses: [10.0.0.18/16]
gateway4: 10.0.0.1
nameservers:
addresses: [223.6.6.6,223.5.5.5]
parameters:
mode: active-backup
mii-monitor-interval: 100
bond1:
interfaces:
- eth2
- eth3
addresses: [10.10.0.18/16]
parameters:
mode: active-backup
mii-monitor-interval: 100
routes:
- to: 10.20.0.0/16
via: 10.10.0.1
- to: 10.30.0.0/16
via: 10.10.0.1
- to: 10.40.0.0/16
via: 10.10.0.1
- to: 10.50.0.0/16
via: 10.10.0.1
root@ubuntu1804:~# netplan apply
root@ubuntu1804:~# ifconfig
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST> mtu 1500
inet 10.0.0.18 netmask 255.255.0.0 broadcast 10.0.255.255
inet6 fe80::2820:b7ff:fea8:5837 prefixlen 64 scopeid 0x20<link>
ether 2a:20:b7:a8:58:37 txqueuelen 1000 (Ethernet)
RX packets 273 bytes 23591 (23.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 364 bytes 42455 (42.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
bond1: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST> mtu 1500
inet 10.10.0.18 netmask 255.255.0.0 broadcast 10.10.255.255
inet6 fe80::c035:83ff:fea1:abbb prefixlen 64 scopeid 0x20<link>
ether c2:35:83:a1:ab:bb txqueuelen 1000 (Ethernet)
RX packets 8 bytes 480 (480.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10 bytes 796 (796.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether 2a:20:b7:a8:58:37 txqueuelen 1000 (Ethernet)
RX packets 4 bytes 240 (240.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether 2a:20:b7:a8:58:37 txqueuelen 1000 (Ethernet)
RX packets 269 bytes 23351 (23.3 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 364 bytes 42455 (42.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth2: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether c2:35:83:a1:ab:bb txqueuelen 1000 (Ethernet)
RX packets 4 bytes 240 (240.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth3: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether c2:35:83:a1:ab:bb txqueuelen 1000 (Ethernet)
RX packets 4 bytes 240 (240.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10 bytes 796 (796.0 B)
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 120 bytes 8344 (8.3 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 120 bytes 8344 (8.3 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@ubuntu1804:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.1 0.0.0.0 UG 0 0 0 bond0
10.0.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0
10.10.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond1
10.20.0.0 10.10.0.1 255.255.0.0 UG 0 0 0 bond1
10.30.0.0 10.10.0.1 255.255.0.0 UG 0 0 0 bond1
10.40.0.0 10.10.0.1 255.255.0.0 UG 0 0 0 bond1
10.50.0.0 10.10.0.1 255.255.0.0 UG 0 0 0 bond1
root@ubuntu1804:~# 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: eth1
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:34:df:9b
Slave queue ID: 0
Slave Interface: eth0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:34:df:91
Slave queue ID: 0
root@ubuntu1804:~# cat /proc/net/bonding/bond1
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth3
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth3
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:34:df:b9
Slave queue ID: 0
Slave Interface: eth2
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:34:df:a5
Slave queue ID: 0
网卡多组绑定+多组桥接
oot@ubuntu1804:~# cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
eth1:
dhcp4: no
dhcp6: no
eth2:
dhcp4: no
dhcp6: no
eth3:
dhcp4: no
dhcp6: no
bonds:
bond0:
interfaces:
- eth0
- eth1
parameters:
mode: active-backup
mii-monitor-interval: 100
bond1:
interfaces:
- eth2
- eth3
parameters:
mode: active-backup
mii-monitor-interval: 100
bridges:
br0:
dhcp4: no
dhcp6: no
addresses: [10.0.0.18/16]
gateway4: 10.0.0.1
nameservers:
addresses: [223.6.6.6,223.5.5.5]
interfaces:
- bond0
br1:
dhcp4: no
dhcp6: no
interfaces:
- bond1
addresses: [10.10.0.18/16]
routes:
- to: 10.20.0.0/16
via: 10.10.0.1
- to: 10.30.0.0/16
via: 10.10.0.1
- to: 10.40.0.0/16
via: 10.10.0.1
- to: 10.50.0.0/16
via: 10.10.0.1
root@ubuntu1804:~# netplan apply
root@ubuntu1804:~# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.96dbd15c1daf no bond0
br1 8000.9e02ab0faeb0 no bond1
root@ubuntu1804:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.1 0.0.0.0 UG 0 0 0 br0
10.0.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br0
10.10.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br1
10.20.0.0 10.10.0.1 255.255.0.0 UG 0 0 0 br1
10.30.0.0 10.10.0.1 255.255.0.0 UG 0 0 0 br1
10.40.0.0 10.10.0.1 255.255.0.0 UG 0 0 0 br1
10.50.0.0 10.10.0.1 255.255.0.0 UG 0 0 0 br1
root@ubuntu1804:~# ifconfig
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST> mtu 1500
ether 2a:20:b7:a8:58:37 txqueuelen 1000 (Ethernet)
RX packets 1749 bytes 154597 (154.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1951 bytes 224182 (224.1 KB)
TX errors 0 dropped 2 overruns 0 carrier 0 collisions 0
bond1: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST> mtu 1500
ether c2:35:83:a1:ab:bb txqueuelen 1000 (Ethernet)
RX packets 441 bytes 43111 (43.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21 bytes 1642 (1.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.18 netmask 255.255.0.0 broadcast 10.0.255.255
inet6 fe80::94db:d1ff:fe5c:1daf prefixlen 64 scopeid 0x20<link>
ether 96:db:d1:5c:1d:af txqueuelen 1000 (Ethernet)
RX packets 164 bytes 11692 (11.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 148 bytes 16165 (16.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
br1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.10.0.18 netmask 255.255.0.0 broadcast 10.10.255.255
inet6 fe80::9c02:abff:fe0f:aeb0 prefixlen 64 scopeid 0x20<link>
ether 9e:02:ab:0f:ae:b0 txqueuelen 1000 (Ethernet)
RX packets 13 bytes 788 (788.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9 bytes 726 (726.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether 2a:20:b7:a8:58:37 txqueuelen 1000 (Ethernet)
RX packets 24 bytes 1906 (1.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether 2a:20:b7:a8:58:37 txqueuelen 1000 (Ethernet)
RX packets 1725 bytes 152691 (152.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1951 bytes 224182 (224.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth2: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether c2:35:83:a1:ab:bb txqueuelen 1000 (Ethernet)
RX packets 24 bytes 1906 (1.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth3: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
ether c2:35:83:a1:ab:bb txqueuelen 1000 (Ethernet)
RX packets 417 bytes 41205 (41.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21 bytes 1642 (1.6 KB)
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 793 bytes 53888 (53.8 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 793 bytes 53888 (53.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@ubuntu1804:~# cat /proc/net/bonding/bond1
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth3
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth3
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:34:df:b9
Slave queue ID: 0
Slave Interface: eth2
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 1
Permanent HW addr: 00:0c:29:34:df:a5
Slave queue ID: 0