Centos7.X下修改SSH默認22埠
Centos7.x下更改SSH的默認埠
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埠1024保存之後
systemctl restart sshd
如果看不太懂這裡,可以看我這篇文章:防止暴力破解,請更換SSH默認埠
增加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
添加新埠1024
semanage port -a -t ssh_port_t -p tcp 1024
檢測是否成功
semanage port -l | grep ssh
當返回值出現1024和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=1024/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運行埠
vi 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
若移除,則成功。
推薦閱讀: