使用sudo没有密码里面的脚本 [英] Use sudo without password INSIDE a script

查看:108
本文介绍了使用sudo没有密码里面的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我需要,为用户,为没有sudo运行一个脚本script.sh,需要root权限才能正常工作。结果
我看到才能把须藤INSIDE script.sh的唯一解决方案。让我们看一个例子:

For some reason I need, as user, to run without sudo a script script.sh which needs root privileges to work.
I saw as the only solution to put sudo INSIDE script.sh. Let's take an example :

script.sh :

#!/bin/sh
sudo apt-get update

当然,如果我执行这个脚本,我得到一个提示,要求我输入密码。然后,我添加到我的sudoers文件(在结束时高于一切):

Of course, if I execute this script, I get a prompt asking me for a password. Then I added to my sudoers file (at the end to override everything else) :

user ALL=(ALL:ALL) NOPASSWD:/path/to/script.sh

顺便说一句,我也试过行:

By the way, I also tried the line :

user ALL=(ALL) NOPASSWD:/path/to/script.sh

(我觉得我没有完全理解的差异)

(I think I didn't fully understand the difference)

但是,如果我不使用sudo来执行这个脚本,这并不解决我的问题:

But this doesn't solve my problem if I don't use sudo to execute this script :

# ./script.sh
[sudo] password for user: 
# sudo ./script.sh
Starts updating...

嘛,所以我对自己说:好吧,这意味着,如果我有在sudoers中所指向的文件和我一样,将不提示工作,只有当我打电话给他使用sudo,究竟是不是我想要的。< BR>
那么,好吧,我创建了另一个脚本script2.sh如下:

Well, so I say to myself "Ok, that means that if I have a file refered in sudoers as I did, it will work without prompt only if I call him with sudo, what is not what I want".
So, ok, I create another script script2.sh as following :

script2.sh

#!/bin/sh
sudo /path/to/script.sh

在事实上,它的工作原理。但我并不真正满意这种解决方案,特别是事实,我必须使用2脚本的每一个命令。

In fact it works. But I am not truly satisfied of this solution, particularly by the fact that I have to use 2 scripts for every command.

这是后话帮助有这个问题,寻找相同的解决方案的人(我没有找到它的一个很好的职位),也许有更好的解决方案,从你们的到来。

This post is then for helping people having this problem and searching for the same solution (I didn't find a good post on it), and perhaps have better solutions coming from you guys.

随时分享您的想法!

编辑1:

我要坚持这样的事实,这种apt-get的更新只是一个例子远非whhat我的脚本实际上是。我的剧本有很多的命令(有一些光盘的根访问仅配置文件),并且解不了好了,只是做它直接apt-get的。

I want to insist on the fact that this "apt-get update" was just an example FAR from whhat my script actually is. My script has a lot of commands (with some cd to root-access-only config files), and the solution can't be "Well, just do it directly with apt-get".

一个例子的原理是,以帮助理解,而不是要借口简化的一般问题的答案。

The principle of an example is to help the understanding, not to be excuse to simplify the answer of the general problem.

推荐答案

如果你想运行须藤的/ usr / bin中/ apt-get的更新没有密码,你需要有sudoers的条目:

If you want to run sudo /usr/bin/apt-get update without a password, you need to have the sudoers entry:

user ALL=(ALL:ALL) NOPASSWD:/usr/bin/apt-get update

有关脚本作为一个整体的更大的问题,有两种可能的方法:

For the larger issue of the script as a whole, there are two possible approaches:

有关在需要的脚本须藤每个命令,创建 sudoers的行专门为命令。在这种情况下,脚本可以通常称为:

For each command in the script that needs sudo, create a line in sudoers specifically for that command. In this case, the script can be called normally:

./script1.sh

方法2

放在 sudoers文件 A线为脚本作为一个整体。当做到这一点,各个命令不需要须藤。然而,须藤必须用于启动脚本是:

Approach 2

Place a line in sudoers for the script as a whole. When this is done, the individual commands do not need sudo. However, sudo must be used to start the script as in:

sudo ./script.sh

这篇关于使用sudo没有密码里面的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