前段时间给自己新购的云服务器装上Debian系统配置iptables时,发现并不能使用以前在CentOS系统上的那种直接命令添加的方法,所以就去Debian官方WiKi上找了一番,然后我就给搬过来了,给自己做个记录也同时给各位一个借鉴;

好了,废话不多说下面开始:

第一种:简单粗暴

添加规则,第一种为tcp协议,第二种为udp协议

iptables -I INPUT -p tcp --dport 28016 -j ACCEPT
iptables -A INPUT -p udp --destination-port 28015 -j ACCEPT

然后保存

iptables-save

看一下

iptables -L

第二种:详细配置

首先输入下面命令使用vi新建一个规则文件

vi /etc/iptables.rules

然后在里面添加下面的规则,并保存退出(这些规则都是Debian官方给出的基本规则)

*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
# The --dport number is the same as in /etc/ssh/sshd_config
-A INPUT -p tcp -m state --state NEW --dport 22 -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
#  note that blocking other types of icmp packets is considered a bad idea by some
#  remove -m icmp --icmp-type 8 from this line to allow all kinds of icmp:
#  https://security.stackexchange.com/questions/22711
-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

COMMIT

然后输入下面命令来启用规则

iptables-restore < /etc/iptables.rules

现在查看一下iptables

iptables -L

4、保存生效的配置,让系统重启的时候自动加载有效配置

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

输入下面命令

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

在里面添加下面的脚本

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

最后设置可执行权限

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

完成!!!!!!

原文:https://wiki.debian.org/iptables (Debian官方维基,英文)