서버/Linux I 2015. 12. 24. 18:31

Linux I - 09. 파일 속성 관리 (퍼미션)

 

 

 

 

파일 정보에는 해당 파일의 소유권 정보가 포함되어 있다. 이때, 소유권 권한(퍼미션) 정보를 변경하면, 사용자 및 그룹에 대한 권한 정보만 변경되며, 그 외 나머지 파일 속성 정보는 변경되지 않고 그대로 유지된다.

 

이때, 퍼미션이란 리눅스 사용자들이 디렉토리 및 파일에 대한 읽기/쓰기/접근 허가권을 의미한다.

 

퍼미션

8진수

파일

디렉토리

 r  (read)

4

파일 내용 볼수 있음

Ex) cat, vi

디렉토리 내용 볼수 있음

Ex) ls

w (write)

2

파일 내용 수정 가능

Ex) 편집기, >, >>

디렉토리 내용 수정 가능

Ex) 파일이름 변경, 파일 생성/삭제

x (excute)

1

파일 실행 가능

Ex) ./testfile

디렉토리 실행 가능

디렉토리 엑세스 가능

Ex) cd /etc

- (denied)

0

거부

거부

 

 

Ex) 퍼미션 예제

 

[root@CentOS /root]# cd /
[root@CentOS /]#

[root@CentOS /]# mkdir test
[root@CentOS /]# cd test
[root@CentOS /test]# echo "hello ccie" > testfile

[root@CentOS /test]# ls -l
합계 4
-rw-r--r-- 1 root root 11 2016-11-10 11:36 testfile

[root@CentOS /test]# useradd ccie
[root@CentOS /test]# chmod 764 testfile

[root@CentOS /test]# ls -l testfile
-rwxrw-r--. 1 root root 11 2015-12-23 21:25 testfile


[root@CentOS /test]# chown ccie testfile
[root@CentOS /test]# ls -l testfile
-rwxrw-r--. 1 ccie root 11 2015-12-23 21:25 testfile

 

-rwxrw-r-- ccie root 11 2015-12-23 21:25 testfile

 [소유자][그룹][다른사용자]

 

 ccent:ccent

 other 권한

 읽기 가능

 ccna:ccna,root

 group 권한

 읽기, 수정 가능

 ccnp:root,ccnp

 group 권한

 읽기, 수정 가능

 ccie:root,ccie

 owner 권한

 읽기, 수정, 실행 가능

 

 -

파일 유형 ( - : 일반 파일, d : 디렉토리 파일)

 

 rwxrw-r--.

퍼미션 모드

chmod

 1

하드 링크 수

ln

ccie

소유자

chown

root

그룹

chgrp

11

파일 크기(Byte)

 

2015-12-23 21:25

생성 및 수정 시간

touch -t

testfile

파일 이름

mv

 

 

1. 'chown' 명령어

 

파일 소유권을 변경할때 사용하는 명령어이다.

 

[root@CentOS /test]# cat /etc/group | grep root
root:x:0:

[root@CentOS /test]# cat /etc/group | grep user1
user1:x:500:


 

Ex1) 'chown' 명령어 이해 (소유권을 변경한다.)

 

[root@CentOS test]# touch file1
[root@CentOS test]# ls -l file1
-rw-r--r--. 1 root root 0 2015-12-24 10:21 file1

[root@CentOS /test]# chown user1 file1
[root@CentOS /test]# ls -l file1
-rw-r--r--. 1 user1 root 0 2015-12-24 10:21 file1

[root@CentOS /test]# chown .user1 file1
[root@CentOS /test]# ls -l file1
-rw-r--r--. 1 user1 user1 0 2015-12-24 10:21 file1

[root@CentOS /test]# chown root.root file1
[root@CentOS /test]# ls -l file1
-rw-r--r--. 1 root root 0 2015-12-24 10:21 file1

 

 

Ex2) 'chown -R' 옵션 명령어 (소유권을 변경할때, 해당 디렉토리와 그 하위 파일들 소요권을 변경한다.)


[root@CentOS /test]# rm -rf /test/*
[root@CentOS /test]# ls

[root@CentOS /test]# mkdir -p dir1/dir2/dir3
[root@CentOS /test]# touch dir1/file1
[root@CentOS /test]# touch dir1/file2
[root@CentOS /test]# tree
.
└── dir1
    ├── dir2
    │   └── dir3
    ├── file1
    └── file2

[root@CentOS /test]# ls -lR dir1
dir1:
합계 4
drwxr-xr-x. 3 root root 4096 2015-12-24 10:32 dir2
-rw-r--r--. 1 root root    0 2015-12-24 10:32 file1
-rw-r--r--. 1 root root    0 2015-12-24 10:32 file2

dir1/dir2:
합계 4
drwxr-xr-x. 2 root root 4096 2015-12-24 10:32 dir3

dir1/dir2/dir3:
합계 0


[root@CentOS /test]# chown -R user1 dir1
[root@CentOS /test]# ls -lR dir1
dir1:
합계 4
drwxr-xr-x. 3 user1 root 4096 2015-12-24 10:39 dir2
-rw-r--r--. 1 user1 root    0 2015-12-24 10:39 file1
-rw-r--r--. 1 user1 root    0 2015-12-24 10:39 file2

dir1/dir2:
합계 4
drwxr-xr-x. 2 user1 root 4096 2015-12-24 10:39 dir3

dir1/dir2/dir3:
합계 0

[root@CentOS /test]# chown -R .user1 dir1
[root@CentOS /test]# ls -lR dir1
dir1:
합계 4
drwxr-xr-x. 3 user1 user1 4096 2015-12-24 10:39 dir2
-rw-r--r--. 1 user1 user1    0 2015-12-24 10:39 file1
-rw-r--r--. 1 user1 user1    0 2015-12-24 10:39 file2

dir1/dir2:
합계 4
drwxr-xr-x. 2 user1 user1 4096 2015-12-24 10:39 dir3

dir1/dir2/dir3:
합계 0

[root@CentOS /test]# chown -R root.root dir1
[root@CentOS /test]# ls -lR dir1
dir1:
합계 4
drwxr-xr-x. 3 root root 4096 2015-12-24 10:39 dir2
-rw-r--r--. 1 root root    0 2015-12-24 10:39 file1
-rw-r--r--. 1 root root    0 2015-12-24 10:39 file2

dir1/dir2:
합계 4
drwxr-xr-x. 2 root root 4096 2015-12-24 10:39 dir3

dir1/dir2/dir3:
합계 0

 

2. 'chgrp' 명령어
 

파일 속성 정보 중 그룹명을 변경하는 명령어이다.

 

 

Ex1) 'chgrp' 명령어 이해 (그룹 속성을 변경한다.)


[root@CentOS /test]# rm -rf /test/*
[root@CentOS /test]# ls

[root@CentOS /test]# touch file1
[root@CentOS /test]# ls -l file1
-rw-r--r--. 1 root root 0 2015-12-24 11:15 file1

[root@CentOS /test]# chgrp user1 file1
[root@CentOS /test]# ls -l file1
-rw-r--r--. 1 root user1 0 2015-12-24 11:15 file1

 


Ex2) 'chgrp -R' 옵션 명령어 (그룹 속성을 변경할때, 해당 디렉토리와 그 하위 파일들 그룹 속성을 변경한다.)


[root@CentOS /test]# rm -rf /test/*
[root@CentOS /test]# ls


[root@CentOS /test]# mkdir -p dir1/dir2/dir3
[root@CentOS /test]# touch dir1/file1
[root@CentOS /test]# touch dir1/file2
[root@CentOS /test]# ls -lR
.:
합계 4
drwxr-xr-x. 3 root root 4096 2015-12-24 11:20 dir1

./dir1:
합계 4
drwxr-xr-x. 3 root root 4096 2015-12-24 11:19 dir2
-rw-r--r--. 1 root root    0 2015-12-24 11:20 file1
-rw-r--r--. 1 root root    0 2015-12-24 11:20 file2

./dir1/dir2:
합계 4
drwxr-xr-x. 2 root root 4096 2015-12-24 11:19 dir3

./dir1/dir2/dir3:
합계 0


[root@CentOS /test]# chgrp user1 dir1
[root@CentOS /test]# ls -lR
.:
합계 4
drwxr-xr-x. 3 root user1 4096 2015-12-24 11:20 dir1

./dir1:
합계 4
drwxr-xr-x. 3 root root 4096 2015-12-24 11:19 dir2
-rw-r--r--. 1 root root    0 2015-12-24 11:20 file1
-rw-r--r--. 1 root root    0 2015-12-24 11:20 file2

./dir1/dir2:
합계 4
drwxr-xr-x. 2 root root 4096 2015-12-24 11:19 dir3

./dir1/dir2/dir3:
합계 0

[root@CentOS /test]# chgrp -R user1 dir1
[root@CentOS /test]# ls -lR
.:
합계 4
drwxr-xr-x. 3 root user1 4096 2015-12-24 11:20 dir1

./dir1:
합계 4
drwxr-xr-x. 3 root user1 4096 2015-12-24 11:19 dir2
-rw-r--r--. 1 root user1    0 2015-12-24 11:20 file1
-rw-r--r--. 1 root user1    0 2015-12-24 11:20 file2

./dir1/dir2:
합계 4
drwxr-xr-x. 2 root user1 4096 2015-12-24 11:19 dir3

./dir1/dir2/dir3:
합계 0

 

3. 'chmod' 명령어
 

파일, 디렉토리를 새로운 권한으로 변경하는 명령어이다. 파일, 디렉토리 소유자 및 권리자만 사용할 수 있으며, 파일 소유자, 파일 그룹, 다른 사용자 별로 권한을 설정 할 수 있다. 'chmod' 명령어는 심볼릭(사용자 기호, 설정 기호, 권한 기호) 모드를 이용하여 권한을 변경한다. 심볼릭 기호 내용은 다음과 같다.

 

1) 사용자 기호

 u

user 

  파일/디렉토리 소유자

 g

group

  파일/디렉토리 그룹 사용자

 o

other

  기타 사용자

 a

all

  소유자, 그룹, 다른 사용자 전체(표시 X)

 

2) 설정 기호

 +

 퍼미션 추가

 지정한 퍼미션을 허가함

 -

 퍼미션 제거

 지정한 퍼미션을 차단함

 =

 퍼미션 변경

 이전 권한과 관계 없이 지정한 퍼미션만 허가하고 나머지는 차단함

 

3) 권한 기호

 r (read)

 w(write)

 x (execute)

 -

 읽기 권한

쓰기 권한 

실행 권한 

권한 없음 

 

 

x(execute) 유

x(execute)  무

 - :파일 

실행 파일

문서 파일

 d : 디렉토리

접근 가능

접근 불가능


만약 읽기 권한이 없으면, 파일 내용을 열어서 확인할 수 없기 때문에 수정 작업이 힘들게 된다. 또한 쓰기 권한이 없다면 파일을 애초에 오픈할 수 없기 때문에 수정 작업이 불가능하다. 즉, 파일 수정 작업이 진행되려면 읽기/쓰기 권한을 부여받아야 한다.

 

 

Ex1) 심볼릭 모드를 이용한 'chmod' 명령어 이해


[root@CentOS /test]# rm -rf /test/*

[root@CentOS /test]# ls

 

[root@CentOS /test]# touch file1
[root@CentOS /test]# ls -l file1
-rw-r--r--. 1 root root 0 2015-12-24 11:38 file1

 

[root@CentOS /test]# chmod u+x file1
[root@CentOS /test]# ls -l file1
-rwxr--r--. 1 root root 0 2015-12-24 11:38 file1

 

[root@CentOS /test]# chmod g-r file1
[root@CentOS /test]# ls -l file1
-rwx---r--. 1 root root 0 2015-12-24 11:38 file1

 

[root@CentOS /test]# chmod u-x,g+x file1
[root@CentOS /test]# ls -l file1
-rw---xr--. 1 root root 0 2015-12-24 11:38 file1

 

[root@CentOS /test]# chmod a=rwx file1
[root@CentOS /test]# ls -l file1
-rwxrwxrwx. 1 root root 0 2015-12-24 11:38 file1

 

 

Ex2) 수치모드(octal mode)를 이용한 'chmod' 명령어 이해

 

 

파일 소유자(Owner)

그룹 사용자(Group) 

다른 사용자(Other) 

 - : 파일 

r   w   x

r   w   x

r   w   x

 d : 디렉토리

0   0   0

1   1   1

 2^2 2^1 2^0

 4    2    1  

0   0   0

1   1   1

 2^2 2^1 2^0

 4    2    1  

0   0   0

1   1   1

 2^2 2^1 2^0

 4    2    1  

(r : read, 읽기 권한, w - write, 쓰기/삭제 권한, x - execute - 실행/디렉토리 접근 권한)

 

 [권한 이해하기]

 ---

권한 없음

 --x

실행 권한

1

 -w-

쓰기 권한

2

 -wx

쓰기 실행

3

 r--

읽기 권한

4

 r-x

읽기 실행

5

 rw-

읽기 쓰기

6

 rwx

읽기 쓰기 실행

7

 


[root@CentOS /test]# rm -rf /test/*

[root@CentOS /test]# ls

 

[root@CentOS /test]# touch file1
[root@CentOS /test]# ls -l file1
-rw-r--r--. 1 root root 0 2015-12-24 12:23 file1

  6  4  4 


[root@CentOS /test]# chmod 744 file1
[root@CentOS /test]# ls -l file1
-rwxr--r--. 1 root root 0 2015-12-24 12:23 file1
  7   4


[root@CentOS /test]# chmod 754 file1
[root@CentOS /test]# ls -l file1
-rwxr-xr--. 1 root root 0 2015-12-24 12:23 file1

  7  5  4 

 

Ex3)  파일 & 디렉토리 퍼미션 이해

 

 [파일 퍼미션]

 r : read

  파일을 읽을 수 있는 권한

 w : write

  파일을 수정할 수 있는 권한

 x : execute

  파일을 실행 할 수 있는 권한

 

 [디렉토리 퍼미션]

 r : read

  디렉토리 안에서 'ls' 명령어를 수행할 수 있는 권한

 w : write

  디렉토리 안의 파일을 삭제/생성을 할 수 있는 권한

 x : execute

  디렉토리 안에서 'cd' 명령어를 수행할 수 있는 권한

 


[root@CentOS /test]# rm -rf /test/*

[root@CentOS /test]# ls

 

- 'root' Putty 창에서 'user2'로 SSH 접속 실시

 

[root@CentOS /test]# ssh user2@localhost
user2@localhost's password: centos
Last login: Fri Dec 18 14:45:28 2015 from localhost
[user2@CentOS /home/user2]$

[user2@CentOS /home/user2]$ ls -ld /home/user2
drwx------. 4 user2 user2 4096 2015-12-18 14:46 /home/user2

  7  0  0

[user2@CentOS /home/user2]$ mkdir dir1
[user2@CentOS /home/user2]$ touch dir1/file1.txt
[user2@CentOS /home/user2]$ ls -lR
.:
합계 4
drwxrwxr-x. 2 user2 user2 4096 2015-12-24 13:37 dir1

   7  7  5

./dir1:
합계 0
-rw-rw-r--. 1 user2 user2 0 2015-12-24 13:37 file1.txt

  6  6  4

 

 [퍼미션 구조]

 /home/user2  (drwx------ user2 user2) - 700

              └── dir1  (drwxrwxr-x user2 user2) - 775
                         └── file1.txt  (-rw-rw-r-- user2 user2) - 664

 

 

파일 소유자(Owner)

그룹 사용자(Group)

다른 사용자(Other)

700

7 rwx

읽기 쓰기 실행

0 ---

권한 없음

0 ---

권한 없음

775

7 rwx

읽기 쓰기 실행

7 rwx

읽기 쓰기 실행

5 r-x

읽기 실행

664

6 rw-

읽기 쓰기

6 rw-

읽기 쓰기

4 r--

읽기 권한

 

 

- Putty 새로운 접속을 실시하여, 'user1'으로 SSH 접속 실시

 

login as: user1
user1@192.168.1.2's password: centos
Last login: Thu Dec 24 10:22:50 2015 from 192.168.1.1
[user1@CentOS ~]$

[user1@CentOS /home/user1]$


[user1@CentOS /home/user1]$ cd /home/user2
-bash: cd: /home/user2: 허가 거부

[user1@CentOS /home/user1]$ cd /home/user2/dir1
-bash: cd: /home/user2/dir1: 허가 거부

 

