본문 바로가기

리눅스-기초명령어/파일 및 유저 권한

chmod

파일 및 디렉토리에 대한 허가권을 변경

          

chmod [옵션] [접근권한] [파일명]

 

옵션

-R : 특정 디렉토리 내의 파일과 디렉토리에 대해 재귀적으로 변경

[root@Linux AAA]# ls
A.txt  B.txt  C.txt
[root@Linux AAA]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 27 12:25 A.txt
-rw-r--r--. 1 root root 0 Apr 27 12:25 B.txt
-rw-r--r--. 1 root root 0 Apr 27 12:25 C.txt
[root@Linux ~]# chmod -R u+x AAA  

# AAA디렉토리에 대해 유저에게 x권한을 반복해서 변경한다 -> AAA 디렉토리 하위 파일 및 디렉토리에 적용
[root@Linux ~]# cd AAA
[root@Linux AAA]# ll
total 0
-rwxr--r--. 1 root root 0 Apr 27 12:25 A.txt
-rwxr--r--. 1 root root 0 Apr 27 12:25 B.txt
-rwxr--r--. 1 root root 0 Apr 27 12:25 C.txt

 

-c : 변경된 파일이나 디렉토리에 대한 자세한 정보를 출력

[root@Linux ~]# chmod -c u=rw AAA  변경 사항이 있으면 출력
mode of 'AAA' changed from 0755 (rwxr-xr-x) to 0655 (rw-r-xr-x)

-f : 대부분의 에러 메세지 출력을 제한

[root@Linux AAA]# chmod -f 640 A.txt B.txt C.txt 1.txt 2.txt
[root@Linux AAA]# ll
total 0
-rw-r-----. 1 root root 0 Apr 27 12:25 A.txt
-rw-r-----. 1 root root 0 Apr 27 12:25 B.txt
-rw-r-----. 1 root root 0 Apr 27 12:25 C.txt

이 옵션은 누락된 파일이나 없다는걸 알아도 에러 메시지를 출력하지 않고 계속 실행 시킬 때 사용이 된다.

--reference : 모드 대신 파일에 지정한 모드를 사용

[root@Linux AAA]# ll
-rw-r-----. 1 root root 0 Apr 27 12:25 A.txt
-rw-r-----. 1 root root 0 Apr 27 12:25 B.txt
-rwxrwxrwx. 1 root root 0 Apr 27 12:25 C.txt
[root@Linux AAA]# chmod --reference=A.txt C.txt  A를 타겟으로 삼아 C의 권한을 변경한다.
[root@Linux AAA]# ll
total 0
-rw-r-----. 1 root root 0 Apr 27 12:25 A.txt
-rw-r-----. 1 root root 0 Apr 27 12:25 B.txt
-rw-r-----. 1 root root 0 Apr 27 12:25 C.txt

즉 C.txt권한이 A.txt의 권한으로 복사가 되었다.

 

-v : 실행한 내용을 자세히 출력을 해준다.

[root@Linux ~]# chmod -v g+w AAA
mode of 'AAA' changed from 0655 (rw-r-xr-x) to 0675 (rw-rwxr-x)

-c 옵션과 다른점은 -c옵션은 변경이 될 경우에만 출력이 되지만 -v옵션은 변경 사항과는 관계없이 출력이 된다.

 

접근권한(문자)

접근권한은 [Who][What][Which]의 조합으로 구성된다

 

Who

who 설명
u user 파일 소유자
g group 파일 소유자 그룹의 멤버
o other 소유자나 파일 그룹 멤버가 아닌 사용자
a all 위 3 그룹 모두

 

what

what 연산 내용
+ 추가 권한을 추가 한다
- 제거 권한을 제거 한다
= 지정 현재 권한과는 상관 없이 =앞에 권한을 일치 시킨다 (덮어쓰기)

ex) 만약 그룹에 r-x 권한이 있는 상태에서 chmod g=w 을 사용하면 그룹의 권한은 -w-가 된다.

 

which

which 모드 설명
r read 파일 및 디렉토리에 대한 읽기 권한
w write 파일 또는 디렉토리에 대한 쓰기 권한
x execute 파일에 대한 실행 권한
X special execute 파일에 대한 실행 권한 또는 실행 비트가 하나 이상 설정된 경우 파일에 대한 실행 권한

 

* 특수권한의 경우

권한 문자
suid(setuid) u+s
sgid(setgid) g+s
sticky o+t

 

1. x와 X의 차이점

X는 그 파일에 대해 다른 u g o 중에 x권한이 있을 경우에만 실행 권한을 준다.

[root@Linux dir]# ll
-rwxr-xr--. 1 root  root         24 Apr 27 10:20 backup.sh
-rw-r--r--. 1 root  root         24 Apr 27 10:22 remove_system.sh

[root@Linux ~]# chmod -R a+X dir/
[root@Linux ~]# cd dir
[root@Linux dir]# ll
-rwxr-xr-x. 1 root  root         24 Apr 27 10:20 backup.sh
-rw-r--r--. 1 root  root         24 Apr 27 10:22 remove_system.sh

X는 그 파일에 대해 다른 u g o 중에 x권한이 있을 경우에만 실행 권한을 준다. -> 중요파일에 실수로 x권한을 주는 상황을 피할 수 있다.  wr- wr- wr- 일때  -a+X로 모두에게 x권한을 줄 경우 x권한이 생기지 않는다. 반대로 wrx wr- wr 일때는 모두에게 x권한이 생긴다.

 

[root@localhost ~]# vi chmod.txt
[root@localhost ~]# ll
total 16
-rw-------. 1 root root 830 May 22 14:45 anaconda-ks.cfg
-rw-r--r--. 1 root root   7 May 30 12:23 chmod.txt
...
[root@localhost ~]# chmod g+w chmod.txt
[root@localhost ~]# ll
total 16
-rw-------. 1 root root 830 May 22 14:45 anaconda-ks.cfg
-rw-rw-r--. 1 root root   7 May 30 12:23 chmod.txt
...

 

한번에 쓰기

[root@localhost ~]# chmod u+x,g+x,o+wx chmod.txt 
[root@localhost ~]# ll
total 16
-rw-------. 1 root root 830 May 22 14:45 anaconda-ks.cfg
-rwxrwxrwx. 1 root root   7 May 30 12:23 chmod.txt
...

2. 접근권한(8진수)

8비트(진수)를 사용하여 권한을 적어서 명령어를 사용할수 있다.



r  = 4, w = 2, x = 1

부여할 권한에 맞춰 숫자를 더한다

 

예를들어

읽기,쓰기,실행권한이 전부있다면 : 7

읽기,쓰기만 있다면 : 6

읽기,실행만 있다면 : 5



[root@Linux ~]# chmod 755 app1.log/

소유자는 권한을 전부주고(7) 그룹에게는 읽기,실행(5) 나머지 에게도 읽기,실행(5)만 준다.

리눅스 환경에서는 보안 때문에 대부분 755으로 설정한다.

 

* 특수권한의 경우

권한 숫자
suid(setuid) 4
sgid(setgid) 2
sticky 1

 

'리눅스-기초명령어 > 파일 및 유저 권한' 카테고리의 다른 글

ACL (Access Control List)  (2) 2023.09.01
umask  (1) 2023.09.01
chgrp  (0) 2023.08.31
chown  (1) 2023.08.31