linux系统对外开放8080等端口,防火墙设置

我们很多时候在liunx系统上安装了web服务应用后(如tomcat、apache等),需要让其它电脑能访问到该应用,而linux系统(centos、redhat等)的防火墙是默认只对外开放了22端口。

 linux系统的端口设置在/etc/sysconfig/iptables文件中配置。使用编辑器打开该文件。内容如下:

 

  1. # Firewall configuration written by system-config-firewall  
  2. # Manual customization of this file is not recommended.  
  3. *filter  
  4. :INPUT ACCEPT [0:0]  
  5. :FORWARD ACCEPT [0:0]  
  6. :OUTPUT ACCEPT [0:0]  
  7. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  
  8. -A INPUT -p icmp -j ACCEPT  
  9. -A INPUT -i lo -j ACCEPT  
  10. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT  
  11. -A INPUT -j REJECT --reject-with icmp-host-prohibited  
  12. -A FORWARD -j REJECT --reject-with icmp-host-prohibited  
  13. COMMIT  

网上说的是如下code

  1. -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3001 -j ACCEPT  

我在CentOS6.5中测试上面的代码,不能成功。

 

如果我们需要对外开放80端口,则上面文件中添加如下code

 

  1. -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT  


同时还需要注意的是,这段代码需要加入到

  1. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT  

之后,否则端口也不能打开。最后的配如下:

  1. # Firewall configuration written by system-config-firewall  
  2. # Manual customization of this file is not recommended.  
  3. *filter  
  4. :INPUT ACCEPT [0:0]  
  5. :FORWARD ACCEPT [0:0]  
  6. :OUTPUT ACCEPT [0:0]  
  7. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  
  8. -A INPUT -p icmp -j ACCEPT  
  9. -A INPUT -i lo -j ACCEPT  
  10. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT  
  11. -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT  
  12. -A INPUT -j REJECT --reject-with icmp-host-prohibited  
  13. -A FORWARD -j REJECT --reject-with icmp-host-prohibited  
  14. COMMIT  

 

编辑上面的文件 需要提供su权限.

 

保存上面的文件后,在终端运行如下命令:更新防火墙配置

  1. service iptables restart  

 

 

下面这个命令可以看到开放的端口

  1. iptables -L -n  

 

linux系统对外开放8080等端口,防火墙设置

 

 

下面的命令可以关闭/打开防火墙(需要重启系统)


  1. 开启: chkconfig iptables on  
  2. 关闭: chkconfig iptables off  

 

下面的代码可以启动和停止防火墙(立即生效,重启后失效)

  1. 开启: service iptables start   
  2. 关闭: service iptables stop