[user1@CentOS /home/user1]$ rm -rf /home/user2/dir1/file1.txt
rm: cannot remove `/home/user2/dir1/file1.txt': 허가 거부

 

퍼미션 결과 : 'user1'으로 'user2' 디렉토리 접근 및 생성/삭제 불가능

 


- 'user2' 사용자 Putty 창

 

[user2@CentOS /home/user2]$ id user2
uid=501(user2) gid=501(user2) groups=501(user2)

 

[user2@CentOS /home/user2]$ chmod 757 /home/user2
[user2@CentOS /home/user2]$ ls -ld /home/user2
drwxr-xrwx. 5 user2 user2 4096 2015-12-24 13:30 /home/user2

  7  5  7


[user2@CentOS /home/user2]$ chmod 755 dir1
[user2@CentOS /home/user2]$ touch dir1/file2
[user2@CentOS /home/user2]$ chmod 646 dir1/file2
[user2@CentOS /home/user2]$ ls -lR
.:
합계 4
drwxr-xr-x. 2 user2 user2 4096 2015-12-24 13:44 dir1

   7  5  5

./dir1:
합계 0
-rw-rw-r--. 1 user2 user2 0 2015-12-24 13:37 file1.txt

  6  6  4
-rw-r--rw-. 1 user2 user2 0 2015-12-24 13:44 file2

  6  4  6

 

 [퍼미션 구조]

 /home/user2  (drwxr-xrwx user2 user2) - 757 

              └── dir1  (drwxr-xr-x user2 user2) - 755
                         ├── file1.txt  (-rw-rw-r-- user2 user2) - 664
                         └── file2 (-rw-r--rw- user2 user2) - 646

 

 

파일 소유자(Owner)

그룹 사용자(Group)

기타 사용자(Other)

757

7 rwx

읽기 쓰기 실행

5 r-x

읽기 실행

7 rwx

일기 쓰기 실행

755

7 rwx

읽기 쓰기 실행

5 r-x

읽기 실행

5 r-x

읽기 실행

664

6 rw-

읽기 쓰기

6 rw-

읽기 쓰기

4 r--

읽기 권한

646

6 rw-

읽기 쓰기

4 r--

읽기 권한

6 rw-

읽기 쓰기

 

 

- 'user1' 사용자 Putty 창

 

[user1@CentOS /home/user1]$ id user1
uid=500(user1) gid=500(user1) groups=500(user1)


[user1@CentOS /home/user1]$ cd /home/user2/dir1
[user1@CentOS /home/user2/dir1]$ rm -f file2
rm: cannot remove `file2': 허가 거부

[user1@CentOS /home/user2/dir1]$ rm -f file1.txt
rm: cannot remove `file1.txt': 허가 거부

[user1@CentOS /home/user2/dir1]$ mkdir dir2
mkdir: `dir2' 디렉토리를 만들 수 없습니다: 허가 거부

