본문 바로가기
IT/리눅스 명령어

리눅스 기본 명령어 chown 옵션 총정리 사용법 - 파일 및 디렉토리 소유자 변경

by 꾸꾸웍스 2022. 5. 3.
반응형

리눅스 기본 명령어 chown 옵션 총정리 사용법 - 파일 및 디렉토리 소유자 변경



※ 목차

1. 리눅스 chown 명령어란?
2. 리눅스 chown 명령어 규칙 및 설명
3. 리눅스 chown 명령어 옵션
4. 리눅스 chown 명령어 옵션 사용법

 

 

1. 리눅스 chown 명령어란?

 

chown 명령어는 Change와 Owner의 앞글자를 조합한 명령어로 파일의 소유권과 그룹을 변경하는 명령어 입니다.

아래 그림과 같이 소유자 또는 소유자그룹 또는 소유자와 소유자그룹을 변경 할 수 있습니다.

빨간 네모의 왼쪽 root는 apple파일의 소유자이고 오른쪽 root는 apple 파일의 소유자그룹을 의미합니다.

소유자는 파일의 권한을 가지고있는 apple 파일의 주인을 의미하고 소유자그룹은 root 그룹안에 있는 사용자들도 권한을 사용할 수 있습니다.

 

 

 

 



 

 

2. 리눅스 chown 명령어 규칙 및 설명

 

chown 명령어 기본 사용법

chown [옵션] 소유자[:소유자그룹] [파일 및 디렉토리]

  • 옵션은 생략이 가능합니다.
  • chown 명령어 사용시 소유자는 반드시 입력해야 되며 소유자그룹은 선택사항입니다.
 chown [OPTION] [OWNER][:[GROUP]] FILE
 
 ### 사용예시
 chown -R user apple

chown 명령어 소유자 변경

chown [옵션] 소유자 [파일 및 디렉토리]

  • 옵션을 사용하지 않고 root 사용자가 apple 파일을 root 소유주로 변경합니다.
root@server # ls -l
total 4
-rw-r--r-- 1 testuser usergroup 11597 Mar 10 13:26 apple

root@server # chown root apple

root@server # ls -l
total 4
-rw-r--r-- 1 root usergroup 11597 Mar 10 13:26 apple

chown 명령어 소유자그룹 변경

chown [옵션] 소유자그룹 [파일 및 디렉토리]

  • 소유자그룹 앞에 . 또는 : 을 붙여 소유자그룹을 변경 할 수 있습니다.
  • 옵션을 사용하지 않고 root 사용자가 apple 파일을 root 소유주그룹을 변경합니다.
### 소유자그룹 지정 시 앞에 . 을 붙여 소유자 그룹만 변경
root@server # ls -l
total 4
drwxr-xr-x 2 testuser usergroup 20 May  2 17:06 apple

root@server # chown .root apple

root@server # ls -l
total 4
drwxr-xr-x 2 testuser root 20 May  2 17:06 apple

-----------------------------------------------------------------
### 소유자그룹 지정 시 앞에 : 을 붙여 소유자 그룹만 변경
root@server # ls -l
total 4
drwxr-xr-x 2 testuser usergroup 20 May  2 17:06 apple

root@server # chown :root apple

root@server # ls -l
total 4
drwxr-xr-x 2 testuser root 20 May  2 17:06 apple

chown 명령어 소유자와 소유자그룹 변경

chown [옵션] 소유자:소유자그룹 [파일 및 디렉토리]

  • 소유자와 소유자그룹은 콜론을 사용해 구분합니다.
  • 소유자그룹을 직접 언급하지 않아도 소유자다음에 . 또는 : 을 사용하여 일괄 변경 할 수 있습니다.
  • 옵션을 사용하지 않고 root 사용자가 apple 파일을 root 소유주와 소유주그룹을 변경합니다.
  • 소유자에 특수기호를 붙이지 않으면 소유자만 변경 됩니다.
### chown 소유자와 소유자그룹 변경 기본사용법

root@server # ls -l
total 0
drwxr-xr-x 2 testuser usergroup 20 May  2 17:06 apple

### 아래 두가지 받법은 같은 결과( . 와 : 의 차이)
root@server # chown root:root apple
root@server # chown root.root apple

### 소유자와 소유자 그룹이 root로 변경
root@server # ls -l
total 0
drwxr-xr-x 2 root root 20 May  2 17:06 apple

