systemd管理网络应用
Posted embedded-linux
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了systemd管理网络应用相关的知识,希望对你有一定的参考价值。
采用systemd-networkd管理网卡
-
主网卡eth0的配置文件/etc/systemd/network/20-eth0.network,静态配置时内容示例如下:
[Match]
Name=eth0
[Network]
Address=192.168.10.111/24
DNS=8.8.8.8
DNS=8.8.4.4
[Route]
Gateway=192.168.10.1
Metric=100
动态配置时内容为:
[Match]
Name=eth0
[Network]
DHCP=yes
[DHCP]
RouteMetric=100
-
无线网卡的配置文件/etc/systemd/network/21-wireless.network,一般设置为动态IP即可,即该文件内容保持不变。
[Match]
Name=wlan0
[Network]
DHCP=yes
[DHCP]
RouteMetric=120
无线网卡的应用服务程序为wpa_supplicant,其systemd配置文件为/lib/systemd/system/wpa_supplicant.service,内容如下,保持不变。
[Unit]
Description=WPA supplicant
Before=network.target
[Service]
Type=simple
ExecStart=/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c /etc/wpa_supplicant.conf
ExecReload=/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c /etc/wpa_supplicant.conf
RestartSec=120
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=dbus-fi.epitest.hostap.WPASupplicant.service
无线网卡上网方式的配置文件为/etc/wpa_supplicant.conf,样例如下:
ctrl_interface=/var/run/wpa_supplicant
update_config=1
network={
scan_ssid=1
ssid="test"
psk="123456"
key_mgmt=WPA-PSK
pairwise=CCMP
}
-
pppoe集成到systemd中,需配置文件/etc/ppp/pppoe.conf和/etc/ppp/chap-secrets、/etc/ppp/pap-secrets。
# cat /etc/ppp/pppoe.conf
DEMAND=no
DNSTYPE=SERVER
PEERDNS=yes
DEFAULTROUTE=yes
CONNECT_TIMEOUT=30
CONNECT_POLL=2
PING="."
CF_BASE=`basename $CONFIG`
PIDFILE="/var/run/$CF_BASE-pppoe.pid"
SYNCHRONOUS=no
CLAMPMSS=1412
LCP_INTERVAL=20
LCP_FAILURE=3
PPPOE_TIMEOUT=80
FIREWALL=NONE
LINUX_PLUGIN=
PPPOE_EXTRA=""
PPPD_EXTRA=""
ETH="eth0"
USER="test"
# cat /etc/ppp/chap-secrets
"test" * "123456"
# cat /etc/ppp/pap-secrets
"test" * "123456"
systemd服务配置文件/lib/systemd/system/pppoe.service内容如下(保持不变)。
# cat /lib/systemd/system/pppoe.service
[Unit]
Description=PPP over ethernet
After=network-online.target systemd-networkd.service
Wants=network-online.target systemd-networkd.service
[Service]
Type=forking
ExecStart=/usr/sbin/pppoe-start
ExecReload=/usr/sbin/pppoe-stop;/usr/sbin/pppoe-start
ExecStop=-/usr/sbin/pppoe-stop
ExecStopPost=-ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
-
static、dhcp、wifi、pppoe方式的切换操作步骤如下:
-
网络部分使用前部署
需安装wpa_supplicant应用程序,并设置该应用开机启动。
apt-get install wpasupplicant
systemctl enable wpa_supplicant
修改wpa_supplicant的systemd配置文件/lib/systemd/system/wpa_supplicant.service内容如下(内容保持不变):
[Unit]
Description=WPA supplicant
Before=network.target
[Service]
Type=simple
ExecStart=/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c /etc/wpa_supplicant.conf
ExecReload=/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c /etc/wpa_supplicant.conf
RestartSec=120
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=dbus-fi.epitest.hostap.WPASupplicant.service
增加无线网卡的配置文件/etc/systemd/network/21-wireless.network。
[Match]
Name=wlan0
[Network]
DHCP=yes
[DHCP]
RouteMetric=120
pppoe部分需要额外配置内核及安装pppoe且增加pppoe服务配置文件。
Device Drivers ->
Network device support ->
<M> ppp(point-to-point protocol) support
<M> PPP BSD-Compress compression
<M> PPP Deflate compression
[*] PPP filtering
<M> PPP MPPE compression (encryption)
[*] PPP multilink support
<M> PPP over Ethernet
<M> PPP support async serial ports
<M> PPP support for sync tty ports
pppoe安装及服务配置文件:
# apt install pppoe
# cat /lib/systemd/system/pppoe.service
[Unit]
Description=PPP over ethernet
After=network-online.target systemd-networkd.service
Wants=network-online.target systemd-networkd.service
[Service]
Type=forking
ExecStart=/usr/sbin/pppoe-start
ExecReload=/usr/sbin/pppoe-stop;/usr/sbin/pppoe-start
ExecStop=-/usr/sbin/pppoe-stop
ExecStopPost=-ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
2) eth0动态上网:
修改/etc/systemd/network/20-eth0.network内容为:
[Match]
Name=eth0
[Network]
DHCP=yes
[DHCP]
RouteMetric=100
之后执行如下命令:
systemctl stop pppoe
systemctl restart systemd-networkd
ifconfig wlan0 down
systemctl restart systemd-resolved
ls -al /etc/resolv.conf | grep systmed >/dev/null || ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
3) eth0静态上网:
根据配置的IP、netmask、Gateway、DNS适当修改/etc/systemd/network/20-eth0.network内容。
[Match]
Name=eth0
[Network]
Address=192.168.10.111/24
DNS=8.8.8.8
DNS=8.8.4.4
[Route]
Gateway=192.168.10.1
Metric=100
之后执行如下命令:
systemctl restart systemd-networkd
ifconfig wlan0 down
systemctl restart systemd-resolved
ls -al /etc/resolv.conf | grep systmed >/dev/null || ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
4) 无线上网:
根据ssid、psk、key_mgmt、pairwise合理修改/etc/wpa_supplicant.conf。
ctrl_interface=/var/run/wpa_supplicant
update_config=1
network={
scan_ssid=1
ssid="test"
psk="123456"
key_mgmt=WPA-PSK
pairwise=CCMP
}
之后执行如下命令:
systemctl stop pppoe
systemctl restart systemd-networkd
systemctl restart wpa_supplicant
ifconfig eth0 down
systemctl restart systemd-resolved
ls -al /etc/resolv.conf | grep systmed >/dev/null || ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
5)pppoe上网:
根据用户名和密码合理修改/etc/ppp/pppoe.conf和/etc/ppp/chap-secrets、/etc/ppp/pap-secrets,其中/etc/ppp/pppoe.conf只需要修改用户名及USER。
# cat /etc/ppp/pppoe.conf
DEMAND=no
DNSTYPE=SERVER
PEERDNS=yes
DEFAULTROUTE=yes
CONNECT_TIMEOUT=30
CONNECT_POLL=2
PING="."
CF_BASE=`basename $CONFIG`
PIDFILE="/var/run/$CF_BASE-pppoe.pid"
SYNCHRONOUS=no
CLAMPMSS=1412
LCP_INTERVAL=20
LCP_FAILURE=3
PPPOE_TIMEOUT=80
FIREWALL=NONE
LINUX_PLUGIN=
PPPOE_EXTRA=""
PPPD_EXTRA=""
ETH="eth0"
USER="test"
# cat /etc/ppp/chap-secrets
"test" * "123456"
# cat /etc/ppp/pap-secrets
"test" * "123456"
之后运行如下命令:
systemctl restart systemd-networkd
ifconfig wlan0 down
systemctl restart pppoe
ifconfig eth0 0.0.0.0 && route del default; route add default dev ppp0
-
上网方式切换时间统计
-
切换方式
切换后成功连通时间
static -> wifi
约8s
dhcp -> wifi
约18s
others
5s内
注:不同网络环境或测试条件下,时间可能有差距。上述测试环境基于北京网络。
-
上网方式切换后网络连通的测试方法
上网方式
切换后连通测试方法
static
Ping网关
dhcp
可正确获取到动态分配的IP
wifi
可正确获取到动态分配的IP
pppoe
有虚拟网卡ppp0且可获取IP
-
开机启动时网络设计
断电重启或reboot后,不同上网方式网卡配置和路由不同,需要根据上网方式来正确配置系统。
在/etc/wan_way下保存当前上网方式,可取值:static、dhcp、wifi、pppoe,在/etc/rc.local中增加网络初始化脚本/usr/local/bin/init_networking后台执行,以配置不同上网方式的网络。
#!/bin/sh
wan_way=$(cat /etc/certusnet_conf/wan_way) case $wan_way in "dhcp" | "static" ) ifconfig wlan0 down ls -al /etc/resolv.conf | grep systmed >/dev/null || ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf echo "dhcp or static" ;; "wifi") ifconfig eth0 down ls -al /etc/resolv.conf | grep systmed >/dev/null || ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf echo "wifi" ;; "pppoe") ifconfig wlan0 down systemctl restart pppoe echo "pppoe" ifconfig eth0 0.0.0.0 num=0 while [ $num -lt 10 ] do num=$(($num+1)) pppoe-status | grep -w inet >/dev/null && { echo "pppoe success after $num *10s" num=100 } done sleep 10 route del default ; route add default dev ppp0 ;; *) echo "not dhcp, static, pppoe or wifi" ;; esac exit 0
-
目前测试发现问题
采用DHCP方式上网,插拔网线时配置其他设备该网关动态IP,此网关不能获取新的IP。
参考:
!-- @page>
以上是关于systemd管理网络应用的主要内容,如果未能解决你的问题,请参考以下文章
LINUX PID 1和SYSTEMD PID 0 是内核的一部分,主要用于内进换页,内核初始化的最后一步就是启动 init 进程。这个进程是系统的第一个进程,PID 为 1,又叫超级进程(代码片段