brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: systemctl -H manages services on another machine over SSH

Systemctl
~/tips/tip-systemctl-remote-host

systemctl -H [email protected] status nginx

Check or restart a remote service without opening a shell there first.

For a quick look at one service on another server, a full SSH session is more than I need. systemctl can talk to the remote machine itself.

systemctl -H [email protected] status nginx

It uses SSH to reach the systemd instance on that host, so your normal keys and ~/.ssh/config entries apply, including a host alias:

systemctl -H web1 list-units --state=failed
systemctl -H web1 is-active nginx

Changing things needs rights on the other side. Reading status works for a normal user, while a restart usually asks for authentication that cannot be given this way, so people connect as an account that is allowed to manage units.

It is neat in a loop over a few machines:

for h in web1 web2 web3; do echo "== $h"; systemctl -H "$h" is-active nginx; done

You need systemd on both ends, and a remote system with a very different version can answer with errors about unknown methods. The command also does not replace configuration management. If you find yourself restarting the same thing on ten hosts every week, that is a job for something that keeps track.

Further reading: systemctl man page

More Systemctl tips sudo systemctl daemon-reload systemctl list-units --type=service --state=running sudo systemctl reboot --firmware-setup systemctl list-jobs sudo systemctl try-restart nginx.service All Systemctl tips (9) All topics

Link