linux class(new) 2 페이지

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


회원로그인

linux class(new)

파일권한설정

페이지 정보

작성자 admin 작성일16-01-17 23:28 조회3,193회 댓글5건

본문

기본 퍼미션(Permission)
1. r(read)읽기 8진수로 4: 파일을 읽을 수 있다.
2. w(write)쓰기 8진수로 2 : 파일을 수정할 수 있다.
3. x(excute)실행 8진수로 1: 파일을 실행할 수 있다.
4.-(denied) : 권한없음.
---------------------------------------------------------------------------------------------
퍼미션 설정 예
-rwxrw-r-- 1 user4 root 1월17일 /tmp/testfile
ex) groups user1 user2 user3 user4
user1:user1 <-- other (제3자) 권한 : 읽을수 있다.
user2:user2,root <-- group 권한 : 읽고 수정할 수 있다
user3:root,user3 <-- group 권한 : 읽고 수정할 수 있다.
user4:root,user4 <-- owner 권한 : 읽고,수정,실행할수 있다
------------------------------------------------------------------------------------------------
*. 파일과 디렉토리 퍼미션의 차이점을 이해하기위해서 파일과 디렉토리의 속성을 이해해야 한다.
일반파일(정규파일) - 파일이 저장된 디스크의 주소(위치)에는
파일의 실제 내용 즉,아스키 코드값이나 바이너리 코드값이 저장되어 있다.

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

파일에 대한 소유자 및 그룹변경은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 퍼미션의 개념과 비슷하다.
파일의 소유그룹권한으로 실행할 수 있는 권한이다.
-----------------------------------------------------------------------------------
디폴트 퍼미션
파일이나 디렉토리 생성시 설정되어 있는 권한.
디폴트 퍼미션은 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.ifconfig 를 puser1,puser2
두사람한테만 실행권한 주고 나머지
모든 사람들을 실행 못하게  퍼미션 설정
(whereis ifconfig)
----------------------------------
2.
cal 는  puser2,puser3 만 실행못하게 하고
나머지 모든 사용자는 실행할수  있게 퍼미션 설정
있게.

참고 명령어 -
groupadd / usermod -g 주그룹 계정명
        usermod -G 보조그룹,.. 계정명
chmod / chown / chgrp

mycolor님의 댓글

mycolor 작성일

3. sysuser1,sysuser2 두명한테만 시스템을 종료할수 있는 권한
(shutdown -h +10)부여
작업완료후 확인방법 ->
su - sysuser1(sysuser2) -> shutdown -h +10 ; 실행되는지 확인.
(*. root 계정 이외에는 sysuser1 과 sysuser2 만 시스템을
종료할수 있도록 설정해야 한다)

4.  user2 계정의 디폴트 퍼미션을 아래처럼 되도록 umask 를 설정.
파일생성시 : rw--------  (640)
디렉토리생성시:rwx------- (750)
--> 작업완료후 확인은 --> user2 계정으로 임의의 파일 및 디렉토리
생성후 확인(ls -l 로 확인)

mycolor님의 댓글

mycolor 작성일

1번,2번문제 풀이
useradd puser1 ~ puser5
ifconfig : puser1,puser2 만 실행가능
  나머지 모든계정은  실행금지

cal :puser2,puser3만실행금지
  나머지 모든계정은 실행가능(o)

답) 아래처럼 설정하면 됩니다.
-rwxr-x---. 1 root pgroup 73936 2012-05-10 17:17 /sbin/ifconfig 또는
-rwx--xr--. 1 root pgroup 73936 2012-05-10 17:17 /sbin/ifconfig
(실행 못하게 하려면 r은 상관없고 x 권한만 없으면 됨)
groups puser1 puser2
puser1:pgroup 또는 puser1:puser1,pgroup (보조그룹이어도 상관없음, 아래도 마찬가지)
puser2:pgroup

-rwx---r-x. 1 root pgroup2 ....  /usr/bin/cal 또는
-rwxr--r-x. 1 root pgroup2....  /usr/bin/cal
groups puser2 puser3
puser2:pgroup,pgroup2
puser3:pgroup2

guest1님의 댓글

guest1 작성일

p.296
01:  ls  -l
02: r:  파일 내용을 읽을수 있는권한
    w: 파일 내용을 수정할수 있는권한
    x: 파일은 실행권한,
        디렉토리는 access 권한
ㄱ\
03:  파일 내용을 읽을수 있는권한(cat,  편집기사용)
      디렉토리 내용을 읽을수 있는 권한,
      (파일 목록을 권한 볼수 있는 권한  ls 사용)
04 ;  cd  로 들어갈수 없다.
      디렉토리 안에 있는 파일을 실행못함.
      파일이 생성 안되고, 삭제도 안되고,
      ls -l 파일리스트를 볼수가 없다.