[user1@CentOS /home/user2/dir1]$ cd /home/user2/dir1
[[user1@CentOS /home/user2/dir1]$ cd
[user1@CentOS /home/user1]$

[user1@CentOS /home/user1]$ mkdir /home/user2/dir1/dir2
mkdir: `/home/user2/dir1/dir2' 디렉토리를 만들 수 없습니다: 허가 거부

[user1@CentOS /home/user1]$ rm -rf /home/user2/dir1
rm: cannot remove `/home/user2/dir1/file2': 허가 거부
rm: cannot remove `/home/user2/dir1/file1.txt': 허가 거부

[user1@CentOS /home/user1]$ rm -rf /home/user2
rm: cannot remove `/home/user2/dir1/file2': 허가 거부
rm: cannot remove `/home/user2/dir1/file1.txt': 허가 거부
rm: cannot remove `/home/user2/.mozilla/extensions': 허가 거부
rm: cannot remove `/home/user2/.mozilla/plugins': 허가 거부

 

퍼미션 결과 : 'user1'으로 'user2' 디렉토리 접근 가능, 대신 생성/삭제 불가능

 


- 'user2' 사용자 Putty 창
 
[user2@CentOS /home/user2]$ id user2
uid=501(user2) gid=501(user2) groups=501(user2)

[user2@CentOS /home/user2]$ chmod 757 dir1
[user2@CentOS /home/user2]$ ls -lR
.:
합계 4
drwxr-xrwx. 2 user2 user2 4096 2015-12-24 13:44 dir1

  7  5  7

./dir1:
합계 0
-rw-rw-r--. 1 user2 user2 0 2015-12-24 13:37 file1.txt
-rw-r--rw-. 1 user2 user2 0 2015-12-24 13:44 file2

 

 [퍼미션 구조]

 /home/user2  (drwxr-xrwx user2 user2) - 757
              └── dir1  (drwxr-xrwx user2 user2) - 757
                         ├── file1.txt  (-rw-rw-r-- user2 user2) - 644
                         └── file2 (-rw-r--rw- user2 user2) - 646

 

 

파일 소유자(Owner)

그룹 사용자(Group)

기타 사용자(Other)

757

7 rwx

읽기 쓰기 실행

5 r-x

읽기 실행

7 rwx

일기 쓰기 실행

757

7 rwx

읽기 쓰기 실행

5 r-x

읽기 실행

7 rwx

일기 쓰기 실행

664

6 rw-

읽기 쓰기

6 rw-

읽기 쓰기

4 r--

읽기 권한

646

6 rw-

읽기 쓰기

4 r--

읽기 권한

6 rw-

읽기 쓰기

 

 

- 'user1' 사용자 Putty 창

 

[user1@CentOS /home/user1]$ id user1
uid=500(user1) gid=500(user1) groups=500(user1)

 

[user1@CentOS user2]$ cd /home/user2/dir1


[user1@CentOS /home/user2/dir1]$ rm -f file2
[user1@CentOS /home/user2/dir1]$ rm -f file1.txt

[user1@CentOS /home/user2/dir1]$ mkdir dir2
[user1@CentOS /home/user2/dir1]$ ls -lR
.:
합계 4
drwxrwxr-x. 2 user1 user1 4096 2015-12-24 14:09 dir2

./dir2:
합계 0

[user1@CentOS /home/user2/dir1]$ rmdir dir2

[user1@CentOS /home/user2/dir1]$ ls -lR
.:
합계 0

 

[user1@CentOS /home/user2/dir1]$ mkdir /home/user2/dir1/dir2
[user1@CentOS /home/user2/dir1]$ ls -lR /home/user2/
/home/user2/:
합계 4
drwxr-xrwx. 3 user2 user2 4096 2015-12-24 14:10 dir1

/home/user2/dir1:
합계 4
drwxrwxr-x. 2 user1 user1 4096 2015-12-24 14:10 dir2

 

/home/user2/dir1/dir2:
합계 0

[user1@CentOS /home/user2/dir1]$ rm -rf /home/user2/dir1
[user1@CentOS /home/user2/dir1]$ ls -lR /home/user2/
/home/user2/:
합계 0

 

퍼미션 결과 : 'user1'으로 'user2' 디렉토리 접근 가능 및 생성/삭제 가능

 


[user2@CentOS /home/user2]$ exit
logout
Connection to localhost closed.

 

[root@CentOS /test]#
[root@CentOS /test]# rm -rf /test/*

[root@CentOS /test]# ls

 

 

 

4. 'umask' 명령어

 

'umask' 명령어는 파일과 디렉토리가 생성될때 기본 퍼미션을 조정하는 명령어이다. 파일과 디렉토리가 생성되면 다음과 같은 기본 퍼미션을 할당받게 된다.

 

 

 파일

디렉토리 

기본 퍼미션

    666 

   777 

umask

- 022

- 022

생성 기본 퍼미션

 = 644 

= 755

 

 

Ex1) 'umask' 명령어 이해 I

 

[root@CentOS /test]# umask
0022

[root@CentOS /test]# touch file1
[root@CentOS /test]# mkdir dir1
[root@CentOS /test]# ls -l
합계 4
drwxr-xr-x. 2 root root 4096 2015-12-24 16:00 dir1
-rw-r--r--. 1 root root    0 2015-12-24 16:00 file1

 

 

사용자 권한 

 기본 퍼미션

 umask

 생성 기본 퍼미션

 디렉토리

 drwxr-xr-x

777

- 022

= 755

 파일

 -rw-r--r--

666

- 022

= 644

 

 

Ex2) 'umask' 명령어 이해 II

 

[root@CentOS /test]# umask 002
[root@CentOS /test]# umask
0002

[root@CentOS /test]# touch file2
[root@CentOS /test]# mkdir dir2
[root@CentOS /test]# ls -l
합계 8
drwxr-xr-x. 2 root root 4096 2015-12-24 16:00 dir1
drwxrwxr-x. 2 root root 4096 2015-12-24 16:20 dir2
-rw-r--r--. 1 root root    0 2015-12-24 16:00 file1
-rw-rw-r--. 1 root root    0 2015-12-24 16:20 file2

 

 

사용자 권한

 기본 퍼미션

 umask

 생성 기본 퍼미션

 디렉토리

 drwxrwxr-x

777

- 002

= 775

 파일

 -rw-rw-r--

666

- 002

= 664

 

 

Ex3) 'umask' 명령어 이해 III

 

[root@CentOS /test]# umask 027
[root@CentOS /test]# umask
0027

[root@CentOS /test]# touch file3
[root@CentOS /test]# mkdir dir3
[root@CentOS /test]# ls -l
합계 12
drwxr-xr-x. 2 root root 4096 2015-12-24 16:00 dir1
drwxrwxr-x. 2 root root 4096 2015-12-24 16:20 dir2
drwxr-x---. 2 root root 4096 2015-12-24 16:23 dir3
-rw-r--r--. 1 root root    0 2015-12-24 16:00 file1
-rw-rw-r--. 1 root root    0 2015-12-24 16:20 file2
-rw-r-----. 1 root root    0 2015-12-24 16:23 file3

 

 

사용자 권한

 기본 퍼미션

 umask

 생성 기본 퍼미션

 디렉토리

 drwxr-x---.

777

- 027

= 750

 파일

-rw-r-----

666

- 027

= 640

 

 

Ex4) /etc/bashrc 파일 'umask' 정보 확인

 

[root@CentOS /test]# umask 022

[root@CentOS /test]# cat /etc/bashrc
~~~ 중간 생략 ~~~


    # By default, we want umask to get set. This sets it for non-login shell.
    # Current threshold for system reserved uid/gids is 200
    # You could check uidgid reservation validity in
    # /usr/share/doc/setup-*/uidgid file
    if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002
    else
       umask 022
    fi

 

 


5. SetUID/SetGID

 

'SetUID/SetGID'는 파일에 대한 소유권을 다른 사용자에게 할당하여, 소유권이 없는 사용자도 파일에 대한 소유권을 갖게할 수 있는 기능을 수행한다. 일반적으로 소유자가 root인 실행 파일에 적용한다.

 

'SetUID/SetGID'에서는 다음과 같은 '특수 권한'을 추가적으로 사용한다.

 

[특수 권한 bit]

 특수 권한

소유자 권한 

그룹 권한 

기타 권한 

 SetUID

SetGID 

Sticky bit 

 r

 w

 r

w

 1 

 1

 1

 1

1

 1

 1

 1

 1 

 1 

4

 2

 1

 4

 2 

 1

 4

 2

1

4

천단위

백단위

십단위

일단위

 

 SetUID

 SetGID

 Sticky bit

 4nnn

 2nnn

 1nnn

 --s --- ---

--- --s --- 

--- --- --t 

 실행 동안 소유자 권한 갖음

 실행 동안 그룹 권한 갖음

 소유자가 아니면 파일 삭제 못함


 

Ex1) 'SetUID/SetGID' 설정 I


[root@CentOS /test]# rm -rf /test/*

[root@CentOS /test]# ls

[root@CentOS /test]# touch file1
[root@CentOS /test]# ls -l file1
-rw-r--r--. 1 root root 0 2015-12-24 17:43 file1

[root@CentOS /test]# chmod 755 file1
[root@CentOS /test]# ls -l file1
-rwxr-xr-x. 1 root root 0 2015-12-24 17:43 file1
 

 특수 권한

소유자 권한 

그룹 권한 

기타 권한 

 SetUID

SetGID 

Sticky bit 

 r

 w

 r

w

 0

 0

0

 1

1

 1

 1

0

 1 

 1 

0

0

0

0

 4

 2 

 1

 4

 0

1

4

0

 0

7

5

5

 

 

Ex2) 'SetUID/SetGID' 설정 II


[root@CentOS /test]# chmod 4755 file1
[root@CentOS /test]# ls -l file1
-rwsr-xr-x. 1 root root 0 2015-12-24 17:43 file1

 

 특수 권한

소유자 권한 

그룹 권한 

기타 권한 

 SetUID

SetGID 

Sticky bit 

 r

 w

 r

w

 1 

 0

0

 1

1

 1

 1

0

 1 

 1 

0

4

0

0

 4

 2 

 1

 4

 0

1

4

0

 4

7

5

5

 

 

Ex3) 'SetUID/SetGID' 설정 III


[root@CentOS /test]# chmod 2755 file1
[root@CentOS /test]# ls -l file1
-rwxr-sr-x. 1 root root 0 2015-12-24 17:43 file1

 

 특수 권한

소유자 권한 

그룹 권한 

기타 권한 

 SetUID

SetGID 

Sticky bit 

 r

 w

 r

w

 0

 1

0

 1

1

 1

 1

0

 1 

 1 

0

0

2

0

 4

 2 

 1

 4

 0

1

4

0

 2

7

5

5

 

 

Ex4) 'SetUID/SetGID' 설정 IV


[root@CentOS /test]# chmod 6755 file1
[root@CentOS /test]# ls -l file1
-rwsr-sr-x. 1 root root 0 2015-12-24 17:43 file1

 

 특수 권한

소유자 권한 

그룹 권한 

기타 권한 

 SetUID

SetGID 

Sticky bit 

 r

 w

 r

w

 1 

1

0

 1

1

 1

 1

0

 1 

 1 

0

4

2

0

 4

 2 

 1

 4

 0

1

4

0

 6

7

5

5

 

 

[root@CentOS /root/test]# rm *
[root@CentOS /root/test]# ls

 

 

Ex5) 'SetUID/SetGID' 이해 I

 

- user1 Putty 창

 

login as: user1
user1@192.168.1.2's password: centos
Last login: Thu Dec 24 16:46:10 2015 from 192.168.1.1
[user1@CentOS /home/user1]$

[user1@CentOS /home/user1]$ cd /test
[user1@CentOS /test]$

[user1@CentOS /test]$ usermod -s /bin/csh user1
-bash: /usr/sbin/usermod: 허가 거부

[user1@CentOS /test]$ chsh
Changing shell for user1.
암호:
New shell [/bin/bash]: /bin/csh
Shell changed.

[user1@CentOS /test]$ grep -w user1 /etc/passwd
user1:x:500:500::/home/user1:/bin/csh

 

 

- root Putty 창


[root@CentOS /test]# whereis chsh
chsh: /usr/bin/chsh /usr/share/man/man1/chsh.1.gz
[root@CentOS /test]#
[root@CentOS /test]# ls -l /usr/bin/chsh
-rws--x--x. 1 root root 20056 2014-10-15 19:38 /usr/bin/chsh

[root@CentOS /test]# cd /usr/bin
[root@CentOS /usr/bin]# chmod u-s chsh
[root@CentOS /usr/bin]# ls -l chsh
-rwx--x--x. 1 root root 20056 2014-10-15 19:38 chsh

 

 

- user1 Putty 창


[user1@CentOS /test]$ chsh
Changing shell for user1.
암호:
New shell [/bin/csh]: /bin/bash
setpwnam: 허가 거부
Shell *NOT* changed.  Try again later.

 


 - root Putty 창


[root@CentOS /usr/bin]# chmod u+s chsh
[root@CentOS /usr/bin]# ls -l chsh
-rws--x--x. 1 root root 20056 2014-10-15 19:38 chsh

 

[root@CentOS /usr/bin]# cd -
/test
[root@CentOS /test]#

 

 

- user1 Putty 창

 

[user1@CentOS /test]$ chsh
Changing shell for user1.
암호:
New shell [/bin/csh]: /bin/bash
Shell changed.

[user1@CentOS /test]$ grep -w user1 /etc/passwd
user1:x:500:500::/home/user1:/bin/bash

 

- SetID가 설정된 파일은 root 권한이 할당된 개념이기 때문에 다른 계정으로 접속하여 사용할 수 있다.

 

 

Ex6) 'SetUID/SetGID'  이해 II

 

 - root Putty 창

 

[root@CentOS /test]# cp /bin/cat newcat
[root@CentOS /test]# ls -l newcat
-rwxr-xr-x. 1 root root 48568 2016-01-26 20:12 newcat

 

 

- user1 Putty 창


[user1@CentOS /test]$ ls -l /etc/shadow
----------. 1 root root 1156 2016-01-20 14:38 /etc/shadow

 

[user1@CentOS /test]$ ./newcat /etc/shadow
./newcat: /etc/shadow: 허가 거부

 

 

 - root Putty 창

 

[root@CentOS /test]# chmod 4755 /test/newcat
[root@CentOS /test]# ls -l newcat
-rwsr-xr-x. 1 root root 48568 2016-01-26 20:12 newcat

 

- user1 Putty 창

 

[user1@CentOS /test]$ ./newcat /etc/shadow
root:$6$7UK6Zk3dYVfNoDBg$oSobCsu1g.z.tYblICZffT8g.g4NcVv/marnvBha7BiahfGUD3p9RZiHnckOydUWqUdjrO/Htpcz/Ili8oMTQ/:16811:0:99999:7:::
~ 중간 생략 ~

 

 

 

Ex7) 'user1'이 'user2' 사용자 권한을 빌리는 경우

 

 - root에서 user2로 ssh 접속 실시


[root@CentOS /test]# ssh user2@localhost
user2@localhost's password: centos
Last login: Thu Dec 24 18:02:19 2015 from 192.168.1.1

[user2@CentOS /home/user2]$
[user2@CentOS /home/user2]$ ls -ld /home/user2
drwx------. 4 user2 user2 4096 2015-12-24 18:05 /home/user2

[user2@CentOS /home/user2]$ chmod 775 /home/user2
[user2@CentOS /home/user2]$ ls -ld /home/user2
drwxrwxr-x. 4 user2 user2 4096 2015-12-24 18:05 /home/user2

[user2@CentOS /home/user2]$ cp /bin/touch /home/user2
[user2@CentOS /home/user2]$ ls -l
합계 52
-rwxr-xr-x. 1 user2 user2 52560 2015-12-24 18:05 touch

 

 /bin/touch                   (rwxr-xr-x 1 root root) - 755

 /home/user2/touch       (rwxr-xr-x 1 user2 user2)  - 755

 

 

[user2@CentOS /home/user2]$ ./touch file1
[user2@CentOS /home/user2]$ ls -l
합계 52
-rw-rw-r--. 1 user2 user2     0 2015-12-24 18:06 file1
-rwxr-xr-x. 1 user2 user2 52560 2015-12-24 18:05 touch

 /home/user2  (rwxrwxr-x user2 user2) - 775

              └── file1 생성 가능  (rw-rw-r-- user2 user2) - 664

 


[user2@CentOS /home/user2]$ chmod 4755 touch
[user2@CentOS /home/user2]$ ls -l
합계 52
-rw-rw-r--. 1 user2 user2     0 2015-12-24 18:06 file1
-rwsr-xr-x. 1 user2 user2 52560 2015-12-24 18:05 touch


'user1' putty 창

 

[user1@CentOS /test]$ cd
[user1@CentOS /home/user1]$


[user1@CentOS /home/user1]$ cd /home/user2
[user1@CentOS /home/user2]$ touch file2
touch: cannot touch `file2': 허가 거부

 

 /home/user2  (rwxrwxr-x user2 user2) - 775

              └── file2 생성 불가능

 

 /bin/touch                   (rwxr-xr-x 1 root root) - 755

 /home/user2/touch       (rwsr-xr-x 1 user2 user2)  - 4755

 

 

[user1@CentOS /home/user2]$ ls -l
합계 52
-rw-rw-r--. 1 user2 user2     0 2016-01-21 18:26 file1
-rwsr-xr-x. 1 user2 user2 52560 2016-01-21 18:21 touch


[user1@CentOS /home/user2]$ ./touch file2
[user1@CentOS /home/user2]$ ls -l
합계 52
-rw-rw-r--. 1 user2 user2     0 2015-12-24 18:06 file1
-rw-rw-r--. 1 user2 user1     0 2015-12-24 18:12 file2
-rwsr-xr-x. 1 user2 user2 52560 2015-12-24 18:05 touch

 


- 'user2' 사용자 Putty 창

 

[user2@CentOS /home/user2]$ chmod 2755 touch
[user2@CentOS /home/user2]$ ls -l touch
-rwxr-sr-x. 1 user2 user2 52560 2015-12-24 18:05 touch

 

 

- 'user1' 사용자 Putty 창


[user1@CentOS /home/user2]$ ./touch file3
[user1@CentOS /home/user2]$ ls -l
합계 52
-rw-rw-r--. 1 user2 user2     0 2015-12-24 18:06 file1
-rw-rw-r--. 1 user2 user1     0 2015-12-24 18:12 file2
-rw-rw-r--. 1 user1 user2     0 2015-12-24 18:15 file3
-rwxr-sr-x. 1 user2 user2 52560 2015-12-24 18:05 touch

 

 /home/user2  (rwxrwxr-x user2 user2) - 775

              └── file3 생성 가능

 

 

- 'user2' 사용자 Putty 창

 

[user2@CentOS /home/user2]$ rm file*
[user2@CentOS /home/user2]$ rm touch

[user2@CentOS /home/user2]$ exit
logout
Connection to localhost closed.


[root@CentOS /root]#

[root@CentOS /root]# cd /
[root@CentOS /]# rm -rf test

 

 

 

6. Sticky Bits

 

파일에 쓰기 권한이 없어도 디렉토리 쓰기 권한이 있는 경우, 디렉토리 권한에 의해서 파일을 삭제할 수 있다. 만약, 특정 디렉토리를 공유 목적으로 사용할 경우, 사용자들에 의해서 파일이 생성될 수 있으나, 디렉토리 파일을 삭제할 수 없도록 제한하려면 'Sticky' 권한을 설정해야 한다.

 

예를 들어, 디렉토리 쓰기(touch, cp, vi 편집기) 권한이 있어도 파일 삭제(rm, mv, vi 편집기 수정 작업) 권한을 제외할 경우 사용한다. 단, 파일에 대한 소유권을 갖고 있는 소유자, 그룹, Root는 이 기능에서 제외된다.

 

Sticky Bits는 디렉토리 퍼미션이 '777'인 경우 사용할 수 있다.

 

 

Ex1) 'Sticky bit' 설정

 

