說明:
CentOS7服務器環境,默認SSH端口為22,考慮到安全方面問題,欲修改端口為62231(機器內網IP為192.168.1.31)
ssh配置文件和iptables中端口開放配置調整時,原先的先不要移除,顯式的同時打開22和62231端口,待配置完成確認62231端口訪問正常后再逐個移除22端口的配置,防止配置過程中出現差錯導致服務器訪問不了。
一、SELinux配置修改
1.先檢查SELinux狀態,如果已關閉則無需相關修改
[root@localhost ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28
2.semanage命令安裝(如果已安裝,則無需此步驟)
# yum -y install semanage
# yum provides semanage
# yum -y install policycoreutils-python-2.5-22.el7.x86_64
3.SELinux中SSH端口開放查詢和配置
# semanage port -l|grep ssh
ssh_port_t tcp 22
# semanage port -a -t ssh_port_t -p tcp 62231
# semanage port -l|grep ssh
ssh_port_t tcp 62231, 22
二、SSH端口配置修改
# vi /etc/ssh/sshd_config
# systemctl restart sshd
三、防火牆配置修改
# firewall-cmd --permanent --add-port=62231/tcp
# firewall-cmd --reload
四、重啟服務器
[root@localhost ~]# reboot
五、驗證配置是否生效
# ssh -p62231 root@192.168.1.31
六、移除sshd_config和iptables中默認端口22配置並重啟sshd和iptables
七、相關參考
CentOS7增加或修改SSH端口號 https://blog.csdn.net/ausboyue/article/details/536
