使用 Systemd 进行网络管理
简介
Systemd 工具用于 networkd 和 resolved,用于配置网络服务。
设备
FRITZ!Box 7360
此路由器提供 DHCP 和 DNS 服务器: FRITZ!Box 7360 手册 (pdf)
FRITZ!Box 7530 AX
此型号取代了 FRITZ!Box 7360: FRITZ!Box 7530 AX 手册 (pdf)。 其 DNS 服务器在使用 IPv6 时存在问题。在客户端上启用 avahi-dnsconfd 可以解决这些问题。
台式电脑
6700k:~ # inxi -zSMCDG
System:
Kernel: 5.19.8-1-default arch: x86_64 bits: 64 Console: pty pts/3 Distro: openSUSE Tumbleweed
20220922
Machine:
Type: Desktop Mobo: ASRock model: Z170 Pro4S serial: <filter> UEFI: American Megatrends
v: P7.50 date: 01/23/2018
CPU:
Info: quad core model: Intel Core i7-6700K bits: 64 type: MT MCP cache: L2: 1024 KiB
Speed (MHz): avg: 3600 min/max: 800/4200 cores: 1: 4000 2: 4000 3: 4000 4: 800 5: 4000
6: 4000 7: 4000 8: 4000
Graphics:
Device-1: Intel HD Graphics 530 driver: i915 v: kernel
Display: x11 server: X.org v: 1.21.1.4 with: Xwayland v: 22.1.3 driver: X:
loaded: modesetting unloaded: fbdev,vesa gpu: i915 tty: 238x52 resolution: 1920x1200
Message: GL data unavailable in console for root.
Drives:
Local Storage: total: 698.65 GiB used: 7.72 GiB (1.1%)
ID-1: /dev/sda vendor: Samsung model: SSD 850 EVO 500GB size: 465.76 GiB
ID-2: /dev/sdb vendor: Crucial model: CT250MX500SSD1 size: 232.89 GiB
6700k:~ #
安装
检查已安装的版本
6700k:~ # zypper search --installed-only --details systemd-network Loading repository data... Reading installed packages... S | Name | Type | Version | Arch | Repository ---+-----------------+---------+-----------+--------+----------------------- i+ | systemd-network | package | 251.4-3.1 | x86_64 | Haupt-Repository (OSS) 6700k:~ #
禁用网络服务
6700k:~ # systemctl disable --now NetworkManager 6700k:~ # systemctl list-unit-files NetworkManager* UNIT FILE STATE PRESET NetworkManager-dispatcher.service disabled disabled NetworkManager-wait-online.service disabled disabled NetworkManager.service disabled disabled 3 unit files listed. 6700k:~ #
禁用通过 Netconfig 修改 DNS
可以使用 Yast2 sysconfig。请验证
6700k:~ # grep NETCONFIG_DNS_POLICY /etc/sysconfig/network/config NETCONFIG_DNS_POLICY="" 6700k:~ #
配置链接
创建一个包含以下内容的文件(文件名必须以“.network”结尾)
6700k:~ # cat /etc/systemd/network/wireless.network [Match] Name=w* # wireless link [Network] DHCP=yes Domains=fritz.box # replace by domain of local network 6700k:~ #
启用网络服务
3400G:~ # systemctl enable --now systemd-networkd 3400G:~ #
查询网络链接状态
6700k:~ # networkctl IDX LINK TYPE OPERATIONAL SETUP 1 lo loopback carrier unmanaged 2 enp0s31f6 ether off unmanaged 3 wlp3s0 wlan routable configured 3 links listed. 6700k:~ #
启用网络名称解析
解析域名、IPv4 和 IPv6 地址、DNS 资源记录和服务;检查和重新配置 DNS 解析器
检查 /etc/resolve.conf 的当前设置
erlangen:~ # ll /etc/resolv.conf lrwxrwxrwx 1 root root 30 Nov 27 07:54 /etc/resolv.conf -> /var/run/netconfig/resolv.conf erlangen:~ #
保存目标文件的路径和内容(在撤销更改时很有用)。
确保 /etc/resolve.conf 由 resolved 管理
3400G:~ # ln -sf /run/systemd/resolve/resolv.conf /etc/ 3400G:~ # systemctl enable --now systemd-resolved 3400G:~ # 6700k:~ # 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 192.168.178.1 nameserver fd00::a96:d7ff:fee2:d6cf search fritz.box 6700k:~ #
查询解析器状态
6700k:~ # resolvectl
Global
Protocols: +LLMNR +mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: uplink
Link 2 (enp0s31f6)
Current Scopes: none
Protocols: -DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Link 3 (wlp3s0)
Current Scopes: DNS LLMNR/IPv4 LLMNR/IPv6
Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
DNS Servers: 192.168.178.1 fd00::a96:d7ff:fee2:d6cf
DNS Domain: fritz.box
6700k:~ #
Wpa Supplicant
Wi-Fi 保护访问客户端和 IEEE 802.1X 认证客户端
需要进行一些编辑。运行 "systemctl edit --full wpa_supplicant@wlp3s0.service"。
6700k:~ # systemctl cat wpa_supplicant@wlp3s0.service # /etc/systemd/system/wpa_supplicant@wlp3s0.service [Unit] Description=WPA Supplicant daemon (interface %i) After=dbus.service network.target Requires=sys-subsystem-net-devices-%i.device # Changed %I to lowercase %i After=sys-subsystem-net-devices-%i.device # Changed %I to lowercase %i [Service] Type=dbus BusName=fi.w1.wpa_supplicant1 ExecStart=/usr/sbin/wpa_supplicant -i%i -c /etc/wpa_supplicant/wpa_supplicant.conf -u -t -f /var/log/wpa_supplicant.log [Install] WantedBy=multi-user.target 6700k:~ #
启用该服务
6700k:~ # systemctl enable --now wpa_supplicant@wlp3s0
6700k:~ # systemctl status wpa_supplicant@wlp3s0
● wpa_supplicant@wlp3s0.service - WPA Supplicant daemon (interface wlp3s0)
Loaded: loaded (/etc/systemd/system/wpa_supplicant@wlp3s0.service; enabled; preset: disabled)
Active: active (running) since Sun 2022-09-25 12:05:51 CEST; 5min ago
Main PID: 9026 (wpa_supplicant)
Tasks: 1 (limit: 4915)
CPU: 64ms
CGroup: /system.slice/system-wpa_supplicant.slice/wpa_supplicant@wlp3s0.service
└─9026 /usr/sbin/wpa_supplicant -iwlp3s0 -c /etc/wpa_supplicant/wpa_supplicant.conf -u -t -f /var/log/wpa_supplicant.log
Sep 25 12:05:51 6700k systemd[1]: Starting WPA Supplicant daemon (interface wlp3s0)...
Sep 25 12:05:51 6700k systemd[1]: Started WPA Supplicant daemon (interface wlp3s0).
Sep 25 12:10:47 6700k systemd[1]: Started WPA Supplicant daemon (interface wlp3s0).
Sep 25 12:11:30 6700k systemd[1]: Started WPA Supplicant daemon (interface wlp3s0).
6700k:~ #
故障排除
FRITZ!Box
重置 DHCP 和 DNS 服务器有助于清除租约和缓存信息
Networkd/Resolved
通过取消注释来启用调试
3400G:~ # cat /etc/systemd/system/systemd-networkd.service.d/override.conf [Service] #Environment=SYSTEMD_LOG_LEVEL=debug 3400G:~ #
撤销更改
参见: 我认为我搞坏了我的互联网
: