無法使用筆記型電腦上的 pubkey 本機連接到 SSH 伺服器以取得 ssh-copy-id

無法使用筆記型電腦上的 pubkey 本機連接到 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

所以事實證明,你只能使用 ssh-copy-id 並PasswordAuthentication設定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. 最後在同一組態中啟用公鑰身份驗證選項。

回到基礎後似乎運作得很好。

相關內容