1.配置文件
bash
# 默认配置文件:/etc/netplan/*.yaml
# 本机
root@ubuntu-server:~# vim /etc/netplan/50-cloud-init.yaml
2.DHCP
bash
network:
ethernets:
ens33:
dhcp4: true
version: 2
3.静态IP设置
- 注意:netplan 说明文件格式存储在下列路径下,该目录下有各种样例文件,可以提供帮助
bash
root@ubuntu-server:~# cd /usr/share/doc/netplan/examples/
root@ubuntu-server:/usr/share/doc/netplan/examples# ls
bonding_router.yaml infiniband.yaml sriov_vlan.yam ......
# 静态IP范例
root@ubuntu-server:/usr/share/doc/netplan/examples# cat static.yaml
network:
version: 2
renderer: networkd
ethernets:
enp3s0: #网卡名
addresses:
- 10.10.10.2/24 #静态IP地址/子网掩码
nameservers:
search: [mydomain, otherdomain] #域名
addresses: [10.10.10.1, 1.1.1.1] #DNS解析地址1 , DNS解析地址2
routes:
- to: default
via: 10.10.10.1 #网关地址

- 查看本机IP信息
bash
# IP and netmask:
root@ubuntu-server:~# ifconfig ens33 #IP:192.168.2.135
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.135 netmask 255.255.255.0 broadcast 192.168.2.255
inet6 fe80::20c:29ff:fe2b:2eed prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2b:2e:ed txqueuelen 1000 (Ethernet)
RX packets 181714 bytes 269446531 (269.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 15485 bytes 1005411 (1.0 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# gateway: 192.168.2.2
root@ubuntu-server:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.2.2 0.0.0.0 UG 0 0 0 ens33
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
# DNS:114.114.114.114


- 设置
bash
root@ubuntu-server:~# vim /etc/netplan/50-cloud-init.yam
# 清除已有内容,将静态IP范例文件内容拷贝到当前配置中,在修改,注意缩进格式
network:
ethernets:
ens33:
dhcp4: no
addresses:
- 192.168.2.135/24
- 192.168.2.150/24
routes:
- to: default
via: 192.168.2.2
nameservers:
addresses: [114.114.114.114]
version: 2
- 生效
bash
root@ubuntu-server:~# netplan apply
