- 리눅스 사용자에게 특정 명령어를 root 권한으로 실행할 수 있게 하기
- 리눅스 계정에 특정 명령어 root 권한 실행할 수 있게 하기
- 리눅스 계정에 특정 명령어 root 실행 권한 주기
- /etc/sudoers
- user1 is not in the sudoers file. This incident will be reported.
1 방법[ | ]
Bash
Copy
vi /etc/sudoers
2 실습[ | ]
- testuser 계정으로 아파치와 MySQL을 제어할 수 있게 해보자.
- 적용 후에도 시작/종료 등의 권한만 있을 뿐, 프로세스 실제 소유자는 root가 된다.[1]
2.1 사전 확인[ | ]
Console
Copy
[root@zetawiki ~]# ps -ef | grep httpd
root 8026 1 0 11:40 ? 00:00:00 httpd -k start
apache 8047 8026 0 11:41 ? 00:00:00 httpd -k start
apache 8048 8026 0 11:41 ? 00:00:00 httpd -k start
apache 8049 8026 0 11:41 ? 00:00:00 httpd -k start
apache 8050 8026 0 11:41 ? 00:00:00 httpd -k start
apache 8051 8026 0 11:41 ? 00:00:00 httpd -k start
apache 8052 8026 0 11:41 ? 00:00:00 httpd -k start
apache 8053 8026 0 11:41 ? 00:00:00 httpd -k start
apache 8054 8026 0 11:41 ? 00:00:00 httpd -k start
apache 8525 8026 0 13:05 ? 00:00:00 httpd -k start
apache 8557 8026 0 13:11 ? 00:00:00 httpd -k start
root 8565 7814 0 13:12 pts/0 00:00:00 grep httpd
- → 현재 httpd는 root가 시작한 상태.
2.2 계정 생성[ | ]
Console
Copy
[root@zetawiki ~]# useradd testuser
[root@zetawiki ~]# echo 'P@ssw0rd' | passwd --stdin testuser
Changing password for user testuser.
passwd: all authentication tokens updated successfully.
- → 실습을 위해 testuser 계정 생성하고 패스워드를 P@ssw0rd로 지정.
2.3 sudo 시도[ | ]
Console
Copy
[root@zetawiki ~]# su - testuser
[testuser@jmnote ~]$ sudo /usr/sbin/httpd -k stop
[sudo] password for testuser:
- → sudo로 root 권한 httpd 종료 시도. testuser의 패스워드를 물어본다.
Console
Copy
testuser is not in the sudoers file. This incident will be reported.
- → 패스워드가 맞더라도 위와 같이 sudoers에 설정되지 않아 수행 불가
2.4 권한 부여[ | ]
Console
Copy
[testuser@jmnote ~]$ logout
[root@zetawiki ~]# cp /etc/sudoers /etc/sudoers.20120613
[root@zetawiki ~]# echo 'testuser ALL=NOPASSWD:/usr/sbin/httpd, /etc/rc.d/init.d/mysqld' >> /etc/sudoers
[root@zetawiki ~]# tail -5 /etc/sudoers
## Allows members of the users group to shutdown this system
# %users localhost=/sbin/shutdown -h now
testuser ALL=NOPASSWD:/usr/sbin/httpd, /etc/rc.d/init.d/mysqld
- → sudoers 파일을 백업해두고, 맨아래에 한줄을 삽입하여 testuser 권한을 추가하고 확인
- → 일반적인 설정에서는 sudo를 할 때마다 자신(testuser)의 패스워드를 입력받게 되어 있으나, 위 예시처럼
NOPASSWD:
를 넣어주면 패스워드를 물어보지 않는다.
2.5 sudo 시도[ | ]
- 아파치 종료
Console
Copy
[root@zetawiki ~]# su - testuser
[testuser@jmnote ~]$ sudo /usr/sbin/httpd -k stop
[testuser@jmnote ~]$ ps -ef | grep httpd
testuser 8635 8584 0 13:14 pts/0 00:00:00 grep httpd
- → httpd 종료된 것 확인.
- 아파치 시작
Console
Copy
[testuser@jmnote ~]$ sudo /usr/sbin/httpd -k start
[testuser@jmnote ~]$ ps -ef | grep httpd
root 11901 1 6 09:57 ? 00:00:00 /usr/sbin/httpd -k start
apache 11902 11901 0 09:57 ? 00:00:00 /usr/sbin/httpd -k start
apache 11903 11901 0 09:57 ? 00:00:00 /usr/sbin/httpd -k start
apache 11904 11901 0 09:57 ? 00:00:00 /usr/sbin/httpd -k start
... (생략)
- → 아파치가 root 계정으로 실행된다. (sudoer는 권한을 위임한다.)
- MySQL 재시작
Console
Copy
[testuser@jmnote ~]$ sudo /etc/rc.d/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
2.6 원상복구[ | ]
실습 전 상황으로 되돌린다(root 권한으로 수행). sudoer 파일을 복구하고 백업본은 삭제. testuser 계정 삭제. 아파치, MySQL 재시작.
- 명령어
Bash
Copy
\cp /etc/sudoers.20120613 /etc/sudoers
rm -f /etc/sudoers.20120613
userdel -r testuser
service httpd restart
service mysqld restart
- 실행예시
Console
Copy
[root@zetawiki ~]# \cp /etc/sudoers.20120613 /etc/sudoers
[root@zetawiki ~]# rm -f /etc/sudoers.20120613
[root@zetawiki ~]# userdel -r testuser
[root@zetawiki ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@zetawiki ~]# service mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]
3 같이 보기[ | ]
4 주석[ | ]
- ↑ testuser가 아님. 즉 root가 실행한 것과 같음.
5 참고[ | ]
편집자 Jmnote Jmnote bot 175.123.120.197
로그인하시면 댓글을 쓸 수 있습니다.
- 분류 댓글:
- /etc (2)
- 리눅스 계정 (2)
리눅스 hosts 파일 ― 올해대박리눅스 hosts 파일 ― Jmnote