Linux Shell脚本调用sudo;如何取消密码提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8111702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell script calls sudo; how do I suppress the password prompt
提问by ro ko
I am writing a simple shell script which changes the mac address of the network hardware. One of the line is :
我正在编写一个简单的 shell 脚本来更改网络硬件的 mac 地址。其中一行是:
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 ???
我的问题是 sudo 脚本提示输入密码。有什么办法可以在不提示用户输入密码的情况下做到这一点???
采纳答案by sehe
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
在这里基本上可以看到我的其他答案:Shell script - Sudo-permissions lost over time
The simplest thing that may work is
可能工作的最简单的事情是
myuser = NOPASSWD: /sbin/ifconfig
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
)
此外,您可以在同一终端 (tty/vty) 上执行任意命令,并将sudo
身份验证缓存一段时间(或直到sudo -k
),因此您可以启动脚本,它会“记住”您之前sudo
调用的凭据。我有时在用 组合管道时会这样做sudo
(只是在它们之前用sudo true
)
回答by Tilo
You need a sudo
configuration line which allows for the command to be executed by the user without password prompt.
您需要一个sudo
配置行,允许用户在没有密码提示的情况下执行命令。
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 ):
您可以为整个用户禁用密码提示(更危险,但如果您是桌面上的唯一用户,则可能没问题——不要在服务器上执行此操作):
yourusername ALL=(ALL) NOPASSWD: ALL
or more restrictive, only allowing the ifconfig command:
或更严格的,只允许 ifconfig 命令:
yourusername ALL= NOPASSWD: /sbin/ifconfig
See: man sudoers
, man sudo
见:man sudoers
,man sudo
http://ubuntuforums.org/showthread.php?t=1132821
http://ubuntuforums.org/showthread.php?t=1132821
回答by Tman
echo "password" | sudo -S ifconfig eth0 hw ether 00:99:99:00:00:00
回答by Abandoned Cart
A safer way to do it would be:
一个更安全的方法是:
sudo visudo -f sudoers
then add
然后加
myuser ALL=NOPASSWD:/sbin/ifconfig
to the editor window that appeared. Once you are done, use :x
to quit
到出现的编辑器窗口。完成后,使用:x
退出
回答by I-Jo
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
这是一个 Zenity 对话框,它的功能类似于 Tman 的评论,
但密码未存储在历史记录中……这可能是一个不错的选择
#!/bin/bash
ENTRY=`zenity --password`
case $? in
0)
pw=$(echo $ENTRY | cut -d'|' -f1)
;;
1)
echo "Stop login.";;
-1)
echo "An unexpected error has occurred.";;
esac
TMP=$(echo "${pw}" | sudo -Sv)
TMP=0