boante 2015-01-30
1、允许通过某一端口
vi/etc/sysconfig/iptables
-AINPUT-mstate--stateNEW-mtcp-ptcp--dport80-jACCEPT(允许80端口通过防火墙)
/etc/init.d/iptablesrestart
#最后重启防火墙使配置生效
只允许特定ip访问某端口?参考下面命令,只允许46.166.150.22访问本机的80端口。如果要设置其他ip或端口,改改即可。
iptables-IINPUT-pTCP--dport80-jDROP
iptables-IINPUT-s46.166.150.22-pTCP--dport80-jACCEPT
在root用户下执行上面2行命令后,重启iptables,serviceiptablesrestart
查看iptables是否生效:
[[email protected]]#iptables-L
ChainINPUT(policyACCEPT)
targetprotoptsourcedestination
ACCEPTtcp--46.166.150.22anywheretcpdpt:http
DROPtcp--anywhereanywheretcpdpt:http
ChainFORWARD(policyACCEPT)
targetprotoptsourcedestination
ChainOUTPUT(policyACCEPT)
targetprotoptsourcedestination
上面命令是针对整个服务器(全部ip)禁止80端口,如果只是需要禁止服务器上某个ip地址的80端口,怎么办?
下面的命令是只允许来自174.140.3.190的ip访问服务器上216.99.1.216的80端口
iptables-AFORWARD-s174.140.3.190-d216.99.1.216-ptcp-mtcp--dport80-jACCEPT
iptables-AFORWARD-d216.99.1.216-ptcp-mtcp--dport80-jDROP
如果您不熟悉linux的ssh命令,那么可以在webmin/virtualmin面板中设置,达到相同效果。参考:webmin面板怎样设置允许特定ip访问80端口,禁止80端口
更多iptables参考命令如下:
1.先备份iptables
#cp/etc/sysconfig/iptables/var/tmp
需要开80端口,指定IP和局域网
下面三行的意思:
先关闭所有的80端口
开启ip段192.168.1.0/24端的80口
开启ip段211.123.16.123/24端ip段的80口
#iptables-IINPUT-ptcp--dport80-jDROP
#iptables-IINPUT-s192.168.1.0/24-ptcp--dport80-jACCEPT
#iptables-IINPUT-s211.123.16.123/24-ptcp--dport80-jACCEPT
以上是临时设置。
2.然后保存iptables
#serviceiptablessave
3.重启防火墙
#serviceiptablesrestart
===============以下是转载================================================
以下是端口,先全部封再开某些的IP
iptables-IINPUT-ptcp--dport9889-jDROP
iptables-IINPUT-s192.168.1.0/24-ptcp--dport9889-jACCEPT
如果用了NAT转发记得配合以下才能生效
iptables-IFORWARD-ptcp--dport80-jDROP
iptables-IFORWARD-s192.168.1.0/24-ptcp--dport80-jACCEPT
常用的IPTABLES规则如下:
只能收发邮件,别的都关闭
iptables-IFilter-mmac--mac-source00:0F:EA:25:51:37-jDROP
iptables-IFilter-mmac--mac-source00:0F:EA:25:51:37-pudp--dport53-jACCEPT
iptables-IFilter-mmac--mac-source00:0F:EA:25:51:37-ptcp--dport25-jACCEPT
iptables-IFilter-mmac--mac-source00:0F:EA:25:51:37-ptcp--dport110-jACCEPT
IPSECNAT策略
iptables-IPFWanPriv-d192.168.100.2-jACCEPT
iptables-tnat-APREROUTING-ptcp--dport80-d$INTERNET_ADDR-jDNAT--to-destination192.168.100.2:80
iptables-tnat-APREROUTING-ptcp--dport1723-d$INTERNET_ADDR-jDNAT--to-destination192.168.100.2:1723
iptables-tnat-APREROUTING-pudp--dport1723-d$INTERNET_ADDR-jDNAT--to-destination192.168.100.2:1723
iptables-tnat-APREROUTING-pudp--dport500-d$INTERNET_ADDR-jDNAT--to-destination192.168.100.2:500
iptables-tnat-APREROUTING-pudp--dport4500-d$INTERNET_ADDR-jDNAT--to-destination192.168.100.2:4500
FTP服务器的NAT
iptables-IPFWanPriv-ptcp--dport21-d192.168.1.22-jACCEPT
iptables-tnat-APREROUTING-ptcp--dport21-d$INTERNET_ADDR-jDNAT--to-destination192.168.1.22:21
只允许访问指定网址
iptables-AFilter-pudp--dport53-jACCEPT
iptables-AFilter-ptcp--dport53-jACCEPT
iptables-AFilter-dwww.ctohome.com-jACCEPT
iptables-AFilter-dwww.guowaivps.com-jACCEPT
iptables-AFilter-jDROP
开放一个IP的一些端口,其它都封闭
iptables-AFilter-ptcp--dport80-s192.168.1.22-dwww.pconline.com.cn-jACCEPT
iptables-AFilter-ptcp--dport25-s192.168.1.22-jACCEPT
iptables-AFilter-ptcp--dport109-s192.168.1.22-jACCEPT
iptables-AFilter-ptcp--dport110-s192.168.1.22-jACCEPT
iptables-AFilter-ptcp--dport53-jACCEPT
iptables-AFilter-pudp--dport53-jACCEPT
iptables-AFilter-jDROP
多个端口
iptables-AFilter-ptcp-mmultiport--destination-port22,53,80,110-s192.168.20.3-jREJECT
连续端口
iptables-AFilter-ptcp-mmultiport--source-port22,53,80,110-s192.168.20.3-jREJECTiptables-AFilter-ptcp--source-port2:80-s192.168.20.3-jREJECT
指定时间上网
iptables-AFilter-s10.10.10.253-mtime--timestart6:00--timestop11:00--daysMon,Tue,Wed,Thu,Fri,Sat,Sun-jDROP
iptables-AFilter-mtime--timestart12:00--timestop13:00--daysMon,Tue,Wed,Thu,Fri,Sat,Sun-jACCEPT
iptables-AFilter-mtime--timestart17:30--timestop8:30--daysMon,Tue,Wed,Thu,Fri,Sat,Sun-jACCEPT
禁止多个端口服务
iptables-AFilter-mmultiport-ptcp--dport21,23,80-jACCEPT
将WAN口NAT到PC
iptables-tnat-APREROUTING-i$INTERNET_IF-d$INTERNET_ADDR-jDNAT--to-destination192.168.0.1
将WAN口8000端口NAT到192。168。100。200的80端口
iptables-tnat-APREROUTING-ptcp--dport8000-d$INTERNET_ADDR-jDNAT--to-destination192.168.1.22:80
MAIL服务器要转的端口
iptables-tnat-APREROUTING-ptcp--dport110-d$INTERNET_ADDR-jDNAT--to-destination192.168.1.22:110
iptables-tnat-APREROUTING-ptcp--dport25-d$INTERNET_ADDR-jDNAT--to-destination192.168.1.22:25
只允许PING202。96。134。133,别的服务都禁止
iptables-AFilter-picmp-s192.168.1.22-d202.96.134.133-jACCEPT
iptables-AFilter-jDROP
禁用BT配置
iptables–AFilter–ptcp–dport6000:20000–jDROP
禁用QQ防火墙配置
iptables-AFilter-pudp--dport!53-jDROP
iptables-AFilter-d218.17.209.0/24-jDROP
iptables-AFilter-d218.18.95.0/24-jDROP
iptables-AFilter-d219.133.40.177-jDROP
基于MAC,只能收发邮件,其它都拒绝
iptables-IFilter-mmac--mac-source00:0A:EB:97:79:A1-jDROP
iptables-IFilter-mmac--mac-source00:0A:EB:97:79:A1-ptcp--dport25-jACCEPT
iptables-IFilter-mmac--mac-source00:0A:EB:97:79:A1-ptcp--dport110-jACCEPT
禁用MSN配置
iptables-AFilter-pudp--dport9-jDROP
iptables-AFilter-ptcp--dport1863-jDROP
iptables-AFilter-ptcp--dport80-d207.68.178.238-jDROP
iptables-AFilter-ptcp--dport80-d207.46.110.0/24-jDROP
只允许PING202。96。134。133其它公网IP都不许PING
iptables-AFilter-picmp-s192.168.1.22-d202.96.134.133-jACCEPT
iptables-AFilter-picmp-jDROP
禁止某个MAC地址访问internet:
iptables-IFilter-mmac--mac-source00:20:18:8F:72:F8-jDROP
禁止某个IP地址的PING:
iptables–AFilter–picmp–s192.168.0.1–jDROP
禁止某个IP地址服务:
iptables–AFilter-ptcp-s192.168.0.1--dport80-jDROP
iptables–AFilter-pudp-s192.168.0.1--dport53-jDROP
只允许某些服务,其他都拒绝(2条规则)
iptables-AFilter-ptcp-s192.168.0.1--dport1000-jACCEPT
iptables-AFilter-jDROP
禁止某个IP地址的某个端口服务
iptables-AFilter-ptcp-s10.10.10.253--dport80-jACCEPT
iptables-AFilter-ptcp-s10.10.10.253--dport80-jDROP
禁止某个MAC地址的某个端口服务
iptables-IFilter-ptcp-mmac--mac-source00:20:18:8F:72:F8--dport80-jDROP
禁止某个MAC地址访问internet:
iptables-IFilter-mmac--mac-source00:11:22:33:44:55-jDROP
禁止某个IP地址的PING:
iptables–AFilter–picmp–s192.168.0.1–jDROP
下面转截
启动iptables
serviceiptablesstart
iptables–list//*查看iptables规则集*//
下面是没有定义规划时iptables的样子:
ChainINPUT(policyACCEPT)
targetprotoptsourcedestination
ChainFORWARD(policyACCEPT)
targetprotoptsourcedestination
ChainOUTPUT(policyACCEPT)
targetprotoptsourcedestination
如何开启/关闭指定端口
例如:
开启81端口:
iptables-IINPUT-ieth0-ptcp–dport81-jACCEPT
iptables-IOUTPUT-oeth0-ptcp–sport81-jACCEPT
关闭81端口:
iptables-IINPUT-ieth0-ptcp–dport81-jDROP
iptables-IOUTPUT-oeth0-ptcp–sport81-jDROP
然后保存
/etc/rc.d/init.d/iptablessave
eth0为网卡名称,可以输入ifconfig来查看网卡信息,注意填写正确的网卡名称。
可以使用lsof命令来查看某一端口是否开放.查看端口可以这样来使用.
我就以81端口为例:
lsof-i:81
如果有显示说明已经开放了,如果没有显示说明没有开放。