Ubuntu网络配置
修改主机名
root@ubuntu1804:~# hostnamectl set-hostname ubuntu1804.longxuan.top
root@ubuntu1804:~# cat /etc/hostname
ubuntu1804.longxuan.top
root@ubuntu1804:~# hostname
ubuntu1804.longxuan.top
root@ubuntu1804:~# echo $HOSTNAME
ubuntu1804
root@ubuntu1804:~# exit
logout
wang@ubuntu1804:~$ sudo -i
root@ubuntu1804:~# echo $HOSTNAME
ubuntu1804.longxuan.top
修改网卡名称为传统命名方式:
#修改配置文件为下面形式
root@ubuntu1804:~# vi /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"
#或者sed修改
root@ubuntu1804:~# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#'
/etc/default/grub
#生效新的grub.cfg文件
root@ubuntu1804:~# grub-mkconfig -o /boot/grub/grub.cfg
#或者
root@ubuntu1804:~# update-grub
root@ubuntu1804:~# grep net.ifnames /boot/grub/grub.cfg
#重启生效
root@ubuntu1804:~# reboot
Ubuntu网卡配置
官网文档:
https://help.ubuntu.com/
https://ubuntu.com/server/docs/network-configuration
配置自动获取IP
网卡配置文件采用YAML格式,必须以/etc/netplan/XXX.yaml 文件命名方式存放
可以每个网卡对应一个单独的配置文件,也可以将所有网卡都放在一个配置文件里
范例:
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: yes
配置静态IP
root@ubuntu1804:~# vim /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses: [172.31.0.19/16,10.0.0.10/8] #或者用下面两行,两种格式不能混用
- 172.31.0.19/16
- 10.0.0.10/8
gateway4: 10.0.0.254
nameservers:
search: [longxuan.com, longxuan.top]
addresses: [180.76.76.76, 8.8.8.8, 1.1.1.1]
查看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@ubuntu2004:~# resolvectl status #Ubuntu 20.04新命令
root@ubuntu1804:~# systemd-resolve --status
Global
DNSSEC NTA: 10.in-addr.arpa
...
MulticastDNS setting: no
DNSSEC setting: no
DNSSEC supported: no
DNS Servers: 180.76.76.76
8.8.8.8
1.1.1.1
DNS Domain: longxuan.com
longxuan.top
配置多⽹卡静态IP和静态路由
root@ubuntu1804:~# vim /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
addresses: [172.31.0.19/16]
gateway4: 172.31.0.254
nameservers:
addresses: [223.6.6.6]
eth1:
dhcp4: no
dhcp6: no
addresses: [172.31.8.20/16]
routes:
- to: 172.31.0.0/16
via: 172.31.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
root@ubuntu1804:~# netplan apply
#或者每个网卡各自一个配置文件
root@ubuntu1804:/etc/netplan# ls
01-netcfg.yaml 02-eth1.yaml
root@ubuntu1804:/etc/netplan# cat 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: yes
root@ubuntu1804:/etc/netplan# cat 02-eth1.yaml
network:
version: 2
renderer: networkd
ethernets:
eth1:
addresses:
- 172.31.0.19/16
- 192.168.0.100/24
gateway4: 172.31.0.254
nameservers:
search: [longxuan.com, longxuan.tcp]
addresses: [180.76.76.76, 8.8.8.8, 1.1.1.1]
routes:
- to: 172.31.0.0/16
via: 172.31.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
单⽹卡桥接
root@ubuntu1804:~# apt install -y bridge-utils
root@ubuntu1804:~# dpkg -L bridge-utils
/sbin/brctl
......
#三个网卡配置使用一个配置文件
root@ubuntu1804:~# cat /etc/netplan/50-cloud-init.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: yes
eth1:
dhcp4: no
dhcp6: no
eth2:
dhcp4: no
bridges:
br0:
dhcp4: no
dhcp6: no
addresses: [172.31.0.19/16]
gateway4: 1072.31.0.254
nameservers:
addresses: [223.6.6.6]
interfaces:
- eth1
- eth2
#桥接配置单独一个文件
[root@ubuntu1804 netplan]# vim /etc/netplan/br0.yaml
network:
version: 2
renderer: networkd
ethernets:
eth1:
dhcp4: no
dhcp6: no
eth2:
dhcp4: no
bridges:
br0:
dhcp4: no
dhcp6: no
addresses: [172.31.0.10/16]
gateway4: 172.31.0.254
nameservers:
addresses: [223.6.6.6]
interfaces:
- eth1
- eth2
root@ubuntu1804:~# netplan apply
root@ubuntu1804:~# ifconfig br0
root@ubuntu1804:~# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.9ebe1d856601 no eth1
eth2
多⽹卡桥接
#有多少个eth 就要有多少块网卡
root@ubuntu1804:~# vim /etc/netplan/50-cloud-init.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
bridges:
br0:
dhcp4: no
dhcp6: no
addresses: [172.31.0.19/16]
gateway4: 172.31.0.254
nameservers:
addresses: [223.6.6.6]
interfaces:
- eth0
br1:
dhcp4: no
dhcp6: no
addresses: [172.31.0.19/16]
routes:
- to: 172.31.0.0/16
via: 172.31.0.1
- to: 10.30.0.0/16
via: 10.10.0.1
- to: 10.4.0.0/16
via: 10.10.0.1
- to: 10.50.0.0/16
via: 10.10.0.1
interfaces:
- eth1
root@ubuntu1804:~# netplan apply
root@ubuntu1804:~# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.96dbd15c1daf no eth0
br1 8000.9e02ab0faeb0 no eth1
root@ubuntu1804:~# ifconfig br0
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.0.18 netmask 255.255.0.0 broadcast 172.31.255.255
root@ubuntu1804:~# ifconfig br1
br1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.0.18 netmask 255.255.0.0 broadcast 172.31.255.255
双⽹卡绑定
支持多网卡绑定七种模式
第一种模式:mod=0,即:(balance-rr) Round-robin policy(平衡抡循环策略)
特点:传输数据包顺序是依次传输(即:第1个包走eth0,下一个包就走eth1...一直循环下去,直到最后一
个传输完毕),此模式提供负载平衡和容错能力。
第二种模式:mod=1,即: (active-backup) Active-backup policy(主-备份策略)
特点:只有一个设备处于活动状态,当一个宕掉另一个马上由备份转换为主设备。mac地址是外部可见得,从
外面看来,bond的MAC地址是唯一的,以避免switch(交换机)发生混乱。此模式只提供了容错能力;由此可
见此算法的优点是可以提供网络连接的可用性,但是它的资源利用率较低,只有一个接口处于工作状态,在有
N 个网络接⼝的情况下,资源利用率为1/N。
第三种模式:mod=2,即:(balance-xor) XOR policy(平衡策略)
特点:基于指定的传输HASH策略传输数据包。缺省的策略是:(源MAC地址 XOR 目标MAC地址) % slave数
量。其他的传输策略可以通过xmit_hash_policy选项指定,此模式提供负载平衡和容错能力。
第四种模式:mod=3,即:broadcast(广播策略)
特点:在每个slave接口上传输每个数据包,此模式提供了容错能力。
第五种模式:mod=4,即:(802.3ad) IEEE 802.3ad Dynamic link aggregation(IEEE
802.3ad 动态链接聚合)
特点:创建一个聚合组,它们共享同样的速率和双工设定。根据802.3ad规范将多个slave工作在同一个激活
的聚合体下。
必要条件:
条件1:ethtool持获取每个slave的速率和双工设定。
条件2:switch(交换机)支持IEEE 802.3ad Dynamic link aggregation。
条件3:多数switch(交换机)需要经过特定配置才能支持802.3ad模式。
第六种模式:mod=5,即:(balance-tlb) Adaptive transmit load balancing(适配器传输负载
均衡)
特点:不需要任何特别的switch(交换机)支持的通道bonding。在每个slave上根据当前的负载(根据速度
计算)分配外出流量。如果正在接受数据的slave出故障了,另一个slave接管失败的slave的MAC地址。
该模式的必要条件:
ethtool支持获取每个slave的速率
第七种模式:mod=6,即:(balance-alb) Adaptive load balancing(适配器适应性负载均衡)
特点:该模式包含了balance-tlb模式,同时加上针对IPV4流量的接收负载均衡(receive load
balance,rlb),而且不需要任何switch(交换机)的支持。
root@ubuntu1804:~# vim /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
addresses: [172.31.0.18/16]
gateway4: 172.31.0.254
nameservers:
addresses: [223.6.6.6,223.5.5.5]
parameters:
mode: active-backup
mii-monitor-interval: 100
root@ubuntu1804:~# netplan apply
root@ubuntu1804:~# ifconfig bond0
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST> mtu 1500
inet 172.31.0.18 netmask 255.255.0.0 broadcast 172.31.255.255
root@ubuntu1804:~# ifconfig eth0
eth0: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
root@ubuntu1804:~# ifconfig eth1
eth1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST> mtu 1500
root@ubuntu1804:~# cat /proc/net/bonding/bond0
...
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 /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: [172.31.0.18/16]
gateway4: 172.31.0.254
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
⽹卡的多组绑定
可以实现网卡的多组绑定,比如: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: [172.31.0.18/16]
gateway4: 172.31.0.254
nameservers:
addresses: [223.6.6.6,223.5.5.5]
parameters:
mode: active-backup
mii-monitor-interval: 100
bond1:
interfaces:
- eth2
- eth3
addresses: [172.31.0.18/16]
parameters:
mode: active-backup
mii-monitor-interval: 100
routes:
- to: 172.31.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
root@ubuntu1804:~# route -n
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
⽹卡多组绑定+多组桥接
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
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: [172.31.0.18/16]
gateway4: 172.31.0.1254
nameservers:
addresses: [223.6.6.6,223.5.5.5]
interfaces:
- bond0
br1:
dhcp4: no
dhcp6: no
interfaces:
- bond1
addresses: [172.31.0.18/16]
routes:
- to: 172.31.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
root@ubuntu1804:~# ifconfig
root@ubuntu1804:~# cat /proc/net/bonding/bond1