我尝试使用公钥身份验证通过 SSH 访问我的服务器,但它不起作用,我不明白为什么。我已经遇到同样的问题两天了。
我使用的是 Ubuntu 20.04。我所做的就是更新操作系统,创建一个名为“webdev”且具有 sudo 权限的用户。然后我配置了/etc/ssh/sshd_config.d/00-server-init.conf
该文件中包含的文件/etc/ssh/sshd_config
。
/etc/ssh/sshd_config.d/00-server-init.conf:
Port 2405
Protocol 2
PermitRootLogin no
AllowUsers webdev
PubkeyAuthentication yes
PasswordAuthentication yes
/etc/ssh/sshd_config:
# $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin
# 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
#Port 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 prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
#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
#PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication 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 ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication 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 /var/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
我现在打开了两个终端窗口:一个在服务器内部(通过 ssh),另一个在本地计算机上。我在本地计算机(Macbook)上创建了一个 ssh-keygen,并将内容key.pub
(使用 vim)复制到另一个终端窗口中的一个~/.ssh/authorized_keys
文件中。该文件的权限现在是 777(我现在可以执行任何操作)。
但是当我在文件中PasswordAuthentication
设置时,我无法使用公钥身份验证进入。我得到了一个no
/etc/ssh/sshd_config.d/00-server-init.conf
@.ssh : ssh webdev@IP -p 2405
webdev@IP: Permission denied (publickey).
错误。尽管我有
我必须更换PubkeyAuthentication yes
。我做错了什么?
答案1
该文件的权限现在为 777(我现在可以执行任何操作)。
这是行不通的。
该文件(以及包含该文件的文件夹)的权限应该不是允许世界写入。这将允许任何用户添加自己的密钥以该用户身份登录。
如果您实际检查了日志,您可能会看到一条错误消息,抱怨该文件是全世界可写的。运行chmod 600 ~/.ssh/authorized_keys; chmod 700 ~/.ssh
。
这第一的当某些东西不起作用时,你应该做的事情是阅读日志。在这种情况下,这可能是/var/log/auth.log
和journalctl -u sshd
。