[root@CentOS /]# mkdir test
[root@CentOS /]# cd test
[root@CentOS /test]# mkdir dir1
[root@CentOS /test]# ls -ld dir1
drwxr-xr-x. 2 root root 4096 2016-01-21 19:59 dir1

[root@CentOS /test]# chmod 1777 dir1
[root@CentOS /test]# ls -ld dir1
drwxrwxrwt. 2 root root 4096 2016-01-21 19:59 dir1

[root@CentOS /test]# chmod 1001 dir1
[root@CentOS /test]# ls -ld dir1
d--------t. 2 root root 4096 2016-01-21 19:59 dir1

[root@CentOS /test]# chmod 1776 dir1
[root@CentOS /test]# ls -ld dir1
drwxrwxrwT. 2 root root 4096 2016-01-21 19:59 dir1


[root@CentOS /test]# cd /
[root@CentOS /]# rm -rf test

 

 

Ex2) 'Sticky bit'를 이용한 파일 삭제 권한 제거

 

[root@CentOS /]# ls -ld /tmp
drwxrwxrwt. 20 root root 4096 2016-01-21 19:56 /tmp

 

 - /tmp 디렉토리는 기본적으로 Sticky bit가 설정되어 있다.

 

 

 - root에서 user2로 ssh 접속 실시


