오경석의 개발노트

Linux_chmod(change mode) 본문

소프트웨어, 운영체제/Linux

Linux_chmod(change mode)

OHSAYU 2024. 1. 14. 15:19

■ chmod(change mode) : 파일들이나 디렉터리의 파일 시스템 모드들을 바꾸는 셸 명령어. 

■ 사용법

chmod [options] mode[,mode] file [file2 ...]

■ 구조

현재 어떤 허가들이 있는지 보기 위해서는 다음과 같이 입력

ls -l file

1. 디렉터리(d) or 파일(-)의 여부
2. owner(소유자) 권한
3. group(그룹) 권한
4. other(다른 사용자) 권한

5. owner(소유자) 이름

6. group(그룹) 이름

■ 옵션

 ● -R : 재귀적으로 파일들과 디렉터리들의 모드들을 바꾼다.

※ -R 이외에는 거의 사용되지 않아 생략

 

1. Symbolic method - 문자열 모드

파일들이나 디렉터리들의 모드를 조절하기 위한 하나의 방법은 기호적인 모드를 지정하는 것이다. 이 기호적인 모드는 세 가지 구성요소로 구성된다.

chmod [references] [operator] [modes] file1 ...

레퍼런스들은 허가가 적용되는 사용자들을 구분하기 위해서 사용된다. 

레퍼런스 클래스 설명
u 사용자 파일의 소유자
g 그룹 그 파일의 그룹 멤버인 사용자
o 다른 사람들 그 파일의 소유자나 혹은 그 그룹의 멤머가 아닌 사용자
a 모든 사람 위의 셋 모두, "ugo"와 같다

파일의 모드들을 조정할 수 있는 연산자는 다음과 같다.

연산자 설명
+ 지정된 모드들은 지정된 클래스들에 더한다
- 지정된 클래스들로부터 지정된 모드들은 지운다
= 지정된 클래스들을 위해서 지정된 모드들이 정확한 모드들로 만들어지게 된다

기본적인 허가들과 일치하는 세 가지 기본적인 모드들은 다음과 같다.

모드 이름 설명
r 읽기(read) 파일을 읽거나 디렉터리 안 내용물의 리스트 나열
w 쓰기(write) 파일이나 디렉터리에 쓰기
x 실행(excute) 파일을 실행하거나 디렉터리 트리로 되돌아가기

 

 

2. Absolute form method - 8진법 숫자

chmod 명령어는 모드들을 나타내는 세 자리 혹은 네 자리 8진수도 받아들인다.

레퍼런스 읽기(r) 쓰기(w) 실행(x)
유저(u) 4 2 1
그룹(g) 4 2 1
다른 사람들(o) 4 2 1

 

위 표에서 허용하는 권한에 대응되는 수를 각 클래스마다 더한 뒤, 유저, 그룹, 다른 사람들의 순서로 모드를 설정할 수 있다. 모든 사용자들에게 모든 권한을 허용하는 chmod 777을 사용하는 것은 권장하지 않는다.

■ 예시

하위 예시들에서 방법들을 보면 정말 다양하게 설정이 가능하다. 이해를 위해 여러 가지 방법으로 표기를 해놓은 것도 있고 가짓수가 많아 생략을 한 것도 있다.

 

 ● sample 파일에 대한 유저, 그룹, 다른 사람들에게 각각 읽기·쓰기·실행, 읽기·실행, 읽기·실행 권한 설정

[root@TEST test]# ls -l sample
----------. 1 root root 0 Jan  9 05:29 sample
[root@TEST test]# chmod 755 sample ############################## sol
[root@TEST test]# chmod u=rwx,go=rx sample ############################## sol
[root@TEST test]# chmod u=r+wx,go=r+x sample ############################## sol
[root@TEST test]# chmod u=+r+w+x,g=+r+x,o=+r+x sample ############################## sol
[root@TEST test]# chmod u=+r+w+x,go=+r+x sample ############################## sol
[root@TEST test]# ls -l sample
-rwxr-xr-x. 1 root root 0 Jan  9 05:29 sample

 

 ● 사용자, 그룹에 sample 파일에  대한 쓰기, 실행 권한 부여

[root@TEST test]# ls -l sample
-rw-r--r--. 1 root root 0 Jan  9 05:29 sample
[root@TEST test]# chmod ug+rx sample ############################## sol
[root@TEST test]# chmod u+rx,g+rx sample ############################## sol
[root@TEST test]# chmod u+r+x,g+r+x sample ############################## sol
[root@TEST test]# ls -l sample
-rwxr-xr--. 1 root root 0 Jan  9 05:29 sample

 

 ● sample 파일에 대한 모든 권한 삭제

[root@TEST test]# ls -l sample
-rwxr-xr-x. 1 root root 0 Jan  9 05:29 sample
[root@TEST test]# chmod a-rwx sample ############################## sol
[root@TEST test]# chmod 000 sample ############################## sol
[root@TEST test]# ls -l sample
----------. 1 root root 0 Jan  9 05:29 sample

 

 ● sample 파일에 대한 사용자, 다른 사람들에게 읽기, 실행 권한 설정

[root@TEST test]# ls -l sample
----------. 1 root root 0 Jan  9 05:29 sample
[root@TEST test]# chmod uo=rx sample ############################## sol
[root@TEST test]# chmod 505 sample ############################## sol
[root@TEST test]# ls -l sample
-r-x---r-x. 1 root root 0 Jan  9 05:29 sample

 

 ● 모두에게 읽기 권한 추가

