用于更改所有者和组的 Linux Chown 命令示例

举报
Tiamo_T 发表于 2022/06/23 17:05:03 2022/06/23
【摘要】 在本文中,我们将讨论“chown”命令,因为它也涵盖了“chgrp”命令的大部分内容。

文件的所有者和组的概念是 Linux 的基础。每个文件都与所有者和组相关联。您可以使用 chown 和 chgrp 命令来更改特定文件或目录的所有者或组。

在本文中,我们将讨论“chown”命令,因为它也涵盖了“chgrp”命令的大部分内容。

即使您已经知道此命令,也可能下面提到的示例之一对您来说可能是新的。

1.更改文件的所有者

# ls -lart tmpfile
-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile

# chown root tmpfile

# ls -l tmpfile
-rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile

所以我们看到文件的所有者从“himanshu”变成了“root”。

2.更改文件的组

通过 chown 命令,也可以更改组(文件所属的组)。

# ls -l tmpfile
-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile

# chown :friends tmpfile

# ls -l tmpfile
-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile

仔细观察,档案的分组从“家人”变成了“朋友”。所以我们看到,只需在新组名后添加一个“:”,就可以更改文件的组。

3.更改所有者和组

# ls -l tmpfile
-rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile

# chown himanshu:friends tmpfile

# ls -l tmpfile
-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile

所以我们看到使用语法'<newOwner>:<newGroup>',可以一次性更改所有者和组。

4. 对符号链接文件使用 chown 命令

这是一个符号链接:


# ls -l tmpfile_symlnk
lrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile

所以我们看到符号链接“tmpfile_symlink”链接到文件“tmpfile”。

让我们看看如果在符号链接上发出 chown 命令会发生什么:

# chown root:friends tmpfile_symlnk

# ls -l tmpfile_symlnk
lrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile

# ls -l tmpfile
-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile

当在符号链接上发出 chown 命令以更改所有者和组时,它是符号链接的指代对象,即所有者和组已更改的“tmpfile”。这是 chown 命令的默认行为。此外,同样存在一个标志“-dereference”。

5. 使用 chown 命令强制更改符号文件的所有者/组。

使用标志“-h”,您可以强制更改符号链接的所有者或组,如下所示。

# ls -l tmpfile_symlnk
lrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile

# chown -h root:friends tmpfile_symlnk

# ls -l tmpfile_symlnk
lrwxrwxrwx 1 root friends 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile

6. 仅当文件归特定用户所有时才更改所有者

使用 chown “-from” 标志,您可以更改文件的所有者,前提是该文件已由特定所有者拥有。

# ls -l tmpfile
-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile

# chown --from=guest himanshu tmpfile

# ls -l tmpfile
-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile

# chown --from=root himanshu tmpfile

# ls -l tmpfile
-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
  • 在上面的示例中,我们验证了文件“tmpfile”的原始所有者/组是 root/friends。
  • 接下来我们使用“-from”标志将所有者更改为“himanshu”,但前提是现有所有者是“guest”。
  • 现在,由于现有所有者不是“客人”。因此,该命令未能更改文件的所有者。
  • 接下来,如果现有所有者是“root”(这是真的),我们尝试更改所有者,并且这次命令成功并且所有者更改为“himanshu”。


7.仅当文件已属于某个组时才更改组

这里也使用了“--from”标志,但方式如下:

# ls -l tmpfile
-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile

# chown --from=:friends :family tmpfile

# ls -l tmpfile
-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile

由于文件 'tmpfile' 实际上属于组 'friends',所以条件正确并且命令成功。

所以我们看到,通过使用标志 '–from=:<conditional-group-name>' 我们可以在特定条件下更改组。

注意:通过遵循模板“–from=<conditional-owner-name>:<conditional-group-name>”,可以同时应用所有者和组的条件。

8. 将所有者/组设置从一个文件复制到另一个文件

这可以通过使用“-reference”标志来实现。

# ls -l file
-rwxr-xr-x 1 himanshu family 8968 2012-04-09 07:10 file

# ls -l tmpfile
-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile

# chown --reference=file tmpfile

# ls -l tmpfile
-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile

在上面的示例中,我们首先检查了参考文件“file”的所有者/组,然后检查了目标文件“tmpfile”的所有者/组。两者都不一样。然后我们使用带有“–reference”选项的 chown 命令将所有者/组设置从参考文件应用到目标文件。该命令成功,并且“tmpfile”的所有者/组设置类似于“文件”。

9. 通过递归遍历目录来更改文件的所有者/组

这可以通过“-R”选项实现。

# ls -l linux/linuxKernel
-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/linuxKernel

# ls -l linux/ubuntu/ub10
-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/ubuntu/ub10

# ls -l linux/redhat/rh7
-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/redhat/rh7

# chown -R himanshu:family linux/

# ls -l linux/redhat/rh7
-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/redhat/rh7

# ls -l linux/ubuntu/ub10
-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/ubuntu/ub10

# ls -l linux/linuxKernel
-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/linuxKernel

所以我们在检查了目录“linux”及其两个子目录“ubuntu”和“redhat”中所有文件的所有者/组之后看到。我们发出带有“-R”选项的 chown 命令来更改所有者和组。命令成功,所有文件的所有者/组已成功更改。

10. 对符号链接目录使用 chown 命令

让我们看看如果我们发出 'chown' 命令递归地更改一个目录中的所有者/文件组,该目录是指向其他目录的符号链接,会发生什么。

这是一个链接到目录 'linux' 的符号链接目录 'linux_symlnk'(已在上面的示例 '9' 中使用):

$ ls -l linux_symlnk
lrwxrwxrwx 1 himanshu family 6 2012-05-22 22:02 linux_symlnk -> linux/

现在,让我们递归地更改这个符号链接目录的所有者(从 hemanshu 到 root):

# chown -R root:friends linux_symlnk

# ls -l linux_symlnk/
-rw-r--r-- 1 himanshu friends    0 2012-05-22 21:52 linuxKernel
drwxr-xr-x 2 himanshu friends 4096 2012-05-22 21:52 redhat
drwxr-xr-x 2 himanshu friends 4096 2012-05-22 21:52 ubuntu

在上面的输出中,我们看到文件和目录的所有者没有改变。这是因为默认情况下“chown”命令不能遍历符号链接。这是默认行为,但也有一个标志“-P”。

11.使用chown递归强制更改符号链接目录的所有者/组

这可以通过使用标志 -H 来实现

# chown -R -H guest:family linux_symlnk

# ls -l linux_symlnk/
total 8
-rw-r--r-- 1 guest family    0 2012-05-22 21:52 linuxKernel
drwxr-xr-x 2 guest family 4096 2012-05-22 21:52 redhat
drwxr-xr-x 2 guest family 4096 2012-05-22 21:52 ubuntu

所以我们看到,通过使用 -H 标志,所有文件/文件夹的所有者/组都被更改了。

12.列出chown命令所做的所有更改

使用详细选项 -v,它将显示文件的所有权是更改还是保留,如下所示。

# chown -v -R guest:friends linux
changed ownership of `linux/redhat/rh7' to guest:friends
changed ownership of `linux/redhat' retained to guest:friends
ownership of `linux/redhat_sym' retained as guest:friends
ownership of `linux/ubuntu_sym' retained as guest:friends
changed ownership of `linux/linuxKernel' to guest:friends
changed ownership of `linux/ubuntu/ub10' to guest:friends
ownership of `linux/ubuntu' retained as guest:friends
ownership of `linux' retained as guest:friends
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。