linux iptables 设置仅22、80端口可访问

来源:互联网 发布:知足知不足阅读答案 编辑:程序博客网 时间:2024/06/04 19:34

【设置仅22、80端口可访问】 


通过命令 netstat -tnl 可以查看当前服务器打开了哪些端口 
Ssh代码
  1. netstat -tnl  

查看防火墙设置 
Ssh代码 
  1. iptables -L -n   

开放22、80端口 
Ssh代码
  1. iptables -A INPUT -p tcp --dport 22 -j ACCEPT  
  2. iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT  
  3. iptables -A INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT  
  4. iptables -A OUTPUT -p tcp --sport 80 -m state --state NEW,ESTABLISHED -j ACCEPT  

取消其他端口的访问规则 
Ssh代码 
  1. iptables -P INPUT DROP  
  2. iptables -P FORWARD DROP  
  3. iptables -P OUTPUT DROP  


允许本地回环接口(即允许本机访问本机) 
Ssh代码 
  1. iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT  

允许已建立的或相关连的通行(如数据库链接) 
Ssh代码 
  1. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  

允许所有本机向外的访问 
Ssh代码 
  1. iptables -A OUTPUT -j ACCEPT  

保存配置: 
Ssh代码 
  1. service iptables save   

参考: 
http://www.vpser.net/security/linux-iptables.html 
http://www.cdus.org/bbs/forum.php?mod=viewthread&tid=42303
0 0
原创粉丝点击