--------------------------------------------------------------
### 소유자그룹과 특수기호로 소유자와 소유자그룹 변경
root@server # ls -l
total 0
drwxr-xr-x 2 testuser usergroup 20 May  2 17:06 apple

### 아래 두가지 받법은 같은 결과( . 와 : 의 차이)
root@server # chown root: apple
root@server # chown root. apple

root@server # ls -l
total 0
drwxr-xr-x 2 root root 20 May  2 17:06 apple

--------------------------------------------------------------

root@server # ls -l
total 0
drwxr-xr-x 2 testuser usergroup 20 May  2 17:06 apple

### 특수기호를 제외한 소유자만 사용시
root@server # chown root apple

### 소유자만 변경됨
root@server # ls -l
total 0
drwxr-xr-x 2 root usergroup 20 May  2 17:06 apple

 


반응형

 

 

3. 리눅스 chown 명령어 옵션

 

번호 옵션 long 옵션 설명
1 -c --changes 기존에 지정된 모드에서 변경되는 경우에만 출력합니다
2 -f --silent, --quiet 오류메세지를 출력하지 않습니다
3 -v --verbose 처리된 모든 파일 및 디렉토리에 대한 메세지를 출력합니다
4 - --dereference 심볼릭링크로 연결된 참조파일이 변경됩니다.
5 -h --no-dereference 심볼릭링크 파일이 변경됩니다.
6 - --from=CURRENT_OWNER:CURRENT_GROUP 기존의 소유주로부터 변경할 소유주로 변경합니다.
7 - --no-preserve-root '/' 최상위 루트 디렉토리를 변경 할 수 있습니다
8 - --preserve-root '/' 최상위 루트로부터 하위를 변경 할 수 없게 합니다.
9 - --reference=RFILE 지정한 파일에 대해 참조할 파일과 동일하게 변경됩니다
10 -R --recursive 지정한 파일 하위까지 변경합니다
11 -H - (-R 옵션같이 사용) 심볼릭링크의 참조파일만 변경됩니다
12 -L - (-R 옵션같이 사용) 심볼릭링크를 제외하고 변경합니다
13 -P - (-R 옵션같이 사용) 심볼릭링크 자체에서 변경됩니다
14 - --help chown 명령어의 사용법을 출력합니다.
15 - --version chown 명령어의 버전을 출력합니다.

 


4. 리눅스 chown 명령어 옵션 사용법

 

chown -c

기존에 지정된 모드에서 변경되는 경우에만 출력합니다.

 

root@server # ls -l 
total 0
-rw-r--r-- 1 testuser root 0 May  2 14:54 apple

root@server # chown -c testuser:usergroup apple

### testuser:root에서 testuser:usergroup로 변경되어 메세지 출력
root@server # chown -c testuser:root apple
changed ownership of 'apple' from testuser:usergroup to testuser:root

chown -f

오류 메세지를 출력하지 않습니다.

 

root@server # ls -l
total 0
-rw-r--r-- 1 testuser root 0 May  2 14:54 apple

### testuser과 usergroup 콜론 대신 스페이스바가 들어가서 파일 또는 디렉토리로 인식
root@server # chown tsetuser usergroup apple
chown: cannot access 'kca': No such file or directory

### -f 옵션을 사용해 오류메세지 출력하지 않음
root@server # chown -f swhong kca apple

chown -v

처리된 모든 파일 및 디렉토리에 대한 메세지를 출력합니다.

 

root@server # ls -l
total 0
-rw-r--r-- 1 testuser usergroup  0 May  2 14:54 apple

### 소유자를 동일하게 명령해도 메세지 출력
root@server # chown -v testuser apple
ownership of 'apple' retained as testuser

### 소유자와 소유자그룹을 동일하게 명령해도 메세지 출력
root@server # chown -v testuser:usergroup apple
ownership of 'apple' retained as testuser:usergroup

chown --dereference

심볼릭링크로 연결된 참조파일이 변경됩니다.

 

root@server # ls -l
total 0
-rw-r--r--  1 testuser root  0 May  2 14:54 apple
lrwxrwxrwx 1 root     root 30 May  2 17:03 banana -> /home/usergroup/testuser/testdir/apple

root@server # chown --dereference bjahn banana

root@server # ls -l
total 0
-rw-r--r-- 1 bjahn root  0 May  2 14:54 apple
lrwxrwxrwx 1 root  root 30 May  2 17:03 banana -> /home/usergroup/testuser/testdir/apple

chown -h

심볼릭링크로 연결된 참조파일이 아니라 심볼릭링크 파일의 소유자와 소유자그룹이 변경됩니다.

 

