欢迎光临
我的个人博客网站

KaLi添加路由问题,设置开机自启动脚本rc.local文件不存在问题

由于项目需要,需要在kali上添加一条路由,并且保证重启后仍然生效。

经查找发现有两种可行的方法:

1、修改/etc/network/interfaces配置文件,添加一行,如:

up route add -net 10.105.10.0 netmask 255.255.255.255 gw 192.168.1.1 eth0

-net指这是一条网络路由(ip地址主机位为0)

gw指下一跳

2、修改rc.local,添加:

route add -net 192.168.114.0/24 dev eth0

route add -net 192.168.114.0/24 gw 192.168.3.254

那么,为了保证重启仍然生效需要将它写入rc.local配置文件。

——————————————–分割线——————————————————–

第一个方法添加路由后,使用route命令查看路由表;的确是添加了,但实际测试中发现没有起作用,目标仍然不可访问。(玄学问题)

第二个方法遇到一个问题,kali把rc.local“ 服务化”了,并没有rc.local配置文件怎么办呢?

解决方法:

vim /etc/systemd/system/rc-local.service

将内容替换为

[Unit]  Description=/etc/rc.local Compatibility  ConditionPathExists=/etc/rc.local  [Service]  Type=forking  ExecStart=/etc/rc.local start  TimeoutSec=0  StandardOutput=tty  RemainAfterExit=yes  SysVStartPriority=99  [Install]  WantedBy=multi-user.target

然后

touch /etc/rc.local(正常情况下/etc/rc.local并不是配置文件本身,而是/etc/rc.d/rc.local的软连接;但徒增麻烦不建议那么做)

记得赋予执行权限

chmod +x /etc/rc.local

vim /etc/rc.local

添加

#!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot.  exit 0 

插入脚本就在exit 0之前即可

重启rc-local服务

systemctl restart rc-local.service

设置开机自启动

systemctl enable rc-local

—————————————你以为这样就结束了?——————————————-

我将添加路由的shell命令添加到rc.local之后重启发现,并没有成功添加路由;

执行systemctl status rc-local.service发现报错显示“网络未启动”。。。。

在检查了各种错误之后我断定,是启动优先级没有起作用

无奈曲线解决,在启动脚本里添加sleep命令延迟执行脚本

详见我的shell脚本:

#!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. /usr/local/bin/route-eth0 exit 0 

/usr/local/bin/route-eth0:
#!/bin/bash sleep 15s route add -net 192.168.114.0/24 dev eth0 exit0

最终问题解决,目的是给更多人带来方便。

 
赞(0) 打赏
未经允许不得转载:张拓的天空 » KaLi添加路由问题,设置开机自启动脚本rc.local文件不存在问题
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