1) 查看ssh服務是否安裝齊全
這里使用”rpm –qa|grep ssh”命令查看。
[root@xuexi ~]# rpm -qa | grep ssh libssh2-1.4.3-10.el7_2.1.x86_64 openssh-7.4p1-16.el7.x86_64 openssh-server-7.4p1-16.el7.x86_64 openssh-clients-7.4p1-16.el7.x86_64
如果不全請使用yum命令安裝,版本無需在意,以本地機器為主。
2)修改ssh的配置文件
在root用戶下使用”vim /etc/ssh/sshd_config”命令(注意這里是sshd_config),找到#Port 22(初始情況)改為Port 22,並添加一行Port 12345。保存並退出。
接着使用”systemctl restart sshd”命令,重啟服務。
3)在防火牆中添加端口
同樣在root用戶下使用命令”firewall-cmd --permanent --add-port=12345/tcp”將端口添加到防火牆中。
接着使用命令”firewall-cmd --reload”重啟防火牆。重啟好后使用命令”firewall-cmd --list-port”查看端口情況。(如下)
[xf@xuexi ~]$ firewall-cmd --list-ports 12345/tcp 22/tcp
4)SELinux內部操作
最簡單的方法是關閉SELinux。關閉SELinux方法請看https://www.cnblogs.com/diantong/p/9604602.html
為安全着想而不想關閉SELinux的用戶,可以先嘗試”semanage -h”命令,如果有顯示幫助信息可以跳過這一步,如果不是幫助信息,請安裝SELinux管理工具semanage。先安裝依賴包”yum install policycoreutils-python”,再安裝管理工具”yum provides semanage”。
安裝好后使用”semanage port –l|grep ssh”查看ssh當前端口。
[root@xuexi ~]# semanage port -l | grep ssh ssh_port_t tcp 22
如果沒有我們指定的端口請使用命令”semanage port –a –t ssh_port_t –p tcp 12345”。
[root@xuexi ~]# semanage port -a -t ssh_port_t -p tcp 12345 [root@xuexi ~]# semanage port -l | grep ssh ssh_port_t tcp 12345, 22
最后重啟系統。
注意:在確保sshd_config文件端口、firewall-cmd端口和SELinux端口正確一致的情況下,shhd處於auto-activing(自動激活)但無法打開,那么請關閉SELinux再嘗試,很大可能是SELinux配置亂了,我暫時沒找到好的解決辦法。
5)嘗試使用新端口連接
[root@CentOS6 桌面]# ssh root@192.168.128.240 -p 12345 The authenticity of host '[192.168.128.240]:12345 ([192.168.128.240]:12345)' can't be established. RSA key fingerprint is 23:83:de:25:21:72:77:d1:9d:ae:59:4b:d7:a4:e3:17. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[192.168.128.240]:12345' (RSA) to the list of known hosts. reverse mapping checking getaddrinfo for xuexi [192.168.128.240] failed - POSSIBLE BREAK-IN ATTEMPT! root@192.168.128.240's password: Last failed login: Tue Oct 30 15:24:03 CST 2018 from centos6 on ssh:notty There was 1 failed login attempt since the last successful login. Last login: Tue Oct 30 15:06:48 2018 [root@xuexi ~]#
6)如果希望做到修改效果的,將不想要的端口在sshd_config文件、firewall-cmd命令和semanage命令中去除,再嘗試連接即可。