2008-11-27 77 views
74

我需要从脚本编辑/ etc/sudoers来添加/删除白名单中的东西。如何从脚本编辑/ etc/sudoers?

假设我有一个可以在普通文件上工作的命令,我该如何将它应用到.etc/sudoers?

我可以复制和修改它,然后visudo用修改后的副本替换原件吗?通过在EDITOR中提供我自己的脚本?

或者我可以只使用相同的锁和cp?

这个问题更多的是潜在的问题,而不仅仅是找到有用的东西。

回答

29

你应该让你的编辑到一个临时文件,然后使用visudo -c -f sudoers.temp以确认该变化是有效的,然后将其拷贝过来的/ etc/sudoers文件的顶部

#!/bin/sh 
if [ -f "/etc/sudoers.tmp" ]; then 
    exit 1 
fi 
touch /etc/sudoers.tmp 
edit_sudoers /tmp/sudoers.new 
visudo -c -f /tmp/sudoers.new 
if [ "$?" -eq "0" ]; then 
    cp /tmp/sudoers.new /etc/sudoers 
fi 
rm /etc/sudoers.tmp 
+0

它看起来像你使用sudoers.tmp作为锁文件,不知道如何确认更改是有效的。我们是不是应该检查visudo的退出状态以确保没有错误? – converter42 2008-11-27 15:58:55

+0

/etc/sudoers.tmp是visudo在交互模式下检查的锁定文件。 visudo -c -f在出现错误时返回1,因此检查返回代码。 – 2008-11-27 16:02:54

+0

我很担心使用sudoers.tmp,因为它看起来像使用visudo的内部接口,即黑客。它是标准的,这意味着它总是被sudoers.tmp用作锁吗?或者他们有没有在未来改变这种自由的自由? – 2008-11-28 10:16:51

8

visudo应该是编辑/etc/sudoers的人机界面。您可以通过直接替换文件来达到相同的效果,但是您必须注意并发编辑和语法验证。注意r--r-----权限。

+3

记住权限+1! – wchargin 2012-01-09 07:01:39

5

设置自定义编辑器。基本上它会是一个接受文件名的脚本(在本例中为/etc/sudoers.tmp),并修改并保存。所以你可以写出这个文件。完成后,退出脚本,visudo将负责为您修改实际的sudoers文件。

sudo EDITOR=/path/to/my_dummy_editor.sh visudo 
42

使用visudo为此与自定义编辑器。这解决了Brian解决方案中的所有竞争条件和“黑客”问题。

#!/bin/sh 
if [ -z "$1" ]; then 
    echo "Starting up visudo with this script as first parameter" 
    export EDITOR=$0 && sudo -E visudo 
else 
    echo "Changing sudoers" 
    echo "# Dummy change to sudoers" >> $1 
fi 

该脚本将在sudoers的末尾添加一行“#dummy change to sudoers”。没有黑客和没有竞争条件。

注释版本说明这个问题实际上是如何工作的:

if [ -z "$1" ]; then 

    # When you run the script, you will run this block since $1 is empty. 

    echo "Starting up visudo with this script as first parameter" 

    # We first set this script as the EDITOR and then starts visudo. 
    # Visudo will now start and use THIS SCRIPT as its editor 
    export EDITOR=$0 && sudo -E visudo 
else 

    # When visudo starts this script, it will provide the name of the sudoers 
    # file as the first parameter and $1 will be non-empty. Because of that, 
    # visudo will run this block. 

    echo "Changing sudoers" 

    # We change the sudoers file and then exit 
    echo "# Dummy change to sudoers" >> $1 
fi 
-1

这个工作对我关根据别人张贴在这里。当我使用其他人的脚本时,它会为我打开visudo,但不会进行编辑。这使得我需要的编辑允许包括标准用户在内的所有用户为safari/firefox安装java 7u17。

#!/usr/bin/env bash 
rm /etc/sudoers.new 
cp /etc/sudoers /etc/sudoers.new 
echo "%everyone ALL = NOPASSWD: /usr/sbin/installer -pkg /Volumes/Java 7 Update 17/Java 7 Update 17.pkg -target /" >> /etc/sudoers.new 
cp /etc/sudoers.new /etc/sudoers 

这增加了%每个人等等等等等等对sudoers文件的底部。 我不得不这样运行脚本。

sudo sh sudoersedit.sh 

好运:d

2

我想补充另一种选择上述答案,如果比赛条件不是主要问题,则可以使用下面的命令,以避免手动复制修改的文件到/etc/sudoers

sudo EDITOR="cp /tmp/sudoers.new" visudo 

这将确保新的文件进行验证,并与权限的更新正确安装。

请注意,如果/tmp/sudoers.new文件中有错误,则visudo会提示用户输入,因此建议先用visudo -c -f /tmp/sudoers.new进行检查。

11

在Debian和它的衍生物,可以插入自定义脚本为/etc/sudoers.d/目录,具有权利0440 –更多信息见/etc/sudoers.d/README

它可能有帮助。

1

我想最直接的解决方案是:

创建一个脚本addsudoers.sh

#!/bin/bash 

while [[ -n $1 ]]; do 
    echo "$1 ALL=(ALL:ALL) ALL" >> /etc/sudoers; 
    shift # shift all parameters; 
done 

,并要其添加为用户称之为:

root prompt> ./addsudoers.sh user1 user2 

对于充分的解释看到这个答案:Adding users to sudoers through shell script

关心!

0

试着回应它。不过,你必须在子shell中运行它。例如:

sudo sh -c "echo \"group ALL=(user) NOPASSWD: ALL\" >> /etc/sudoers"

86

旧线,但怎么样:

echo 'foobar ALL=(ALL:ALL) ALL' | sudo EDITOR='tee -a' visudo 
3

如果你的sudo允许在/etc/sudoers.d添加条目,那么你可以通过@ dragon788使用这样的回答:

https://superuser.com/a/1027257/26022

基本上你使用visudo验证文件,然后将其复制到sudoers.d中,以便你可以确定你不会为任何人破坏sudo。

visudo -c -q -f filename 

这将检查并返回成功(0),如果它是有效的,所以你可以if&&和其它脚本布尔运算使用它。一旦你验证,只需将其复制到/etc/sudoers.d,它应该工作。确保它由root拥有,不能被其他人写入。