When I boot my Raspberry pi running Stretch 9, the syntax checks out alright, but when it goes to start the following error is what it gets:
pi@hostname:~ $ sudo systemctl status ssh.service
* ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service: enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2020-03-22 22:10:39 EDT; 12min ago
Process: 562 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=255)
Process: 536 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 562 (code=exited, status=255)
Mar 22 22:10:38 hostname systemd[1]: Starting OpenBSD Secure Shell server...
Mar 22 22:10:39 hostname systemd[1]: ssh.service: Main process exited, code=exited, status=255/n/a
Mar 22 22:10:39 hostname systemd[1]: Failed to start OpenSSH Secure Shell server.
Mar 22 22:10:39 hostname systemd[1]: ssh.service: Unit entered failed state.
Mar 22 22:10:39 hostname systemd[1]: ssh.service: Failed with result `exit-code`.
It is strange that it does not start as if I start it manually after it boots and I login from the main console (keyboard mouse and monitor, not through SSH) I'm able to get the daemon to start:
pi@hostname:~ $
* ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service: enabed; vendor preset: enabled)
Active: active (running) since Sun 2020-03-22 22:23:24 EDT; 5s ago
Process: 1124 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 1127 (sshd)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/ssh.service
`-1127 /usr/sbin/sshd -D`
Mar 22 22:23:24 hostname systemd[1]: Starting OpenSSH Secure Shell server...
Mar 22 22:23:24 hostname sshd[1127]: Server listening on 192.168.1.164 port 22
Mar 22 22:23:24 hostname systemd[1]: Started OpenBSD Secure Shell Server.
If I weren't trying to run this machine headless this wouldn't bother me, but I'm trying to run it from the network and not my personal screen and keyboard.
I already tried to fix it using a workaround for Ubuntu using a Cron job that runs on reboot to basically restart the service later, but it doesn't seem to fix the issue, and it doesn't explain why it works after it reboots.
Here is my sshd.service unit file as requested:
pi@hostname:/etc/systemd/system $ cat ./sshd.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