服务器的环境:容器下的Ubantu22.04系统
需求:FunHPC平台(DeepLn)不保存系统镜像,只保存用户的数据镜像(存在/data路径下),再加上需要频繁释放和恢复实例,而且每次恢复都会在不同的主机上,所以配置好ssh密钥对能节省不少时间
流程:
- 将公钥文件
xxx.pub的内容添加到自定义的路径下的authorized_keys文件中(我放在/data/.ssh/authorized_keys下) - 使用
chmod 600 /data/.ssh/authorized_keys赋予权限 - 修改
/etc/ssh/sshd_config文件,确保AuthorizedKeysFile项生效,并添加自定义的authorized_keys路径
如:AuthorizedKeysFile .ssh/authorized_keys /data/.ssh/authorized_keys - 重启ssh服务:
sudo systemctl restart sshd
问题:执行systemctl restart sshd重启ssh,报错:-bash: systemctl: command not found
- 原因:可能是在一个 Docker 容器、LXC 容器或其他轻量级虚拟化环境中运行 Ubuntu 22.04,并且该容器被配置为使用 s6 作为其初始化系统。
验证:执行ps -p 1 -o comm=显示s6-svscan,执行cat /etc/os-release显示PRETTY_NAME="Ubuntu 22.04.4 LTS" - 解决思路:找出 SSH 服务是如何启动和被 s6 监控的,查找ssh服务的实际路径,然后用
s6-svc重启ssh服务 - 尝试:
- 执行
ps aux | grep [s]shd,查看输出:
root 7 0.0 0.0 248 116 ? S 20:48 0:00 s6-supervise 01sshd
root 9 0.0 0.0 15448 9408 ? Ss 20:48 0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root 144 0.0 0.0 16204 10336 ? Ss 20:50 0:00 sshd: root@pts/0 - 执行
find /etc /run /var -type d -name "01sshd" 2>/dev/null,尝试在常见的s6服务目录中查找 01sshd 的目录,查看输出:
/etc/services.d/01sshd - 执行
sudo s6-svc -r /path/to/01sshd重启ssh服务 - 再次执行
ps aux | grep [s]shd,查看输出(发现sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups进程号和启动时间已变):
root 7 0.0 0.0 248 116 ? S 20:48 0:00 s6-supervise 01sshd
root 144 0.0 0.0 16204 10336 ? Ss 20:50 0:00 sshd: root@pts/0
root 465 0.5 0.0 15448 8916 ? Ss 21:12 0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups - 重新通过ssh连接,不用手动输密码,成功
- 执行
