博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux chown命令_Linux chown命令示例
阅读量:2533 次
发布时间:2019-05-11

本文共 4538 字,大约阅读时间需要 15 分钟。

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

样品输出

and make sure the user has the sudo privileges. 运行命令,并确保用户具有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.txtfile2.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

翻译自:

linux chown命令

转载地址:http://wclzd.baihongyu.com/

你可能感兴趣的文章
二月二——头牙诗词
查看>>
《吴忠与富平》之四:汉三水属国(北地属国、安定属国)
查看>>
丁酉立秋喜逢风雨
查看>>
vim删除#开头的行和正则表达式笔记
查看>>
python3 提成计算
查看>>
VBA赋值给指定单元格
查看>>
抽象类和接口总结回顾
查看>>
【语言处理与Python】5.3使用Python字典映射词及其属性
查看>>
设备信息
查看>>
Android Volley框架的使用(三)
查看>>
[错误总结] 控件frame设置无效
查看>>
Redis Java API
查看>>
oracle 查询表的定义语句
查看>>
Android 笔记之 Android 系统架构
查看>>
状压dp终极篇(状态转移的思想)
查看>>
AtCoder Grand Contest 031 B - Reversi
查看>>
完整成功配置wamp server小记
查看>>
build.gradle添加Oracle jdbc6 链接
查看>>
影响系统性能的20个瓶颈--转自开源中国
查看>>
根据ISBN获取豆瓣API提供的图书信息
查看>>