iptables可以提供包过滤,网络地址转换(NAT)其他包分割iptables的两个最常见的用途提供支持防火墙和NAT。手动配置Iptables对于初学者来说是具有挑战的,但Iptables提供了向导和期其它工具来协助初学者。

查看已配置规则,使用如下命令

iptables -L

会输出以下内容

Chain INPUT (policy ACCEPT)
target     prot opt source               destination        

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination       

上面的规则说明,允许任何人从任何地方访问。

首先来创建一个Iptables的文件

nano /etc/iptables.test.rules

输入以下规则

*filter

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

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

 # Allows all outbound traffic
 # You could modify this to only allow certain traffic
  -A OUTPUT -j ACCEPT

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

 # Allows SSH connections for script kiddies
 # THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
  -A INPUT -p tcp -m state --state NEW --dport 30000 -j ACCEPT

 # Now you should read up on iptables rules and consider whether ssh access
 # for everyone is really desired. Most likely you will only allow access from certain IPs.

 # Allow ping
  -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

 # log iptables denied calls (access via 'dmesg' command)
  -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

 # Reject all other inbound - default deny unless explicitly allowed policy:
  -A INPUT -j REJECT
  -A FORWARD -j REJECT

这看到比较复杂,但细看每一部分,你会发现只是关闭除了我们允许所有端口,在这种情况下80和443端口标准的网络浏览器端口前面定义SSH端口

激活这些新的规则

iptables-restore < /etc/iptables.test.rules

再来看看有什么不同

iptables -L

我们看到,只有上面定义的端口是关闭的,其余都是关闭的。如果是这样的,将保存Iptables文件

iptables-save > /etc/iptables.up.rules

为了确保iptables规则开始重新启动我们将创建一个文件

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

输入下面内容

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

更改一下权限

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

3 对 “Debian配置iptables规则”的想法;

回复 宁波肛肠医院 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注