I have more than 10 Linux machines. How may I power off all machines using a single script?

The password and user is the same for all of the machines.

4 Answers 4


Try to use ansible.

Install ansible:

apt-get install ansible

Add your hosts to hosts file:

vim /etc/ansible/hosts

Generate ssh key and add it on remote servers:

cat /etc/ansible/hosts | xargs -i ssh-copy-id {}

Run shutdown on servers:

ansible all -m shell -a "shutdown -h now"

You can check hosts availability by command before and after shutdown:

ansible all -m ping

Source 1, Source 2


There's many mays to do it.

One option is to use ssh key pairs instead of passwords to ssh without prompting for password. Then, you can do this :


for server; do ssh $server 'halt; exit'; done


./script.bash server1 server2

Or you can use a better approach with a tool like or pssh

  • foo,bar,base are hostnames ?
    – MikePappa
    Oct 17, 2017 at 16:17
  • Sure it is servers names... Oct 17, 2017 at 16:20
  • will ip address work instead of hostname?
    – MikePappa
    Oct 17, 2017 at 16:22
  • Yes, it will work, for sure... Oct 17, 2017 at 16:24
  • 3
    Maybe you can try by yourself and start experimenting ?! Oct 17, 2017 at 16:32

Setup an ssh key for the user “shutdown”. If you look at /etc/passwd, this user’s default shell is /sbin/shutdown. So just logging in will execute the shutdown command.


I use fabric (http://www.fabfile.org/). You'd write a python script and can then run it on remote systems. It's quick and works pretty well for me for remote system administration.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .