'Reboot Linux with SSH command
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 1:[1]
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
Solution 2:[2]
I haven’t tried ssh.exec but unless you’re logged in as root, you can only call shutdown with sudo.
Solution 3:[3]
systemctl reboot -i
is the command for all linux machine rebooting condition.
Solution 4:[4]
To reboot your Linux pc using command line use:
sudo reboot
Enter your sudo
password
Wait system will reboot automatically
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ethan Doh |
Solution 3 | jrs |
Solution 4 | Tyler2P |