netplan简介
参考:
https://netplan.io/
https://netplan.io/examples
https://github.com/canonical/netplan
Ubuntu使用 Netplan 管理网络,Netplan是一个用于在 linux 系统上轻松配置网络的实用程序。只需创建所需网络接口的 YAML 描述以及每个应配置的功能,根据此描述,Netplan 将为您选择的渲染器工具生成所有必要的配置。
Netplan 读取由管理员、安装人员、云映像实例化或其他操作系统部署编写的/etc/netplan/*.yaml
网络配置。在早期启动期间,Netplan 会在/run
生成后端特定的配置文件,以将设备的控制权移交给特定的网络守护进程。
Netplan 目前可与这些受支持的渲染器一起使用
- NetworkManager
- Systemd-networkd
配置静态或动态IP地址
以 ubuntu server LTS 22.04为例:
1、修改网络配置文件
配置静态IP地址示例,其中routes.via
为网关地址
cat > /etc/netplan/00-installer-config.yaml <<EOF
network:
version: 2
renderer: networkd
ethernets:
ens33:
addresses:
- 192.168.92.4/24
nameservers:
search: [mydomain, otherdomain]
addresses: [114.114.114.114, 8.8.8.8]
routes:
- to: default
via: 192.168.92.2
EOF
配置DHCP动态IP示例:
cat > /etc/netplan/00-installer-config.yaml <<EOF
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: true
EOF
2、测试配置文件
sudo netplan try
3、应用配置文件
sudo netplan apply
如果遇到错误可以执行debug
netplan --debug apply
4、验证 IP 地址
root@ubuntu:~# 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:14:45:93 brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 192.168.92.4/24 brd 192.168.92.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe14:4593/64 scope link
valid_lft forever preferred_lft forever
查看网关配置
root@ubuntu:~# ip route
default via 192.168.92.2 dev ens33 proto static
192.168.92.0/24 dev ens33 proto kernel scope link src 192.168.92.4
查看dns配置
root@ubuntu:~# resolvectl status
Global
Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub
Link 2 (ens33)
Current Scopes: DNS
Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 114.114.114.114
DNS Servers: 114.114.114.114 8.8.8.8
DNS Domain: mydomain otherdomain
配置DNS
查看/etc/resolv.conf 文件,其中nameserver默认为 127.0.0.5,修改dns地址直接配置netplan文件即可,不要直接修改/etc/resolv.conf
配置文件
root@ubuntu:~# cat /etc/resolv.conf
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 127.0.0.53
options edns0 trust-ad
search .
禁用本地 dns 53端口监听
默认/etc/resolv.conf
软链接到/run/systemd/resolve/stub-resolv.conf
,该文件由systemd-resolved管理:
root@ubuntu:~# ll /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Apr 21 09:00 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
而 /run/systemd/resolve/*
生成的配置文件是由/etc/systemd/resolved.conf
管理,该/run/systemd/resolve/目录下有两个resolv.conf文件:
root@ubuntu:~# ll /run/systemd/resolve/
total 8
drwxr-xr-x 2 systemd-resolve systemd-resolve 100 Apr 26 17:31 ./
drwxr-xr-x 24 root root 580 Apr 26 17:31 ../
srw-rw-rw- 1 systemd-resolve systemd-resolve 0 Apr 26 17:31 io.systemd.Resolve=
-rw-r--r-- 1 systemd-resolve systemd-resolve 809 Apr 26 17:31 resolv.conf
-rw-r--r-- 1 systemd-resolve systemd-resolve 920 Apr 26 17:31 stub-resolv.conf
查看/run/systemd/resolve/resolv.conf
文件,与netplan文件中配置的nameserver一致,修改netplan配置中的nameserver该文件将会自动同步更新:
root@ubuntu:/run/systemd/resolve# cat resolv.conf
# This is /run/systemd/resolve/resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 114.114.114.114
nameserver 8.8.8.8
search .
查看systemd-resolved服务,请勿禁用该服务,否则将导致外网解析失败。
systemctl status systemd-resolved
systemd-resolved服务默认监听53端口,如果直接启用其他dns服务器会报端口冲突错误:“listen tcp 0.0.0.0:53: bind: address already in use” .
root@bastion:~# lsof -i :53
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd-r 1687 systemd-resolve 12u IPv4 45468 0t0 UDP localhost:domain
systemd-r 1687 systemd-resolve 13u IPv4 45469 0t0 TCP localhost:domain (LISTEN)
可以通过编辑 /etc/systemd/resolved.conf,修改DNSStubListener
参数禁用53端口监听
root@bastion:~# vim /etc/systemd/resolved.conf
......
[Resolve]
DNSStubListener=no
或者通过创建配置文件修改
mkdir -p /etc/systemd/resolved.conf.d/
cat >/etc/systemd/resolved.conf.d/98-disable-127-53.conf << EOF
[Resolve]
DNSStubListener=no
EOF
重启systemd-resolved服务
systemctl daemon-reload
systemctl restart systemd-resolved.service
systemctl status -l systemd-resolved.service --no-pager
查看53端口已经取消占用
root@bastion:~# lsof -i :53
在禁用systemd-resolved端口监听后,查看/etc/resolv.conf 中的变化
root@ubuntu:~# cat /etc/resolv.conf
# This is /run/systemd/resolve/resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 114.114.114.114
nameserver 8.8.8.8
search .
查看stub-resolv.conf文件变化,作为软连接重新指向了resolv.conf
root@ubuntu:~# ll /run/systemd/resolve/
total 4
drwxr-xr-x 2 systemd-resolve systemd-resolve 100 Apr 26 18:37 ./
drwxr-xr-x 25 root root 600 Apr 26 18:37 ../
srw-rw-rw- 1 systemd-resolve systemd-resolve 0 Apr 26 18:37 io.systemd.Resolve=
-rw-r--r-- 1 systemd-resolve systemd-resolve 809 Apr 26 18:00 resolv.conf
lrwxrwxrwx 1 systemd-resolve systemd-resolve 11 Apr 26 18:37 stub-resolv.conf -> resolv.conf