[root@CentOS /]# ssh user2@localhost
user2@localhost's password: centos
Last login: Thu Jan 21 19:52:32 2016 from localhost
[user2@CentOS /home/user2]$

 

[user2@CentOS /home/user2]$ id
uid=501(user2) gid=501(user2) groups=501(user2)


[user2@CentOS /home/user2]$ cd /tmp
[user2@CentOS /tmp]$ mkdir stickybit
[user2@CentOS /tmp]$ echo 1111 > file1
[user2@CentOS /tmp]$ echo 2222 > stickybit/file2
[user2@CentOS /tmp]$ ls -l | grep user2
-rw-rw-r--. 1 user2 user2        5 2016-01-21 20:04 file1
drwxrwxr-x. 2 user2 user2     4096 2016-01-21 20:04 stickybit

 

 

 

- 새로운 Putty 창을 이용하여 'user1' 접속 실시

 

login as: user1
user1@192.168.1.2's password: centos
Last login: Thu Jan 21 18:18:15 2016 from 192.168.1.1
[user1@CentOS /home/user1]$
[user1@CentOS /home/user1]$ id
uid=500(user1) gid=500(user1) groups=500(user1)

[user1@CentOS /home/user1]$ cd /tmp
[user1@CentOS /tmp]$ mkdir linux
[user1@CentOS /tmp]$ echo 3333 > file3
[user1@CentOS /tmp]$ echo 4444 > linux/file4


