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命令使您可以根据需要修改文件和目录权限。 这非常方便,尤其是在配置功能部件和服务时。 让我们看看如何使用该命令。
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.
让我们看一下该命令附带的几个选项。
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
样品输出
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.
这次,我们设法更改了文件的组。
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
样品输出
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.
正如您已经敏锐地指出的那样,尽管更改了目录所属的用户和组,但目录内容保持不变。 为了将文件所有权归为文件夹内容,我们将递归更改权限,如您将在下一个子主题中学习的那样。
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'目录中包含的文件生效。
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 1000和gid 1000,请执行以下操作:
chown 1000:1000 newfile.txt
Sample 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命令