如何从脚本编辑/ etc / sudoers?

问题描述 投票:98回答:12

我需要从脚本编辑/etc/sudoers来添加/删除白名单中的内容。

假设我有一个可以在普通文件上运行的命令,我怎么能将它应用到/etc/sudoers

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

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

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

linux shell sudo
12个回答
123
投票

旧线程,但是怎么样:

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

1
投票

我认为最直接的解决方案是:

创建一个脚本addsudoers.sh

#!/bin/sh

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"


-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


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

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

13
投票

在Debian及其派生词上,您可以将自定义脚本插入到/etc/sudoers.d/directory中,使用rights0440-有关更多信息,请参阅/etc/sudoers.d/README

它可能有所帮助。


9
投票

visudo应该是编辑/etc/sudoers的人机界面。您可以通过直接替换文件来实现相同目的,但您必须自己关注并发编辑和语法验证。记住r--r-----权限。


6
投票

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

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

基本上你在将文件复制到visudo之前使用/etc/sudoers.d验证文件,所以你可以确定你没有为任何人打破sudo

visudo -c -q -f filename

如果它有效,则检查它并返回success(0),因此您可以将它与if&&和其他脚本布尔运算一起使用。验证后,只需将其复制到/etc/sudoers.d即可。确保它由root拥有,而不是由其他人写的。


5
投票

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

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

4
投票

很多答案,一直在使用sudo for yonks但是现在还没有必要自动化设置配置。我使用了上面的一些答案的混合,将我的配置行写入/etc/sudoers.d包含位置,所以我不必修改主sudoers文件,然后检查该文件的语法,简单示例如下:

将您的行写入sudoers包含文件:

sudo bash -c 'echo "your_user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/99_sudo_include_file'

检查你的sudoers包含文件是否通过了visudo语法检查:

sudo visudo -cf /etc/sudoers.d/99_sudo_include_file

2
投票

只是为上面的答案添加一个选项,如果竞争条件不是主要问题,那么可以使用以下命令来避免手动将修改后的文件复制到/etc/sudoers

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

这将确保通过权限更新验证并正确安装新文件。

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

© www.soinside.com 2019 - 2024. All rights reserved.