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