openEuler22.03的/etc/rc.local配置路由重启主机后不生效

cat /etc/rc.local
/usr/sbin/route add -net 10.114.66.0 netmask 255.255.255.0 gw 192.168.137.1

主机重启后路由未生效。

1)手工执行脚本:
# /usr/sbin/route add -net 10.114.66.0 netmask 255.255.255.0 gw 192.168.137.2

检查路由:

# route -n |grep 10.114.66.0
10.114.66.0     192.168.137.2   255.255.255.0   UG    0      0        0 ens33

此时发现:手工执行脚本中的命令是没问题的。

2)检查rc-local服务的状态:

# systemctl is-active rc.local.service
inactive

此时发现:rc-local服务是启动失败的。

3)/etc/rc.d/rc.local开机启动脚本由rc-local服务引导执行,可以修改在network-online.target后启动该服务。查看rc-local.servcie的配置:

# cat /usr/lib/systemd/system/rc-local.service |grep -v "^#"

[Unit]
Description=/etc/rc.d/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

4)在[Unit]模块中添加或修改Requires和After项值为network-online.target:

# vi /usr/lib/systemd/system/rc-local.service

[Unit]
Description=/etc/rc.d/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.d/rc.local
Requires=network-online.target
After=network-online.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

5)重新加载配置文件:
# systemctl daemon-reload

6)重启主机后进行验证:

# route -n|grep 10.114.66.0
10.114.66.0     192.168.137.2   255.255.255.0   UG    0      0        0 ens33

此时发现路由已添加成功!

1 Like