리눅스에서 특정 줄에 내용을 삽입하기 위해 조건부 판단을 할 수 있나요?

리눅스에서 특정 줄에 내용을 삽입하기 위해 조건부 판단을 할 수 있나요?

리눅스에서 특정 줄에 내용을 삽입하기 위해 조건부 판단을 할 수 있나요?

예를 들어, 루트로 전환할 수 있도록 사용자 "test"를 /etc/sudoers에 추가하고 싶습니다.

  1 #
  2 # This file MUST be edited with the 'visudo' command as root.
  3 #
  4 # Please consider adding local content in /etc/sudoers.d/ instead of
  5 # directly modifying this file.
  6 #
  7 # See the man page for details on how to write a sudoers file.
  8 #
  9 Defaults        env_reset
 10 Defaults        mail_badpass
 11 Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
 12 
 13 # Host alias specification
 14 
 15 # User alias specification
 16 
 17 # Cmnd alias specification
 18 
 19 # User privilege specification
 20 root    ALL=(ALL:ALL) ALL
 21 
 22 # Members of the admin group may gain root privileges
 23 %admin ALL=(ALL) ALL
 24 
 25 # Allow members of group sudo to execute any command
 26 %sudo   ALL=(ALL:ALL) ALL
 27 
 28 # See sudoers(5) for more information on "#include" directives:
 29 
 30 #includedir /etc/sudoers.d

내 명령이 행 20(if root ALL=(ALL:ALL) ALL )을 찾고 행 21에 아래 내용을 추가하도록 하고 싶습니다.

test    ALL=(ALL:ALL) ALL

Linux 명령으로 이를 수행할 수 있습니까? 아니면 Perl이나 Python과 같은 코딩을 통해서만 수행할 수 있습니까?

저는 Linux를 처음 접했습니다. 도움을 주시면 감사하겠습니다!

답변1

줄을 어디에 추가하든 상관 없으며 sudo는 순서에 신경 쓰지 않습니다.

끝에 추가하면 됩니다.

echo 'test    ALL=(ALL:ALL) ALL' | sudo tee -a /etc/sudoers

또는 더 나은 방법은 include 디렉토리를 사용하는 것입니다.

echo 'test    ALL=(ALL:ALL) ALL' | sudo tee /etc/sudoers.d/test

이렇게 하면 패키지 업데이트 중에 sudoers 파일이 변경될 때 수동으로 업데이트할 필요가 없습니다.

관련 정보