如何在没有密码的情况下在 Alpine Linux 中通过 ssh 登录?

How to login by ssh in Alpine Linux without passwords?

我正在使用 Android 电池 phone。我在 Termux 中编写 python 程序和 运行 程序。但是在Android cell phone、sklearn 和tensorflow 中有些lib 无法安装或使用。所以我决定尝试在我的编程环境中使用 docker 容器。 我创建了一个 docker 图像并将其更新为 docker-hub: zwdnet/mypython。然后我在Termux中安装了qemu,在Termux中安装了Alpine Linux (alpine-virt-3.10.1-x86_64)。我按照这个页面的方法(这是中文博客,不是英文):https://stageguard.top/2019/08/15/run-docker-on-qemu-alpine/#1-Docker 在此之后,我使用命令

qemu-system-x86_64 -hda python.img -boot c -m 4096 -netdev user,id=nde1,hostfwd=tcp::2222-:22 -device e1000,netdev=nde1,id=d-net1 -nographic &

在后面开始高山linux并且

ssh root@localhost -p 2222

登录。 (然后我安装了 docker 和 运行 容器成功,这是另一个问题。) 现在我想在不输入密码的情况下登录 Apline linux。 首先我用

ssh-keygen -t rsa

生成密钥,并使用

ssh-copy-id -i ./mykey.pub root@localhost -P 2222

将公钥发送到 Apline linux 并更改 /etc/ssh/sshd_config 文件。 最后我重新启动了 sshd 服务并注销。 但是当我再次登录时,它要求我再次输入密码。 我想也许那是因为我使用 root 登录。所以我登录并创建一个新用户名,然后重复前面的操作符。但是我失败了。 ssh 要求我再次输入密码。 这是我的 mod ~/.ssh 和密钥文件。

$ssh zym@localhost -p 2222 "ls -la"
zym@localhost's password:
total 20
drwxr-sr-x    3 1000     zym           4096 May 16 15:04 .
drwxr-xr-x    3 root     root          4096 May 16 14:44 ..
-rw-------    1 1000     zym            251 May 16 15:04 .ash_history
drwx------    2 1000     zym           4096 May 16 14:56 .ssh
-rw-------    1 root     zym            883 May 16 15:04 .viminfo
$ ssh zym@localhost -p 2222 "ls -la .ssh"
zym@localhost's password:
total 12
drwx------    2 1000     zym           4096 May 16 14:56 .
drwxr-sr-x    3 1000     zym           4096 May 16 15:04 ..
-rw-------    1 root     zym            571 May 16 14:56 authorized_keys

这是我的 /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=/bin:/usr/bin:/sbin:/usr/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.

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

#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 yes
#PermitEmptyPasswords no

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes

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

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

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

#AllowAgentForwarding yes
# Feel free to re-enable these if your use case requires them.
AllowTcpForwarding no
GatewayPorts no
X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#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

登录信息为

$ ssh zym@localhost -p 2222 -v
OpenSSH_8.2p1, OpenSSL 1.1.1g  21 Apr 2020
debug1: Reading configuration data /data/data/com.termux/files/usr/etc/ssh/ssh_config
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to localhost [127.0.0.1] port 2222.
debug1: Connection established.
debug1: identity file /data/data/com.termux/files/home/.ssh/id_rsa type 0
debug1: identity file /data/data/com.termux/files/home/.ssh/id_rsa-cert type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_dsa type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_dsa-cert type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_ecdsa type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_ecdsa-cert type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_ecdsa_sk type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_ed25519 type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_ed25519-cert type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_ed25519_sk type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_xmss type -1
debug1: identity file /data/data/com.termux/files/home/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.1
debug1: match: OpenSSH_8.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to localhost:2222 as 'zym'
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: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:Vwx+lpRpyl04g5gaO73Es9C1IslCcD7f9CUfBFyi96M
debug1: Host '[localhost]:2222' is known and matches the ECDSA host key.
debug1: Found key in /data/data/com.termux/files/home/.ssh/known_hosts:3
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: /data/data/com.termux/files/home/.ssh/id_rsa RSA SHA256:l/1hMa3CC1MHkHsJT6R7CRZJzYEXU
debug1: Will attempt key: /data/data/com.termux/files/home/.ssh/id_dsa
debug1: Will attempt key: /data/data/com.termux/files/home/.ssh/id_ecdsa
debug1: Will attempt key: /data/data/com.termux/files/home/.ssh/id_ecdsa_sk
debug1: Will attempt key: /data/data/com.termux/files/home/.ssh/id_ed25519
debug1: Will attempt key: /data/data/com.termux/files/home/.ssh/id_ed25519_sk
debug1: Will attempt key: /data/data/com.termux/files/home/.ssh/id_xmss
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->
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: /data/data/com.termux/files/home/.ssh/id_rsa RSA SHA256:l/1hMa3CC1MHkHsJT6R7CRZJzU
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Trying private key: /data/data/com.termux/files/home/.ssh/id_dsa
debug1: Trying private key: /data/data/com.termux/files/home/.ssh/id_ecdsa
debug1: Trying private key: /data/data/com.termux/files/home/.ssh/id_ecdsa_sk
debug1: Trying private key: /data/data/com.termux/files/home/.ssh/id_ed25519
debug1: Trying private key: /data/data/com.termux/files/home/.ssh/id_ed25519_sk
debug1: Trying private key: /data/data/com.termux/files/home/.ssh/id_xmss
debug1: Next authentication method: keyboard-interactive
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: password
zym@localhost's password:

