"리눅스 sudo 패스워드 없이 사용"의 두 판 사이의 차이

 
(다른 사용자 한 명의 중간 판 하나는 보이지 않습니다)
7번째 줄: 7번째 줄:
==방법==
==방법==
*[[/etc/sudoers]] 파일에 맨아래(★)에 다음 형식으로 내용 추가
*[[/etc/sudoers]] 파일에 맨아래(★)에 다음 형식으로 내용 추가
<source lang='bash'>
<syntaxhighlight lang='bash'>
사용자명 ALL=NOPASSWD: ALL
사용자명 ALL=NOPASSWD: ALL
</source>
</syntaxhighlight>
:→ 여기서 <code>NOPASSWD:</code>를 빼면 sudo 실행시 자신의 패스워드를 입력해야만 함
:→ 여기서 <code>NOPASSWD:</code>를 빼면 sudo 실행시 자신의 패스워드를 입력해야만 함
<source lang='bash'>
<syntaxhighlight lang='bash'>
사용자명 ALL=NOPASSWD: 명령어1, 명령어2
사용자명 ALL=NOPASSWD: 명령어1, 명령어2
</source>
</syntaxhighlight>
:→ 지정한 명령어들만 sudo 사용가능
:→ 지정한 명령어들만 sudo 사용가능


==실습 1: 모든 명령어 사용가능==
==실습 1: 모든 명령어 사용가능==
*신규 계정 testuser1 생성
*신규 계정 testuser1 생성
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost ~]# useradd testuser1
[root@localhost ~]# useradd testuser1
[root@localhost ~]# echo 'P@ssw0rd1' | passwd --stdin testuser1
[root@localhost ~]# echo 'P@ssw0rd1' | passwd --stdin testuser1
25번째 줄: 25번째 줄:
[root@localhost ~]# cat /etc/passwd | grep testuser1
[root@localhost ~]# cat /etc/passwd | grep testuser1
testuser1:x:500:500::/home/testuser1:/bin/bash
testuser1:x:500:500::/home/testuser1:/bin/bash
</source>
</syntaxhighlight>


*testuser1 sudo 권한 추가(+NOPASSWD)
*testuser1 sudo 권한 추가(+NOPASSWD)
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost ~]# echo 'testuser1 ALL=NOPASSWD: ALL' >> /etc/sudoers
[root@localhost ~]# echo 'testuser1 ALL=NOPASSWD: ALL' >> /etc/sudoers
[root@localhost ~]# cat /etc/sudoers | tail -2
[root@localhost ~]# cat /etc/sudoers | tail -2
#includedir /etc/sudoers.d
#includedir /etc/sudoers.d
testuser1 ALL=NOPASSWD: ALL
testuser1 ALL=NOPASSWD: ALL
</source>
</syntaxhighlight>


*testuser1 계정으로 sudo 실행
*testuser1 계정으로 sudo 실행
<source lang='console'>
<syntaxhighlight lang='console'>
[root@localhost ~]# su - testuser1
[root@localhost ~]# su - testuser1
[testuser1@localhost ~]$ reboot
[testuser1@localhost ~]$ reboot
42번째 줄: 42번째 줄:
[testuser1@localhost ~]$ sudo reboot
[testuser1@localhost ~]$ sudo reboot
The system is going down for reboot NOW!
The system is going down for reboot NOW!
</source>
</syntaxhighlight>
:→ 패스워드 입력 없이 sudo reboot 가능
:→ 패스워드 입력 없이 sudo reboot 가능


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


==같이 보기==
==같이 보기==
* [[리눅스 계정에 특정 명령어 root 실행 권한 주기]]
* [[/etc/sudoers]]
* [[sudo]]
* [[sudo]]
* [[sudo -i]]
* [[sudo -i]]
* [[/etc/sudoers]]
* [[리눅스 계정에 특정 명령어 root 실행 권한 주기]]


==주석==
==주석==

2023년 9월 29일 (금) 16:06 기준 최신판

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 }}