如何授予 sudo 访问 bash 脚本的权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11468984/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:45:09  来源:igfitidea点击:

How to give sudo access to a bash script?

bashsudosudoers

提问by ronnie

I have a bash script(chbr.sh) to change my display brightness from terminal as my brightness keys doesn't work.

我有一个 bash 脚本 ( chbr.sh) 来从终端更改我的显示亮度,因为我的亮度键不起作用。

`sudo setpci -s 00:02.0 F4.B=30`

Now, every time I run that script it asks for password which I don't like. So, I googled a little and found out that one can edit /etc/sudoersfile to disable the password feature.

现在,每次我运行该脚本时,它都会要求输入我不喜欢的密码。所以,我用谷歌搜索了一下,发现可以编辑/etc/sudoers文件以禁用密码功能。

So, I edited my sudoersfile with the below content

所以,我sudoers用以下内容编辑了我的文件

ronnie ALL = (ALL) NOPASSWD: /home/ronnie/chbr.sh

Now when I run my script as ./chbr.shit again asks for my password. So, is this not the right way to give sudo access to a bash script or what am I doing wrong here.

现在,当我运行我的脚本时,./chbr.sh它再次询问我的密码。那么,这不是让 sudo 访问 bash 脚本的正确方法,还是我在这里做错了什么。

ronnie@ronnie:~$ ls -l chbr.sh
~rwxrwxr-x 1 ronnie ronnie 46 Jul 13 15:59 /home/ronnie/chbr.sh

采纳答案by Igor Chubin

You make all correct, but execute the script with te full path:

你做的一切都正确,但用完整路径执行脚本:

$ sudo /home/ronnie/chbr.sh

回答by fragmentedreality

Do you run sudo /home/ronnie/chbr.sh?

你跑sudo /home/ronnie/chbr.sh吗?

With the content of the file being

随着文件的内容

setpci -s 00:02.0 F4.B=30

Or you allow user ronnie to sudo setpciwithout password:

或者您允许用户 ronnie 在setpci没有密码的情况下执行 sudo :

ronnie ALL = (ALL) NOPASSWD: /sbin/setpci<-- or whatever path your setpci resides in.

ronnie ALL = (ALL) NOPASSWD: /sbin/setpci<-- 或您的 setpci 所在的任何路径。

回答by Jakob Guldberg Aaes

For anyone who stumbles into this old forum. You don't need sudo to change the brightness. It can be done with the “light” program where the

对于偶然进入这个旧论坛的任何人。你不需要 sudo 来改变亮度。它可以通过“轻”程序来完成,其中

light -A 5

increases brightness with 5 and

增加亮度 5 和

light -U 5

decreases the brightness with 5.

用 5 降低亮度。