linux最基础最常用的命令之一,也有不少小细节:chown实例

命令简介

chown是linux最基础的命令之一,也是最常用的命令之一。可以通过chown修改文件、文件夹、符号链接的所有者信息。

示例

chown需要root权限执行。root权限需要root用户登录,或者sudo权限。

1 使用chown修改文件的所有者

更改文件的所有者,是chown命令的简单应用。参数需要一个新的用户名,以及一个文件名。用户名必须为系统中可用账户。
命令格式:

$ sudo chown new_owner file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ls -l
total 4
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 11 13:47 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown test1 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ls -l friutes.txt
-rw-r--r-- 1 test1 yunzhong 77 Nov 11 13:47 friutes.txt

2 使用chown更改文件所在组

Chown命令用于修改文件所属组。在新组名前必须使用冒号。否则,它将被视为新owner。
命令格式:

$ sudo chown :new_group file_name

命令示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown :yunzhong friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 4
-rw-r--r-- 1 test1 yunzhong 77 Nov 11 13:47 friutes.txt

3 chown命令使用用户ID修改文件的所有者

chown也可以通过用户ID来更改文件的所有者。但这里有个问题:系统怎么知道传入的参数是用户ID,还是用户名?其实系统也无法区分,如果有一个用户的名字和用户ID相同,系统则认为传入的为用户名。可以使用id命令查看用户ID。

id -u user_name

命令格式,和通过用户名更改所有者是一样的。
示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ id -u yunzhong
1000
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown 1000 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 4
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 11 13:47 friutes.txt

如果有一个用户名为:1000,那么所有者就改成了1000.

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo useradd 1000
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown 1000 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 4
-rw-r--r-- 1 1000 yunzhong 77 Nov 11 13:47 friutes.txt

4 使用group ID 更改文件所在组

和修改所有者类似,group也可以通过id来修改。查看group ID的命令:

id -g group_name

命令格式:

$ sudo chown :group_id file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 4
-rw-r--r-- 1 test1 test1 77 Nov 11 13:47 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ id -g yunzhong
1000
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown :1000 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 4
-rw-r--r-- 1 test1 1000 77 Nov 11 13:47 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ id -g 1000
1006
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ id -g test1
1002
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown :1002 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 4
-rw-r--r-- 1 test1 test1 77 Nov 11 13:47 friutes.txt

和上一节同理,如果group ID和另外一个group名字重复,则认为参数是group名字。

5 chown修改多个文件的所有者

当需要修改多个文件为一个所有者时,chown只需要执行一次就可以做到。chown支持一次传入多个文件名,通过空格分割。

$ sudo chown new_owner file_name1 file_name2 file_name3

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 test1 test1 77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 test1 test1 77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 test1 test1 77 Nov 14 08:32 friutes.txt.2
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown yunzhong:yunzhong friutes.txt friutes.txt.1 friutes.txt.2
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 14 08:32 friutes.txt.2

如上面的示例,文件名有相同的前缀,我们可以用更简单的方式批量修改所有者:*通配符。

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 14 08:32 friutes.txt.2
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown test1 friutes.txt*
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 test1 yunzhong 77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 test1 yunzhong 77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 test1 yunzhong 77 Nov 14 08:32 friutes.txt.2

6 chown命令支持同时修改所有者和所在组

chown支持同时输入所有者和所在组,一次修改文件属性。
命令格式:

$ sudo chown new_owner:new_group file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 test1 yunzhong 77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 test1 yunzhong 77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 test1 yunzhong 77 Nov 14 08:32 friutes.txt.2
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown yunzhong:test1 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 yunzhong test1    77 Nov 11 13:47 friutes.txt

7 使用chown命令将一个文件的所有者、所在组同步到另外一个文件

命令格式:

$ sudo chown --reference=souce_file destination_file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 yunzhong test1    77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 test1    yunzhong 77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 test1    yunzhong 77 Nov 14 08:32 friutes.txt.2
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown --reference = friutes.txt friutes.txt.1
chown: failed to get attributes of '=': No such file or directory
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown --reference=friutes.txt friutes.txt.1
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 yunzhong test1    77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 yunzhong test1    77 Nov 14 08:32 friutes.txt.1

注意一点,参数--reference = 之后不能有空格,如果有空格,则认为参数错误,如上示例。

8 打印chown 命令的变更记录

chown可以打印文件被自己操作的记录。如果参数不传入文件,则什么都不打印。
命令格式:

$ sudo chown -v

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 yunzhong test1    77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 yunzhong test1    77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 test1    yunzhong 77 Nov 14 08:32 friutes.txt.2
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -v test1 friutes.txt
changed ownership of 'friutes.txt' from yunzhong to test1
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -v test1 friutes.txt
ownership of 'friutes.txt' retained as test1

9 chown只有在文件所有者、所在组发生变化的时候才打印信息

和上面的-v参数不同,-c命令只有在所有者、所在组发生变化的时候才会打印。
命令格式:

$ sudo chown -c 

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 test1    test1    77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 yunzhong test1    77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 test1    yunzhong 77 Nov 14 08:32 friutes.txt.2
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -c test1:test1 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -c yunzhong:yunzhong friutes.txt
changed ownership of 'friutes.txt' from test1:test1 to yunzhong:yunzhong
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$

