如何让’sudo’从文件中获取密码?
我想运行一个需要sudo密码的命令说:
sudo apt-get update
这不应该工作(我已将密码存储在普通文本文件passwd.txt
):
sudo apt-get update <~/passwd.txt
这是我为什么它应该工作的逻辑:当需要密码时,要求用户从键盘输入密码。 但重定向stdin
以从passwd.txt
文件中读取应该可以工作。
不应该吗?
默认情况下, sudo
不会从stdin
读取密码。 从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.
所以你应该运行:
sudo -S apt-get update <~/passwd.txt
请记住,将密码存储在文件中并不是一种好习惯。 你应该阅读:
- 如何在不使用sudo的情况下允许执行?
- 如何在没有密码的情况下运行特定的sudo命令?
- 如何在没有密码的情况下使用sudo运行应用程序?