Linux – How to get systemd “socket” unit to automatically restart? specifically dropbear.socket

linuxsshsshdsystemd

On my BeagleBone Black Angstrom system, the dropbear.socket UNIT seems to spontaneously die after a while. The effect is that I can not ssh into the machine without restarting the dropbear.socket UNIT. This fixes the problem in the short term:

systemctl restart dropbear.socket

I tried adding Restart=always to the /lib/systemd/system/dropbear.socket file, but that is not allowed (I get an error in the system log).

[Unit]
Conflicts=dropbear.service

[Socket]
ListenStream=22
Accept=yes
Restart=always

[Install]
WantedBy=sockets.target
Also=dropbearkey.service

What's the correct way of getting dropbear.socket to restart automatically? It would be nice to fix the root cause, but this seems like an easy workaround and will make the system more robust.

Update

Here is the list of failed units (I didn't filter it, in case the music service (which I am not using) is a suspect).

% systemctl list-units --failed --full
UNIT                                                  LOAD   ACTIVE SUB    DESCRIPTION
dropbear@1-192.168.0.43:22-192.168.0.40:55370.service loaded failed failed SSH Per-Connection Server
mpd.service                                           loaded failed failed Music Player Daemon

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

2 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

Also here is /lib/systemd/system/dropbear@.service

[Unit]
Description=SSH Per-Connection Server
Requires=dropbearkey.service
After=syslog.target dropbearkey.service

[Service]
ExecStart=-/usr/sbin/dropbear -i -r /etc/dropbear/dropbear_rsa_host_key -p 22
ExecReload=/bin/kill -HUP $MAINPID
StandardInput=socket
KillMode=process

Best Answer

Have you tried setting the OnFailure flag? Example:

[Unit]
Conflicts=dropbear.service
OnFailure=dropbear.socket

[Socket]
ListenStream=22
Accept=yes
Restart=always

[Install]
WantedBy=sockets.target
Also=dropbearkey.service
Related Question