美文网首页技术干货
Centos7下启用端口

Centos7下启用端口

作者: Havoc_Zhang | 来源:发表于2018-04-03 11:14 被阅读14次

    今天写了一下node的小demo,于是想放到阿里云服务器上,但是访问时却发生了错误。考虑了下,觉得问题在于端口号上,我写的nodedemo是监听的3000端口,然而linux下有防火墙默认只有80端口开放,于是尝试了下将服务器的3000端口打开,期间还是遇坑不少,下面就梳理下整个过程:

    关闭防火墙

    网上的大部分资料都是用iptables防火墙的,但是阿里云的centos 7默认防火墙是firewall。最为简单的方法其实就是关闭我们的防火墙:

    • 查看下防火墙的状态:
    systemctl status firewalld
    
    这里写图片描述
    • 关闭防火墙:
    systemctl stop firewalld
    

    这样就解决了,在访问下ip地址:端口号就可以看到所写的应用啦!
    但是这样的问题是很不安全,其实可以将firewall服务禁用,应用iptables服务(网上大部分启用端口的资料都是基于iptables服务)。

    安装iptables

    由于没有防火墙会造成不安全,所以给服务器安装一应用更广的防火墙iptables,首先要禁用firewall,通过yum安装iptables

    systemctil disable firewalld
    yum install -y iptables-services
    

    启动iptables

    systemctl start iptables
    

    启动后可以通过systemctl status iptables查看状态。

    更改iptables规则

    • iptables文件备份下:
    cp -a /etc/sysconfig/iptables /etc/sysconfig/iptables.bak
    
    • 设置 INPUT 方向所有的请求都拒绝
    iptables -P INPUT DROP
    
    • 放开所需端口
    iptables -I INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
    iptables -I INPUT -p tcp --dport 3000 -m state --state NEW -j ACCEPT
    
    • 保存规则
    iptables-save > /etc/sysconfig/iptables
    
    • 设置为开机启动并且重启
    systemctl enable iptables.service
    systemctl reboot
    

    相关文章

      网友评论

        本文标题:Centos7下启用端口

        本文链接:https://www.haomeiwen.com/subject/kceqhftx.html