10 chown命令修改一个目录的所有者、所在组

和修改文件的操作类似,只要传输的改成目录名就可以。
命令格式:

# 修改文件夹的所有者
$ sudo chown new_owner directory_name
# 修改文件夹的所在组
$ sudo chown :new_group directory_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ ll
drwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 08:32 chowntest
yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chown test1:test1 chowntest/
yunzhong@DESKTOP-9VB7LN7:/tmp$ ll
drwxr-xr-x 2 test1    test1    4096 Nov 14 08:32 chowntest

11 只有所有者和指定参数匹配,才修改所有者

通过设定参数--from,可以校验文件当前的所有者是否匹配。只有在匹配的情况下才会更改。
命令格式:

$ sudo chown --from=current_owner new_owner file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 11 13:47 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown --from yunzhong test1 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 test1    yunzhong 77 Nov 11 13:47 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown --from yunzhong 1000 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 test1    yunzhong 77 Nov 11 13:47 friutes.txt

12 只有所在组和指定参数匹配,才修改所在组

其实,用户可以通过参数单独校验所有者,所在组,也可以同时校验两者。
命令格式:

$ sudo chown --from=:current_group :new_group file_name
或者
$ sudo chown --from current_owner:current_group new_owner:new_group file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 test1    yunzhong 77 Nov 11 13:47 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown --from yunzhong:yunzhong yunzhong:test1 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 test1    yunzhong 77 Nov 11 13:47 friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown --from test1:yunzhong yunzhong:yunzhong friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 12
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 11 13:47 friutes.txt

13 更改一个文件夹下所有内容的所有者或所在组

使用-R参数,可以递归修改文件夹下所有的子文件、子文件夹。当修改大量文件的时候,这个参数可以帮助我们一次调用实现修改。

命令格式:

sudo chown -R new_owner:new_group directory_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp$ ll -R chowntest/
chowntest/:
total 16
-rw-r--r-- 1 yunzhong yunzhong   77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 yunzhong yunzhong   77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 yunzhong yunzhong   77 Nov 14 08:32 friutes.txt.2
drwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdir

chowntest/subdir:
total 12
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 14 10:50 friutes.txt
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 14 10:50 friutes.txt.1
-rw-r--r-- 1 yunzhong yunzhong 77 Nov 14 10:50 friutes.txt.2
yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chown -R test1:test1 chowntest/
yunzhong@DESKTOP-9VB7LN7:/tmp$ ll -R chowntest/
chowntest/:
total 16
-rw-r--r-- 1 test1 test1   77 Nov 11 13:47 friutes.txt
-rw-r--r-- 1 test1 test1   77 Nov 14 08:32 friutes.txt.1
-rw-r--r-- 1 test1 test1   77 Nov 14 08:32 friutes.txt.2
drwxr-xr-x 2 test1 test1 4096 Nov 14 10:50 subdir

chowntest/subdir:
total 12
-rw-r--r-- 1 test1 test1 77 Nov 14 10:50 friutes.txt
-rw-r--r-- 1 test1 test1 77 Nov 14 10:50 friutes.txt.1
-rw-r--r-- 1 test1 test1 77 Nov 14 10:50 friutes.txt.2

14 修改符号链接的所有者、所在组

默认情况下,chown修改的是符号链接源文件的所有者、所在组。可以使用-h修改符号链接文件的信息。
命令格式:

$ sudo -h new_owner:new_group sym_file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 16
-rw-r--r-- 1 yunzhong yunzhong   77 Nov 11 13:47 friutes.txt
drwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdir
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ln -s friutes.txt friutes.txt.ln
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 16
-rw-r--r-- 1 yunzhong yunzhong   77 Nov 11 13:47 friutes.txt
lrwxrwxrwx 1 yunzhong yunzhong   11 Nov 14 10:59 friutes.txt.ln -> friutes.txt
drwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdir
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown test1:test1 friutes.txt.ln
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 16
-rw-r--r-- 1 test1    test1      77 Nov 11 13:47 friutes.txt
lrwxrwxrwx 1 yunzhong yunzhong   11 Nov 14 10:59 friutes.txt.ln -> friutes.txt
drwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdir
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -h test1:test1 friutes.txt.ln
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ ll
total 16
-rw-r--r-- 1 test1    test1      77 Nov 11 13:47 friutes.txt
lrwxrwxrwx 1 test1    test1      11 Nov 14 10:59 friutes.txt.ln -> friutes.txt
drwxr-xr-x 2 yunzhong yunzhong 4096 Nov 14 10:50 subdir

屏蔽错误信息

默认情况下,chown执行错误信息会打印到终端。可以使用-f参数,屏蔽错误信息。
命令格式:

$ sudo chown -f new_owner file_name

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown yunzhong not_found_file
chown: cannot access 'not_found_file': No such file or directory
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ sudo chown -f yunzhong not_found_file
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ chown yunzhong friutes.txt
chown: changing ownership of 'friutes.txt': Operation not permitted
yunzhong@DESKTOP-9VB7LN7:/tmp/chowntest$ chown -f yunzhong friutes.txt



展开阅读全文

页面更新:2024-04-11

标签:命令   所有者   示例   文件夹   符号   实例   用户名   细节   参数   格式   文件   基础   用户

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top