目录
1 实验环境
192.168.0.0/24
访问用机器(Centos) --------------------- iptables设定机器(Ubuntu)
192.168.0.211 192.168.0.203
2 iptables常用用法
2.1 查看iptables链
语法:iptables -t [table] -nvL
[table] :raw ,mangle,filter,nat,security
-n:让IP地址和端口号都表示成数字
-v:显示最详细的内容
-L:打印链的所有规则
使用例
# 显示nat表的所有链的规则
root@node2:~# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 39 packets, 45144 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 39 packets, 45144 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 4 packets, 279 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 4 packets, 279 bytes)
pkts bytes target prot opt in out source destination
# 显示nat表的INPUT链的规则
root@node2:~# iptables -t nat -nvL INPUT
Chain INPUT (policy ACCEPT 39 packets, 45144 bytes)
pkts bytes target prot opt in out source destination
2.2 规则追加
iptables -t [table] -A [chain] [match] [target]
[]里的参数的设定方式可以参照上一篇文章
使用例
# 在filter表的INPUT链追加规则,icmp的时候,拒绝并告知送信方
root@node2:~# iptables -t filter -A INPUT -p icmp -j REJECT
# 可以看到规则添加成功
root@node2:~# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 142 packets, 58712 bytes)
pkts bytes target prot opt in out source destination
4 336 REJECT icmp -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
# 在Centos上ping Ubuntu发现被拒绝
[root@centos ~]# ping 192.168.0.203 -c 4
PING 192.168.0.203 (192.168.0.203) 56(84) bytes of data.
From 192.168.0.203 icmp_seq=1 Destination Port Unreachable
From 192.168.0.203 icmp_seq=2 Destination Port Unreachable
From 192.168.0.203 icmp_seq=3 Destination Port Unreachable
From 192.168.0.203 icmp_seq=4 Destination Port Unreachable
--- 192.168.0.203 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3023ms
2.3 规则删除
iptables -t [table] -D [chain] [linenumber]
使用例
# 先查看规则的编号(-t省略的时候默认是filter表)
root@node2:~# iptables -nvL INPUT --line-numbers
Chain INPUT (policy ACCEPT 70 packets, 15375 bytes)
num pkts bytes target prot opt