Linux iptables配置实例1

使用Linux内置防火墙实现如下8个要求:

1. reject all ftp packets from external networks, but still allow internal ftp.
2. allow ssh remote connections but deny telnet.
3. deny ping.
4. reject all traffic coming to port 21 and 80.
5. reject all traffic coming to all UDP ports (see if you can block all of them, if you
cannot then try to block some UDP ports).
6. block all email coming in and out of your network. Internal email is allowed.
7. block all traffic from two particular networks. You can pick any two networks you like.
8. allow traffic coming to a particular port but reject traffic coming out through that port.

配置实例:

iptabls规则是从上往下应用,当找到能通过的规则,不管下面是否有规则冲突,也一样通过
eth0 连接内部网络的网卡
eth1 连接外部网络的网卡
lo   本地环路
1,丢弃所有来自外网的ftp包,内网例外
iptables -A -i lo -j ACCEPT(允许本机内部所有网络通信,必须的)
iptables -A -i eht0 -p tcp --dport 21 -j ACCEPT(在本机开放21端口,即ftp控制端口)
iptables -A -i eth0 -p tcp --dport 20 -j ACCEPT(在本机开放20端口,即ftp传输端口)
iptables -A -i eth1 -j DROP(禁止所有数据包通过){禁止外部ftp}
2,允许ssh禁止telnet
iptables -A -i lo -j ACCEPT(允许本机内部所有网络通信,必须的)
iptables -A -i eth0 -p tcp --dport 22 -j ACCEPT
iptables -A -i eth1 -p tcp --dport 22 -j ACCEPT
(在本机开放22端口,即ssh服务端口)
iptables -A -i eth1 -p tcp --dport 23 -j DROP(在本机关闭23端口,即telnet服务端口)或者用iptables -A -i eth0 -j DROP
3,禁止使用ping命令,ping本机
iptables -A  -p icmp --icmp-type 8 -s 0/0 -j DROP(0/0所有网络)
iptables -A  -p icmp --icmp-type 0 -s 0/0 -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type 0 -s 192.168.29.1(本机ip) -j DROP
iptables -A OUTPUT -p icmp --icmp-type 8 -s 192.168.29.1 -j ACCEPT
这样的配置是你能ping别人,别人不能ping你
4,禁止访问21端口(ftp)和80端口(web)
iptables -A -i eth1 -p tcp --dprot 21 -j DROP
iptables -A -i eth0 -p tcp --dprot 21 -j DROP
iptables -A -i eth1 -p tcp --dprot 80 -j DROP
iptables -A -i eth0 -p tcp --dprot 80 -j DROP
5,禁止所有udp端口
iptables -A -i eth0 -p udp -j DROP
iptables -A -i eth1 -p udp -j DROP
6,禁止外部邮件通信,内部允许(禁止pop3,110和smtp,25) 
iptables -A -i eth0 -p tcp --dprot 25 -j ACCEPT
iptables -A -i eth0 -p tcp --dprot 110 -j ACCEPT
iptables -A OUTPUT -i eth1 -p tcp --sprot 25 -j DROP
iptables -A OUTPUT -i eth1 -p tcp --sprot 110 -j DROP
7,禁止2个特定网络访问本机
iptables -A -i eth1 -s 192.168.1.0/24 -j DROP
iptables -A -i eth1 -s 172.16.0.0/16 -j DROP
8,允许从特的端口进入,但禁止对外
iptables -A -i eht1 --dport [端口号] -j ACCEPT
iptables -A OUTPUT -i eht1 --dport [端口号] -j DROP
posted @ 2011-04-12 15:12  fhefh  阅读(5727)  评论(0编辑  收藏  举报