본문 바로가기
+ OS/Linux & Unix

[Linux] chmod 명령어로 파일 / 디렉토리 권한 변경하기(Changing file/directory permissions with chmod command)

by :: Teacher :: 2020. 12. 26.
728x90
반응형

 

리눅스(Linux)에서 파일이나 디렉토리의 권한을 소유자, 소유 그룹, 그 외 사용자들에 맞게 설정하여 사용하고 있다. 

이때 특정 파일이나 디렉토리의 경우 어떠한 이유(보안상 이유 등)로 권한을 줄 수도 있고 뺄 수도 있다. 

이럴 때 사용하는 명령어가 chmod이다. chmod는 Change mode의 약어이다.  

그럼 이제 chmod 명령어를 통해서 파일 / 디렉토리의 권한을 변경해보도록 하자. 

1. chmod 명령어 사용법

기본적으로 chmod 명령어 아래와 같이 사용한다.

  • $ chmod 755 test.sh
# chmod 명령어 사용법

$ chmod --help
사용법: chmod [옵션]... MODE[,MODE]... FILE...
  또는: chmod [옵션]... 8진수-MODE FILE...
  또는: chmod [옵션]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.

  -c, --changes          like verbose but report only when a change is made
  -f, --silent, --quiet  suppress most error messages
  -v, --verbose          output a diagnostic for every file processed
      --no-preserve-root  do not treat '/' specially (the default)
      --preserve-root    fail to operate recursively on '/'
      --reference=RFILE  use RFILE's mode instead of MODE values
  -R, --recursive        change files and directories recursively
      --help     이 도움말을 표시하고 끝냅니다
      --version  버전 정보를 출력하고 끝냅니다

Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report chmod translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/chmod>
or available locally via: info '(coreutils) chmod invocation'

728x90

2. 파일 / 디렉토리 권한 정보

리눅스 파일 / 디렉토리는 소유자(User), 소유 그룹(Group), 그외 사용자(Others)에 읽기(Read), 쓰기(Write), 실행(Execute) 권한을 부여할 수 있다. 

예를 들어 "ls" 명령을 통해서 파일에 부여된 권한을 확인할 수 있다. 

# Test File 권한

$ ls -alth test.sh
-rw-r--r--. 1 root root 0 12월 26 15:34 test.sh

해당 test.sh 파일은 소유자(User)는 읽기(Read), 쓰기(Write) 권한을 가지고 있고, 소유 그룹(Group)과 모든 사용자(Others)에는 읽기(Read) 권한을 가지고 있는 것을 확인할 수 있다.

그럼 디렉토리도 한번 확인해보도록 하자.

# Test Directory 권한

$ ls -alth
drwxr-xr-x. 2 root root    6 12월 26 15:40 test
-rw-r--r--. 1 root root    0 12월 26 15:34 test.sh

해당 test 디렉토리는 소유자(User)는 읽기(Read), Write(쓰기), 실행(Execute권한을 가지고 있고, 소유 그룹(Group)과 모든 사용자(Others)에는 읽기(Read), 실행(Execute) 권한을 가지고 있는것을 확인할 수 있다.

다만 파일과 디렉토리의 차이점은 맨 앞에 위치한 부분에 "-" 경우에는 파일이고 "d" 경우에는 디렉토리로 구분할 수 있다. 

레퍼런스 내용 설명
User 소유자 파일의 소유자
Group 소유 그룹 파일의 소유 그룹 맴버 사용자
Others 그외 사용자 소유자 및 소유 그룹 맴법가 아닌 사용자

3. 파일 / 디렉토리 권한 변경 방법

파일 / 디렉토리의 권한 변경을 해보도록 하자.

그전에 chmod 명령어를 사용하는 경우 대부분 8진수를 사용하여 변경을 하게 되는데 8진수를 어떻게 나타내는지 알아보도록 하자. 

3.1 8진수 표현법

예로 확인했던 test.sh를 보면 "-rw-r--r--" 이렇게 되어 있다. 

이중 맨 처음은 파일 / 디렉토리 구분 자이며, 맨 처음을 제외하면 3개씩 3가지로 구분을 할 수 있다.

  • rw- : 6
  • r--  : 4
  • r--  : 4

위에서도 읽기(Read), Write(쓰기), 실행(Execute)에 대해서 설명했으며 각 권한에 값을 나타내고 있다. 

  • r(Read) : 4
  • w(Write) : 2
  • x(Execute) : 1
  • r + w + x : 7

3.2 변경 방법 예제

8진수 표현법도 확인했으니 정말로 변경 하는 방법을 예제로 확인해보자. 

# 8진수로 권한 추가 / 삭제

# 기본 권한
$ ls -alt test.sh
-rw-r--r--. 1 root root 0 12월 26 15:34 test.sh

# 권한 추가
$ chmod 777 test.sh
$ ls -alth test.sh
-rwxrwxrwx. 1 root root 0 12월 26 15:34 test.sh

# 권한 삭제
$ chmod 600 test.sh
$ ls -alth test.sh
-rw-------. 1 root root 0 12월 26 15:34 test.sh

8진수 표현법 이외에도 +, - 를 통해서도 권한을 추가, 삭제할 수 있다. 

# +,- 를 이용한 권한 추가 / 삭제

# 기본 권한
$ ls -alt test.sh
-rw-r--r--. 1 root root 0 12월 26 15:34 test.sh

# 권한 추가
$ chmod u+x,g+w,o+wx test.sh
$ ls -alth test.sh
-rwxrw-rwx. 1 root root 0 12월 26 15:34 test.sh

# 권한 삭제
$ chmod u-r,g-r,o-r test.sh
$ ls -alth test.sh
--wx-w--wx. 1 root root 0 12월 26 15:34 test.sh

8진수 혹은 +,- 를 이용하던 chmod 명령어를 통해서 파일 / 디렉토리의 권한을 추가, 삭제할 수 있다. 

권한 변경을 통해서 보안적 이슈가 발생할수도 있어 권한 변경을 할 때는 사용에 맞도록 잘 맞춰 변경을 해야 한다. 

728x90
반응형

댓글


loading