ubuntu网卡配置

网卡配置文件采用YAML格式,必须以 /etc/netplan/XXX.yaml 文件命名方式存放
可以每个网卡对应一个单独的配置文件,也可以将所有网卡都放在一个配置文件里

自动获取IP

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

# 修改网卡配置文件后需执行命令生效:
root@ubuntu1804:~#netplan apply

配置静态IP

root@ubuntu1804:~#vim /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
ethernets:
  eth0:
    dhcp4: no
    addresses: [192.168.8.10/24,10.0.0.10/8]  #或者用下面两行,两种格式不能混用
    - 192.168.8.10/24
    - 10.0.0.10/8
    gateway4: 10.0.0.2
    nameservers:
    search: [baidu.com, google.org]
      addresses: [114.114.114.114, 8.8.8.8, 1.1.1.1]

image

配置多网卡静态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: [10.0.0.100/16]
      gateway4: 10.0.0.2
      nameservers:
        addresses: [223.6.6.6]
    eth1:
      dhcp4: no
      dhcp6: no 
      addresses: [10.20.0.100/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

root@ubuntu1804:~#netplan apply
root@ubuntu1804:~#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   0    0     0 eth0
10.0.0.0     0.0.0.0     255.255.0.0   U   0    0     0 eth0
10.20.0.0    0.0.0.0     255.255.0.0   U   0    0     0 eth1
10.30.0.0    10.20.0.1    255.255.0.0   UG   0    0     0 eth1
10.40.0.0    10.20.0.1    255.255.0.0   UG   0    0     0 eth1
10.50.0.0    10.20.0.1    255.255.0.0   UG   0    0     0 eth1
10.60.0.0    10.20.0.1    255.255.0.0   UG   0    0     0 eth1
posted @ 2021-04-18 17:01  AlexG  阅读(1558)  评论(0编辑  收藏  举报