Shell脚本调用sudo;如何抑制密码提示
我正在编写一个简单的 shell 脚本来更改网络硬件的 mac 地址。 其中一行是:
sudo ifconfig eth0 hw ether 00:99:99:00:00:00
我的问题是 sudo 脚本提示输入密码。有什么方法可以在不提示用户输入密码的情况下执行此操作???
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
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 ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最肯定的是,如果您不介意为该特定用户“免费使用”该特定命令:
基本上请参阅我的其他答案:Shell 脚本 - Sudo 权限随时间丢失
最简单的可行方法是
此外,您可以在同一终端 (tty/vty) 上 sudo 任意命令,并且
sudo
会将身份验证缓存一段时间(或直到sudo -k
),因此您可以开始该脚本将“记住”您之前调用 sudo 时的凭据。我有时在使用sudo
编写管道时这样做(只是在它们之前加上sudo true
)Most definitely, if you don't mind making that particular command 'free for use' for that particular user:
See basically my other answer here: Shell script - Sudo-permissions lost over time
The simplest thing that may work is
Also, you could sudo an arbitrary command on the same terminal (tty/vty), and
sudo
will cache the authentication for a while (or untilsudo -k
), so you may start the script and it will 'remember' the credentials from your earliersudo
invocation. I sometimes do this when composing pipes withsudo
(just preceded them withsudo true
)您需要一个 sudo 配置行,它允许用户在没有密码提示的情况下执行命令。
您可以禁用整个用户的密码提示(更危险,但如果您是桌面上的唯一用户,也许没问题 - 不要在服务器上执行此操作):
或更严格,仅允许 ifconfig 命令:
请参阅:
man sudoers
、man sudo
http://ubuntuforums.org/showthread.php?t=1132821
http://www.linuxhelp.net/guides/sudo/
You need a
sudo
configuration line which allows for the command to be executed by the user without password prompt.You can disable the password prompt for a whole user (more dangerous, but perhaps ok if you are the only user on your desktop -- DONT do this on a server ):
or more restrictive, only allowing the ifconfig command:
See:
man sudoers
,man sudo
http://ubuntuforums.org/showthread.php?t=1132821
http://www.linuxhelp.net/guides/sudo/
更安全的方法是:
然后添加
到出现的编辑器窗口。完成后,使用
:x
退出A safer way to do it would be:
then add
to the editor window that appeared. Once you are done, use
:x
to quit这是一个 Zenity 对话框,其功能与 Tman 的评论类似,
虽然密码没有存储在历史记录中......
这可能是一个不错的选择
Here is a Zenity dialog that does something similar to Tman's comment,
though the password isn't stored in history...
This may be a good alternative