Shell脚本调用sudo;如何隐藏密码提示 [英] Shell script calls sudo; how do I suppress the password prompt
问题描述
我正在编写一个简单的shell脚本,该脚本会更改网络硬件的mac地址. 其中一行是:
I am writing a simple shell script which changes the mac address of the network hardware. One of the line is :
sudo ifconfig eth0 hw ether 00:99:99:00:00:00
我的问题是sudo脚本提示输入密码.有什么办法可以在不提示用户输入密码的情况下执行此操作?
My problem is with sudo the script prompts for password. Is there any way that I could do this without prompting the user for password ???
推荐答案
最肯定的是,如果您不介意为特定用户将特定命令免费使用":
Most definitely, if you don't mind making that particular command 'free for use' for that particular user:
基本上可以在这里看到我的其他答案: Shell脚本-Sudo -权限随时间流逝
See basically my other answer here: Shell script - Sudo-permissions lost over time
可能最简单的方法是
myuser = NOPASSWD: /sbin/ifconfig
此外,您可以在同一终端上(tty/vty)sudo一个任意命令,并且sudo
将在一段时间内(或直到sudo -k
)缓存身份验证,因此您可以启动脚本并它将记住"您先前的sudo
调用中的凭据.在用sudo
组成管道时(有时在sudo true
之前)我有时会这样做
Also, you could sudo an arbitrary command on the same terminal (tty/vty), and sudo
will cache the authentication for a while (or until sudo -k
), so you may start the script and it will 'remember' the credentials from your earlier sudo
invocation. I sometimes do this when composing pipes with sudo
(just preceded them with sudo true
)
这篇关于Shell脚本调用sudo;如何隐藏密码提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!