Ubuntu_SSH远程登录安全加固
1、编辑ssh服务配置文件
vim /etc/ssh/sshd_config
2、加固ssh服务参数,sshd_config配置文件如下:
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
Include /etc/ssh/sshd_config.d/*.conf
# When systemd socket activation is used (the default), the socket
# configuration must be re-generated after changing Port, AddressFamily, or
# ListenAddress.
#
# For changes to take effect, run:
#
# systemctl daemon-reload
# systemctl restart ssh.socket
#
Port 44330 #修改ssh连接端口为44330,默认22号端口容易被黑客工具扫描
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin no #no表示禁用root账号ssh登录
#StrictModes yes
MaxAuthTries 3 #密码错误3次自动断开连接
AllowUsers zhangsan #白名单用户,只允许zhangsan用户ssh远程登录,这条需要自己手动添加
#MaxSessions 10
PubkeyAuthentication yes #允许public公钥认证登录
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
AuthorizedKeysFile .ssh/authorized_keys #ssh公钥文件路径
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no #no禁用密码ssh远程登录,只开放私钥远程登录
#PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
KbdInteractiveAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the KbdInteractiveAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin prohibit-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
# override default of no subsystems
Subsystem sftp /usr/lib/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
3、重启ssh服务
#####重启ssh服务
sudo sshd -t #使用sshd -t命令检查一下sshd_config配置文件是否有错误
sudo systemctl restart ssh.service
sudo systemctl restart ssh.socket
#####查询ssh服务状态sudo systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/usr/lib/systemd/system/ssh.service; disabled; preset: enabled)
Active: active (running) since Sun 2026-09-13 08:31:58 UTC; 4h 2min ago
TriggeredBy: ● ssh.socket
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 11778 (sshd)
Tasks: 1 (limit: 4543)
Memory: 1.2M (peak: 4.2M)
CPU: 478ms
CGroup: /system.slice/ssh.service
└─11778 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
#####查询ssh服务端口
sudo ss -anltp | grep sshd
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* users:(("sshd",pid=5204,fd=8))
LISTEN 0 128 0.0.0.0:44330 0.0.0.0:* users:(("sshd",pid=11778,fd=3))
LISTEN 0 128 [::1]:6010 [::]:* users:(("sshd",pid=5204,fd=7))
LISTEN 0 128 [::]:44330 [::]:* users:(("sshd",pid=11778,fd=4))
#或以lsof命令查询
sudo lsof -i :44330
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 11778 root 3u IPv4 64638 0t0 TCP *:44330 (LISTEN)
sshd 11778 root 4u IPv6 64640 0t0 TCP *:44330 (LISTEN)
#####如果重启后ssh服务后端口还没有变化,需要修改ssh.socket文件中的端口,保存后再重启ssh服务
sudo systemctl edit ssh.socket
4、创建用户
sudo useradd zhangsan #创建用户zhangsan
sudo echo 123456 | passwd --stdin zhangsan #为用户zhangsan设置密码123456,或使用passwd zhangsan交互式命令设置密码
5、ssh远程连接测试,可以看到现在只能以私钥认证登录服务器

如果私钥文件存在你主机上,你也可以 -i 指定私钥登录
ssh -i C:\Users\administrator\.ssh\id_ecdsa zhangsan@192.168.1.190 -p 44330
6、配置ssh客户端超时自动登出时间
sudo vim /etc/ssh/sshd_config.d/10-idle-timeout.conf #在/etc/ssh/ssh_config.d下创建一个新的自动超时配置文件,因在sshd_config文件中已经引用了此文件夹下的所有配置文件Include /etc/ssh/sshd_config.d/*.conf
10-idle-timeoute.conf 配置文件内容如下
ChannelTimeout session=10m #10 分钟无任何 shell 输入输出,断开该 session 通道
UnusedConnectionTimeout 1m #所有通道关闭后 1 分钟杀掉空连接(清理用)
SetEnv TMOUT=600 #给登录 shell 下发 600 秒无输入自动 exit(bash/zsh 认 TMOUT)
检查sshd配置参数(语法检查,避免参数错误导致无法ssh远程登录)
sudo sshd -t #无任何输出时,表示ssh配置文件通过
sudo sshd -T | grep -i unused
sudo sshd -T | grep ChannelTimeout
重启ssh服务
sudo systemctl restart ssh.service
sudo systemctl restart ssh.socket
客户端重新连接ssh;查看ssh登出超时时间
echo $TMOUT
600 #输出显示600,表示客户端无操作10分钟后自动登出

浙公网安备 33010602011771号