Jump to content

6.Debian預設防火牆更改為iptables防火牆 / 設置 rc.local 開機啟動


Jack

Recommended Posts

Posted (edited)

-------------------------------------------
Debian預設防火牆更改為iptables防火牆
-------------------------------------------

看看預設是否為 ufw 防火牆
ufw status

 

顯示 Status: active

這家主機商的Debian預設防火牆是安裝 ufw 防火牆

而有部份主機商或舊版Debian可能是 nftables 防火牆

 

 

我習慣使用iptables防火牆,因此移除ufw防火牆
sudo apt remove ufw

 

安裝iptables防火牆
sudo apt install iptables

 

重開機
sudo reboot

 

看看iptables防火牆是否運作
iptables -L -v

 


------------------------------
設定防火牆規則
------------------------------

cd /usr/local/bin


vi firewall.sh

將iptables防火牆的規則,複製貼上到firewall.sh檔案裡

主要是 IN_TCP_PORTALLOWED="22,你的IP 888,你的IP 80 443"

這一行修改為你的IP,讓 22 port / 888 port 只讓你指定IP能連入

若使用在寶塔面板 xxxxx port,就是該 寶塔port 只能讓你連入,其餘IP都無法連

 


iptables防火牆的規則,放在附件中

firewall.sh

 

 

儲存並離開
:wq

 

變更權限
chmod +x /usr/local/bin/firewall.sh

 

測試防火牆
./firewall.sh

./firewall.sh start

 

 

-------------------------
設置 rc.local 開機啟動
-------------------------

網路找到 Debian 11 rc.local 開機啟動


cat /lib/systemd/system/rc-local.service

systemctl status rc-local

 

 

將底下代碼寫入到rc.local

vi /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0
EOF

 


儲存並離開
:wq

 

執行 變更權限
chmod +x /etc/rc.local

 


啟動 rc-local 服務
systemctl enable --now rc-local


無視警告,因為這個服務沒有任何依賴的系統服務,只是開機啟動 /etc/rc.local 腳本

 


將需要開機啟動的命令加到 /etc/rc.local 檔案中,放在 exit 0 前面即可,然後重開機


再次修改rc.local
vi /etc/rc.local

 

把這一行,放在 exit 0 前面即可

/usr/local/bin/firewall.sh start

 

儲存並離開
:wq

 

重開機
sudo reboot

 


確認iptables防火牆的規則,有無成功執行,就是要看到你指定的IP有無出現
iptables -L

 

 

再次查看rc.local狀態
systemctl status rc-local.service 

已經是 active (exited)

 

Edited by Jack
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...