随笔 - 361,  文章 - 0,  评论 - 62,  阅读 - 160万

  chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID;组可以是组名或者组ID;文件是以空格分开的要改变权限的文件列表,支持通配符。系统管理员经常使用chown命令,在将文件拷贝到另一个用户的名录下之后,让用户拥有使用该文件的权限。 

  只有文件主和超级用户才可以便用该命令。

语法

  chown(选项)(参数)

选项

-c或——changes:效果类似“-v”参数,但仅回报更改的部分;

-f或--quite或——silent:不显示错误信息;

-h或--no-dereference:只对符号连接的文件作修改,而不更改其他任何相关文件;

-R或——recursive:递归处理,将指定目录下的所有文件及子目录一并处理;

-v或——version:显示指令执行过程;

--dereference:效果和“-h”参数相同;

--help:在线帮助;

--reference=<参考文件或目录>:把指定文件或目录的拥有者与所属群组全部设成和参考文件或目录的拥有者与所属群组相同;

--version:显示版本信息。

参数

用户:组:指定所有者和所属工作组。当省略“:组”,仅改变文件所有者;

文件:指定要改变所有者和工作组的文件列表。支持多个文件和目标,支持shell通配符。

常用示例

1、新增用户以及用户组

[root@CTU1000094641 ~]# groupadd test    #新增用户组test          
[root@CTU1000094641 ~]# useradd -m -g test temptest  #新增用户组test下的用户temptest
[root@CTU1000094641 ~]# passwd temptest    #给用户temptest设置密码,然后就可以使用了
Changing password for user temptest.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

2.修改文件的用户以及用户组

  命令:chown  用户:用户组  文件   

复制代码
[root@CTU1000094641 test]# ll -lR
.:
total 136
-rw-r--r--. 1 root      root 26323 Dec  1 15:18 test1.xlsx
-rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
-rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
-rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub

./testsub:
total 28
-rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
[root@CTU1000094641 test]# chown temptest:test test1.xlsx 
[root@CTU1000094641 test]# ll -lR
.:
total 136
-rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
-rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
-rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
-rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub

./testsub:
total 28
-rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
复制代码

  如果用户是唯一的标识,那么也可以用:chown  用户:  文件

复制代码
drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub
[root@CTU1000094641 test]# ll -lR
.:
total 136
-rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
-rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
-rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
-rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub

./testsub:
total 28
-rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
[root@CTU1000094641 test]# chown temptest: test3.xlsx
[root@CTU1000094641 test]# ll -lR
.:
total 136
-rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
-rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
-rw-r--r--. 1 temptest  test 22974 Dec  1 15:18 test3.xlsx
-rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub

./testsub:
total 28
-rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
复制代码

3、修改文件的用户为当前用户组下的用户(即使当前用户组没有该用户) 

  命令:chown  用户  文件    

复制代码
[root@CTU1000094641 test]# ll -lR
.:
total 136
-rw-r--r--. 1 root      root 26323 Dec  1 15:18 test1.xlsx
-rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
-rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
-rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub

./testsub:
total 28
-rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
[root@CTU1000094641 test]# chown temptest test1.xlsx      #事实上root用户组并没有temptest用户
[root@CTU1000094641 test]# ll -lR
.:
total 136
-rw-r--r--. 1 temptest  root 26323 Dec  1 15:18 test1.xlsx
-rw-r--r--. 1 root      root 41035 Dec  1 15:18 test2.xlsx
-rw-r--r--. 1 root      root 22974 Dec  1 15:18 test3.xlsx
-rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub

./testsub:
total 28
-rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
复制代码

4、修改文件所属的用户组

复制代码
[root@CTU1000094641 test]# ll -lR
.:
total 136
-rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
-rw-r--r--. 1 temptest  test 41035 Dec  1 15:18 test2.xlsx
-rw-r--r--. 1 temptest  test 22974 Dec  1 15:18 test3.xlsx
-rw-r--r--. 1 root      root 33698 Dec  1 15:18 test4.xlsx
drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub

./testsub:
total 28
-rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
[root@CTU1000094641 test]# chown :test test4.xlsx
[root@CTU1000094641 test]# ll -lR
.:
total 136
-rw-r--r--. 1 temptest  test 26323 Dec  1 15:18 test1.xlsx
-rw-r--r--. 1 temptest  test 41035 Dec  1 15:18 test2.xlsx
-rw-r--r--. 1 temptest  test 22974 Dec  1 15:18 test3.xlsx
-rw-r--r--. 1 root      test 33698 Dec  1 15:18 test4.xlsx
drwxr-xr-x. 2 devdeploy root  4096 Dec  1 15:20 testsub

./testsub:
total 28
-rw-r--r--. 1 devdeploy root 26323 Dec  1 15:20 test5.xlsx
复制代码

5、修改目录及其下面的所有文件、子目录的文件主

  命令:chown -R  -v  拥有者:群组 文件目录(除了-R外与单文件修改没啥不同)

复制代码
[root@CTU1000094641 attachment]# chown -R -v temptest:test ./test
changed ownership of `./test/test4.xlsx' to temptest:test
ownership of `./test/test1.xlsx' retained as temptest:test
ownership of `./test/test2.xlsx' retained as temptest:test
changed ownership of `./test/testsub/test5.xlsx' to temptest:test
changed ownership of `./test/testsub' to temptest:test
ownership of `./test/test3.xlsx' retained as temptest:test
changed ownership of `./test' to temptest:test
[root@CTU1000094641 attachment]# cd test
[root@CTU1000094641 test]# ll -lR
.:
total 136
-rw-r--r--. 1 temptest test 26323 Dec  1 15:18 test1.xlsx
-rw-r--r--. 1 temptest test 41035 Dec  1 15:18 test2.xlsx
-rw-r--r--. 1 temptest test 22974 Dec  1 15:18 test3.xlsx
-rw-r--r--. 1 temptest test 33698 Dec  1 15:18 test4.xlsx
drwxr-xr-x. 2 temptest test  4096 Dec  1 15:20 testsub

./testsub:
total 28
-rw-r--r--. 1 temptest test 26323 Dec  1 15:20 test5.xlsx
复制代码
posted on   kosamino  阅读(337)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示