当前位置: 首页>>技术教程>>正文


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

, ,

问题描述

我通常在登录时运行我的 iptables 规则。我从终端输入;

sudo sh firewall.sh

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

我为我姐姐的计算机编写的脚本包含;

#!/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

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

以 root 身份从终端运行此脚本效果很好。

输入后;

sudo sh firewall.sh

我在终端输入

sudo iptables -L -v

我得到

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

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

最佳思路

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


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

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

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

次佳思路

假设您的防火墙规则位于:

/etc/iptables.up.rules

也许最明显的答案是在以下位置创建一个名为 iptables 的文件:

/etc/network/if-pre-up.d

内容:

#!/bin/bash
/sbin/iptables-restore < /etc/iptables.up.rules

并使用使其可执行

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

这样,在激活网络接口之前,您的规则将被加载。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/14047.html,未经允许,请勿转载。