Centos7與之前的版本最大的不同,在於Centos6和之前的版本使用的iptables,而Centos7版本以及未來以后的版本則默認使用 FirewallD。
鑒於Centos7的趨勢化,收集並學習如何在Centos7下更改SSH默認22端口。
FirewallD 簡介
FirewallD 是 iptables 的前端控制器,用於實現持久的網絡流量規則。它提供命令行和圖形界面,在大多數 Linux 發行版的倉庫中都有。與直接控制 iptables 相比,使用 FirewallD 有兩個主要區別:
- FirewallD 使用區域和服務而不是鏈式規則。
- 它動態管理規則集,允許更新規則而不破壞現有會話和連接。
更多了解可以去Linux中國網站查看:https://linux.cn/
修改shhd_config
vi etc/ssh/sshd_config
在增加Port端口5654保存之后
Port 22 增加 5654端口 Port 5654 systemctl restart sshd
增加SElinux端口
在Centos7系統更改shhd_config的過程中,你會看到這段注釋:
# If you want to change the port on a SELinux system, you have to tell # SELinux about this change. # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
所以,下一步就是告訴SElinux這步操作,我們需要用到semanage
首先,我們安裝下semanage
yum provides semanage
yum -y install policycoreutils-python
添加新端口5654
semanage port -a -t ssh_port_t -p tcp 5654
檢測是否成功
semanage port -l | grep ssh
當返回值出現5654和22即為成功。
配置防火牆FirewallD
首先檢測防火牆是否已經啟用,啟用返回值runing,反之,為not running
firewall-cmd --state
若沒有啟用,需要啟用
systemctl start firewalld
systemctl enable firewalld
若已經啟用,則進行下一步
查看防火牆的默認、活躍區域(zones)
firewall-cmd --get-default-zone firewall-cmd --get-active-zones
看兩條命令的返回值是否含有public,有則為正確。
端口永久開放
為了防止出錯,22端口一同開放
與臨時開放的區別在於多了permanent
firewall-cmd --permanent --zone=public --add-port=22/tcp firewall-cmd --permanent --zone=public --add-port=5654/tcp
防火牆重載
firewall-cmd --reload
查看已暴露端口
firewall-cmd --permanent --list-port firewall-cmd --zone=public --list-all
重啟SSH
systemctl restart sshd.service
之后用Putty、Xshell之類的軟件換成1024端口登錄,看能否成功登錄。
禁用22端口
首先,刪除ssh運行端口
vim etc/ssh/sshd_config
在Port 22前加#成為#Port 22后保存退出即可
在把防火牆中的22端口移除
firewall-cmd --permanent --zone=public --remove-port=22/tcp
重啟並查看是否移除
firewall-cmd --reload firewall-cmd --permanent --list-port