linux下打開對外開放端口號

第一種方式

(1)查看對外開放的端口狀態
         查詢已開放的端口 netstat  -ntulp | grep 端口號:可以具體查看某一個端口號
         查詢指定端口是否已開 firewall-cmd --query-port=666/tcp
           提示 yes,表示開啓;no表示未開啓。


(2)查看防火牆狀態
         查看防火牆狀態 systemctl status firewalld
         開啓防火牆 systemctl start firewalld  
         關閉防火牆 systemctl stop firewalld
         開啓防火牆 service firewalld start 
         若遇到無法開啓
         先用:systemctl unmask firewalld.service 
         然後:systemctl start firewalld.service
 


(3)對外開發端口
       查看想開的端口是否已開:firewall-cmd --query-port=6379/tcp
       添加指定需要開放的端口:firewall-cmd --add-port=123/tcp --permanent
       重載入添加的端口:firewall-cmd --reload
       查詢指定端口是否開啓成功:firewall-cmd --query-port=123/tcp

       移除指定端口:firewall-cmd --permanent --remove-port=123/tcp
 

第二種方式

             安裝iptables-services : yum install iptables-services

             進入下面目錄進行修改: /etc/sysconfig/iptables

          

 linux系統的端口設置在/etc/sysconfig/iptables文件中配置。使用編輯器打開該文件。內容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

# 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 -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

網上說的是如下code

?

1

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3001 -j ACCEPT

我在CentOS6.5中測試上面的代碼,不能成功。

如果我們需要對外開放80端口,則上面文件中添加如下code

1

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

同時還需要注意的是,這段代碼需要加入到

1

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

之後,否則端口也不能打開。最後的配如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# 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 8080 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

編輯上面的文件 需要提供su權限.

保存上面的文件後,在終端運行如下命令:更新防火牆配置

1

service iptables restart

下面這個命令可以看到開放的端口

1

/sbin/iptables -L -n

 

下面的命令可以關閉/打開防火牆(需要重啓系統)

 

1

2

開啓: chkconfig iptables on

關閉: chkconfig iptables off

下面的代碼可以啓動和停止防火牆(立即生效,重啓後失效)

 

1

2

開啓: service iptables start 

關閉: service iptables stop

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章