无法使用笔记本电脑上的公钥本地连接到 SSH 服务器以获取 ssh-copy-id

无法使用笔记本电脑上的公钥本地连接到 SSH 服务器以获取 ssh-copy-id

我无论如何也想不出我错在哪里,需要别人来指出我的错误。我在本地的 arch 桌面上运行 sshd 服务,使用 ufw 打开指定的 ssh 端口。

当我启用密码验证时,密码验证可以工作,但是即使这样,我也无法使用 传输我的公钥ssh-copy-id。当我将 切换sshd_config为仅公钥时,我被拒绝连接。

调试ssh [insert additional commands] -vvv数据只是确认机器还没有我的公钥,但是当我在本地网络运行时,它只是说还有 1 个密钥需要安装,并且如果提示我安装密钥,但它会拒绝连接。ssh-copy-id -p 31221 -i ~/.ssh/ed25519key.pub [email protected]

我的 ufw 规则,注意特定的 IP 地址是我试图消除的最后一件事,它并不是一直都在那里。

[john@thedream ssh]$ sudo ufw status
[sudo] password for john:
Status: active

To                         Action      From
--                         ------      ----
WWW Full                   ALLOW       Anywhere
31221                      ALLOW       192.168.1.0/24
31221                      ALLOW       192.168.1.251
WWW Full (v6)              ALLOW       Anywhere (v6)

我的 sshd_config

#   $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $

# 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/bin

# 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.

#Port 22
Port 31221
#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
#StrictModes yes
#MaxAuthTries 50
#MaxSessions 10

PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile  .ssh/authorized_keys

#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
AuthenticationMethods publickey
#PermitEmptyPasswords no

# Change to no to disable s/key passwords
KbdInteractiveAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication yes
#GSSAPICleanupCredentials 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

# override default of no subsystems
Subsystem   sftp    /usr/lib/ssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#   X11Forwarding no
#   AllowTcpForwarding no
#   PermitTTY no
#   ForceCommand cvs server

我的 sshd 服务正在运行

[john@thedream ssh]$ sudo systemctl status sshd
● sshd.service - OpenSSH Daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: disabled)
     Active: active (running) since Tue 2023-02-21 18:02:28 CST; 2s ago
   Main PID: 9451 (sshd)
      Tasks: 1 (limit: 38319)
     Memory: 1.1M
        CPU: 4ms
     CGroup: /system.slice/sshd.service
             └─9451 "sshd: /usr/bin/sshd -D [listener] 0 of 10-100 startups"

Feb 21 18:02:28 thedream systemd[1]: Started OpenSSH Daemon.
Feb 21 18:02:28 thedream sshd[9451]: Server listening on 0.0.0.0 port 31221.
Feb 21 18:02:28 thedream sshd[9451]: Server listening on :: port 31221.

我的端口 31221 是开放的

[john@thedream ssh]$ sudo lsof -i:31221
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    9451 root    3u  IPv4  88455      0t0  TCP *:31221 (LISTEN)
sshd    9451 root    4u  IPv6  88457      0t0  TCP *:31221 (LISTEN)

答案1

因此,事实证明,就我所见,您只能使用PasswordAuthentication设置为 的ssh-copy-id。yes

引用自ssh-copy-id手册页: (presumably using a login password, so password authentication should be enabled, unless you've done some clever use of multiple identities)

需要注意的是,如果您有一个多身份设置,并且其中一个已经在服务器上保存了公钥,则不确定如何诚实地进行设置或是否需要其他步骤,但我相信使用您的客户端 ssh 配置会很容易,就像您对其他服务一样。

我之前肯定弄错了什么,因为我之前也启用了密码验证,而我却没有发现它。

解决方法是:

  1. 在主机上使用 sshd 设置一个基本但安全的 SSH 服务器
  2. 然后在客户端生成一对密钥并将其用于ssh-copy-id将您的公钥推送到服务器。
  3. 然后在服务器上的 sshd_config 中禁用密码验证。
  4. 最后在同一个配置中启用公钥认证选项。

回归基础之后似乎工作正常。

相关内容