我們默認通過ssh登錄的端口是22,這個端口是默認的也就意味著這個端口對于所有人是透明的,如果你買臺服務器,沒有經過修改的話,那別人就等于已經猜到你的登錄端口是22。出于基本的安全考慮,我們是需要修改一下這個端口的。
1、修改配置文件: /etc/ssh/sshd_config
配置文件中的內容:
# Package generated configuration file
# See the sshd_config(5) manpage for details
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024
# Logging
LogLevel INFO
# Authentication:
LoginGraceTime 120
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Change to no to disable tunnelled clear text passwords
# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PAM authentication via ChallengeResponseAuthentication may bypass
# If you just want the PAM account and session checks to run without
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
UseDNS no
AddressFamily inet
PermitRootLogin yes
SyslogFacility AUTHPRIV
PasswordAuthentication yes
2、在改之前需要先新建一個連接窗口,保持登錄狀態,防止修改失敗導致ssh連不上。
3、端口的范圍是0-65536
由于0-1024最好不要使用,因為這些可能是被系統默認占用的端口,所以我們配置端口時為了避免和系統端口沖突,最好采用1024以后的端口。
端口號改成39999
UseDNS保證為no
底部加一行允許登錄的用戶
重啟ssh
測試:
嘗試用22連接被拒絕
使用39999端口登錄成功
如果登錄依然不成功的話,可能是阿里云的安全組配置規則需要修改:
具體可參考文檔:
https://yq.aliyun.com/articles/168203
4、修改配置文件,不允許root用戶登錄:
修改配置文件中PermitRootLogin為no