我们很多时候在liunx系统上安装了web服务应用后(如tomcat、apache等),需要让其它电脑能访问到该应用,而linux系统(centos、redhat等)的防火墙是默认只对外开放了22端口。
linux系统的端口设置在/etc/sysconfig/iptables文件中配置。使用编辑器打开该文件。内容如下:
[plain] view plain copy
- # Firewall configuration written by system-config-firewall
- # Manual customization of this file is not recommended.
- *filter
- :INPUT ACCEPT [0:0]
- :FORWARD ACCEPT [0:0]
- :OUTPUT ACCEPT [0:0]
- -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- -A INPUT -p icmp -j ACCEPT
- -A INPUT -i lo -j ACCEPT
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
- -A INPUT -j REJECT --reject-with icmp-host-prohibited
- -A FORWARD -j REJECT --reject-with icmp-host-prohibited
- COMMIT
网上说的是如下code
[plain] view plain copy
- -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3001 -j ACCEPT
我在CentOS6.5中测试上面的代码,不能成功。
如果我们需要对外开放80端口,则上面文件中添加如下code
[plain] view plain copy
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
同时还需要注意的是,这段代码需要加入到
[plain] view plain copy
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
之后,否则端口也不能打开。最后的配如下:
[plain] view plain copy
- # Firewall configuration written by system-config-firewall
- # Manual customization of this file is not recommended.
- *filter
- :INPUT ACCEPT [0:0]
- :FORWARD ACCEPT [0:0]
- :OUTPUT ACCEPT [0:0]
- -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- -A INPUT -p icmp -j ACCEPT
- -A INPUT -i lo -j ACCEPT
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
- -A INPUT -j REJECT --reject-with icmp-host-prohibited
- -A FORWARD -j REJECT --reject-with icmp-host-prohibited
- COMMIT
编辑上面的文件 需要提供su权限.
保存上面的文件后,在终端运行如下命令:更新防火墙配置
[plain] view plain copy
- service iptables restart
下面这个命令可以看到开放的端口
[plain] view plain copy
- /sbin/iptables -L -n
下面的命令可以关闭/打开防火墙(需要重启系统)
[plain] view plain copy
- 开启: chkconfig iptables on
- 关闭: chkconfig iptables off
下面的代码可以启动和停止防火墙(立即生效,重启后失效)
[plain] view plain copy
- 开启: service iptables start
- 关闭: service iptables stop