IT 전용글/Linux

파일권한(Permission)

회상형인간 2008. 11. 27. 18:03

퍼미션(Permission)

1. r(read)읽기 8진수로 4: 파일을 읽을 수 있고, 디렉토리의 내용을 볼 수 있다.
2. w(write)쓰기 8진수로 2 : 파일에 저장 및 삭제, 디렉토리에 파일저장, 디렉토리의 이름 변경, 삭제를 할 수 있다.
3. x(excute)실행 8진수로 1: 파일을 실행, 디렉토리 access 가 가능하다.

[lee@river lee]$ ls -l /etc/passwd
-rw-r--r--    1 root     root         3197  5월  4 12:57 /etc/passwd
여기서 첫 번째의 root 는 이 파일에 대한 소유자를 뜻한다.
두 번째 root 이 파일에 대한 소유그룹을 뜻한다..

[lee@river lee]$ groups lee
bin
[lee@river lee]$ head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

lee 와 bin 그룹에 속해있는 lee 사용자가 /etc/passwd 파일에 대해서 행사할 수 있는 권한은 제3자의 권한이다.
제3자가 /etc/passwd 파일에 대해 가질 수 있는 권한은 "r" 이므로 읽기가 가능하다.

[lee@river lee]$ ls -l /etc/shadow
-r--------    1 root     root         2764  5월  4 12:57 /etc/shadow

[lee@river lee]$ head -2 /etc/shadow
head: /etc/shadow: 허가 거부됨
당연히 볼 수 없다. shadow 파일은 소유자만 읽을 수 있는 퍼미션으로 되어있다.

-rwxr-xr--    1 root     root        11217  2월 11  2003 makewhatis
이 파일에 대해서는 lee 사용자가 실행할 수 없다.(제3자권한에 x 퍼미션이 없으므로)

[lee@river lee]$ /usr/sbin/makewhatis
-bash: /usr/sbin/makewhatis: 허가 거부됨

[lee@river lee]$ ls -ld /root
drwxr-x---    4 root     root         4096  5월  4 13:07 /root
디렉토리 파일의 r 권한은 디렉토리내의 파일리스트를 출력할 수 있는 권한을 뜻한다.
디렉토리 파일의 w 권한은 디렉토리내에서 파일을 생성하거나 삭제하거나 이름을 변경할 수 있는 권한을 뜻한다.
디렉토리 파일의 x 권한은 디렉토리를 access 할 수 있는 권한을 뜻한다.

