Programing

스크립트에서 / etc / sudoers를 어떻게 편집합니까?

lottogame 2020. 8. 17. 09:30
반응형

스크립트에서 / etc / sudoers를 어떻게 편집합니까?


/etc/sudoers화이트리스트에서 항목을 추가 / 제거하려면 스크립트에서 편집해야 합니다.

일반 파일에서 작동하는 명령이 있다고 가정하면 어떻게 적용 할 수 /etc/sudoers있습니까?

복사 및 수정 한 다음 visudo원본을 수정 된 사본으로 대체 할 수 있습니까 ? 내 자신의 스크립트를 제공하여 $EDITOR?

아니면 동일한 잠금 장치를 사용할 수 cp있습니까?

문제는 단순히 작동하는 것을 찾는 것보다 잠재적 인 문제에 관한 것입니다.


오래된 스레드, 그러나 어떨까요?

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

이를 위해 사용자 정의 편집기로 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

임시 파일을 편집 한 다음 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

데비안과 그 파생물에서는 /etc/sudoers.d/권한이있는 사용자 정의 스크립트를 디렉토리에 삽입 할 수 있습니다 . 0440자세한 내용은 /etc/sudoers.d/README를 참조하십시오 .

도움이 될 수 있습니다.


visudo는 편집을위한 휴먼 인터페이스 여야합니다 /etc/sudoers. 파일을 직접 교체하여 동일한 결과를 얻을 수 있지만 동시 편집 및 구문 유효성 검사에주의해야합니다. r--r-----권한에 유의하십시오 .


sudo항목을 추가 /etc/sudoers.d할 수있는 경우 @ dragon788의 답변을 사용할 수 있습니다.

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

Basically you use visudo to verify the file before you copy it into /etc/sudoers.d, so you can be sure you're not breaking sudo for anyone.

visudo -c -q -f filename

This checks it and returns success (0) if it's valid, so you can use it with if, && and other script boolean operations. Once you validate, just copy it into /etc/sudoers.d and it should work. Make sure its owned by root and not writable by other.


Set up a custom editor. Basically it will be a script that accepts the filename (in this case /etc/sudoers.tmp), and modify and save that in place. So you could just write out to that file. When you are done, exit the script, and visudo will take care of modifying the actual sudoers file for you.

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

Lots of answers, been working with sudo for yonks but did not have a need to automate the setup config till now. I used a mix of some of the answers above, writing my config line to the /etc/sudoers.d include location so i don't have to modify the main sudoers file, then checked that file for syntax , simple example below:

Write your line to a sudoers include file:

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

Check that your sudoers include file passed the visudo syntax checks:

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

Just to add a further option to the answers above, if the race condition is not a major concern, then the following command can be used to avoid manually copying a modified file to /etc/sudoers

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

This will ensure that the new file is validated and installed correctly with permissions update.

Note that if there is an error in the /tmp/sudoers.new file then visudo will prompt for user input so it is advisable to check it with visudo -c -f /tmp/sudoers.new first.


I think the most straight forward solution is to:

Create a script addsudoers.sh

#!/bin/sh

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

and call it with the users you want to add it as:

root prompt> ./addsudoers.sh user1 user2

For the full explanation see this answer: Adding users to sudoers through shell script

Regards!


Try to echo it. You have to run it in a subshell, though. Example:

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


This worked for me based off what others posted here. When i used other peoples script it would open visudo for me but would not make the edit. This made the edit i needed to allow all users, including standard users, to install java 7u17 for safari/firefox.

#!/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

This added the %everyone blah blah blah to the bottom of the sudoers file. I had to run the script like this.

sudo sh sudoersedit.sh

Good luck :D

참고URL : https://stackoverflow.com/questions/323957/how-do-i-edit-etc-sudoers-from-a-script

반응형