Linux常用命令-chown

命令

chown

描述

change file owner and group
更改文件所有者和所属组

用法

1
2
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

选项

1
2
3
4
5
6
7
8
9
10
Options:
-c 只显示权限更改情况,如果没有更改则不显示
-f 不显示错误信息
-v 显示详情
-R 递归选项
--dereference 更改源文件属性
-h, --no-dereference 仅更改符号链接本身而不更改源文件属性
--no-preserve-root 允许更改/根属性(默认选项)
--preserve-root 不允许递归更改/根属性
--reference=RFILE 根据指定文件属性更改目标文件属性

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 默认是更改源文件属性,而非软链接本身,注意软链接本身的权限就是777
$ ll
drwxr-xr-x 3 root root 48 Jan 11 22:04 dir2/
lrwxrwxrwx 1 root root 5 Jan 11 21:51 dir3 -> dir2/
$ chown usera dir3
$ ll
drwxr-xr-x 3 usera root 48 Jan 11 22:04 dir2/
lrwxrwxrwx 1 root root 5 Jan 11 21:51 dir3 -> dir2/
# 递归更改dir2的目录属性为usera用户和usera组
$ chown -R usera:usera dir2
$ ll dir2
-rwxr-xr-x 1 usera usera 3 Jan 11 21:19 a
drwxr-xr-x 2 usera usera 26 Jan 11 21:56 dir4/
lrwxrwxrwx 1 usera usera 4 Jan 11 21:55 dir5 -> dir4/
# 同时更改用户和组可以简写为
$ chown -R usera. dir2

$ echo aaa > file
$ ll file
-rw-r--r-- 1 root root 4 Feb 22 14:44 file
# 更改文件的所有者
$ chown usera file
$ ll file
-rw-r--r-- 1 usera root 4 Feb 22 14:44 file
# 同时更改文件的所有者和所属组
$ chown usera:usera file
$ ll file
-rw-r--r-- 1 usera usera 4 Feb 22 14:44 file
$ chown root. file
$ ll file
-rw-r--r-- 1 root root 4 Feb 22 14:44 file
# 更改文件的所属组
$ chown -v :usera file
changed ownership of ‘file’ from root:root to :usera
$ chown .usera file
$ ll file
-rw-r--r-- 1 root usera 4 Feb 22 14:44 file