05;  rwx (소유자)  r-x(그룹)  r-x(기타사용자권한)

06:  rw-  (소유자) r-- (그룹)  r-- (기타사용자권한)
07:  rw-r------  640
08: rwxr-xr-x --> r-x-------
        chmod  u-w,g-rx,o= 파일명
11  rwSr--r-- ; 소유자 권한에 x 가 없음
                  8진수인경우 4644 가 됨.
12  파일을 소유자가 아니면 삭제금지
13  find  /usr/bin  -perm -4000  -ls

admin님의 댓글

admin 작성일

setfacl
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...

주요옵션
-m : acl 설정
-x : acl 삭제
-b : 모든 acl 삭제
--mask : mask 재설정
-R : 하위디렉토리까지 포함.

[root@centos1 acl]# setfacl -m u:user1:rwx a.txt
[root@centos1 acl]# ls -l a.txt
-rw-rwxr--+ 1 root root 6  8월  5 08:50 a.txt ; acl 설정이 된 경우 퍼미션 맨끝에 '+' 문자가 보인다.
[root@centos1 acl]# getfacl a.txt  ; acl 설정확인을 할 수 있다.
# file: a.txt
# owner: root
# group: root
user::rw-
user:user1:rwx
group::r--
mask::rwx
other::r--

[root@centos1 acl]# setfacl -x u:user1 a.txt ; acl 설정중 user1 권한 삭제
[root@centos1 acl]# setfacl -b a.txt ; 모든 acl 설정 삭제.

[root@centos1 acl]# setfacl -m u:user1:r-x,u:user2:rw a.txt
[root@centos1 acl]# ls -l a.txt
-rw-rwxr--+ 1 root root 6  8월  5 08:50 a.txt ; 파일의 그룹권한이 바뀐다.
[root@centos1 acl]#

[root@centos1 acl]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
user:user1:r-x
user:user2:rw-
group::r--
mask::rwx          ; mask 값은 파일그룹권한과 같다.
other::r--

[root@centos1 acl]#
[root@centos1 acl]# chmod 755 a.txt ; acl 설정이 된 파일의 퍼미션을 바꾸면...
[root@centos1 acl]# ls -l a.txt
-rwxr-xr-x+ 1 root root 6  8월  5 08:50 a.txt
[root@centos1 acl]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rwx
user:user1:r-x
user:user2:rw-                  #effective:r--    ; effective 권한이 실제권한이다.
group::r--                                              effective 권한은 설정권한과 mask 값의 교집합으로 결정된다.
mask::r-x                 
other::r-x

[root@centos1 acl]# setfacl -b a.txt
[root@centos1 acl]# setfacl -m u:user1:rwx,g:staff:r-x,g:root:rwx,m:r-x a.txt
[root@centos1 acl]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rwx
user:user1:rwx                  #effective:r-x
group::r--
group:root:rwx                  #effective:r-x
group:staff:r-x
mask::r-x
other::r-x
[root@centos1 acl]#

[root@centos1 acl]# mkdir d1
[root@centos1 acl]# setfacl -m default:u:user1:rw-,g:staff:rwx d1 
default 옵션을 주면 d1 디렉토리내의 파일과 디렉토리는 acl권한이
자동으로 설정된다.
[root@centos1 acl]# getfacl d1
# file: d1
# owner: root
# group: root
user::rwx
group::r-x
group:staff:rwx
mask::rwx
other::r-x
default:user::rwx
default:user:user1:rw-
default:group::r-x
default:mask::rwx
default:other::r-x
[root@centos1 acl]#

[root@centos1 d1]# echo "hello" > a.txt
[root@centos1 d1]# ls -l
합계 8
-rw-rw-r--+ 1 root root 6  8월  5 09:15 a.txt
[root@centos1 d1]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
user:user1:rw-
group::r-x                      #effective:r--
mask::rw-
other::r--

[root@centos1 d1]#

* 디렉토리 생성시 defuatl acl mask 값은
777
설정할때의  acl mask의 교집합으로 결정된다.
*. 파일생성시 default acl의 mask 값은
666 과 설정할때의  acl mask의 교집합으로 결정된다.


===============================================
ACL 설정 예제.
1.
/tmp/acl/a.txt  파일을 하나 생성하고 아래처럼 acl 권한설정

설정권한            effective 권한
user1:rwx  ----------- r-x
ac1:r--   
acgroup1:r-x
acgroup2:rw- --------- r--
------------------------------------------------------------------

2. /tmp/acl/project 디렉토리에(Default ACL로 설정)
*.file 생성하였을때
설정권한      effective 권한
user1:rwx      r--
*.Directory 생성하였을때
설정권한          effective 권한
user1:rwx          r-x


접속자집계

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