Linux · 2008-11-22

iptables入门学习之iptables的INPUT,OUTPUT,FORWARD

说来笑话,我一个网管对iptables竟然一窍不通?自己也觉得实在说不过去,今天学习一下iptables的入门,记录下来如何使用iptables禁止1个IP的连接,如何删除iptables已定义的规则。
测试机的IP分别为 192.168.0.4 与 192.168.0.8,以下简称.4与.8
在.4上用iptables禁止禁止.8所有的INPUT数据包


.8:~# ping 192.168.0.4
PING 192.168.0.4 (192.168.0.4) 56(84) bytes of data.
64 bytes from 192.168.0.4: icmp_seq=1 ttl=64 time=0.648 ms
64 bytes from 192.168.0.4: icmp_seq=2 ttl=64 time=0.160 ms
64 bytes from 192.168.0.4: icmp_seq=3 ttl=64 time=0.341 ms

— 192.168.0.4 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.160/0.383/0.648/0.201 ms



在.4上禁止.8数据包的进入,这时候.8的数据就进不来了。
-A 是append chain
INPUT是数据包流进.
-s 是source 来源,IP地址或IP段
-j 是jump target 跳跃的目标?粗鲁的理解呵呵!
DROP就是丢弃了,ACCEPT是接受,DENY就是拒绝,但是会发送一条拒绝的信息给对方!所以一般都是直接DROP
INPUT是指定数据规则的类型是进入,还有OUTPUT跟FORWARD,分别为数据流出与数据包转发。

.4:~# iptables -A INPUT -s 192.168.0.8 -j DROP


.8:~# ping 192.168.0.4
PING 192.168.0.4 (192.168.0.4) 56(84) bytes of data.

— 192.168.0.4 ping statistics —
2 packets transmitted, 0 received, 100% packet loss, time 1000ms



查看INPUT的所有规则,并删除刚才那条规则

.4:~# iptables -L INPUT –line-numbers
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP 0 — 8.0.168.192.localhost anywhere

.4:~# iptables -D INPUT 1
# -D就是删除咯
# –delete -D chain Delete matching rule from chain



此时.8是数据允许流入,这样ping .4这台机器的时候就可以通过了,因为我刚才删除了那条规则。

.8:~# ping 192.168.0.4
PING 192.168.0.4 (192.168.0.4) 56(84) bytes of data.
64 bytes from 192.168.0.4: icmp_seq=1 ttl=64 time=0.359 ms
64 bytes from 192.168.0.4: icmp_seq=2 ttl=64 time=0.187 ms
64 bytes from 192.168.0.4: icmp_seq=3 ttl=64 time=0.003 ms

— 192.168.0.4 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.003/0.183/0.359/0.145 ms