我将 mykey 和 mykey.pub 复制到 /data/data/com.termux/files/home/.ssh/ ,没用。 你能帮我看看如何解决这个问题吗?谢谢! 我是中国人,我的英语很差。文字错误请见谅

谢谢@MarcoLucidi,但是 ssh 中的 -i 不起作用。

$ ssh -i ./mykey -p 2222 zym@localhost -v
OpenSSH_8.2p1, OpenSSL 1.1.1g  21 Apr 2020
debug1: Reading configuration data /data/data/com.termux/files/usr/etc/ssh/ssh_config
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to localhost [127.0.0.1] port 2222.
debug1: Connection established.
debug1: identity file ./mykey type 0
debug1: identity file ./mykey-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.1
debug1: match: OpenSSH_8.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to localhost:2222 as 'zym'
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: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:Vwx+lpRpyl04g5gaO73Es9C1IslCcD7f9CUfBFyi96M
debug1: Host '[localhost]:2222' is known and matches the ECDSA host key.
debug1: Found key in /data/data/com.termux/files/home/.ssh/known_hosts:3
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: ./mykey RSA SHA256:4rVLy6uWZoHmCMqYYyaASSV2iaoIdIbvIDtA1EBNuW0 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->
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: ./mykey RSA SHA256:4rVLy6uWZoHmCMqYYyaASSV2iaoIdIbvIDtA1EBNuW0 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
zym@localhost's password:

您在评论中指出此用户 "zym" 的 UID 为 0。您还表明 zym 的主目录和 .ssh 目录归 UID 1000 所有:

drwxr-sr-x    3 1000     zym           4096 May 16 15:04 .
drwxr-xr-x    3 root     root          4096 May 16 14:44 ..
-rw-------    1 1000     zym            251 May 16 15:04 .ash_history
drwx------    2 1000     zym           4096 May 16 14:56 .ssh

OpenSSH 服务器enforces strict permissions on the authorized_keys file:

~/.ssh/authorized_keys Lists the public keys (DSA, ECDSA, Ed25519, RSA) that can be used for logging in as this user. The format of this file is described above. The content of the file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others.

If this file, the ~/.ssh directory, or the user's home directory are writable by other users, then the file could be modified or replaced by unauthorized users. In this case, sshd will not allow it to be used unless the StrictModes option has been set to “no”.

sshd 执行的实际权限检查很复杂。但基本上它会检查两件事:

  1. authorized_keys 文件必须为登录用户所有,并且不能是组或全局可写的。
  2. 目录包含 authorized_keys.ssh 目录,主目录必须由 root 或用户登录,并且不能是组或世界可写的。

OpenSSH 服务器可能忽略了您的 authorized_keys 文件,因为未满足这些要求。您正在尝试以 UID 为 0 的用户身份登录,而 authorized_keys 文件、.ssh 目录和主目录属于不同的 UID。

您可以通过使 UID 匹配来解决此问题。要么将 "zym" 设置为 UID 1000,要么将 zym 的主目录和其中包含的文件更改为 zym 的实际 UID 0 所有。

或者,您可以通过在服务器 sshd_config 中将 StrictModes 设置为 "no" 并重新启动 sshd 来禁用此权限检查。

我发现 alpine linux 图像附带了一个尚未设置密码的根用户。由于某种原因,似乎没有为 root 设置密码会阻止 public 密钥身份验证在尝试在 root 帐户上进行 ssh 时成功。

尝试为 root 用户设置密码:例如:

echo 'root:dummy_passwd'|chpasswd

然后重试:

shh root@<ip_address>

现在该命令应该可以使用 public 密钥验证。不要问我为什么。

注意:我发现因为我可以连接到普通用户会话,但不能连接到具有相同 sshd_config、在同一台机器上和具有相同 authorized_keys 文件的根会话(与正确的权限和正确的所有权)。这是我唯一能想到的,它让一切变得不同。

在尝试使用 passwd -l 禁用我的 root 用户密码后,或者通过在 /etc/shadow 中直接将密码散列设置为 ! 后,我惊讶地发现我尝试登录通过 ssh 密钥被拒绝并在 /var/log/messages.

中报告为“不允许用户 root,因为帐户被锁定”

根据 https://unix.stackexchange.com/a/193131/353183 的指导,我将 /etc/shadow 中的密码哈希从 ! 更改为 *,这让我成功登录。

以感叹号开头的散列表示锁定的用户,OpenSSH 拒绝锁定用户的登录,即使他们使用授权密钥也是如此。星号仍然通过充当无法匹配的哈希来禁用密码登录,但它不会将帐户标记为已锁定。有关详细信息,请参阅 。