iptables 防火墙规则
650) this.width=650;” onclick=’window.open(“http://blog.51cto.com/viewpic.php?refimg=” + this.src)’ onclick=’window.open(“http://blog.51cto.com/viewpic.php?refimg=” + “http://blog.51cto.com/viewpic.php?refimg=” + this.src)’ alt=”” src=”http://cdn.verydemo.com/upload/2013_04_30/13672649971390.jpg” border=”0″ />
#把环境搭建好,测试相邻设备之间的连通性。
#在LINUX服务器上开启路由功能
# echo 1 >/proc/sys/net/ipv4/ip_forward
#开启路由后,2个网段之间就可以互相通信了。
#定义三条防火墙策略,拒绝所有的访问。
iptables ?P INPUT DROP
iptables ?P OUTPUT DROP
iptables ?P FORWARD DROP
//定义后,不管内网还是外网都不能访问iptables防火墙了。
//防火墙也不能访问其他的PC了(内网和外网都不能访问了)
#如何允许防火墙访问其他PC了,又要防火墙权限开到最小。
iptables ?t filter ?A OUTPUT ?d 0.0.0.0/0 ?j ACCEPT
//允许防火墙本地流量可以出去到任何网络
iptables ?A INPUT ?p icmp –icmp-type echo-reply ?j ACCEPT
//允许所有的网络回显到防火墙本身接口(这样就可以看到ping通的回显了)
#假如还有很多服务,防火墙有一条默认规则出去,但有些服务必须还有回来访问到本地(Telnet、ssh)怎样允许服务到本地防火墙呢?
iptables ?A INPUT ?p tcp ?sport 22 ?j ACCEPT
(1).允许PC1可以ping通外网的服务器,而PC2的拒绝ping通外网?
源 目标 协议
# iptables ?t filter ?A FORWARD ?s 192.168.10.1 ?d 202.103.100.0/24 ?p icmp
请求 允许
–icmp-type echo-request ?j ACCEPT //允许pc1的ping请求到外网服务器。
源 目标 协议
# iptables ?t filter ?A FORWARD ?s 202.103.100.0/24 ?d 192.168.10.1 ?p icmp -?icmp-type
echo-reply ?j ACCEPT //允许外网ping的回显到内网PC1
#做完这两步后,pc1就可以ping通外网了,而pc2不能ping通外网
(2).允许PC1可以查看外网web服务,而pc2不可以访问外网web服务器?
iptables ?t filter ?A FORWARD ?s 192.168.10.1 ?d 202.103.100.0/24 ?p tcp
–dport 80 ?j ACCEPT //允许pc1访问外网的http服务器
iptables ?t filter ?A FORWARD ?s 202.103.100.0/24 ?d 192.168.10.1 ?p tcp
–sport 80 ?j ACCEPT //允许外网Web服务器到内网
#做完2步后,PC1可以访问外网web服务器了,而PC2就不可以访问外网web。
(3).允许pc1可以访问外网DNS服务器,而PC2不可以访问?
iptables ?t filter ?A FORWARD ?s 192.168.10.1 ?d 202.103.100.0/24 ?p udp
–dport 53 ?j ACCEPT //允许pc1访问外网的DNS服务器。
iptables ?t filter ?A FORWARD ?s 202.103.100.0/24 ?d 192.168.10.1 ?p udp
–sport 53 ?j ACCEPT //允许外网的53端口访问PC1。
(4).允许pc1可以访问外网FTP服务器,而PC2不可以访问?
iptables ?t filter ?A FORWARD ?s 192.168.10.1 ?d 202.103.100.0/24 ?p
tcp ?m multiport – -dport 20,21 ?j ACCEPT
//允许内网PC1访问到外网的ftp服务器20.21号端口
iptables ?t filter ?A FORWARD ?s 202.103.100.0/24 ?d 192.168.10.1 ?p tcp ?m multiport
–sport 20,21 ?j ACCEPT
//允许外网FTP服务器访问到内网PC
(5).拒绝外网PC、服务器ping通iptables防火墙的eth1接口IP?
只要开启了三条链的默认规则DORP,默认外网就不能访问、ping通
防火墙接口了。
(6).允许外网100.1可以ping通IPTABLES的公网IP,而100.2不可以ping通?
iptables ?t filter ?A INPUT ?s 202.103.100.1 ?d 202.103.100.200 ?p icmp
–icmp-request ?j ACCEPT
//允许100.1的ping请求进站到达iptables防火墙。
iptables ?t filter ?A OUTPUT ?s 202.103.100.200 ?d 202.103.100.1 ?p icmp
–icmp-reply ?j ACCEPT
//允许iptables的回显出站到100.1服务器
iptables上允许被开放的服务
iptables -A INPUT -p tcp –sport 80 -j ACCEPT //允许开放TCP的80端口
iptables -A INPUT -p udp –sport 53 -j ACCEPT //允许开启udp的53端口
iptables -A INPUT -p tcp –sport 25 -j ACCEPT //允许开启TCP的25号端口
iptables ? NAT转换规则
iptables -t nat -L //查看NAT链中所有的规则
iptables -t nat -F //清除NAT里所有的规则
iptables -t nat -L -n –line-number //以序号显示NAT中的规则
iptables -t nat -D PREROUTING 1 //删除PREROUTING中的第1条链
把内网的服务器发布到外网接口
650) this.width=650;” onclick=’window.open(“http://blog.51cto.com/viewpic.php?refimg=” + this.src)’ onclick=’window.open(“http://blog.51cto.com/viewpic.php?refimg=” + “http://blog.51cto.com/viewpic.php?refimg=” + this.src)’ alt=”” src=”http://cdn.verydemo.com/upload/2013_04_30/13672649973691.jpg” border=”0″ />
#DNAT重定向,允许访问公网接口重定向到内网的服务器(53、80、443、25等)
一.Web服务器的发布:
链 入站 协议 防火墙公网接口 服务端口 目NAT 重定向到内网的80接口
iptables -t nat -A PREROUTING -p tcp -d 202.103.100.1 –dport 80 -j DNAT –to 192.168.1.11:80
#在防火墙默认的情况下,这样做后,所有的外网用户在访问eth1接口时,Linux服务器都会把该请求转交给2003web服务器。
#如果我们把所有的策略都设置成了拒绝(DROP),那还要写2条规则允许外网PC访问内网PC,规则如下:
1. 允许所有外网的IP访问到内网服务器。
iptables ?t filter ?A FORWARD ?s 0.0.0.0/0 ?d 192.168.1.11 ?p tcp ?dport 80 ?j ACCEPT
2. 允许内网web服务器的数据出去。
iptables ?t filter ?A FORWARD ?s 192.168.1.11 ?d 0.0.0.0/0 ?p tcp ?sport 80 ?j ACCEPT
旅游时最好的习惯:找个舒适的小店,挑张雅致的明信片,