0

I am trying to configure my system so that I can remotely turn it off. The command I want to execute is this one:

ssh -i $HOME/.ssh/id_rsa myuser@myserver 'sudo poweroff'

Doing this returns an error though:

sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required

I believe this behavior should the be same as when I login as this same user on the system (Ubuntu 24.04) and type sudo poweroff, because if I do that I am prompted for a password, which I do not want to be prompted for. I did already change sudoers using sudo visudo, telling the system to the user to use /sbin/poweroff without password, but this did not prevent the error from happening:

myuser    ALL=(ALL) NOPASSWD: /sbin/poweroff

I also checked the paths using whereis poweroff resulting in:

poweroff: /usr/sbin/poweroff /usr/share/man/man8/poweroff.8.gz

Therefore, I changed the sudoers to this:

myuser    ALL=(ALL) NOPASSWD: /usr/sbin/poweroff

The error persists when using SSH though and being logged into the system also prompts me for the password when entering sudo poweroff. What musst I do so that I am not prompted for any password when entering sudo poweroff?

Socrates
  • 2,645

1 Answers1

-2

sudo is picky, because paranoia is Good in security.

The command you're allowing in sudoers is "/usr/sbin/poweroff", but the command you're passing is "poweroff". Use:

ssh -i $HOME/.ssh/id_rsa myuser@myserver 'sudo /usr/sbin/poweroff`

BUT

Simply powering off is rude. It doesn't let the system save what it needs to, and can lead to disk corruption, fsck requirements, slow startup, stale lock files, leftover temporary files, ... You will be unjoyful.

Use shutdown --poweroff instead.

Suddenly powering off (hitting the Big Red EPO buton) is mostly a leftover from the days of Mainframes, and Field Engineers Wearing Ties, and lots of rotating machinery. In the currend house-of-cards software environment we have, poweroff should be used judiciously, if at all.

Read man shutdown poweroff.

waltinator
  • 37,856