[리눅스] Read, Write, Execute 권한

정성준 (Seongjun Chung)·2021년 7월 19일
0

TIL

목록 보기
1/1
post-thumbnail

평소 개발을 공부하면서 혹은, 어떤 에러를 만났을 때, 이 내용은 구글링을 하다보면 나오는 내용이였지만 그 당시에는 이해하기 어려웠고 어떤걸 찾아봐야하는지도 모르던 때가 있었다.
그 당시 직면한 문제를 해결하기 위한 목표는 있었어도 이 내용들을 궁금해하거나 찾아보려고 하진 않았었던 것 같다. 마침, 코스 수업 내용으로 나왔고 해당 내용들이 평소 궁금했던 내용들을 잘 설명해주고 있어 정리해두려고 한다.📝


우선 리눅스 CLI 환경에서의 폴더와 파일 생성은 아래와 같다.

mkdir linux		// linux 폴더 생성
nano helloworld.js	// helloworld 파일 생성

궁금했던 ls -l 의 정체

CLI 프롬프트에 ls -l을 입력했을 때 만날 수 있는 화면이다.

여기서 username은 사용자 이름이다.

출력의 표현 중 첫 시작인 -d 는 not directory, directory 를 나타낸다. 폴더이면 d, 파일이면 - 로 표시된다.
그리고 이어지는 rwx 는 각 알파벳이 read, write, execute인 읽기, 쓰기, 실행에 대한 각각의 권한이 있는지를 나타내는 표현이다. 여기서 rwx 가 3번씩 반복되는 이유는 사용자, 그룹 그리고 나머지에 대한 권한을 표시하기 때문이다. 그러므로 위에서 helloworld.js의 파일에 나타나는 권한은 소유자는 읽기와 쓰기가 가능하고, 그룹과 나머지는 읽기만 가능하다는 뜻이다.

폴더나 파일에 대한 권한 정보

user, group, other

user:

  • user는 파일의 소유자, 기본적으로 파일을 만든 사람이 소유자가 된다. user를 소유자라고 하기도 한다.

group:

  • group에는 여러 user가 포함될 수 있다. 그룹에 속한 모든 유저는 파일에 대한 동일한 그룹 접근 권한을 갖는다. 예를 들어, 많은 사람이 파일에 접근해야하는 프로젝트가 있을 때, 각 유저에게 일일이 권한을 할당하는 대신 그룹에 모든 유저를 추가하고 파일에 그룹 권한을 할당할 수 있다.

other:

  • 파일에 대한 접근 권한이 다른 유저를 뜻한다. 이 뜻은 결국 파일을 만들지 않은 다른 모든 유저를 의미한다. 따라서 other설정은 global 권한 설정이라고도 볼 수 있다.

chmod - 권한을 변경하는 명령어

명령어 chmod 를 이용하여 폴더나 파일의 rwx 권한을 변경 가능하다. 만약 OS에 로그인한 사용자와, 폴더나 파일의 소유자가 다를 경우 관리자 권한 획득 명령어인 sudo 를 이용하여 파일의 권한을 변경할 수 있다.

chmod 로 권한을 변경하는 방식에는 두 가지가 있다.

  • 첫번째, 더하기(+), 빼기(-), 할당(=)과 접근권한 유형을 표기하여 변경하는 Symbolic method
  • 두번째, rwx 를 3bit로 해석하여, 숫자 3자리로 권한을 표기하여 변경하는 Absolute form

Symbolic method

Access classOperatorAccess Type
u (user)+ (add access)r (read)
g (group)- (remove access)w (write)
o (other)= (set exact access)x (execute)
a (all: u, g, o)

Symbolic method의 구분

사용법은 명령어 chmod 뒤에 변경할 권한을 입력한다. 변경할 권한은 액세스 클래스의 u, g, o, a를 변경 조합에 따라 입력하고, 연산자와 액세스 타입을 순서대로 입력한다. 예시는 이렇다.

chmod g-r filename # removes read permission from group
chmod g+r filename # adds read permission to group
chmod g-w filename # removes write permission from group
chmod g+w filename # adds write permission to group
chmod g-x filename # removes execute permission from group
chmod g+x filename # adds execute permission to group
chmod o-r filename # removes read permission from other
chmod o+r filename # adds read permission to other
chmod o-w filename # removes write permission from other
chmod o+w filename # adds write permission to other
chmod o-x filename # removes execute permission from other
chmod o+x filename # adds execute permission to other
chmod u+x filename # adds execute permission to user

symbolic method 사용 예시

chmod a=rw helloworld.js # -rw-rw-rw-
chmod u= helloworld.js # ----rw-rw-
chmod a+rx helloworld.js # -r-xrwxrwx
chmod go-wx helloworld.js # -r-xr--r--
chmod a= helloworld.js # ----------
chmod u+rwx helloworld.js # -rwx------

chmod 명령어와 symbolic method로 helloworld.js 파일의 권한을 변경

Absolute form

Absolute form은 숫자 7까지 나타내는 3 bits의 합으로 표기한다.
사용자, 그룹, 나머지사용자의 그룹마다 rwx 가 나타나고, 각 영역의 boolean 값으로 표기 가능하다.
|Permission|Number|
|---|---|
|Read (r)|4|
|Write (w)|2|
|Execute (x)|1|

Absolute form 구분

만약, 사용자는 rwx, 그룹과 나머지는 r-x 로 권한을 변경하려고 한다면, 위 표의 숫자의 합을 사용자, 그룹, 나머지 순으로 입력하여 사용한다.

// u=rwx (4 + 2 + 1 = 7), go=r-x (4 + 0 + 1 = 5)
chmod 755 helloworld.js		// -rwxr-xr-x
#SumrwxPermission
74(r) + 2(w) + 1(x)rwxread, write and execute
64(r) + 2(w) + 0(-)rw-read and write
54(r) + 0(-) + 1(x)r-xread and execute
44(r) + 0(-) + 0(-)r--read only
30(-) + 2(w) + 1(x)-wxwrite and execute
20(-) + 2(w) + 0(-)-w-write only
10(-) + 0(-) + 1(x)--xexecute only
00(-) + 0(-) + 0(-)---none

Absolute form 의 숫자별 권한

profile
ZEP에서 프론트엔드 개발을 하고 있습니다.

0개의 댓글

관련 채용 정보