[lee@river lee]$ cd /root
-bash: cd: /root: 허가 거부됨 (디렉토리에 "x" 권한이 없으므로)
[lee@river lee]$ ls -l /root
ls: /root: 허가 거부됨 (디렉토리에 "r" 권한이 없으므로)
[lee@river lee]$ touch /root/a.txt
touch: creating `/root/a.txt': 허가 거부됨 (디렉토리에 "w" 권한이 없으므로)

퍼미션 변경
chmod(change mode)

1. chhmod octal mode file 명

ex)[root@river tmp]# chmod 654 a.txt
[root@river tmp]# ls -l a.txt
-rw-r-xr--    1 root     root            5  5월  6 21:30 a.txt

[root@river tmp]# chmod 203 a.txt

[root@river tmp]# ls -l a.txt
--w-----wx    1 root     root            5  5월  6 21:30 a.txt
[root@river tmp]#

2. chmod  symbolic mode file명
u : user
g: group
o: other
a: all

symmbolic mode 에는 세 개의 연산자(+, - , =)가 사용될 수 있다.

+ : 특정권한 추가
- : 특정권한 제거
= : 특정권한 변경

[root@river tmp]# ls -l a.txt
--w-----wx    1 root     root            5  5월  6 21:30 a.txt
[root@river tmp]# chmod u=rx,g=rw,o=x a.txt
[root@river tmp]# ls -l a.txt
-r-xrw---x    1 root     root            5  5월  6 21:30 a.txt

[root@river tmp]# chmod a-x a.txt ( a-x : u-x,g-x,o-x 와 같다)

[root@river tmp]# ls -l a.txt
-r--rw----    1 root     root            5  5월  6 21:30 a.txt

[root@river tmp]# ls -l a.txt
-r--rw----    1 root     root            5  5월  6 21:30 a.txt

[root@river tmp]# chmod u=rwx,g-r,o+x a.txt

[root@river tmp]# ls -l a.txt
-rwx-w---x    1 root     root            5  5월  6 21:30 a.txt

[root@river tmp]#

파일에 대한 소유자 및 그룹변경은chown, chgrp 명령어로 한다.

chown 소유자 file명
chgrp 그룹 file명 또는
chown 소유자:그룹 file 명

ex)[root@river tmp]# ls -l a.txt
-rwx-w---x    1 lee      root            5  5월  6 21:30 a.txt

[root@river tmp]# chgrp sys a.txt

[root@river tmp]# ls -l a.txt
-rwx-w---x    1 lee      sys             5  5월  6 21:30 a.txt

[root@river tmp]# chown root:bin a.txt

[root@river tmp]# ls -l a.txt
-rwx-w---x    1 root     bin             5  5월  6 21:30 a.txt

[root@river tmp]#

그 외에 아래와 같은 세 가지 퍼미션이 더 있다.

1. s(set UID) (4nnn n은 8진수) : --s-------  
소유자 권한에 s 가 있으면 setUID 퍼미션이다.(실행하는 동안 소유자 권한을 갖는다)
2. s(set GID) (2nnn): ------s---  :
그룹 권한에 s 가 있으면 setGID 퍼미션이다.(실행하는 동안 그룹권한을 갖는다)
3. t(sticky bit) (1nnn): --------t :
3자권한에 t 가 있으면 sticky bit 퍼미션이다.(파일을 만들 수 있다. 그러나 파일소유자만 파일을 삭제할 수 있다)

sticky bit 퍼미션
[root@river var]# mkdir share

[root@river var]# ls -ld share
drwxr-xr-x    2 root     root         4096  5월  6 20:22 share
[root@river var]# ls -ld share
drwxrwxrwx    2 root     root         4096  5월  6 20:22 share

[lee@river lee]$ id
uid=500(lee) gid=500(lee) groups=500(lee),1(bin)

[lee@river lee]$ ls -l /var/share/a.txt
-rw-------    1 root     root            5  5월  6 20:27 /var/share/a.txt

[lee@river lee]$ rm -f /var/share/a.txt

[lee@river lee]$ ls -l /var/share/a.txt
ls: /var/share/a.txt: 그런 파일이나 디렉토리가 없음

share 디렉토리에 모든 사용자가 자신의 파일을 저장하고 사용하게 하려면 777 퍼미션을 줘야 한다.
그러나 퍼미션이 777 이면 디렉토리내의 파일명을 생성, 수정 그리고 삭제할 수 있는 권한을 갖게 되므로
문제가 된다. (자신이 만든 파일 퍼미션을 어떤 식으로 하더라고 다른 사람이 삭제할 수 있게 된다)
이럴 때 사용되는 퍼미션이 sticky bit 퍼미션이다.
sticky bit 퍼미션이 설정되어 있으면 소유자 외에는 파일을 삭제할 수 없게 되므로 자신의 파일을
다른 사용자로부터 보호할 수 있다.

[root@river var]# chmod 1777 share

[root@river var]# ls -ld share
drwxrwxrwt    2 root     root         4096  5월  6 20:22 share

[root@river var]# su - lee

[lee@river lee]$ whoami
lee

[lee@river lee]$ ls -ld /var/share
dwxrwxrwt    2 root     root         4096  5월  6 20:30 /var/share

[lee@river lee]$ ls -l /var/share/a.txt
-rw-------    1 root     root            5  5월  6 20:30 /var/share/a.txt

[lee@river lee]$ rm -f /var/share/a.txt
rm: cannot remove `/var/share/a.txt': 명령이 허용되지 않음[lee@river lee]$

