linux class 2 페이지

본문 바로가기
사이트 내 전체검색


회원로그인

linux class

파일권한

페이지 정보

작성자 admin 작성일15-12-23 12:16 조회223회 댓글1건

본문

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

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

*. 파일과 디렉토리의 퍼미션을 이해하려면 파일과 디렉토리의
속성과 차이점을 먼저 이해를 해야 한다.

일반파일(정규파일) - 파일이 저장된 디스크의 주소(위치)에는
파일의 실제 내용 즉,아스키 코드값이나 바이너리 코드값이 저장되어 있다.

디렉토리(디렉토리 파일) - 디렉토리가 저장된 디스크 주소(위치)에는
파일명과 파일의 실제내용이 저장된 주소값만이 저장되어 있다.
파일내용은 디렉토리에 저장되지 않는다.

*. 파일과 디렉토리의 주소값은 ls -i 옵션으로 볼수 있음.

파일 및 디렉토리 권한

퍼미션 파일권한 디렉토리권한

-----------------------------------------------------------------------------------------------------

r 파일내용을 볼수 있음 디렉토리 내용을 볼수 있음

cat,vi 명령어로. ls 명령어로.

------------------------------------------------------------------------------------------------------

w 파일내용을 수정할수 있음 디렉토리 내용을 수정할 수 있음

편집기, > , >> 파일이름변경,파일생성,파일삭제 가능

------------------------------------------------------------------------------------------------------

x 파일을 실행할 수 있음 디렉토리를 실행할 수 있음

ex) ./testfile (디렉토리 access 가능)

ex) cd /etc

------------------------------------------------------------------------------------------------------

*. 디렉토리에 읽기권한이나 쓰기권한을 주려면 디렉토리에

x 권한을 추가로 설정해 줘야 한다.




[lee@river lee]$ groups lee
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 파일은 소유자만 읽을 수 있는 퍼미션으로 되어있다.

[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)
- 퍼미션 설정변경은 8진수 모드와 symbolic mode 두가지로
설정할 수 있다.

1. chmod octal mode(8진수 모드) file 명

rwxrw-r-x --> 이것을 8진수 모드로 표현하면
421420401 <-- 이렇게 된다.
이 숫자를 세개씩 끊어서 다 더하면 765 가 됨.

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 <-- user,group,other 를 다 포함한다.

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 <== 이것은 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 -ef | 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 -ef | 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 퍼미션의 개념과 다르지 않다.
파일의 소유 그룹권한으로 실행할 수 있는 권한이다.

*.참고사항.
아래는 bash 메뉴얼 페이지의 일부내용이다.
--------------------------------------------------------------------------------------------
setuid 퍼미션을 테스트할때 알아두면 도움이 된다.

-p Turn on privileged mode. In this mode, the $ENV and
$BASH_ENV files are not processed, shell functions are
not inherited from the environment, and the SHELLOPTS
variable, if it appears in the environment, is ignored.
If the shell is started with the effective user (group)
id not equal to the real user (group) id, and the -p
option is not supplied, these actions are taken and the
effective user id is set to the real user id. If the -p
option is supplied at startup, the effective user id is
not reset. Turning this option off causes the effective
user and group ids to be set to the real user and group
ids.

set [-o 옵션] [인수...]
-p : privileged 모드를 켠다. 이모드에서는 $ENV 파일을 처리하지 않으며
셀함수를 환경으로부터 상속하지 않는다. 유효사용자(그룹) id와 실제 사용자(그룹) id 가 일치하지
않으면 시동할 때 자동으로 작동한다. 이옵션을 끄면 유효사용자, 그룹 id를 실제 사용자, 그룹id로
설정한다.
--------------------------------------------------------------------------------------------

*.디폴트 퍼미션
파일이나 디렉토리 생성시 설정되어 있는 권한.
디폴트 퍼미션은 umask 값으로 결정된다.
아래계산방법으로 결정된다(공식임)


파일인경우 디렉토리인경우

666 777

bit& umask값의 1의보수 bit& umask값의 1의보수

-------------------------------- --------------------------------

디폴트 퍼미션 디폴트 퍼미션




bit& 연산은 이진연산이므로 8진수를 이진수로
바꿔서 계산해야 한다.


*. umask 값이 022(또는 0022) 일때의 디폴트 퍼미션?
666(8진수) --- 이진수 변환 --> 110 110 110(2진수)
022(8진수) --- 이진수 변환 --> 000 010 010(2진수)
000 010 010 -- 1의보수로 변환 --> 111 101 101(1의보수)
*. 1의 보수는 0은 1로 1은 0으로 서로 반대로 바꾸면 된다.


==> 110 110 110
bit&111 101 101
-------------
110 100 100 ==> 644

*. bit& 연산은 피연산자가 둘다 1일때 결과값이 1이되는 연산자.
ex)
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0


*. 디렉토리도 같은 방법으로 계산하면 umask값이 022 일때
디렉토리 퍼미션은 755 가 됨.

------------------------------------------------------------------------------
*. 디폴트 퍼미션을 계산하기 위한 방법 2.
- 공식적인 방법은 아니지만 아래처럼 쉽게 할수 있는 방법도 있다.

a. file 인경우 :

666
-umask
----------
디폴트 파일 퍼미션.

b. directory 인 경우
777
-umask
----------
디폴트 디렉토리 퍼미션

c. binary file 인경우(이 경우는 계산방법이 디렉토리와 같다)
777
-umask
----------
디폴트 파일퍼미션(바이너리 파일)

*. 위에 방법으로 계산된 umask 값이 file 인 경우와 directory 인경우에
동시에 적용되지 않으면 directory 계산값 기준으로 하면 됨.

댓글목록

mycolor님의 댓글

mycolor 작성일

퍼미션 실습 예제.
권한설정.
1. /tmp/test/a.txt
user2(rwx)
tuser1,tuser2(rw-)
나머지 모든 계정(r--)
-------------------------------------
solve)
groupadd testgroup(그룹이름은 마음대로)
usermod -g testgroup tuser1
usermod -g testgroup tuser2
cd /tmp/test
chgrp testgroup a.txt
chmod 764 a.txt

---------------------------------------
2. /sbin/ifconfig
puser2,puser3 (두사람만 실행할수있게)
-------------------------------------
groupadd pgroup(그룹이름은 마음대로)
usermod -g pgroup puser2
usermod -g pgroup puser3
chgrp pgroup /sbin/ifconfig
chmod o-x /sbin/ifconfig
------------------------------------
3. /usr/bin/cal
puser3,puser4(두사람만 실행못하게)

---------------
solve)
groupadd pgroup2
usermod -G pgroup2 puser3
usermod -g pgroup2 puser4
chgrp pgroup2  /usr/bin/cal
chmod g-x  /usr/bin/cal

* groupadd / useradd / usermod
usermod(add) -g 주그룹(-G 보조그룹,..)
chmod / chown / chgrp ...


접속자집계

오늘
61
어제
75
최대
611
전체
448,111
개인정보취급방침 서비스이용약관 twoseven.kr All rights reserved.
상단으로