Ubuntu Desktop 20.04 LTSでOS起動時にsshdが起動してこない
作成: 2021-05-24
更新: 2021-05-24
Virtual Box
上のUbuntu Desktop 20.04
でopenssh-server
をインストールして、systemctl enable ssh
でOS起動時にSSHサービスが起動するようにしたところ、Ubuntuのリブート後にホストOSからsshログインを試すと以下のエラーが出てログインできなかった。
$ ssh ubuntu-desktop
ssh: connect to host ubuntu-desktop port 22: Connection refused
有効にならなかったのかな?と思い、Virtual Box
のディスプレイから直接Ubuntuにログインし、systemctl status ssh
すると、エラーになっており、起動しようとした痕跡はあった。
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2021-05-24 21:29:50 JST; 26min ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 741 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Process: 775 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=255/EXCEPTION)
Main PID: 775 (code=exited, status=255/EXCEPTION)
5月 24 21:29:48 ubuntu-desktop systemd[1]: Starting OpenBSD Secure Shell server...
5月 24 21:29:50 ubuntu-desktop sshd[775]: error: Bind to port 22 on 192.168.56.101 failed: Cannot assign requested address.
5月 24 21:29:50 ubuntu-desktop sshd[775]: fatal: Cannot bind any address.
5月 24 21:29:50 ubuntu-desktop systemd[1]: ssh.service: Main process exited, code=exited, status=255/EXCEPTION
5月 24 21:29:50 ubuntu-desktop systemd[1]: ssh.service: Failed with result 'exit-code'.
5月 24 21:29:50 ubuntu-desktop systemd[1]: Failed to start OpenBSD Secure Shell server.
以下の行のエラー内容を見る限り、起動時にIPアドレスにバインドする時に失敗しているみたいだった。
5月 24 21:29:50 ubuntu-desktop sshd[775]: error: Bind to port 22 on 192.168.56.101 failed: Cannot assign requested address.
調べたところ、sshd failed due to network not yet availableが見つかった。
sshdの設定でリッスンするIPアドレスを指定した状態で、IPアドレスが利用可能になる前にsshdが起動してしまったときに起きるらしい。
デフォルトは0.0.0.0
が設定されているため、特定のIPに依存せず問題ないようだ。
確かに、ホストオンリーアダプターのIPアドレスに限定した記憶があるので、以下の手順を試してみたところ解決した。
sudo su -
mkdir /etc/systemd/system/sshd.service.d
cat > /etc/systemd/system/sshd.service.d/network-online.conf <<EOF
[Unit]
After=network-online.target
EOF
systemctl daemon-reload
exit
詳しく調べていないが、After=network-online.target
ということなので、ネットワークが使用可能になってからsshdサービスを起動する設定のようだ。
Ubuntu をリブートしてみると、無事SSHできるようになった。
$ ssh ubuntu-desktop
Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.8.0-53-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Sun May 23 11:51:26 2021 from 192.168.56.1
mani@ubuntu-desktop:~$