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 is caused by the "sudo" command within the stop_commands.txt file. By default, "sudo" requires a terminal to read the password for security reasons. Since Plink is run in non-interactive mode with the "-batch" option, it does not provide a terminal for "sudo" to read the password.
To get around this, you have two options:
- Use the "-S" option with "sudo" to read the password from standard input and pass the password via "echo":
In your stop_commands.txt file, replace the line with:
codeecho '%PASS%' | sudo -S shutdown -h now- Configure a passwordless sudo for a specific command:
This is a more secure method, as it does not require passing the password in plaintext.
On your Linux server, execute the following command:
codesudo visudoThis will open the sudoers file in an editor. Add the following line at the end of the file (replace "your_username" with your actual username and ensure the path to the 'shutdown' command matches your system):
- User
I have a few questions. Why is number two a more secure method because of not requiring passing of the password in plaintext? Isn't it SSH anyways, and does logging into the server via plink not anyways means you have your username and password written down inside the batchfile in plaintext? If plink can automate the SSH login, which means it passes the user password, why can't it do the same when the server asks for that same password when sudoing? Also, do I still need sudo with the second method when using "shutdown -h now"?
- Assistant
You're correct that the SSH connection itself is secure, but the concern comes from the potential of having the password visible within the command or visible to other processes running on the server.
In the first method, the sudo password is stored in the stop_commands.txt file and is echoed within the command, which can be visible or accidentally leaked if someone else gains access to the file or if it is logged somewhere.
In the second method, you remove the need for storing the sudo password in stop_commands.txt at all, thus reducing the risk of leaking or exposing it. When you configure passwordless sudo for the specific
shutdowncommand, you're allowing the user to execute only that command without requiring a password. Hence, it reduces the attack surface. Keep in mind that this method is for a specific command and not granting the user full root access without a password. - User
Thank you very much, then my last question about that: Why is it different then from using this line? "plink.exe -ssh %USER%@%SERVER% -pw %PASS% -batch -m stop_commands.txt" with "set USER=..." and "set SERVER=..." and "set PASS=..."?
- Assistant
Using the line "plink.exe -ssh %USER%@%SERVER% -pw %PASS% -batch -m stop_commands.txt" with "set USER=...", "set SERVER=..." and "set PASS=..." is essentially passing your SSH login username and password as environmental variables, which then get substituted when you run the plink command.
In this scenario, plink reads the values of USER, SERVER, and PASS from the environment, and these values are substituted in the command when it is executed. This can be convenient for writing a script since it allows you to easily modify the values of USER, SERVER, and PASS in one location instead of having to update them in multiple places throughout the script.
However, it is important to note that storing sensitive information like passwords in plaintext in a script file can be risky. Anyone who can access the script or accidentally leaks the file might gain unauthorized access to your server. It is always a good practice to secure your credentials in a safer manner like using an encrypted password manager or, in the case of SSH, using key-based authentication instead of a password.
- User
Wait, no that wasn't what I wanted to know. I mean this: Where is the difference between storing the password in the batchfile and running it with "plink.exe -ssh...", and storing the same data in the textfile plink uses and then using it for a shutdown? Is there a technical difference that makes one a bigger security issue than the other?
- Assistant
Apologies for the confusion. I misunderstood your question earlier.
There isn't a significant difference between storing the password in the batch file and running it with "plink.exe -ssh..." and storing the password in the "stop_commands.txt" file when it comes to the risk of exposing the password. In both cases, you have the password stored in plaintext in a file, which can be a security risk.
The primary risk comes from the fact that the password is stored in plaintext in either file, and if someone gets unauthorized access to the file, they will have your password. However, I should clarify that this risk applies to any place where you pass the password in plaintext. So, whether it is in the primary batch file or in the "stop_commands.txt" file, the risk is similar.
In both cases, using a more secure method for authentication, such as key-based authentication, or limiting the risk by using passwordless sudo for specific commands only, would be a better practice.
The difference in the methods I described earlier (passwordless sudo vs. echoing the password from the script) is about the level of exposure and risk of the password, but it doesn't directly refer to whether the password is stored in the batch file or the "stop_commands.txt" file.
- User
So I take the same risk by just logging into my server via plink in an automated manner, no? Because in the batch file "plink.exe -ssh..." uses my username and password - the same password needed for root access.
- Assistant
Yes, you are correct. Storing the password in plaintext, whether it's for logging into your server via Plink or for root access, has inherent risks. Automated login using a plaintext password can be a security concern if someone gets unauthorized access to the batch file or if the file gets leaked accidentally.
To enhance the security and avoid using plaintext passwords, you can use key-based authentication for SSH. With key-based authentication, you will use a private-public key pair instead of a plaintext password.
To set up key-based authentication for your server:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.