Monday, April 18, 2022

[SOLVED] Reboot Linux with SSH command

Issue

I'm using simple-ssh with nodeJS and expressJS, and I'm trying to reboot a remote linux machine with ssh command, this is my code (it does not work, but if I try to execute a ls command that works.) help please!

ssh.exec('shutdown -r now', {
out: function(stdout) {
     console.log(stdout)}}).start();

Solution

While I'm sure the user executing the command has sufficient privileges to run ls, do they have enough reboot? Check the logs!

EDIT: post revised to include my answer below. I provided it in a comment to this answer after the OP confirmed they have sufficient perms:

Given your reply that your user has sudo privileges, note that sudo's default behaviour is to prompt for a password.

To use the visudo editor (e.g. sudo visudo) to allow a given username to run the reboot command without a password prompt, try adding the following lines to your configuration:

username ALL = (root) NOPASSWD: /path/to/reboot 
Defaults!/path/to/reboot !requiretty 

Your username should no longer be prompted, and your call to ssh.exec() should work as desired. You can follow a similar pattern for any other command you need to run as another user / root.

If you need to determine the /path/to/reboot for your particular system use the command: sudo which reboot



Answered By - firxworx
Answer Checked By - Willingham (WPSolving Volunteer)