使用
在Docker中运行Alpine Linuxdocker run -it alpine
在容器中执行以下命令:安装openssh,生成密钥,授权密钥,尝试连接:
apk add --no-cache --update openssh
/usr/bin/ssh-keygen -A ## For Alpine only, not for Ubuntu
ssh-keygen -o -t rsa -b 4096 -f ~/.ssh/id_rsa -P ""
cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys
/usr/sbin/sshd
ssh-keyscan -t rsa localhost >>~/.ssh/known_hosts; chmod 600 ~/.ssh/known_hosts
ssh -vv -oPasswordAuthentication=no localhost echo test
输出:
... debug1: Authentications that can continue: publickey,password,keyboard-interactive debug1: Trying private key: /root/.ssh/id_dsa debug1: Trying private key: /root/.ssh/id_ecdsa debug1: Trying private key: /root/.ssh/id_ed25519 debug1: Trying private key: /root/.ssh/id_xmss debug2: we did not send a packet, disable method debug1: Next authentication method: keyboard-interactive debug2: userauth_kbdint debug2: we sent a keyboard-interactive packet, wait for reply debug1: Authentications that can continue: publickey,password,keyboard-interactive debug2: we did not send a packet, disable method debug1: No more authentication methods to try. root@localhost: Permission denied (publickey,password,keyboard-interactive).
与Ubuntu docker run -it ubuntu
相同,工作很好:
apt-get update -qyy && apt install -qyy --no-install-recommends
openssh-server openssh-client ca-certificates
ssh-keygen -o -t rsa -b 4096 -f ~/.ssh/id_rsa -P ""
cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys
mkdir -p /run/sshd ## Ubuntu only, fix a bug
/usr/sbin/sshd
ssh-keyscan -t rsa localhost >>~/.ssh/known_hosts; chmod 600 ~/.ssh/known_hosts
ssh -vv -oPasswordAuthentication=no localhost echo test
输出
test
阿尔卑斯山怎么了?或使用我的命令序列到Alpine?
更新:这是通过 Mac上的Docker 使用的。
Upd2:@KamilCuk在一行中进行排序:
docker run -ti alpine sh -x -c 'apk add --no-cache --update openssh; /usr/bin/ssh-keygen -A; ssh-keygen -o -t rsa -b 4096 -f ~/.ssh/id_rsa -P ""; cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys; /usr/sbin/sshd; ssh-keyscan -t rsa localhost >>~/.ssh/known_hosts; chmod 600 ~/.ssh/known_hosts; ssh -oPasswordAuthentication=no localhost echo test'