iptables 命令实际操作

参考链接:iptables 图文详解
【服务器防护】iptables 配置详解(非常棒的案例)
iptables 为什么一定要有 --state ESTABLISHED,RELATED -j ACCEPT ?
Linux中iptables设置详细
iptables工具命令详细

one、四种表
filter 表:负责过滤功能,防火墙
nat 表:网络地址转换
mangle 表:拆解报文,做出修改,并重新封装
raw 表:关闭 nat 表上启用的连接追踪机制
two、五种链
prerouting : 路由前
input :  输入
forward : 转发
output : 输出
postrouting :  路由后
Three、三种规则
accept : 放行
reject : 主动拒绝
drop :  丢弃
Four、实际操作
**|> iptables -I INPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT  # 放在 iptables 文件第一位
1、iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT   # 注意 d 是 目的
2、iptables -t filter -A OUTPUT -p tcp --sport 22 -j ACCEPT  # 注意 s 指源 
3、iptables -t filter -P INPUT  DROP
4、iptables -t filter -P OUTPUT  DROP
5、iptables -A INPUT  -i lo   -j ACCEPT  # 用于php-fpm 成功运行
6、 iptables -A OUTPUT  -o lo   -j ACCEPT  # 和上一个配套
7、iptables -t filter -A INPUT -p tcp --dport 9000 -j ACCEPT   # php-pfm
8、iptables -t filter -A OUTPUT -p tcp --sport 9000 -j ACCEPT  # php-pfm
# 在网卡上打开ping功能
9、iptables -A INPUT -i eth0 -p icmp --icmp-type 8 -j ACCEPT
10、iptables -A OUTPUT -o eth0 -p icmp --icmp-type 8 -j ACCEPT
# 将这条指令插到第一位 (filter 表)
11、iptables -I INPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT
# 开启 dns 服务
-A INPUT -i eth+ -p udp -m udp --sport 53 -j ACCEPT
-A INPUT -i eth+ -p tcp -m tcp --sport 53 -j ACCEPT
-A OUTPUT -o eth+ -p udp -m udp --dport 53 -j ACCEPT
-A OUTPUT -o eth+ -p tcp -m tcp --dport 53 -j ACCEPT
#  执行 wget 命令
-A INPUT -i eth0 -p tcp -m tcp --sport 443 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --sport 80 -j ACCEPT
-A OUTPUT -o eth0 -p tcp -m tcp --dport 443 -j ACCEPT
-A OUTPUT -o eth0 -p tcp -m tcp --dport 80 -j ACCEPT
# DHCP 
-A INPUT -p udp -m multiport --dports 67,68 -j ACCEPT
-A INPUT -p udp -m multiport --sports 67,68 -j ACCEPT

PS:注意:写命令时,关键字要大写;

1、当未指定规则表时,则一律视为是 filter 表。
2、如果没有 :-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT  
就要这样写:-A INPUT -m state --state ESTABLISHED,RELATED -m tcp -p tcp --dport 22 -j ACCEPT    
3、nat 表不能 drop
4、在某一个链中,如:output,表的优先级是:raw > mangle > nat > filter
5、如果远程服务器 mangle 表 input 链和 output 链是 drop 策略,表中 input 和 output 均无允许 22 端口的记录,
filter 表 input 链和 output 链也是 drop 策略,并且表中 input 和 output 均有允许 22 端口的记录,则我们 ssh 连不上远程服务器,
因为,mangle 表优先级高且无允许 22 端口的记录,服务器拒绝了 ssh 连接;
但如果这个 mangle 表和 filter 表中 22 端口记录互换,有变无,无变有,则也不可以 ssh 连接远程服务器,
因为 22 端口经过 mangle 表的允许后,到 filter 表,被拒绝了,所以,也不能 ssh 连服务器,
因此,这 4 个表是如果 input 和 output 两个链(假如有)均是 drop 策略,则 22 端口能否连通是串联的,有一个表不通,整个 ssh 通道就不通。
6、设置策略时,只需 drop filter 表 input 、output、(forward) 链即可,然后允许必要端口,无需 drop 所有表(假如这个表能 drop)。

你可能感兴趣的:(iptables 命令实际操作)