如何设置这些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
,并按照提示操作。 当它要求保存当前规则时,在两个提示处都单击“是”。 现在,重新启动时,将恢复您的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
这样,在激活网络接口之前,您的规则将被加载。