linux chown命令_Linux chown命令示例

linux chown命令

Once in a while, you will be faced with the prospect of changing permissions of files and folders. Linux chown command enables you to modify file and directory permissions as you deem fit. This comes in handy, especially when configuring features and services. Let’s see how the command can be used.

有时,您将面临更改文件和文件夹权限的前景。 Linux chown命令使您可以根据需要修改文件和目录权限。 这非常方便,尤其是在配置功能部件和服务时。 让我们看看如何使用该命令。

Linux chown命令语法 (Linux chown Command Syntax)

The syntax for chown command usage is as shown:

chown命令用法的语法如下所示:

chown [option] [owner]:[group] file

Let’s take a look at a couple of options that come with the command.

让我们看一下该命令附带的几个选项。

更改文件所有权 (Changing file ownership)

How do you change ownership of a file? The syntax is quite simple as shown below.

您如何更改文件的所有权? 语法非常简单,如下所示。

chown [owner] file

Let’s look at a file 'newfile.txt' created by user ‘james’. By default, this file belongs to user “james” and group “james” as shown in the output below.

让我们看一下用户“ james”创建的文件'newfile.txt' ”。 默认情况下,此文件属于用户“ james”和组“ james”,如下面的输出所示。

ls -l

as shown below.

如下所示。

Sample output

样品输出

As illustrated, the first attribute after the file permissions denotes the user who owns the file and the second attribute denotes the group the file belongs to.

如图所示,文件权限后的第一个属性表示拥有文件的用户 ,第二个属性表示文件所属的组。

To change file ownership to a different user, for instance, root user, execute:

要将文件所有权更改为其他用户(例如root用户),请执行:

chown root newfile.txt

To verify the change in ownership, once again use the ls -l command.

要验证所有权更改,请再次使用ls -l命令。

Sample output

样品输出

sudo and make sure the user has the sudo privileges. sudo运行命令,并确保用户具有sudo特权。

更改文件所属的组 (Changing the group a file belongs to)

From the previous example, we managed to change the ownership of a file from one user to another. However, if you were keen enough, you must have observed that the group did not change. Changing the group a file belongs to is very similar to changing the user. The syntax differs a little as shown

在上一个示例中,我们设法将文件的所有权从一个用户更改为另一个用户。 但是,如果您足够敏锐,则必须已观察到组没有改变。 更改文件所属的组与更改用户非常相似。 语法略有不同,如下所示

chown :[group-name] [file-name]

For instance, to change group ownership to root user run

例如,将组所有权更改为root用户运行

chown :root newfile.txt

Sample output

样品输出

This time around, we’ve managed to change the file’s group.

这次,我们设法更改了文件的组。

通过一个命令更改文件所属的用户和组 (Changing both the user and group the file belongs to in one command)

If you want to make your work easy and change both the user and the group to which the file belongs, then use the syntax

如果您想简化工作并更改用户和文件所属的组,请使用以下语法

chown user:group newfile.txt

For instance to change both the user and the group to ‘james‘ execute:

例如,将用户和组都更改为“ james ”,执行:

chown james:james newfile.txt

Sample output

样品输出

更改目录所有权 (Changing directory ownership)

Let’s now talk about changing ownership of directories. The syntax remains fairly similar,

现在让我们谈谈更改目录所有权。 语法仍然非常相似,

chown user:group ./directory-name/

I have a directory called linux created by root user and it contains 3 text files:

我有一个由root用户创建的名为linux的目录,它包含3个文本文件:

file1.txt
file2.txt 
file3.txt

To change user and group to ‘james’ run

将用户和组更改为“ james”运行

chown james:james ./linux/

Sample output

样品输出

As you have keenly noted, despite changing the user and group to which the directory belongs, the directory contents remain unaltered. To cascade file ownerships down to the folder contents, we are going to recursively change permissions as you will learn in the next sub-topic.

正如您已经敏锐地指出的那样,尽管更改了目录所属的用户和组,但目录内容保持不变。 为了将文件所有权归为文件夹内容,我们将递归更改权限,如您将在下一个子主题中学习的那样。

递归更改目录的文件权限 (Recursively change file permissions of a directory)

To recusively effect file permissions, use the -R option

要回溯性地影响文件权限,请使用-R选项

chown -R root:root linux

Sample output

样品输出

From the output, we can clearly see that the file permissions have been effected on the files contained in the ‘linux’ directory.

从输出中,我们可以清楚地看到文件权限已对'linux'目录中包含的文件生效。

更改文件的UID和GID (Change UID and GID of a file)

Instead of specifying the user or group in chown command, one may opt to specify the GID or UID to which the file will belong. To accomplish this, use the syntax

可以选择指定文件所属的GID或UID,而不是在chown命令中指定用户或组。 为此,请使用语法

chown uid:gid [filename]

For example, to change the newfile.txt to uid 1000 and gid 1000 execute:

例如,要将newfile.txt更改为uid 1000gid 1000,请执行以下操作:

chown 1000:1000 newfile.txt

Sample output

样品输出

显示详细输出 (Display verbose output)

You can choose to display the operation that’s taking place on the terminal as permissions are being changed. To do this, use the -v options

您可以选择显示更改权限时在终端上正在进行的操作。 为此,请使用-v选项

chown root:root newfile.txt -v

翻译自: https://www.journaldev.com/30987/linux-chown-command-examples

linux chown命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值