1. 程式人生 > 實用技巧 >KaLi新增路由問題,設定開機自啟動指令碼rc.local檔案不存在問題

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

最終問題解決,目的是給更多人帶來方便。