pa4*_*080 29 server ssh startup openvz 16.04
我的 VPS 大约 3 个月没有重新启动。它托管在 OpenVZ 虚拟化类型的服务器上,操作系统为 Ubuntu 16.04。出于某种原因,我重新启动了 VPS,之后,我无法通过 ssh 连接到服务器,我收到的消息是:
ssh: connect to host srvname.com port 22: Connection refused
Run Code Online (Sandbox Code Playgroud)
所以我在 VPS 上打开了一个串行控制台并开始调查......我已经清除并重新安装了但openssh-server没有成功。我花了两个小时阅读有关 Internet 上类似问题的文章、问题和答案。
最后我设法理解该目录/var/run/sshd不是在系统启动期间创建的。一旦我手动创建它,我就可以毫无问题地启动 SSH 服务,但是在下次重新启动时问题仍然存在。所以我的问题是:
这个问题的原因可能是什么?为什么/var/run/sshd在系统启动时没有创建?
我怎样才能以正确的方式解决问题?我找到了本文末尾提到的临时解决方案。
问题是否可能与 VPS 的 OpenVZ 主机有关?我应该要求托管服务提供商解决吗?
的输出systemctl status ssh.service,sshd -Ddp 22并且journalctl -xe是:
ssh: connect to host srvname.com port 22: Connection refused
Run Code Online (Sandbox Code Playgroud)
的内容/usr/lib/tmpfiles.d/sshd.conf和/etc/init/ssh.conf为:
# systemctl status ssh.service
? ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: failed (Result: start-limit-hit) since ?? 2019-01-15 12:58:08 EET; 22s ago
Process: 407 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=255)
??? 15 12:58:07 srvname systemd[1]: Failed to start OpenBSD Secure Shell server.
??? 15 12:58:07 srvname systemd[1]: ssh.service: Unit entered failed state.
??? 15 12:58:07 srvname systemd[1]: ssh.service: Failed with result 'exit-code'.
??? 15 12:58:08 srvname systemd[1]: ssh.service: Service hold-off time over, scheduling restart.
??? 15 12:58:08 srvname systemd[1]: Stopped OpenBSD Secure Shell server.
??? 15 12:58:08 srvname systemd[1]: ssh.service: Start request repeated too quickly.
??? 15 12:58:08 srvname systemd[1]: Failed to start OpenBSD Secure Shell server.
??? 15 12:58:08 srvname systemd[1]: ssh.service: Unit entered failed state.
??? 15 12:58:08 srvname systemd[1]: ssh.service: Failed with result 'start-limit-hit'.
# $(which sshd) -Ddp 22
debug1: sshd version OpenSSH_7.2, OpenSSL 1.0.2g 1 Mar 2016
debug1: private host key #0: ssh-rsa SHA256:...
debug1: private host key #1: ssh-dss SHA256:...
debug1: private host key #2: ecdsa-sha2-nistp256 SHA256:...
debug1: private host key #3: ssh-ed25519 SHA256:...
Missing privilege separation directory: /var/run/sshd
# journalctl -xe
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit ssh.service has begun starting up.
??? 15 13:21:21 srvname sshd[1688]: Missing privilege separation directory: /var/run/sshd
??? 15 13:21:21 srvname systemd[1]: ssh.service: Control process exited, code=exited status=255
??? 15 13:21:21 srvname systemd[1]: Failed to start OpenBSD Secure Shell server.
-- Subject: Unit ssh.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit ssh.service has failed.
--
-- The result is failed.
??? 15 13:21:21 srvname systemd[1]: ssh.service: Unit entered failed state.
??? 15 13:21:21 srvname systemd[1]: ssh.service: Failed with result 'exit-code'.
??? 15 13:21:22 srvname systemd[1]: ssh.service: Service hold-off time over, scheduling restart.
??? 15 13:21:22 srvname systemd[1]: Stopped OpenBSD Secure Shell server.
-- Subject: Unit ssh.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit ssh.service has finished shutting down.
??? 15 13:21:22 srvname systemd[1]: Starting OpenBSD Secure Shell server...
-- Subject: Unit ssh.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit ssh.service has begun starting up.
??? 15 13:21:22 srvname sshd[1691]: Missing privilege separation directory: /var/run/sshd
??? 15 13:21:22 srvname systemd[1]: ssh.service: Control process exited, code=exited status=255
??? 15 13:21:22 srvname systemd[1]: Failed to start OpenBSD Secure Shell server.
-- Subject: Unit ssh.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit ssh.service has failed.
--
-- The result is failed.
??? 15 13:21:22 srvname systemd[1]: ssh.service: Unit entered failed state.
??? 15 13:21:22 srvname systemd[1]: ssh.service: Failed with result 'exit-code'.
??? 15 13:21:22 srvname systemd[1]: ssh.service: Service hold-off time over, scheduling restart.
??? 15 13:21:22 srvname systemd[1]: Stopped OpenBSD Secure Shell server.
-- Subject: Unit ssh.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit ssh.service has finished shutting down.
??? 15 13:21:22 srvname systemd[1]: ssh.service: Start request repeated too quickly.
??? 15 13:21:22 srvname systemd[1]: Failed to start OpenBSD Secure Shell server.
-- Subject: Unit ssh.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit ssh.service has failed.
--
-- The result is failed.
??? 15 13:21:22 srvname systemd[1]: ssh.service: Unit entered failed state.
??? 15 13:21:22 srvname systemd[1]: ssh.service: Failed with result 'start-limit-hit'.
Run Code Online (Sandbox Code Playgroud)
有关系统的其他信息:
# cat /usr/lib/tmpfiles.d/sshd.conf
d /var/run/sshd 0755 root root
# cat /etc/init/ssh.conf | sed '/^#/ d'
description "OpenSSH server"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 10 5
umask 022
env SSH_SIGSTOP=1
expect stop
console none
pre-start script
test -x /usr/sbin/sshd || { stop; exit 0; }
test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
mkdir -p -m0755 /var/run/sshd
end script
exec /usr/sbin/sshd -D
Run Code Online (Sandbox Code Playgroud)
临时解决方案:
我发现这/var/run是一个指向 的符号链接/run,我不知道为什么需要这样做,但是当我/usr/lib/tmpfiles.d/sshd.conf从以下位置修改文件内容时:
# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.5 LTS
Release: 16.04
Codename: xenial
# uname -a
Linux srvname 2.6.32-042stab127.2 #1 SMP Thu Jan 4 16:41:44 MSK 2018 x86_64 x86_64 x86_64 GNU/Linux
# apt show openssh-server | grep 'Version'
Version: 1:7.2p2-4ubuntu2.6
Run Code Online (Sandbox Code Playgroud)
到:
d /var/run/sshd 0755 root root
Run Code Online (Sandbox Code Playgroud)
系统启动一切正常,SSH服务正常启动,我可以通过SSH登录。
pa4*_*080 35
我发现这是当前版本的 systemd 和一些 VPS privdes 使用的旧内核的错误,就像我的情况一样。这个错误不时出现,正如我们在 Launchpad 上看到的:Bug #45234,Bug #1811580;或在 ServerFault 上:为什么每次启动后我都缺少 /var/run/sshd?
这个问题的解决方法很少,它们都聚集在一起,/var/run/sshd在运行 SSH 服务器之前创建替代方法。以下是三种可能的解决方案。
解决方法一:修改/usr/lib/tmpfiles.d/sshd.conf如下:
d /run/sshd 0755 root root
Run Code Online (Sandbox Code Playgroud)
正如问题中提到的那样,/var/run是到 的符号链接/run,最终结果是相同的:/var/run/sshd被创建。我不知道为什么,但这有效。
解决方法 2:使用将创建/var/run/sshd并重新启动 SSH 服务器的Cron 作业,您可以crontab为此使用 root 用户- 执行sudo crontab -e并添加以下条目:
@reboot mkdir -p -m0755 /var/run/sshd && systemctl restart ssh.service
Run Code Online (Sandbox Code Playgroud)
目前我正在使用这个解决方案,所以它也经过了测试。
解决方法3:用/etc/rc.local做与上述相同,因为它是在此评论中所示的错误报告#45234。
小智 5
您能否检查您的/(根文件系统)权限是否未更改?必须root:root像下面的两行:
drwxr-xr-x 25 root root 4096 ??? 21 06:45 ..
drwxr-xr-x 25 root root 4096 ??? 21 06:45 .
Run Code Online (Sandbox Code Playgroud)
如果所有者是另一个用户(而不是 root),这将阻止 systemd 在系统启动期间创建所有临时文件。您也可以使用以下命令进行检查:
systemd-tmpfiles --create
Run Code Online (Sandbox Code Playgroud)
如果根文件夹 ( /) 具有不同的权限,请使用以下命令进行更改:
chown root: /
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42137 次 |
| 最近记录: |