Restart hoặc shutdown máy cài Linux sử dụng SSH

Để shutdown máy cài Linux thông qua SSH ta phải thực hiện

  • SSH đến máy Linux không cần mật khẩu
  • Sử dụng lệnh shutdown -h now

Thông thường, máy sử dụng Linux sẽ không cho SSH với username là root nên bài viết này thực hiện trên username bất kì ví dụ onlinekiemcom

SSH không cần mật khẩu

ssh-copy-id -i ~/.ssh/id_rsa.pub 22829.local
ssh-copy-id -i ~/.ssh/id_rsa.pub 452c3.local
ssh-copy-id -i ~/.ssh/id_rsa.pub 0442e.local
ssh-copy-id -i ~/.ssh/id_rsa.pub 861d9.local
ssh-copy-id -i ~/.ssh/id_rsa.pub a0ce8.local
ssh-copy-id -i ~/.ssh/id_rsa.pub 32f8e.local
ssh-copy-id -i ~/.ssh/id_rsa.pub 0100a.local

ssh -t [email protected] 'shutdown -h now'

Bash script

for host in 22829.local 452c3.local 0442e.local 861d9.local a0ce8.local 32f8e.local 0100a.local;do
ssh $host shutdown -h now
done

Tham khảo

https://askubuntu.com/questions/191257/how-could-i-shutdown-a-remote-host-in-my-network-thru-ssh-with-a-local-host

https://linuxcommando.blogspot.com/2013/04/one-liner-to-shutdown-remote-host.html

https://superuser.com/questions/766339/if-we-have-a-collection-of-linux-computers-on-a-lan-how-to-write-shell-script-t

https://www.tldp.org/LDP/abs/html/loops1.html

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

computer1 computer2 192.168.1.1

Use this script:

while read host; do ssh root@${host} shutdown -h now done < inventory.txt