Linux SSH RSA远程登录配置:/etc/ssh/sshd_config 文件详解
创建新用户
useradd ccpit # 创建用户ccpit
passwd ccpit # 为该用户设置密码
编辑 /etc/ssh/sshd_config 配置文件
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# 全局SSH服务配置文件。更多信息请参考sshd_config(5)手册页。
# 编译时使用的PATH路径
# 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.
# 如果你在SELinux环境中更改端口,需通知SELinux此变更。
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#ListenAddress 0.0.0.0
#ListenAddress ::
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
# 加密与密钥交换
#RekeyLimit default none
# 日志级别
#LogLevel INFO
# 认证相关:
#LoginGraceTime 2m
StrictModes no
#MaxAuthTries 6
#MaxSessions 10
RSAAuthentication yes
PubkeyAuthentication yes
# 默认会检查 .ssh/authorized_keys 和 .ssh/authorized_keys2
# 此处覆盖默认行为,仅检查 .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# 需要主机密钥位于 /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# 若不信任 ~/.ssh/known_hosts 文件,则设为yes
#IgnoreUserKnownHosts no
# 禁止读取 ~/.rhosts 和 ~/.shosts 文件
#IgnoreRhosts yes
# 禁用明文隧道密码,改为no
#PermitEmptyPasswords no
# 关闭s/key密码验证
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos 相关配置
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes
# GSSAPI 相关配置
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
# 启用PAM认证、账户及会话处理
# 若启用,PAM认证将通过ChallengeResponseAuthentication进行
# 若只想执行PAM账户和会话检查,可关闭ChallengeResponseAuthentication
# WARNING: 'UsePAM no' 不被Red Hat Enterprise Linux支持,可能导致问题
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# 默认无banner路径
#Banner none
# 接受本地化环境变量
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
# 子系统默认设置
Subsystem sftp /usr/libexec/openssh/sftp-server
# 用户特定配置示例
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
UseDNS no
AddressFamily inet
PermitRootLogin no
SyslogFacility AUTHPRIV
PasswordAuthentication no
AllowUsers ccpit
在 /home/ccpit 目录下生成SSH密钥对
将私钥内容追加到 authorized_keys 文件中(如不存在则需手动创建)
cat id_rsa >> authorized_keys
重启SSH服务
systemctl restart sshd.service
完成上述步骤后,退出并重新登录即可使用RSA密钥登录
常用参数说明
UseDNS no
AddressFamily inet
PermitRootLogin no
# SyslogFacility 定义日志记录的设施代码
SyslogFacility AUTHPRIV
# PasswordAuthentication 控制是否允许密码登录
PasswordAuthentication no
# AllowUsers 指定允许登录的用户列表
AllowUsers root
# X11Forwarding 是否启用X11转发功能
X11Forwarding yes
# UsePAM 启用PAM认证机制
UsePAM yes
# GSSAPIAuthentication 是否允许GSSAPI认证
GSSAPIAuthentication yes
# GSSAPICleanupCredentials 用户登出时是否清理凭证缓存
GSSAPICleanupCredentials no
# ChallengeResponseAuthentication 是否允许挑战-响应认证方式
ChallengeResponseAuthentication no
# RSAAuthentication 是否启用RSA密钥认证
RSAAuthentication yes
# PubkeyAuthentication 是否启用公钥认证
PubkeyAuthentication yes
# AuthorizedKeysFile 公钥认证文件位置
AuthorizedKeysFile .ssh/authorized_keys
# StrictModes 控制是否在登录前检查用户主目录权限
StrictModes no
# MaxAuthTries 最大认证失败尝试次数
# MaxAuthTries 6