天天看點

Linux 中如何開啟端口?

Linux 端口的開啟

  • Linux 端口的開啟
  • 一、firewall 方式 (centOS7.*)
  • 二、修改 iptables 方式 (centOS6.*)

一、firewall 方式 (​

​centOS7.*​

​)

  1. 檢視防火牆狀态
firewall-cmd --state      
Linux 中如何開啟端口?
Linux 中如何開啟端口?

如果傳回的是 “not running”,那麼需要先開啟防火牆;

  1. 開啟防火牆
systemctl start firewalld.service      
Linux 中如何開啟端口?

再次檢視防火牆狀态,發現已開啟!

  1. 開啟指定端口
firewall-cmd --znotallow=public --add-port=3306/tcp --permanent      

顯示 success 表示成功

–znotallow=public 表示作用域為公共的

–add-port=443/tcp 添加 tcp 協定的端口端口号為 443

–permanent 永久生效,如果沒有此參數,則隻能維持目前 服 務生命周期内,重新啟動後失效;

Linux 中如何開啟端口?
  1. 重新開機防火牆
systemctl restart firewalld.service      

系統沒有任何提示表示成功!

  1. 重新加載防火牆
firewall-cmd --reload      

顯示 success 表示成功

  1. 其他指令
#檢視已開啟的端口
firewall-cmd --list-ports      
#關閉指定端口
firewall-cmd --znotallow=public --remove-port=8080/tcp --permanent
systemctl restart firewalld.service
firewall-cmd --reload      
#檢視端口被哪一個程序占用
netstat -lnpt |grep 5672
# centos7預設沒有 netstat 指令,需要安裝 net-tools 工具:
# 安裝 net-tools
yum install -y net-tools      
# 臨時關閉防火牆
systemctl stop firewalld.service
# 或者
systemctl stop firewalld      
# 永久關閉防火牆(必須先臨時關閉防火牆,再執行該指令,進行永久關閉)
systemctl disable firewalld.service
# 或者
systemctl disable firewalld      

二、修改 iptables 方式 (​

​centOS6.*​

​)

​centOS6.*​

​ 的linux版本是自帶iptables的,是以可以直接使用該方式,centOS7 不自帶iptables的,是以要使用該方式,需要手動安裝iptables後,再使用該方式!

2.1 修改 iptables 檔案

#修改iptables
[root@localhost sbin]# vi /etc/sysconfig/iptables      
Linux 中如何開啟端口?

2.2 重新開機防火牆

[root@localhost sbin]# /etc/init.d/iptables restart      
Linux 中如何開啟端口?

三、注意事項

繼續閱讀