首页 > 其他 > 详细

systemd管理网络应用

时间:2019-03-16 10:28:46      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:服务配置   虚拟   -s   保持   网络设置   compress   index   status   resolve   

采用systemd-networkd管理网卡

  1. 主网卡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

  1. 无线网卡的配置文件/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

}

  1. 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

  1. staticdhcpwifipppoe方式的切换操作步骤如下:

  1. 网络部分使用前部署

需安装wpa_supplicant应用程序,并设置该应用开机启动。

apt-get install wpasupplicant

systemctl enable wpa_supplicant

修改wpa_supplicantsystemd配置文件/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静态上网:

根据配置的IPnetmaskGatewayDNS适当修改/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 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

4) 无线上网:

根据ssidpskkey_mgmtpairwise合理修改/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

5pppoe上网:

根据用户名和密码合理修改/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

  1. 上网方式切换时间统计

切换方式

切换后成功连通时间

static -> wifi

8s

dhcp -> wifi

18s

others

5s

注:不同网络环境或测试条件下,时间可能有差距。上述测试环境基于北京网络。

  1. 上网方式切换后网络连通的测试方法

    上网方式

    切换后连通测试方法

    static

    Ping网关

    dhcp

    可正确获取到动态分配的IP

    wifi

    可正确获取到动态分配的IP

    pppoe

    有虚拟网卡ppp0且可获取IP

  2. 开机启动时网络设计

断电重启或reboot后,不同上网方式网卡配置和路由不同,需要根据上网方式来正确配置系统。

/etc/wan_way下保存当前上网方式,可取值:staticdhcpwifipppoe,在/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
  1. 目前测试发现问题

采用DHCP方式上网,插拔网线时配置其他设备该网关动态IP,此网关不能获取新的IP


参考:

  1. systemd 的网络管理

  2. 树莓派+1USB有线网卡+1USB无线网卡做路由器

  3. Linux From Scratch - Version 7.7-systemd 通用网络配置

  4. systemd-networkd (Archlinux wiki)

  5. Network configuration (Archlinux wiki)

  6. 5章 网络设置(debian manuals

 

systemd管理网络应用

标签:服务配置   虚拟   -s   保持   网络设置   compress   index   status   resolve   

原文:https://www.cnblogs.com/embedded-linux/p/10540426.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号