0

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();

4 Answers4

0

I haven’t tried ssh.exec but unless you’re logged in as root, you can only call shutdown with sudo.

Ethan Doh
  • 508
  • 1
  • 7
  • 9
  • Does this help? https://stackoverflow.com/questions/44840459/how-do-i-execute-sudo-command-over-ssh-in-node-js – Ethan Doh Aug 07 '18 at 16:40
  • This should also help. https://stackoverflow.com/questions/27559265/use-pty-with-ssh2-in-node-to-execute-command-with-sudo – Ethan Doh Aug 07 '18 at 16:42
0

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

firxworx
  • 1,161
  • 11
  • 10
  • Yes I have privileges – Ben Mustapha Sabrine Aug 07 '18 at 16:36
  • What error messages are you getting? If your user has sudo privileges, the default behaviour is to prompt for a password. Use `visudo` (e.g. `sudo visudo`) and to allow a given username to run the `reboot` command try the settings: `username ALL = (root) NOPASSWD: /path/to/reboot` followed by another line `Defaults!/path/to/reboot !requiretty` If you need to determine `/path/to/reboot` use the command: `sudo which reboot` – firxworx Aug 07 '18 at 16:58
0

systemctl reboot -i

is the command for all linux machine rebooting condition.

jrs
  • 1
  • 3
-1

To reboot your Linux pc using command line use:

sudo reboot

Enter your sudo password Wait system will reboot automatically

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 14 '22 at 16:28