發表於2024年5月12日1年前 -------------------------------------------Debian預設防火牆更改為iptables防火牆-------------------------------------------看看預設是否為 ufw 防火牆ufw status 顯示 Status: active這家主機商的Debian預設防火牆是安裝 ufw 防火牆而有部份主機商或舊版Debian可能是 nftables 防火牆 由於我慣用iptables防火牆,因此移除ufw防火牆apt remove ufw 安裝iptables防火牆apt install iptables 看看iptables防火牆是否運作iptables -L -v ------------------------------設定防火牆規則------------------------------cd /usr/local/binvi 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 開機啟動-------------------------網路找到 rc.local 開機啟動cat /lib/systemd/system/rc-local.servicesystemctl status rc-local 將底下代碼寫入到rc.localvi /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.localvi /etc/rc.local 把這一行,放在 exit 0 前面即可/usr/local/bin/firewall.sh start 儲存並離開:wq 重開機reboot確認iptables防火牆的規則,有無成功執行,就是要看到你指定的IP有無出現iptables -L再次查看rc.local狀態systemctl status rc-local.service 已經是 active (exited) 本文章最後於6月10日6月10日,由Jack編輯
建立帳號或登入後發表意見