赞
踩
应用顺序:由上至下
表 | 支持的链 |
---|---|
raw | PREROUTING、OUTPUT |
mangle | PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD |
nat | PREROUTING、POSTROUTING、OUTPUT |
fIlter | INPUT、OUTPUT、FORWARD |
iptables [-t 要操作的表] <操作命令> [要操作的链] [规则号码] [匹配条件] [-j 匹配后的动作]
# 小写 大写 大写 小写 大写
-L
:查看,v详细,n不反解-A
:追加,放置最后一条-I
:插入,默认插入成第一条(第一条生效后不会考虑后边规则
)-D
:删除-F
:清空flush-X
:删除空的自定义链-P
:设置默认策略-Z
:计数器归零-N
:建立自定义链-s 192.168.2.0/24
:源地址-d 192.168.2.1
:目标地址-p tcp|upd|icmp
:协议-i eth0
:input 从eth0接口进入的数据包-o eth0
:output 从eth0出去的数据包-p tcp --sport 80
:源端口是80的数据包-p tcp --dport 80
:目标端口是80-j ACCEPT
:接受-j REJECT
:拒绝-j DROP
:丢弃-j LOG
:记录日志DROP和REJECT的区别:
REJECT
:直接拒绝链接,和客户端断开。更适合可控网络。DROP
:丢弃——意味着不予回复,客户端只能等超时。更适合服务器安全和面对攻击。
-j SNAT
:源地址转换-j DNAT
:目标地址转换-j MASQUERADE
:伪装-j REDIRECT
:端口转换-j MARK
:标记设置默认值,禁止所有穿过防火墙
iptables -P FORWARD DROP
iptables -I INPUT -p tcp --dport 80 -j DROP
iptables -nvL
iptables -t filter -nvL
iptables -nL --line-number
service iptables save
该命令会把防火墙规则保存在
/etc/sysconfig/iptables
文件中
[root@liubei-02 ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Tue Jan 10 16:06:09 2023
*mangle
:PREROUTING ACCEPT [31154:2463094]
:INPUT ACCEPT [10892:1355724]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [15203:10068473]
:POSTROUTING ACCEPT [15203:10068473]
COMMIT
# Completed on Tue Jan 10 16:06:09 2023
# Generated by iptables-save v1.4.21 on Tue Jan 10 16:06:09 2023
*raw
:PREROUTING ACCEPT [31154:2463094]
:OUTPUT ACCEPT [15203:10068473]
COMMIT
# Completed on Tue Jan 10 16:06:09 2023
# Generated by iptables-save v1.4.21 on Tue Jan 10 16:06:09 2023
*filter
:INPUT ACCEPT [5015:664817]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [7751:5368662]
-A INPUT -p tcp -m tcp --sport 80 -j DROP
COMMIT
# Completed on Tue Jan 10 16:06:09 2023
# Generated by iptables-save v1.4.21 on Tue Jan 10 16:06:09 2023
*nat
:PREROUTING ACCEPT [20546:1128148]
:INPUT ACCEPT [235:18006]
:OUTPUT ACCEPT [1055:72144]
:POSTROUTING ACCEPT [1055:72144]
COMMIT
# Completed on Tue Jan 10 16:06:09 2023
如上可见:
*mangle
:表明:INPUT ACCEPT [5015:664817]
:这种是默认规则-A INPUT -p tcp -m tcp --sport 80 -j DROP
:这种是我们在每个表下加的规则
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type echo-reply -j REJECT
或
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 0 -j REJECT
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type echo-request -j REJECT
或
iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8 -j REJECT
iptables -D CHAIN_NAME N
N为规则编号。
iptables -F
iptables -N MYCHAIN
iptables -X MYCHAIN
访问本机100端口转发至本机80端口
iptables -t nat -I PREROUTEING -p tcp --dport 100 -j REDIRECT --to-port 80
[root@liubei-02 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
如上可见,当初没有任何规则。
iptables -I INPUT -p tcp --dport 80 -j DROP
[root@liubei-02 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- anywhere anywhere tcp dpt:http
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
如上可见,nat表中多了一条记录
[root@liubei-02 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]
如上可见,规则保存在
/etc/sysconfig/iptables
中
# Generated by iptables-save v1.4.21 on Tue Jan 10 17:22:38 2023
*mangle
:PREROUTING ACCEPT [1960:156458]
:INPUT ACCEPT [675:88821]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [961:649865]
:POSTROUTING ACCEPT [961:649865]
COMMIT
# Completed on Tue Jan 10 17:22:38 2023
# Generated by iptables-save v1.4.21 on Tue Jan 10 17:22:38 2023
*raw
:PREROUTING ACCEPT [1960:156458]
:OUTPUT ACCEPT [961:649865]
COMMIT
# Completed on Tue Jan 10 17:22:38 2023
# Generated by iptables-save v1.4.21 on Tue Jan 10 17:22:38 2023
*filter
:INPUT ACCEPT [312:38483]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [457:343983]
-A INPUT -p tcp -m tcp --dport 80 -j DROP
COMMIT
# Completed on Tue Jan 10 17:22:38 2023
# Generated by iptables-save v1.4.21 on Tue Jan 10 17:22:38 2023
*nat
:PREROUTING ACCEPT [1300:68667]
:INPUT ACCEPT [15:1030]
:OUTPUT ACCEPT [77:5220]
:POSTROUTING ACCEPT [77:5220]
COMMIT
# Completed on Tue Jan 10 17:22:38 2023
如上可见,我们创建的规则被保存在文件中。
先查看规则的编号
[root@liubei-02 ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
Chain FORWARD (policy DROP)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
如上可见,我们刚才创建的规则是
nat
表INPUT
链中编号为1
的规则
删除规则
[root@liubei-02 ~]# iptables -D INPUT 1
默认是
nat
表,因此我们不用写-t nat
[root@liubei-02 ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
如上可见,之前创建的规则被删除了。
要求:使用新建的MYCHAIN创建允许80端口访问的规则
[root@liubei-02 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@liubei-02 ~]# iptables -N MYCHAIN
[root@liubei-02 ~]# iptables -I INPUT -p tcp --dport 80 -d 10.10.239.32 -j MYCHAIN
[root@liubei-02 ~]# iptables -I MYCHAIN -p tcp --dport 80 -j ACCEPT
测试一下,此时80端口可以访问了。
[root@liubei-02 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
MYCHAIN tcp -- anywhere liubei-02 tcp dpt:http
DROP tcp -- anywhere anywhere tcp dpt:http
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain MYCHAIN (1 references)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:http
如上可见
- INPUT链中添加了一个拒绝规则,添加了一条规则转发的规则
- 多了一个MYCHAIN链,下边有一条允许80端口通过规则。
作用:内网客户端访问公网
内网 | 路由 | 公网 |
---|---|---|
client | LAN:192.168.1.1--------WAN:10.10.239.32 | web:10.252.96249 |
iptables -t nat -I POSTROUTING -s 10.252.96.0/24 -p tcp -j SNAT --to 10.10.239.32
理解路由后过滤,在请求出路由的时候把请求的源地址改成路由本机,使得web可以返回给路由。
作用:公网访问内网
内网 | 路由 | 公网 |
---|---|---|
web:192.168.1.110 | LAN:192.168.1.1,WAN 10.10.239.3 | client |
iptables -t nat -I PREROUTING -d 10.10.239.32 -p tcp --dport 80 -j DNAT --to 192.168.1.110
理解路由前过滤:请求目标是路由地址,在进入路由前被修改,否则将直接被路由本机接收。
如果转发端口不同
路由8080端口转发至内网web服务器8080端口
iptables -t nat -I PREROUTING -d 10.10.239.32 -p tcp --dport 8080 -j DNAT --to 192.168.1.110:80
iptables -A INPUT -p tcp --dport 20 -j ACCEPT # 开放FTP主动数据端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT # 对外开放FTP控制端口
iptables -A INPUT -p tcp --dport 50000:60000 -j ACCEPT # 对外开放FTP数据端口
iptables -A INPUT -j REJECT
iptables-save > /etc/iptables-script
iptables-restore < /etc/iptables-script
/etc/rc.local
/etc/rc.local
需要执行权限。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。