I have lab and in that ,collection of Linux computer are connected in LAN. i want how to writ e shell script to shut down all PC when we run that shell script.


Put the hostnames or IP addresses in a file named inventory.txt like this:


Use this script:

while read host; do
    ssh root@${host} shutdown -h now
done < inventory.txt
  • I shuddered when I saw root@ for many more than just one reason... – Breakthrough Jun 9 '14 at 3:41
  • @Breakthrough, is that why you voted it down? – Cristian Ciupitu Jun 9 '14 at 3:41
  • That, the fact that OP might not have an SSH daemon, the root user should be disabled over SSH already, this would require hard-coding the root passwords if there were any (or keeping the keys all together), and you don't need root to shut the machine down. – Breakthrough Jun 9 '14 at 3:43
  • @Breakthrough, if the OP doesn't have SSH already set up or this answer doesn't suit him, he's free to say so. I also don't see how logging in as root can be a reason for down voting. I can understand it being a reason for not up voting, but not down voting. – Cristian Ciupitu Jun 9 '14 at 3:56

I apologize I am not allowed to add a comment (not high enough rep?) so unfortunately have to post this as an answer

I'm curious too why answer was voted down too. There are a lot of merits in what @Cristian is answering and I agree that if OP doesn't have SSH daemon he would either state he needs a workaround OR he would realize maybe he needs it installed. Considering most of the answers for this type of thing would use SSH daemon I think it's a good answer.

That being said, I also agree that you should use root to do this and use ssh key authentication. That's the best and most secure answer. Of course you do not use a password in an ssh script - harder to do (probably would need to use expect) and less secure.

I don't know about using a non-root user to do a poweroff/reboot. Using the link you sent, you realize that the sudo user is configured to have no password and then poweroff the machine. That means any user that knows the username can sudo to that user and poweroff. Is that what you really want?

Apologies again that this is posted as a separate answer - I want to add it as a comment to @Cristian's answer as his answer is one I would vote for

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