天天看點

CentOS開啟,關閉,檢視端口

Centos7 開啟 , 關閉端口

CentOS7

預設沒有使用iptables , 是以不能通過編輯

iptables

的配置檔案來開啟端口 ,

CentOS7

采用了

firewalld

防火牆

如要查詢是否開啟3306端口則 :

# firewall-cmd --query-port=3306/tcp
           

開啟端口 :

# firewall-cmd --zone=public --add-port=3306/tcp --permanent
           
指令含義 :

--zone #作用域

,

--add-port=80/tcp #添加端口 , 格式為 : 端口/通訊協定

,

--permanent #永久生效 , 沒有此參數重新開機後失效

重新開機防火牆 :

# firewall-cmd --reload
           

關閉端口 :

# firewall-cmd --zone=public --remove-port=3306/tcp --permanent
           

檢視防火牆狀态 :

# systemctl status firewalld
           

開啟防火牆服務 :

# systemctl start firewalld.service
           

設定防火牆開機啟動 :

# systemctl enable firewalld.service
           

Centos6.X 開啟 , 關閉端口

使用

iptables

開放如下端口 :

# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
           

儲存 :

# /etc/rc.d/init.d/iptables save
           

重新開機服務 :

# /etc/rc.d/init.d/iptables restart
           

檢視端口是否開放 :

# /etc/init.d/iptables status
           

關閉端口 :

# /sbin/iptables -I INPUT -p tcp --dport 3306 -j DROP
           

清除 iptables :

# /sbin/iptables -F
           

檢視預設連接配接規則 :

# iptables -L
           

設定入方向預設規則為接受連接配接 :

# /sbin/iptables -P INPUT ACCEPT
           

檢視系統端口占用情況

1.

lsof -i:

, 用于檢視某一端口的占用情況 , 比如檢視 8000 端口使用情況 :

# lsof -i:22
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     1087 root    3u  IPv4  17344      0t0  TCP *:ssh (LISTEN)
sshd     1087 root    4u  IPv6  17353      0t0  TCP *:ssh (LISTEN)
           
出現

-bash: lsof: command not found

時 , 是指令沒安裝 , 直接安裝

yum install lsof

2.

netstat -tunlp

, 用于檢視系統端口占用情況

# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10083/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1087/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1409/master         
           
說明一下幾個參數的含義 :

-t (tcp) 僅顯示tcp相關選項

,

-u (udp)僅顯示udp相關選項

,

-n 拒絕顯示别名,能顯示數字的全部轉化為數字

,

-l 僅列出在Listen(監聽)的服務狀态

,

-p 顯示建立相關連結的程式名

3.

netstat -apn

, 用于檢視所有的程序和端口使用情況

# netstat -apn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10083/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1087/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1409/master         
tcp        0     52 192.168.0.150:22        192.168.0.102:2565      ESTABLISHED 15354/sshd: [email protected] 
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1087/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1409/master  
           
作者 Github : tojohnonly , 部落格 : EnskDeCode

繼續閱讀