Docker SSH 连接到本地主机 运行 权限被拒绝(公钥、密码、键盘交互)
Docker SSH Connection To LocalHost In RUN Permission denied (publickey,password,keyboard-interactive)
我正在为单节点 hadoop 容器构建一个 docker 容器,我在为 hadoop 用户设置无密码 ssh 登录时遇到问题(我没有使用 root
到 运行 hadoop 服务)。我一直在互联网上搜索可能的修复方法,authorized_keys、/.ssh 等权限看起来都不错。以下是调试日志的相关部分:
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/hdadmin/.ssh/id_rsa
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
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.
Permission denied (publickey,password,keyboard-interactive).
尝试 ssh 之前的文件权限:
drwx------ 1 hdadmin hdadmin 4096 Feb 25 14:33 /home/hdadmin/.ssh
-rw------- 1 hdadmin hdadmin 402 Feb 25 04:54 authorized_keys
-rw------- 1 hdadmin hdadmin 87 Feb 25 04:54 config
-rw------- 1 hdadmin hdadmin 1671 Feb 25 04:54 id_rsa
-rw-r--r-- 1 hdadmin hdadmin 402 Feb 25 04:54 id_rsa.pub
-rw------- 1 hdadmin hdadmin 1637 Feb 25 04:54 known_hosts
以及来自 docker 文件的相关代码:
RUN ... \
&& mkdir -p /home/hdadmin \
&& mkdir -p /home/hdadmin/.ssh \
&& touch /home/hdadmin/.ssh/authorized_keys \
&& touch /home/hdadmin/.ssh/config \
&& touch /home/hdadmin/.ssh/known_hosts \
&& addgroup hadoop \
&& adduser -D -g hadoop -h /home/hdadmin hdadmin -s /etc/passwd \
&& chown -R hdadmin:hdadmin /home/hdadmin \
&& chmod -R 0700 /home/hdadmin \
&& ssh-keygen -A
&& HOST_KEY="$(cat /etc/ssh/ssh_host_rsa_key.pub)" \
&& echo "127.0.0.1 ${HOST_KEY}" >> /home/hdadmin/.ssh/known_hosts \
&& echo "localhost ${HOST_KEY}" >> /home/hdadmin/.ssh/known_hosts \
&& echo "$HOSTNAME ${HOST_KEY}" >> /home/hdadmin/.ssh/known_hosts \
&& echo "0.0.0.0 ${HOST_KEY}" >> /home/hdadmin/.ssh/known_hosts \
&& su-exec hdadmin ssh-keygen -q -N '' -t rsa -f /home/hdadmin/.ssh/id_rsa \
&& cat /home/hdadmin/.ssh/id_rsa.pub >> /home/hdadmin/.ssh/authorized_keys \
&& echo "Host *" >> /home/hdadmin/.ssh/config \
&& echo -e "\tUser hdadmin" >> /home/hdadmin/.ssh/config \
&& echo -e "\tPubKeyAuthentication yes" >> /home/hdadmin/.ssh/config \
&& echo -e "\tIdentityFile /home/hdadmin/.ssh/id_rsa" >> /home/hdadmin/.ssh/config \
&& eval $(ssh-agent) \
&& ssh-add /home/hdadmin/.ssh/id_rsa \
&& chmod 0600 /home/hdadmin/.ssh/* \
&& chmod 0644 /home/hdadmin/.ssh/*.pub \
&& /usr/sbin/sshd \
&& su-exec hdadmin ssh -vvv localhost
sshd_config文件都是默认的,没有被修改过。无法弄清楚为什么这不起作用,我认为我做的一切都是正确的。
编辑 1:向 docker 文件示例添加了更多内容。
所以我最终弄明白了。首先,我安装了一个系统日志功能,这样我就可以实际查看日志消息。这是 Alpine linux 上的 运行,因此我在我的 dockerfile 中执行了以下操作,以便收到消息:
COPY init.sh /tmp/init.sh # This sh file just contains /usr/sbin/rsyslogd -n -f /etc/rsyslog.conf & so that it runs in the background
RUN apk add --no-cache rsyslog \
&& echo "$ModLoad inmark.so" >> /etc/rsyslog.conf \
&& echo "$IncludeConfig /etc/rsyslog.d/*.conf" >> /etc/rsyslog.conf \
&& echo "auth,authpriv.* /var/log/auth.log" >> /etc/rsyslog.conf \
&& sed -i "s/#LogLevel.*/LogLevel DEBUG/g" /etc/ssh/sshd_config \
&& sed -i "s/#SyslogFacility.*/SyslogFacility AUTH/g" /etc/ssh/sshd_config \
我这样做后,在 /var/log/auth.log
文件中我发现:
2018-02-25T19:23:18.942070+00:00 c97682336915 sshd[29]: User hdadmin not allowed because account is locked
我没有为该帐户设置密码,因为它不会以交互方式使用。所以我在 dockerfile 中设置了一个:
usermod -p '*' hdadmin
然后我收到了这些消息:
2018-02-25T19:23:18.942070+00:00 c97682336915 sshd[29]: User hdadmin not allowed because shell /etc/passwd is not executable
所以我将 adduser 行更改为:
&& adduser -D -g hadoop -h /home/hdadmin -s /bin/bash hdadmin \
现在一切都很好。我相信这仍然会使 hdadmin 帐户处于无法使用密码或任何交互式登录的状态,但它仍然可以通过 public 密钥身份验证以编程方式使用。
我正在为单节点 hadoop 容器构建一个 docker 容器,我在为 hadoop 用户设置无密码 ssh 登录时遇到问题(我没有使用 root
到 运行 hadoop 服务)。我一直在互联网上搜索可能的修复方法,authorized_keys、/.ssh 等权限看起来都不错。以下是调试日志的相关部分:
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/hdadmin/.ssh/id_rsa
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
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.
Permission denied (publickey,password,keyboard-interactive).
尝试 ssh 之前的文件权限:
drwx------ 1 hdadmin hdadmin 4096 Feb 25 14:33 /home/hdadmin/.ssh
-rw------- 1 hdadmin hdadmin 402 Feb 25 04:54 authorized_keys
-rw------- 1 hdadmin hdadmin 87 Feb 25 04:54 config
-rw------- 1 hdadmin hdadmin 1671 Feb 25 04:54 id_rsa
-rw-r--r-- 1 hdadmin hdadmin 402 Feb 25 04:54 id_rsa.pub
-rw------- 1 hdadmin hdadmin 1637 Feb 25 04:54 known_hosts
以及来自 docker 文件的相关代码:
RUN ... \
&& mkdir -p /home/hdadmin \
&& mkdir -p /home/hdadmin/.ssh \
&& touch /home/hdadmin/.ssh/authorized_keys \
&& touch /home/hdadmin/.ssh/config \
&& touch /home/hdadmin/.ssh/known_hosts \
&& addgroup hadoop \
&& adduser -D -g hadoop -h /home/hdadmin hdadmin -s /etc/passwd \
&& chown -R hdadmin:hdadmin /home/hdadmin \
&& chmod -R 0700 /home/hdadmin \
&& ssh-keygen -A
&& HOST_KEY="$(cat /etc/ssh/ssh_host_rsa_key.pub)" \
&& echo "127.0.0.1 ${HOST_KEY}" >> /home/hdadmin/.ssh/known_hosts \
&& echo "localhost ${HOST_KEY}" >> /home/hdadmin/.ssh/known_hosts \
&& echo "$HOSTNAME ${HOST_KEY}" >> /home/hdadmin/.ssh/known_hosts \
&& echo "0.0.0.0 ${HOST_KEY}" >> /home/hdadmin/.ssh/known_hosts \
&& su-exec hdadmin ssh-keygen -q -N '' -t rsa -f /home/hdadmin/.ssh/id_rsa \
&& cat /home/hdadmin/.ssh/id_rsa.pub >> /home/hdadmin/.ssh/authorized_keys \
&& echo "Host *" >> /home/hdadmin/.ssh/config \
&& echo -e "\tUser hdadmin" >> /home/hdadmin/.ssh/config \
&& echo -e "\tPubKeyAuthentication yes" >> /home/hdadmin/.ssh/config \
&& echo -e "\tIdentityFile /home/hdadmin/.ssh/id_rsa" >> /home/hdadmin/.ssh/config \
&& eval $(ssh-agent) \
&& ssh-add /home/hdadmin/.ssh/id_rsa \
&& chmod 0600 /home/hdadmin/.ssh/* \
&& chmod 0644 /home/hdadmin/.ssh/*.pub \
&& /usr/sbin/sshd \
&& su-exec hdadmin ssh -vvv localhost
sshd_config文件都是默认的,没有被修改过。无法弄清楚为什么这不起作用,我认为我做的一切都是正确的。
编辑 1:向 docker 文件示例添加了更多内容。
所以我最终弄明白了。首先,我安装了一个系统日志功能,这样我就可以实际查看日志消息。这是 Alpine linux 上的 运行,因此我在我的 dockerfile 中执行了以下操作,以便收到消息:
COPY init.sh /tmp/init.sh # This sh file just contains /usr/sbin/rsyslogd -n -f /etc/rsyslog.conf & so that it runs in the background
RUN apk add --no-cache rsyslog \
&& echo "$ModLoad inmark.so" >> /etc/rsyslog.conf \
&& echo "$IncludeConfig /etc/rsyslog.d/*.conf" >> /etc/rsyslog.conf \
&& echo "auth,authpriv.* /var/log/auth.log" >> /etc/rsyslog.conf \
&& sed -i "s/#LogLevel.*/LogLevel DEBUG/g" /etc/ssh/sshd_config \
&& sed -i "s/#SyslogFacility.*/SyslogFacility AUTH/g" /etc/ssh/sshd_config \
我这样做后,在 /var/log/auth.log
文件中我发现:
2018-02-25T19:23:18.942070+00:00 c97682336915 sshd[29]: User hdadmin not allowed because account is locked
我没有为该帐户设置密码,因为它不会以交互方式使用。所以我在 dockerfile 中设置了一个:
usermod -p '*' hdadmin
然后我收到了这些消息:
2018-02-25T19:23:18.942070+00:00 c97682336915 sshd[29]: User hdadmin not allowed because shell /etc/passwd is not executable
所以我将 adduser 行更改为:
&& adduser -D -g hadoop -h /home/hdadmin -s /bin/bash hdadmin \
现在一切都很好。我相信这仍然会使 hdadmin 帐户处于无法使用密码或任何交互式登录的状态,但它仍然可以通过 public 密钥身份验证以编程方式使用。