This is just an example of what you could do. Modify for your own situation. There are a bunch of different ways to accomplish this with varying degrees of safety/security (eg. using sshpass with your password on the command line is really insecure and adding your user ssh key to root on the server is really, really insecure).
This assumes your user name is myuser
.
On the server as root
.
echo 'myuser ALL=(ALL) NOPASSWD: /usr/sbin/shutdown now' >> /etc/sudoers.d/99-myuser-shutdown
On the client/desktop/workstation as myuser
:
ssh-keygen -t Ed25519 -N '' -f ~/.ssh/id_ed25519_shutdown
(echo -n 'command="/usr/bin/sudo /usr/sbin/shutdown now" ' &&
cat ~/.ssh/id_ed25519_shutdown.pub) |
ssh myuser@server 'mkdir -pm 700 .ssh && cat >> .ssh/authorized_keys'
Now on the client to shutdown the server just do:
ssh -i ~/.ssh/id_ed25519_shutdown myuser@server
Even if your client user account gets hacked all they can do is shutdown the server. They won't have shell access on the server unless you add another key to authorized_keys or something and they certainly won't have root access(!).
ssh your-server sudo shutdown now
. Use a ssh key instead of ssh password (google). You still need to do something about the sudo password, though. – dirkt Mar 12 '22 at 14:02ssh -t ...
– Chris Davies Mar 12 '22 at 14:28-t
would be needed. There's no TUI application involved. – frabjous Mar 12 '22 at 18:20