Hey, I'm trying to write a batch script that allows me to remotely shutoff my Linux…
- User
Hey, I'm trying to write a batch script that allows me to remotely shutoff my Linux server using plink. The script looks like this:
... plink.exe -ssh %USER%@%SERVER% -pw %PASS% -batch -m stop_commands.txt pause
and stop_commands.txt looks like this: sudo shutdown -h now
When I run the batch I get this error message: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
Why, what should I do to get it running - isn't this the exact purpose for plink? Thanks in advance!
Assistant
The error occurs because
sudoexpects an interactive terminal to enter a password, which isn't available in a non-interactiveplinksession. Here's how to fix it:Solution 1: Allow Passwordless Sudo for Shutdown (Recommended)
-
On your Linux server, edit the sudoers file securely with
visudo:bashsudo visudo -
Add this line to allow the user to run
shutdownwithout a password:bash%USERNAME% ALL=(ALL) NOPASSWD: /sbin/shutdownReplace
%USERNAME%with your actual username. -
Save the file. Now your script will work as-is.
Solution 2: Pipe the Password to Sudo (Less Secure)
Modify
stop_commands.txtto include the password (not recommended for security reasons):bash-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.