리눅스 sudo 패스워드 없이 사용

Linux sudo 패스워드 없이 사용
sudo 패스워드 입력 없이 사용
패스워드 물어보지 않고 sudo 실행
sudoers 등록

1 방법[ | ]

  • /etc/sudoers 파일에 맨아래(★)에 다음 형식으로 내용 추가
사용자명 ALL=NOPASSWD: ALL
→ 여기서 NOPASSWD:를 빼면 sudo 실행시 자신의 패스워드를 입력해야만 함
사용자명 ALL=NOPASSWD: 명령어1, 명령어2
→ 지정한 명령어들만 sudo 사용가능

2 실습 1: 모든 명령어 사용가능[ | ]

  • 신규 계정 testuser1 생성
[root@localhost ~]# useradd testuser1
[root@localhost ~]# echo 'P@ssw0rd1' | passwd --stdin testuser1
Changing password for user testuser1.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# cat /etc/passwd | grep testuser1
testuser1:x:500:500::/home/testuser1:/bin/bash
  • testuser1 sudo 권한 추가(+NOPASSWD)
[root@localhost ~]# echo 'testuser1 ALL=NOPASSWD: ALL' >> /etc/sudoers
[root@localhost ~]# cat /etc/sudoers | tail -2
#includedir /etc/sudoers.d
testuser1 ALL=NOPASSWD: ALL
  • testuser1 계정으로 sudo 실행
[root@localhost ~]# su - testuser1
[testuser1@localhost ~]$ reboot
reboot: Need to be root
[testuser1@localhost ~]$ sudo reboot
The system is going down for reboot NOW!
→ 패스워드 입력 없이 sudo reboot 가능

3 실습 2: 지정한 명령어만 사용가능[ | ]

  • vi /etc/sudoers 또는 visudo
[root@localhost ~]# vi /etc/sudoers
변경 전
... (생략)
#includedir /etc/sudoers.d
testuser1 ALL=NOPASSWD: ALL
변경 후
... (생략)
#includedir /etc/sudoers.d
testuser1 ALL=NOPASSWD: /usr/sbin/useradd, /usr/sbin/userdel
[root@localhost ~]# cat /etc/sudoers | tail -2
#includedir /etc/sudoers.d
testuser1 ALL=NOPASSWD: /usr/sbin/useradd, /usr/sbin/userdel
  • testuser1 계정으로 sudo 실행 테스트
[root@localhost ~]# su - testuser1
[testuser1@localhost ~]$ sudo reboot
[sudo] password for testuser1: 
Sorry, user testuser1 is not allowed to execute '/sbin/reboot' as root on localhost.localdomain.
→ sudo reboot에 대해서는 패스워드 물어본다.
→ 자신의 패스워드(P@ssw0rd1)를 정확히 입력해도 권한없어 실행불가
[testuser1@localhost ~]$ sudo useradd mallory
[testuser1@localhost ~]$ cat /etc/passwd | grep mallory
mallory:x:501:501::/home/mallory:/bin/bash
→ sudo useradd에 대해서는 패스워드 입력 없이 실행가능

4 같이 보기[ | ]

5 주석[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}