重新載入 iptables

重新載入 iptables

我在 Ubuntu 中對 iptables 設定檔進行了更改,/etc/iptables/filter並希望重新載入它們。我閱讀了手冊頁並用谷歌搜尋但找不到資訊。任何幫助將不勝感激。

答案1

通常您的防火牆規則位於設定檔中/etc/iptables.firewall.rules

要啟動檔案中定義的規則,您必須將它們傳送到iptables-restore(如果需要,您可以使用另一個檔案):

sudo iptables-restore < /etc/iptables.firewall.rules

您可以透過以下方式檢查它們是否已啟動:

sudo iptables -L

如果您想在每次啟動電腦時啟動相同的規則,請建立以下文件:

sudo nano /etc/network/if-pre-up.d/firewall

有了這個內容:

#!/bin/sh
/sbin/iptables-restore < /etc/iptables.firewall.rules

並授予其執行權限:

sudo chmod +x /etc/network/if-pre-up.d/firewall

希望對你有幫助 =)

範例檔/etc/iptables.firewall.rules

*filter

#  Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -j REJECT

#  Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

#  Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

#  Allow SSH connections
#
#  The -dport number should be the same port number you set in sshd_config
#
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

#  Allow ping
-A INPUT -p icmp -j ACCEPT

#  Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

#  Drop all other inbound - default deny unless explicitly allowed policy
-A INPUT -j DROP
-A FORWARD -j DROP

COMMIT

2021-08 編輯:

剛剛升級到 Ubuntu 20.04.2 LTS 時遇到問題。的位置iptables-restore從 更改/sbin/iptables-restore/usr/sbin/iptables-restore

請務必檢查whereis iptables-restore您的系統位置,否則您的網路介面將不會被提升。

如果升級後沒有網絡,可以透過 ,sudo systemctl status networking.service -l以我為例查看原因:

Failed to start Raise network interfaces.
if-pre-up.d/firewall: 2: /sbin/iptables-restore: not found

答案2

最簡單的方法是重新啟動(如果下面不起作用,請重新啟動,檢查是否進行了更改)。

第二個最簡單的方法是使用 iptables 設定重新啟動守護程式(google:restart daemon ubuntu)。

範例(取決於您的配置):

/etc/init.d/iptables restart  

/etc/init.d/networking restart  

/etc/init.d/firewall restart

答案3

如果您已執行規則,則它們已經在運行,無需重新載入。如果您有設定檔但尚未執行,到目前為止我見過的最佳方法是使用iptables-apply(iptables 擴充功能)。

iptables-apply -t 60 your_rules_file

這會將規則套用 60 秒(預設為 10 秒),如果您不確認,則將其恢復。如果您因規則而被逐出系統(例如,如果您透過 ssh 進行操作),這將拯救您。

您可以使用以下內容作為替代:

iptables-restore < your_rules_file; sleep 60; iptables-restore < clean_rules

答案4

經過一番谷歌搜尋後,我發現這是重新啟動 iptables 的方法。 。 。 sudo /etc/init.d/firewall restart

相關內容