![是否可以不输入密码关闭远程计算机?](https://linux22.com/image/1369369/%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E4%B8%8D%E8%BE%93%E5%85%A5%E5%AF%86%E7%A0%81%E5%85%B3%E9%97%AD%E8%BF%9C%E7%A8%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%EF%BC%9F.png)
我使用以下 bash 关闭远程计算机,但是对于每台计算机,我都必须输入密码。是否可以自动输入密码?
#!/bin/bash
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
ssh -t [email protected] "sudo poweroff"
答案1
你不需要。因为你登录时root
根本没有理由使用sudo
,所以直接运行poweroff
或(更好)shutdown -h
。
如果由于某种问题中未解释的原因而您确实需要sudo
,请使用sudo
的-S
选项。来自man sudo
:
-S The -S (stdin) option causes sudo to read
the password from the standard input
instead of the terminal device. The
password must be followed by a newline
character.
例如:
#!/bin/bash
ssh -t [email protected] "echo PASSWORD1 | sudo -S poweroff"
ssh -t [email protected] "echo PASSWORD2 | sudo -S poweroff"
答案2
为了避免输入密码,请使用ssh-keygen用于身份验证密钥的创建和上传公钥到您所有的服务器。顺便说一句,terdon 说得对,如果您是 root 用户,请不要使用sudo
。
ssh [email protected] poweroff
ssh [email protected] poweroff