[user1@CentOS /tmp]$ ls -l | grep user1
-rw-rw-r--. 1 user1 user1        5 2016-01-21 20:06 file3
drwxrwxr-x. 2 user1 user1     4096 2016-01-21 20:06 linux

 

[user1@CentOS /tmp]$ vi file3

 

 1 3333

 2 4444 <--- 추가

 

: wq!

 
[user1@CentOS /tmp]$ cat file3
3333
4444

[user1@CentOS /tmp]$ ls -l | grep user
-rw-rw-r--. 1 user2 user2        5 2016-01-21 20:30 file1
-rw-rw-r--. 1 user1 user1       10 2016-01-21 20:32 file3
drwxrwxr-x. 2 user1 user1     4096 2016-01-21 20:31 linux
drwxrwxr-x. 2 user2 user2     4096 2016-01-21 20:30 stickybit

[user1@CentOS /tmp]$ rm -rf linux
[user1@CentOS /tmp]$ rm file3
[user1@CentOS /tmp]$ ls -l file1
-rw-rw-r--. 1 user2 user2 5 2016-01-21 20:04 file1

[user1@CentOS /tmp]$ rm file1
rm: remove write-protected 일반 파일 `file1'? y
rm: cannot remove `file1': 명령을 허용하지 않음

[user1@CentOS /tmp]$ rm -rf stickybit
rm: cannot remove `stickybit/file2': 허가 거부

[user1@CentOS /tmp]$ mv file1 file2
mv: cannot move `file1' to `file2': 명령을 허용하지 않음

[user1@CentOS /tmp]$ vi file1

 

 1 1111

 2 2222 <- 추가

 

:wq!

 

"file1"
"file1" E212: 쓸 파일을 열 수 없습니다
Press ENTER or type command to continue

:q!

 


[user1@CentOS /tmp]$ cp file1 file2
[user1@CentOS /tmp]$ ls -l file1 file2
-rw-rw-r--. 1 user2 user2 5 2016-01-21 20:04 file1
-rw-rw-r--. 1 user1 user1 5 2016-01-21 20:10 file2
[user1@CentOS /tmp]$

 

 - 자신이 소유권을 갖고 있지 않은 파일에 대해서는 원본 파일을 수정할 수 있는 명령어(rm, mv)는 허용되지 않는다.

 


- 'user2' 사용자 Putty 창

 

[user2@CentOS /tmp]$ rm -rf stickybit
[user2@CentOS /tmp]$ rm file1
[user2@CentOS /tmp]$ exit
logout
Connection to localhost closed.
[root@CentOS /]#

[root@CentOS /]# rm -rf test

Posted by 김정우 강사(카카오톡 : kim10322)
,


Q