如何设置这些 iptables 规则在启动时运行

Mik*_*ane 25 iptables scripts firewall

每当我登录时,我通常都会运行我的 iptables 规则。从终端我输入;

sudo sh firewall.sh
Run Code Online (Sandbox Code Playgroud)

设置我姐姐的电脑,我想给她一些基本的防火墙保护。她不会以管理员身份登录,只是一个标准帐户。如何让防火墙脚本在她每次登录时都运行而无需输入任何密码?

我为我姐姐的电脑写的脚本包含;

#!/bin/sh

modprobe ip_conntrack
iptables -F
iptables -X
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

iptables -I OUTPUT -p tcp --dport 80 --sport 32768:61000 -j ACCEPT
iptables -I OUTPUT -p udp --dport 53 --sport 32768:61000 -j ACCEPT
iptables -I OUTPUT -p tcp --dport 443 --sport 32768:61000 -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -I OUTPUT -p icmp -j DROP

iptables -I INPUT -p icmp -j DROP
iptables -I INPUT -p udp -j DROP
iptables -I INPUT -p tcp -m tcp --syn -j DROP
iptables -I INPUT -i lo -j ACCEPT
iptables -I INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

我将它作为 firewall.sh 放在她的主文件夹中,并将其设置为可执行文件(右键单击该文件,然后在权限选项卡中选中“允许将文件作为程序执行”选项)。

以 root 身份从终端运行此脚本工作正常。

打字后;

sudo sh firewall.sh
Run Code Online (Sandbox Code Playgroud)

我在终端输入

sudo iptables -L -v
Run Code Online (Sandbox Code Playgroud)

我得到

Chain INPUT (policy DROP 0 packets, 0 bytes)  pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  any    any     anywhere             anywhere             ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere            
    0     0 DROP       tcp  --  any    any     anywhere             anywhere             tcpflags: FIN,SYN,RST,ACK/SYN
    0     0 DROP       udp  --  any    any     anywhere             anywhere            
    0     0 DROP       icmp --  any    any     anywhere             anywhere            

Chain FORWARD (policy DROP 0 packets, 0 bytes)  pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy DROP 0 packets, 0 bytes)  pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       icmp --  any    any     anywhere             anywhere            
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp spts:32768:61000 dpt:https
    0     0 ACCEPT     udp  --  any    any     anywhere             anywhere             udp spts:32768:61000 dpt:domain
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp spts:32768:61000 dpt:http
    0     0 ACCEPT     all  --  any    lo      anywhere             anywhere
Run Code Online (Sandbox Code Playgroud)

我怎样才能让这个脚本在登录时自动运行,或者可能为我的姐妹电脑永久保存这些规则?您能否提供一些详细的代码,因为我对 rc.local 方法和 iptables-save 的第一次尝试并不是很成功。每次重新启动时,所有 INPUT、OUTPUT 和 FORWARD 链都将重置为 ACCEPT,当我键入时没有列出任何策略sudo iptables -L -v

Tho*_*ard 47

您可能希望使用该iptables-persistent软件包而不是弄乱您的引导脚本。首先,运行您的脚本来设置防火墙规则。其次,运行sudo apt-get install iptables-persistent,并按照提示操作。当它要求保存当前规则时,在两个提示下都点击“是”。现在,在重新启动时,您的 iptables 规则将被恢复。


注意:如果您在此之后更改规则,则需要在更改后执行以下命令:

要保存您的 IPv4 iptables 规则: sudo su -c 'iptables-save > /etc/iptables/rules.v4'

要保存您的 IPv6 ip6tables 规则: sudo su -c 'ip6tables-save > /etc/iptables/rules.v6'