我试图通过禁用密码访问并仅允许通过私钥进行 SSH 连接来使我的 pi 更安全。我不知道为什么,但每当我尝试在没有密钥的情况下访问我的圆周率(例如ssh ubuntu@myip
)时,它都会起作用。
我希望能够通过 SSH 连接到我的 pi 的唯一方法是使用私钥,如下命令:
ssh ubuntu@ip -i ./.ssh/the_key
为什么不使用私钥仍然可以访问?我已经尝试了明显的步骤,例如添加PasswordAuthentication no
、ChallengeResponseAuthentication no
、PubkeyAuthentication yes
等。另外,我确实在我的密钥中添加了一个关键短语,尽管它在通过密钥进行 ssh 时只询问了我一次,但不再要求它,我不是确定为什么会发生。
我的 /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
#AllowUsers root otheruser
#PermitRootLogin without-password
PubkeyAuthentication 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
谢谢
答案1
如果您使用类似的东西ssh-copy-id
,则不需要指定密钥。这一切都为你处理好了。例子:
[root@paw .ssh]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'stig.example.com (<no hostip for proxy command>)' can't be established.
ECDSA key fingerprint is SHA256:of39GMe8Hq+x8LylBcVvaKwaQYUnyS5+2Oi+jY8IU1Y.
ECDSA key fingerprint is MD5:a9:48:f6:0d:eb:1e:e2:11:55:dc:16:6e:c6:8f:5e:b8.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@paw .ssh]# ssh [email protected]
Last login: Sat Aug 28 07:26:05 2021 from 172.16.0.8
[root@stig ~]#
如果您想验证仅使用您的密钥,请检查authorized_keys
远程主机上的 。
如果您认为存在某种配置错误并且您根本没有使用密码或密钥,您可以使用 SSH 的PubkeyAuthentication=no
选项进行验证:
[root@paw .ssh]# ssh -o PubkeyAuthentication=no stig.example.com
[email protected]'s password:
Last login: Sat Aug 28 07:27:18 2021 from 172.16.0.8
[root@stig ~]#
否则,您可以ssh -v user@host
直接使用并查找后面的行Authentications that can continue
:
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug1: Authentication succeeded (publickey).