[root@TEST test]# ls -l sample
--wx--x--x. 1 root root 0 Jan  9 05:29 sample
[root@TEST test]# chmod +r sample ############################## sol
[root@TEST test]# ls -l sample
-rwxr-xr-x. 1 root root 0 Jan  9 05:29 sample

 

 ● 모두에게 실행 권한 제거

[root@TEST test]# ls -l sample
-rwxr-xr-x. 1 root root 0 Jan  9 05:29 sample
[root@TEST test]# chmod -x sample ############################## sol
[root@TEST test]# ls -l sample
-rw-r--r--. 1 root root 0 Jan  9 05:29 sample

 

 ● 파일의 읽기 권한과 쓰기 권한을 소유자에게 설정, 그룹이나 다른 사람들에게 모든 허가 제거

[root@TEST test]# ls -l sample
-rwxr-xr-x. 1 root root 0 Jan  9 05:29 sample
[root@TEST test]# chmod u=rw,go= sample ############################## sol
[root@TEST test]# chmod 700 sample ############################## sol
[root@TEST test]# ls -l sample
-rwx------. 1 root root 0 Jan  9 05:29 sample

 

 ● 모두에게 읽기, 쓰기 권한 추가

[root@TEST test]# ls -l sample
-rw-r--r--. 1 root root 0 Jan  9 05:29 sample
[root@TEST test]# chmod ugo+rw sample ############################## sol
[root@TEST test]# chmod a+rw sample ############################## sol
[root@TEST test]# ls -l sample
-rw-rw-rw-. 1 root root 0 Jan  9 05:29 sample

 

 ● 모두에게 모든 권한 제거

[root@TEST test]# ls -l sample
-rw-rw-rw-. 1 root root 0 Jan  9 05:29 sample
[root@TEST test]# chmod ugo= sample ############################## sol
[root@TEST test]# chmod u=,g=,o= sample ############################## sol
[root@TEST test]# chmod a= sample ############################## sol
[root@TEST test]# chmod a-r-w-x sample ############################## sol
[root@TEST test]# chmod a=-r-w-x sample ############################## sol
[root@TEST test]# chmod 000 sample ############################## sol
[root@TEST test]# ls -l sample
----------. 1 root root 0 Jan  9 05:29 sample

 

 ● docs 디렉터리와 docs 내의 모든 파일 및 디렉토리에 대해 유저에게 읽기·쓰기·실행 권한 설정, 그룹과 다른 사람들에게 읽기·실행 권한 설정

[root@TEST test]# ls -l docs/
total 0
-rw-r--r--. 1 root root 0 Jan  9 07:33 test
-rw-r--r--. 1 root root 0 Jan  9 07:33 test1
[root@TEST test]# chmod -R 755 docs/ ############################## sol
[root@TEST test]# chmod -R u=rwx,go=rx docs/ ############################## sol
[root@TEST test]# chmod -R u=+r+w+x,go=+r+x docs/ ############################## sol
[root@TEST test]# ls -l docs/
total 0
-rwxr-xr-x. 1 root root 0 Jan  9 07:33 test
-rwxr-xr-x. 1 root root 0 Jan  9 07:33 test1

 

※ 그룹(group) : group은 여러 user가 포함될 수 있다. 그룹에 속한 모든 user는 파일에 대해 동일한 권한을 갖게 된다. 많은 사람이 접근해야 하는 프로젝트라 가정하면, 각 user에게 접근 권한을 부여하는 것보다 user를 group에 추가하고 파일 권한을 group에 부여하는 것이 효율적일 것이다.

 

※ 다른 사용자(other) : user와 group이 아닌 나머지 사용자를 뜻한다. 따라서 other 권한을 설정하면, 해당 권한을 다른 사용자가 접근할 수 있어 global 권한 설정이라고 볼 수 있다.

 

 

출처 : https://ko.wikipedia.org/wiki/Chmod

 

chmod - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. chmod(change mode의 축약어)명령어는 유닉스와 유닉스 계통 환경 안에서 쓰이는 셸 명령어이다. 이 명령어는 파일들이나 디렉터리의 파일 시스템 모드들을 바꾼다.

ko.wikipedia.org

출처 : https://ittrue.tistory.com/83

 

[Linux] 리눅스의 사용자 권한과 chmod 명령어

리눅스의 사용자 권한 확인하기 리눅스의 파일과 디렉토리는 사용권한이 존재한다. 사용 권한에 대해 알아보기 위해 다음 명령어로 디렉토리와 파일을 생성한다. mkdir hello nano hello.java 파일과

ittrue.tistory.com

출처 : https://recipes4dev.tistory.com/175

 

리눅스 chmod 명령어 사용법. (Linux chmod command) - 리눅스 파일 권한 변경.

1. 리눅스 파일 사용 권한 리눅스에서, 파일(File)을 사용해 할 수 있는 작업은 크게 세 가지로 나눌 수 있습니다. 파일에 저장된 데이터를 읽기. (r = read). 파일에 데이터를 쓰기. (w = write). 파일 실

recipes4dev.tistory.com

 

'소프트웨어, 운영체제 > Linux' 카테고리의 다른 글

Linux_tree  (0) 2024.01.20
Linux_chgrp(change group)  (1) 2024.01.15
Linux_chown(change the owner of a file)  (1) 2024.01.08
Linux_정의 및 특징  (0) 2023.11.28
Linux_배포판 종류 및 특징  (0) 2023.11.27
Comments