Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The question is simple.
What would be the script I would have to use to shut down a computer in my network thru ssh.

Normaly i would go to command line and:

ssh desktop

delik@desktop's password: 

delik@desktop:~$ sudo shutdown -P 0

To power on I created a file and wrote:

wakeonlan xx:xx:xx:xx:xx:xx

And gave it the executable bit

That way to power on it requires only a double click. Would i be capable of doing the same to shutdown?

share|improve this question
up vote 3 down vote accepted

Assuming that the user you are going to use in is the same you use in
In order to do that you have to first authorize your to connect to you with no password.
To do that you have to: (as described here)

Step 1: Install ssh

sudo apt-get install ssh

Step 2: Create public and private keys using ssh-key-gen on local-host

On your local host, enter this command:


You should save the generated key in:


Press enter twice to leave the passphrase empty.

Your identification has been saved in /home/yourusername/.ssh/id_rsa.
Your public key has been saved in /home/yourusername/.ssh/
The key fingerprint is:
XX:XX:XX:xX:XX:xX:XX:XX:XX:XX:XX:XX:XX:XX yourusername@local-host

Step 3: Copy the public key to remote-host using ssh-copy-id

yourusername@local-host$ ssh-copy-id -i ~/.ssh/ remote-host
yourusername@remote-host's password:

Now try logging into the machine, with "ssh remote-host", and check in:


to make sure we haven't added extra keys that you weren't expecting.

Note: ssh-copy-id appends the keys to the remote-host’s /home/yourusername/.ssh/authorized_key

Step 4: Login to remote-host without entering the password

ssh remote-host$

Access to remote-host with no password. Success!

Now you have to be able to execute the sudo shutdown -P 0 with no password.

Modify /etc/sudoers on with visudo

That way user "yourusername" can execute the shutdown command with no password asked.
Login to the remote host.



sudo visudo

By running visudo, it leads to edit /etc/sudoers.
Adding the line below to that file.

yourusername ALL = NOPASSWD: /sbin/shutdown

Done that, get back to your
Create a new empty file and paste this line, modifying the's name

ssh sudo shutdown -P 0

Save and close it, go to it's Properties slide to Permissions, and tic the execute this file as a program

Script Done!

share|improve this answer
Thanks mene for your head's up :) – DeLiK Sep 21 '12 at 12:22

You can use an user-account with a keyfile instead of a password (so you don't have to type in the password to login). You can also give the user a "default shell" that is not bash or sh, but "" or something like this, which will exec your shutdown-code.

share|improve this answer
Would you improve your answer with some examples on how to do that exactly? – DeLiK Sep 21 '12 at 9:09
You can check this out for the login part:… You can use usermod -s /home/username/ username to change the default shell. The script should be something like #!/bin/bash shutdown -P now – mene Sep 21 '12 at 9:17
You are right, the ssh login problem is solved, but now I have to build a script with a "pause" so it waits for ssh desktop to happen succesfully. And after that i have another problem how to give the command in sudo and enter the password? – DeLiK Sep 21 '12 at 9:52
I manage to do that. Now I can shut down with sudo shutdown -P 0 with no password – DeLiK Sep 21 '12 at 10:09
you can pause a script with the "sleep" command. "sleep 10" for 10 seconds. – mene Sep 21 '12 at 10:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.