* 아래의 두 개의  퍼미션은 Process 에 대한 이해가 필요하다.

setUID 퍼미션

보안과 관련되어 있는 퍼미션으로 이 퍼미션을 잘못관리하면 보안상 문제가 발생할 수 있다.
소유자 권한으로 파일을 실행할 수 있다.

[lee@river lee]$ id
uid=500(lee) gid=500(lee) groups=500(lee),1(bin)
[lee@river lee]$

[lee@river lee]$ find /root -name .bashrc
find: /root: 허가 거부됨

프로세스의 권한은 사용자 권한을 그대로 따른다 root 사용자는 /root 디렉토리에 읽고,쓰고,실행할 수 있는
권한을 가지고 있다. 그러면 root 가 실행한 프로세스도 마찬가지다.
그리고 lee 사용자는 /root 디렉토리에 대해 아무런 권한도 없으므로 lee 사용자가 실행한 find 프로세스도
마찬가지이다.

예를 들면
[lee@river lee]$ sleep 30 &
[2] 31083

[lee@river lee]$ ps -aux | grep sleep
lee      31083  0.0  0.1  4692  528 pts/1    S    21:03   0:00 sleep 30
lee      31086  0.0  0.1  4668  652 pts/1    S    21:03   0:00 grep sleep
[lee@river lee]$sleep 프로세스의 소유자는 프로세스를 실행한 lee가 된다.그러나 sleep 프로세스에
set uid 퍼미션이 설정되어 있다면
ls -l /bin/sleep
-rwsr-xr-x    1 root     root        12444  2월 19  2003 /bin/sleep
[lee@river lee]$ sleep 30 &
[1] 31133

[lee@river lee]$ ps -aux | grep sleep
root     31133  0.0  0.1  4692  528 pts/1    S    21:06   0:00 [sleep]
[lee@river lee]$프로세스 소유자가 lee가 아니라 root 인 것을 알 수 있다.

이제 find 명령어에 setuid 퍼미션을 설정하고 다시 테스트해보자

[root@river root]# whereis find
find: /usr/bin/find /usr/share/man/man1/find.1.gz

[root@river root]# chmod 4755 /usr/bin/find

[root@river root]# ls -l /usr/bin/find
-rwsr-xr-x    1 root     root        51028  1월 25  2003 /usr/bin/find
[root@river root]#

find 명령어에 setuid 퍼미션이 설정되었으며 소유자는 현재 root로 되어 있다.

[root@river root]# su - lee

[lee@river lee]$ find /root -name .bashrc
/root/.bashrc
[lee@river lee]$

find 프로세스가 root 권한으로 실행되므로 처음과 달리 lee 사용자가 /root 아래에 있는
.bashrc 파일을 검색할 수 있게 되었다.

setUID 퍼미션은 왜 필요한가?

예를들면

모든 사용자의 비밀번호는 /etc/shadow 라는 파일에 저장되어 있다.
사용자가 자신의 패스워드를 변경한다면 그 변경된 패스워드는 shadow 파일에 기록이 되어져야만 한다.
그렇다고 shadow 파일을 수정할 수 있는 권한을 준다면 당연히 보안상 문제가 발생한다.
setuid 퍼미션은 그럴 때 필요한 것이다. 사용자가  root 권한이 필요한 파일에 엑세스 해야 하는경우 
setuid 퍼미션이 필요하다.

setGID 퍼미션

setGID 퍼미션의 개념은 setUID 퍼미션의 개념과 다르지 않다.
파일의 소유 그룹권한으로 실행할 수 있는 권한이다.

'IT 전용글 > Linux' 카테고리의 다른 글

리눅스 서버 시간 확인 및 동기화 명령어~  (0) 2021.01.26
Process 관리  (0) 2008.11.27
사용자 계정 관리  (0) 2008.11.27
vi 편집기  (0) 2008.11.27
리눅스 기본 명령어  (0) 2008.11.27