由於項目需要,需要在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
最終問題解決,目的是給更多人帶來方便。