当我尝试运行 apt-get 或安装 Ubuntu 升级时,无法启动 OpenBSD 安全 Shell 服务器错误

当我尝试运行 apt-get 或安装 Ubuntu 升级时,无法启动 OpenBSD 安全 Shell 服务器错误

我在使用 Ubuntu 16.04 LTS。

我已经运行了 ssh 服务器并且它正常运行了好几个月,直到有一次出现问题导致它失败。

现在,我无法通过 Canonical 进行常规 Ubuntu 更新。我无法运行apt-get更新。例如

$ sudo apt-get upgrade python3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3 is already the newest version (3.5.1-3).
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  containerd libllvm5.0 libpaps0 paps python3-rlp runc swarm
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Setting up openssh-server (1:7.2p2-4ubuntu2.6) ...
insserv: warning: script 'S02ssh-start.sh' missing LSB tags and overrides
insserv: warning: script 'ssh-start.sh' missing LSB tags and overrides
Job for ssh.service failed because the control process exited with error code. See "systemctl status ssh.service" and "journalctl -xe" for details.
invoke-rc.d: initscript ssh, action "restart" failed.
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2019-01-28 14:10:23 EST; 6ms ago
  Process: 79047 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=255)
  Process: 79038 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
 Main PID: 79047 (code=exited, status=255)

Jan 28 14:10:23 joeslinux systemd[1]: Starting OpenBSD Secure Shell server...
Jan 28 14:10:23 joeslinux systemd[1]: ssh.service: Main process exited, code=exited, status=255/n/a
Jan 28 14:10:23 joeslinux systemd[1]: Failed to start OpenBSD Secure Shell server.
Jan 28 14:10:23 joeslinux systemd[1]: ssh.service: Unit entered failed state.
Jan 28 14:10:23 joeslinux systemd[1]: ssh.service: Failed with result 'exit-code'.
dpkg: error processing package openssh-server (--configure):
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 openssh-server
E: Sub-process /usr/bin/dpkg returned an error code (1)

当我检查 ssh 服务器的状态时,

sudo service ssh status

● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2019-01-28 14:10:23 EST; 2min 37s ago
  Process: 79047 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=255)
  Process: 79038 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
 Main PID: 79047 (code=exited, status=255)

Jan 28 14:10:23 joeslinux systemd[1]: Starting OpenBSD Secure Shell server...
Jan 28 14:10:23 joeslinux systemd[1]: ssh.service: Main process exited, code=exited, status=255/n/a
Jan 28 14:10:23 joeslinux systemd[1]: Failed to start OpenBSD Secure Shell server.
Jan 28 14:10:23 joeslinux systemd[1]: ssh.service: Unit entered failed state.
Jan 28 14:10:23 joeslinux systemd[1]: ssh.service: Failed with result 'exit-code'.

谢谢你的帮助。

-- 编辑 1/28/19 --

我不知道这是否有帮助,但我有 /etc/init.d以下 2 个脚本......

ssh.sh

这似乎是 Ubuntu 提供的 shell 脚本

ssh-start.sh

其中包含

sudo service ssh start

除此之外,我还有/etc/systemd/system/sshd.service,这是/lib/systemd/system/ssh.service

其中包含:

[Unit]
Description=OpenBSD Secure Shell server
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run

[Service]
EnvironmentFile=-/etc/default/ssh
ExecStartPre=/usr/sbin/sshd -t
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/usr/sbin/sshd -t
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify

[Install]
WantedBy=multi-user.target
Alias=sshd.service

------------ 编辑 2019 年 2 月 18 日 -----------------------

仍然有问题。它们现在导致诸如 apt install 之类的操作失败。

谢谢,安德鲁·邓恩,但我研究过,答案和解决方案对我没有用。

以下是输出journalctl -xe

joe@joeslinux:~$ journalctl -xe
Feb 18 21:42:34 joeslinux start-mining-monero-minergate-cpu.sh[1640]: [2019-02-18 21:42:34] Diff: 8782 CPU/GPU: 953.8 H/s
Feb 18 21:42:35 joeslinux start-mining-monero-minergate-cpu.sh[1640]: [2019-02-18 21:42:35] Diff: 8782 CPU/GPU: 964.0 H/s
Feb 18 21:42:36 joeslinux sudo[5685]: joe : TTY=pts/6 ; PWD=/home/joe ; USER=root ; COMMAND=/usr/sbin/service ssh restart
Feb 18 21:42:36 joeslinux sudo[5685]: pam_unix(sudo:session): session opened for user root by (uid=0)
Feb 18 21:42:36 joeslinux 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.
Feb 18 21:42:36 joeslinux 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.
Feb 18 21:42:36 joeslinux sshd[5703]: error: Bind to port 22 on 0.0.0.0 failed: Address already in use.
Feb 18 21:42:36 joeslinux sshd[5703]: error: Bind to port 22 on :: failed: Address already in use.
Feb 18 21:42:36 joeslinux sshd[5703]: fatal: Cannot bind any address.
Feb 18 21:42:36 joeslinux systemd[1]: ssh.service: Main process exited, code=exited, status=255/n/a
Feb 18 21:42:36 joeslinux 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.
Feb 18 21:42:36 joeslinux systemd[1]: ssh.service: Unit entered failed state.
Feb 18 21:42:36 joeslinux systemd[1]: ssh.service: Failed with result 'exit-code'.
Feb 18 21:42:36 joeslinux sudo[5685]: pam_unix(sudo:session): session closed for user root

注意:错误:绑定到端口 22... 行。

答案1

此问题是由以下配置错误引起的:/etc/ssh/sshd_config文件。当服务尝试启动时,它无法识别此配置文件的每个字段。为了解决这个问题,您必须使用工具

/usr/sbin/sshd -T

如果 /etc/ssh/sshd_config 错误,这将用线条显示错误的参数。

您必须纠正此问题,然后重新启动服务:

/etc/init.d/sshd restart

答案2

如果在安装 Ubuntu Server 后立即发生这种情况,请按Ctrl+F2或 F3、...F7 切换到另一个 TTY(如果您没有空闲的终端)。它会要求您登录。然后,您应该能够运行sudo ssh-keygen -A,然后重新启动,它应该能够启动该服务。

答案3

当我想禁用 SSH 密钥登录时,发生了这种情况。首先,我设置如下:

sudo nano /etc/ssh/sshd_config

// set this in the file:
PasswordAuthentication yes (from no)

// save and call:
sudo service ssh restart

然后发生错误然后我也在上面的文件中添加了:

PubkeyAuthentication no

// then call
sudo service ssh restart

看来它修复了错误。

答案4

我遇到了这个问题,问题是 SSH 守护进程在网络准备就绪之前启动。当网络未准备好时,sshd 会以代码 255 退出(这出现在 OP 的 journalctl -xe 输出中),这会阻止 systemd 在配置中指定失败时重新启动 sshd,因为后续配置指令:

RestartPreventExitStatus=255

可以通过注释掉该配置指令并将以下配置指令添加到[Service]之后的部分来纠正这个问题Restart=on-failure

RestartSec=30

如果愿意,您可以将其设置为 30 以外的值,这会告诉 systemd 在失败后在指定的秒数后尝试重新启动该服务。

如果您已将其设置为使用 DHCP,这将允许服务在网络完全初始化并配置后重新启动。

我确实发现你的问题与我的并不完全相同。尝试将 sshd 绑定到 /etc/ssh/sshd_config 中配置的特定 ip 地址:

ListenAddress x.x.x.x

运行ifconfig -a以获取您想要连接的网络接口的 IP。

相关内容