您的位置:首页 > 运维架构 > Linux

Linux 如何打开端口

2015-10-11 20:49 477 查看
今天开通了一个阿里云主机,在安装tomcat服务后发现80、8080等端口不通,网上找了资料,先是按如下步骤操作:

vi /etc/sysconfig/iptables

-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火墙)
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT(允许3306端口通过防火墙)
特别提示:很多网友把这两条规则添加到防火墙配置的最后一行,导致防火墙启动失败,正确的应该是添加到默认的22端口这条规则的下面

添加好之后防火墙规则如下所示:

######################################
# 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 80 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited
-A FORWARD -j REJECT –reject-with icmp-host-prohibited
COMMIT
#####################################

/etc/init.d/iptables restart
#最后重启防火墙使配置生效

--以上来源于:http://www.myhack58.com/Article/48/66/2012/34999.htm

按照以上方法配置在重启时报错,找到了另一篇文章:
http://www.php114.net/n/learn/server/20141011/534.html
/etc/init.d/iptables restart
iptables:清除防火墙规则: [确定]
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则:Bad argument `–-state'
Error occurred at line: 11
Try `iptables-restore -h' or 'iptables-restore --help' for more information.
[失败]
发现这种方法并不好使,于是尝试另外一种,通过命令去添加端口的方法。
[root@centos httpd]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@centos httpd]# /etc/rc.d/init.d/iptables save
[root@centos httpd]# /etc/init.d/iptables restart
这样就搞定了,查看效果
[root@centos httpd]# /etc/init.d/iptables status
-----------------------------------------------------------
今天换了个centos7的主机,发现没有iptables了,找到下面的方法:

CentOS 7 默认没有使用iptables,所以通过编辑iptables的配置文件来开启80端口是不可以的
CentOS 7 采用了 firewalld 防火墙
如要查询是否开启80端口则:

1

2
[root@joe-pc ~]# firewall-cmd --query-port=80/tcp

no

显然80端口没有开启
下面我们开启80端口:

1

2
[root@joe-pc ~]# firewall-cmd --add-port=80/tcp

success

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux 端口 防火墙