如何使用 RSA 密钥从 Linux 连接到 Windows 10

如何使用 RSA 密钥从 Linux 连接到 Windows 10

我需要从 Ubuntu 服务器通过 SSH 连接到 Windows 10(OpenSSH)。当我使用用户名和密码连接时,一切都很顺利,但如果我引入 RSA 密钥,就会遇到麻烦

  1. 在 Linux 上,我运行ssh-keygen并生成了没有密码的“id_rsa”和“id_rsa.pub”(位置 ~/.ssh)。

  2. 在 Windows 上,我将 id_rsa.pub 的内容复制/粘贴到 C:\Users\foobar\.ssh\authorized_keys

  3. 在 Linux 上,我尝试使用以下命令进行连接

ssh -i /home/foobar/.ssh/id_rsa 192.168.100.101

但是它只是要求输入用户“foobar”的密码,如果我输入密码,我就可以毫无问题地进入,但它不使用密钥。

我遗漏了什么?我猜测是 Windows 端出了问题,但是什么问题呢?

以下是详细输出:

autotunnel@ercserver:~$ ssh -v -i /home/autotunnel/.ssh/id_rsa 192.168.100.101
OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.100.101 [192.168.100.101] port 22.
debug1: Connection established.
debug1: identity file /home/autotunnel/.ssh/id_rsa type 0
debug1: identity file /home/autotunnel/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_for_Windows_7.7
debug1: match: OpenSSH_for_Windows_7.7 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.100.101:22 as 'autotunnel'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:uk5rA8yTEzwrtTOQqC04u5SKnv56BbA1pBSiafkV3Ro
debug1: Host '192.168.100.101' is known and matches the ECDSA host key.
debug1: Found key in /home/autotunnel/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/autotunnel/.ssh/id_rsa RSA SHA256:7RJoq/HURzcbte/G+GI0/CY3ed6B44mDt+GevScznFI explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: /home/autotunnel/.ssh/id_rsa RSA SHA256:7RJoq/HURzcbte/G+GI0/CY3ed6B44mDt+GevScznFI explicit
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: keyboard-interactive
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: password
[email protected]'s password:

这是 sshd_config

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# 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
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_dsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ecdsa_key
#HostKey __PROGRAMDATA__/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

# 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

# For this to work you will also need host keys in %programData%/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 yes
#PermitEmptyPasswords no

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#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

# override default of no subsystems
Subsystem   sftp    sftp-server.exe

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

Match Group administrators
       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

答案1

谢谢大家的帮助,最终让我找到了解决方案。

这一切都与authorized_keysWindows 中的文件权限有关。@dave_thompson_085 提供的链接中推荐了解决方案,但它不再起作用,因为所需的“OpenSSHUtils”已弃用,无法正式下载。所以基本上我做了同样的事情,但手动进行。

底线:

  1. 在 Windows 中登录到您要 ssh 连接的用户
  2. 删除现有.ssh文件夹(从头开始)
  3. 创建.ssh文件夹并authorized_keys在其中创建文件(其中复制/粘贴了 RSA 密钥)
  4. 删除文件夹和文件的权限继承
  5. 删除所有用户(包括管理员),只保留当前用户和系统用户(我尝试过不使用系统用户,但显然这很重要)。

我没有在 sshd 配置文件中更改任何内容。

希望它可以帮助某人节省时间。

相关内容