root@server # ls -l
total 0
-rw-r--r-- 1 bjahn root  0 May  2 14:54 apple
lrwxrwxrwx 1 root  root 30 May  2 17:03 banana -> /home/usergroup/testuser/testdir/apple

root@server # chown -h testuser banana

root@server # ls -l
total 0
-rw-r--r-- 1 bjahn  root  0 May  2 14:54 apple
lrwxrwxrwx 1 testuser root 30 May  2 17:03 banana -> /home/usergroup/testuser/testdir/apple

chown --from=CURRENT_OWNER:CURRENT_GROUP

기존의 소유주로부터 변경할 소유주로 변경합니다.

 

### apple 파일은 소유자가 testuser, 소유자그룹이 usergroup
root@server # ls -l
total 0
drwxr-xr-x 2 testuser usergroup 20 May  2 17:06 apple

### 기존 소유자 testuser로 부터 root 사용자로 변경
root@server # chown --from=testuser root apple

### 소유자만 root 변경
root@server # ls -l
total 0
drwxr-xr-x 2 root usergroup 20 May  2 17:06 apple

-----------------------------------------------------------
### apple 파일은 소유자가 testuser, 소유자그룹이 usergroup
root@server # ls -l
total 0
drwxr-xr-x 2 testuser usergroup 20 May  2 17:06 apple

### 기존 소유자그룹 usergroup로 부터 root 소유자그룹으로 변경(콜론사용)
root@server # chown --from=:usergroup :root apple

root@server # ls -l
total 0
drwxr-xr-x 2 testuser root 20 May  2 17:06 apple

-----------------------------------------------------------
### apple 파일은 소유자가 testuser, 소유자그룹이 usergroup
root@server # ls -l
total 0
drwxr-xr-x 2 testuser usergroup 20 May  2 17:06 apple

### 기존 소유자와 소유자그룹을 둘다 변경
root@server # chown --from=testuser. root. apple

root@server # ls -l
total 0
drwxr-xr-x 2 root root 20 May  2 17:06 apple

chown --no-preserve-root

'/' 최상위 루트로부터 하위를  변경 할 수 있습니다

  • 최상위 루트 디렉토리로 부터 하위에 대한 소유자나 소유자 그룹을 변경하게 된다면 시스템에
    치명적인 오류를 주기때문에 취소한다고해도 이미 손을 쓸 수없게 빠르게 작동하여 시스템을
    사용하지 못하는 단계가 됩니다.
  • 사용을 하더라도 모든디렉토리에서 작동하는 것을 거부합니다.

chown --preserve-root

'/' 최상위 루트로부터 하위를 변경 할 수 없게 합니다.

  • 최상위 루트 디렉토리로 부터 하위에 대한 소유자나 소유자 그룹을 변경하게 되면
    시스템이 사용할 수 없는 상태가 되기 때문에 루트를 보호하는데 사용하는 인수입니다.
  • -R 옵션과 함께 사용하면 아무 작업도 수행되지 않고 메세지가 나타납니다.

 

root@server # chown -cfR --preserve-root alan /
chown: it is dangerous to operate recursively on '/'
chown: use --no-preserve-root to override this failsafe

chown --reference=RFILE

지정한 파일에 대해 참조할 파일과 동일하게 변경됩니다.

 

root@server # ll
total 0
-rw-r--r-- 1 testuser usergroup  0 May  2 17:05 apple
-rw-r--r-- 1 root     root       0 May  2 17:05 banana

### apple 파일의 소유자와 소유권그룹을 참조하여 banana 파일에 적용
root@server # chown --reference=apple banana


root@server # ll
total 0
-rw-r--r-- 1 testuser usergroup 0 May  2 17:05 apple
-rw-r--r-- 1 testuser usergroup 0 May  2 17:05 banana

chown -R

지정한 파일 및 디렉토리 하위까지 소유자 및 소유자그룹을 변경합니다.

 

### root소유자를 가진 apple 디렉토리 밑에 root 소유자 banana 파일이 존재
root@server # ls -l
total 0
drwxr-xr-x 2 root root 20 May  2 17:06 apple

### -R 옵션을 사용해서 apple 디렉토리 하위 까지 testuser 소유자로 변경
root@server # chown -R testuser apple

root@server # ls -l
total 0
drwxr-xr-x 2 testuser root 20 May  2 17:06 apple

### apple 디렉토리로 이동
root@server # cd apple/

