输入命令链中 sudo 命令的密码
在 Linux 中,如何输入需要 sudo 的命令链中的其中一个命令的密码。我能想到的一个例子是,在运行了长时间的编译作业后,我想关闭机器。
make ; sudo init 0
我希望 shutdown 命令仅在 make 完成后运行,但想立即输入密码,因为当第一个命令完成时我不会在那里。另外,我不想以超级用户权限运行“make”。因此切换到 root 并运行命令也是不可能的。
谢谢!
In Linux how can I enter the password for one of the commands in a chain of commands which requires a sudo. One example which I can think of is when after running a long compile job, I want to shutdown the machine.
make ; sudo init 0
I want the shutdown command to run only after make finishes, but would like to enter the password right away, because I won't be there when the first command is done. Also, I don't want to run "make" with super user privileges. So switching to root and running the commands is also out of the question.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尽早并经常更换您的液体。这就是芝加哥方式!
Change your uid early and often. That's the Chicago way!
您可以运行
sudo -v
来更新sudo
时间戳 - 这意味着您在 x 分钟内无需输入密码(默认为5)。您可以通过编辑 sudoerstimestamp_timeout 来更改超时a> 文件。您可能还想将命令更改为
仅在 make 成功完成时才会关闭。
You can run
sudo -v
to update thesudo
timestamp - this means you won't need to enter a password for x minutes (default is 5). You can change the timeout by editing thetimestamp_timeout
in the sudoers file.You also might want to change your command to
which will only shut down if make completes successfully.
一种可能性是使用 Expect
自动输入密码。
以下是期望和密码的简单操作方法: http://www.linux.com/archive/ feed/56066
另一种可能性是编辑 /etc/sudoers 并将用户设置为无需密码即可使用 sudo。如果您检查该文件,它将在注释中进行解释。
One possibility is to use Expect
to automate the password input.
Here's a simple HOW-TO for expect and passwords: http://www.linux.com/archive/feed/56066
Another possibility is to edit your /etc/sudoers and set your user to be able to use sudo without password. if you check the file it will be explained in the comments there.
为什么不将密码通过管道传输到 sudo 中?
制作;回显'密码'| sudo -S init 0
Why not pipe the password into
sudo
?make; echo 'password' | sudo -S init 0
将 sudo 的超时更改为足够长的时间以满足您的使用情况,然后运行 sudo echo 之类的命令进行身份验证,然后运行其他命令。只要未达到密码超时,它就应该执行而无需再次询问。
Change the timeout for sudo to something long enough to cover your usage, then run something like
sudo echo
to authenticate, then run your other commands. As long as the password timeout hasn't been reached, it should execute without asking again.您可以将其添加到 sudoers 文件中,
其中 username 是您想要允许的用户名。
You can add that to your sudoers file
where username is the username you want to allow.