我正在用Java编写程序来关闭Ubuntu服务器。
echo password | sudo -S shutdown -h now
sudo -p 'password' shutdown -h now
其中一个命令都不起作用。
我需要sudo命令关闭并通过传递密码重新启动我的服务器。 因为我从程序中执行此操作,所以不应提示输入密码。密码也应该与命令一起发送。任何建议都会有所帮助.....
答案 0 :(得分:1)
sudo的-p选项指定提示,而不是密码。看看-A选项。来自sudo手册页的摘录 - :
-A Normally, if sudo requires a password, it will read it from the current
terminal. If the -A (askpass) option is specified, a
helper program is executed to read the user's password and output the
password to the standard output. If the SUDO_ASKPASS environment variable
is set, it specifies the path to the helper program. Otherwise, the value
specified by the askpass option in sudoers(5) is used.
帮助程序必须输出正确的密码。所以要指定帮助程序 - :
export SUDO_ASKPASS="/path/to/helper"