### banana파일이 testuser로 변경되었는지 확인
root@server # ls -l
total 0
-rw-r--r-- 1 testuser root 0 May  2 17:06 banana

chown -H

(-R 옵션같이 사용) 심볼릭링크의 참조파일만 변경됩니다

 

### apple 디렉토리 하위에 파일 및 디렉토리가 존재
root@server # ls -l
total 0
drwxr-xr-x 3 root root 34 May  3 15:56 apple

### -RH 옵션을 사용하여 변경
root@server # chown -RH testuser apple/

root@server # ls -l
total 0
drwxr-xr-x 3 testuser root 34 May  3 15:56 apple

root@server # cd apple/

### 심볼릭링크가 참조하는 파일의 소유자가 변경
root@server # ls -l
total 0
lrwxrwxrwx 1 root     root  7 May  3 15:26 test5 -> testdir
drwxr-xr-x 2 testuser root 33 May  3 15:56 testdir

chown -L

(-R 옵션같이 사용) 심볼릭링크를 제외하고 변경합니다

 

### apple 디렉토리 하위에 파일 및 디렉토리가 존재
root@server # ls -l
total 0
drwxr-xr-x 3 root root 34 May  3 15:56 apple

### -RL 옵션을 사용하여 변경
root@server # chown -RL testuser apple/

root@server # ls -l
total 0
drwxr-xr-x 3 testuser root 34 May  3 15:56 apple

root@server # cd apple/

### 심볼릭링크를 제외하고 변경
root@server # ls -l
total 0
lrwxrwxrwx 1 root     root  7 May  3 15:26 test5 -> testdir
drwxr-xr-x 2 testuser root 33 May  3 15:56 testdir

chown -P

(-R 옵션같이 사용) 심볼릭링크 자체에서 변경됩니다

 

### apple 디렉토리 하위에 파일 및 디렉토리가 존재
root@server # ls -l
total 0
drwxr-xr-x 3 root root 34 May  3 15:56 apple

### -RP 옵션을 사용하여 변경
root@server # chown -RP testuser apple/

root@server # ls -l
total 0
drwxr-xr-x 3 testuser root 34 May  3 15:56 apple

root@server # cd apple/

### 심볼릭링크 자체 변경, testdir 디렉토리는 apple 디렉토리 하위라서 변경
root@server # ls -l
total 0
lrwxrwxrwx 1 testuser root  7 May  3 15:26 test5 -> testdir
drwxr-xr-x 2 testuser root 33 May  3 15:56 testdir

chown --version

chown 명령어의 사용법을 출력합니다.

 

root@server # chown --help
Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...
  or:  chown [OPTION]... --reference=RFILE FILE...
Change the owner and/or group of each FILE to OWNER and/or GROUP.
With --reference, change the owner and group of each FILE to those 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
      --dereference      affect the referent of each symbolic link (this is
                         the default), rather than the symbolic link itself
  -h, --no-dereference   affect symbolic links instead of any referenced file
                         (useful only on systems that can change the
                         ownership of a symlink)
      --from=CURRENT_OWNER:CURRENT_GROUP
                         change the owner and/or group of each file only if
                         its current owner and/or group match those specified
                         here.  Either may be omitted, in which case a match
                         is not required for the omitted attribute
      --no-preserve-root  do not treat '/' specially (the default)
      --preserve-root    fail to operate recursively on '/'
      --reference=RFILE  use RFILE's owner and group rather than
                         specifying OWNER:GROUP values
  -R, --recursive        operate on files and directories recursively

The following options modify how a hierarchy is traversed when the -R
option is also specified.  If more than one is specified, only the final
one takes effect.

  -H                     if a command line argument is a symbolic link
                         to a directory, traverse it
  -L                     traverse every symbolic link to a directory
                         encountered
  -P                     do not traverse any symbolic links (default)

      --help     display this help and exit
      --version  output version information and exit

Owner is unchanged if missing.  Group is unchanged if missing, but changed
to login group if implied by a ':' following a symbolic OWNER.
OWNER and GROUP may be numeric as well as symbolic.

Examples:
  chown root /u        Change the owner of /u to "root".
  chown root:staff /u  Likewise, but also change its group to "staff".
  chown -hR root /u    Change the owner of /u and subfiles to "root".

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'chown invocation'

chown --version

chown 명령어의 버전을 출력합니다.

 

root@server # chown --version
chown (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie and Jim Meyering.

 

 



 


 

반응형
그리드형

댓글