由于最近sshd服務默認端口號22被匿名進行試探性的進行登入,為防止匿名用戶再次進行試探性的登入,將sshd服務的默認端口號進行修改。
環境:centos 7.3 xshell
思路:先將sshd的被指文件進行修改,把sshd服務的默認端口號修改為所需要的端口號,然后重啟sshd服務,發現重啟報錯。找其原因是應為selinux不允許自定義sshd服務設置端口放行,然后進行為sshd服務添加selinux放行端口。
實驗:
一、使用sshd服務默認端口22進行訪問,修改端口默認端口號,重啟sshd服務
a) 登入
Connecting to 192.168.204.26:22…
Connection established.
To escape to local shell, press ‘Ctrl+Alt+]’.
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Thu Apr 6
20:26:28 2017 from 192.168.204.1
[chen@192 ~]$
b) 切換root,更改sshd默認端口號
[chen@192 ~]$ su
Password:
[root@192 chen]# vim /etc/ssh/sshd_config
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
將#Port 22這行修改為Port 22222,如:
Port 22222
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
c)重啟sshd服務
[root@192
chen]# systemctl restart sshd
Job for sshd.service failed because the control process exited with
error code. See “systemctl status sshd.service” and “journalctl
-xe” for details.
二、排錯
a)查看防火墻配置
[root@192 conf]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt
source destination
沒有設置防火墻策略。
b)查看shelinux狀態,并關閉selinux,嘗試重啟sshd服務
[root@192
conf]# getenforce
Enforcing
[root@192
conf]# setenforce 0
[root@192
conf]# getenforce
Permissive
[root@192
conf]# systemctl restart sshd
重啟成功。
c) 使用自定義sshd端口號遠程連接linux主機
Connecting
to 192.168.204.26:22222…
Connection
established.
To escape
to local shell, press ‘Ctrl+Alt+]’.
WARNING!
The remote SSH server rejected X11 forwarding request.
Last
login: Thu Apr 6 21:50:15 2017 from
192.168.204.1
[chen@192 ~]$
登入成功,但不能把selinux關閉了,會造成linux主機安全性能下降。
三、修改selinux端口標簽
a)啟用selinux
[root@192 conf]# setenforce 1
查看selinux狀態
[root@192 conf]# getenforce
Enforcing
b) 查看selinux中sshd服務放行的端口號
[root@192
conf]# semanage port -l | grep ssh
ssh_port_t tcp 22
c)添加自定義sshd服務端口至selinux中
[root@192
conf]# semanage port -a -t ssh_port_t -p tcp 22222
[root@192
conf]# semanage port -l | grep ssh
ssh_port_t
tcp 22222, 22
d)測試
注意:中途不要斷開ssh服務,導致出現的一些問題。
原創文章,作者:chenbin,如若轉載,請注明出處:http://www.www58058.com/72697