Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習 Linux

CentOS 7 放行端口:firewalld 與 iptables 實操

Centos7 下啟用端口

在阿里雲等環境部署 Node.js 服務時,若防火牆未放行對應端口,外部就無法訪問。本文以 3000 端口為例,說明如何在 CentOS 7 上停用 firewalld、改用 iptables,並放行所需端口。

CentOS 7 放行端口:firewalld 與 iptables 實操

查看與停用 firewalld

systemctl status firewalld
systemctl stop firewalld

停用後即可訪問 ip 加端口,但這種方式較不安全,建議改用 iptables 並設定規則。

安裝並啟動 iptables

systemctl disable firewalld
yum install -y iptables-services
systemctl start iptables

設定端口規則

先備份設定檔,再設定預設拒絕,然後放行 80 與 3000 端口。

cp -a /etc/sysconfig/iptables /etc/sysconfig/iptables.bak
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

重啟後執行 node app.js,即可透過 3000 端口訪問應用。

Copyright © Linux教程網 All Rights Reserved