linux chown命令_如何在Linux上使用chown命令

linux chown命令

linux chown命令

A terminal window on a Linux system
Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com

Files and directories in Linux systems all belong to someone. You can change their ownership with the chown command. We show you how.

Linux系统中的文件和目录都属于某人。 您可以使用chown命令更改其所有权。 我们向您展示如何。

每个文件都属于一个用户和一个组 (Each File Belongs to a User and a Group)

Linux is a multi-user system. The operating system allows multiple user accounts to be defined and for any valid user to log on to the computer. Moreover, multiple users can use a single computer at the same time.

Linux是一个多用户系统。 操作系统允许定义多个用户帐户,并且任何有效用户都可以登录到计算机。 此外,多个用户可以同时使用一台计算机。

To maintain a record of which files belong to which user and to enforce some security, Linux uses the concept of ownership. Every file belongs to an owner—a user—and to a group.

为了维护哪些文件属于哪个用户并增强安全性,Linux使用所有权的概念。 每个文件都属于所有者(用户)和组。

When a file is created, its owner is the user who created it. The group that the file belongs to—the “owning” group—is the user’s current group. Users and groups have names, and they have numeric identities too, called a user (or unique) identifier (UID) and a group identifier (GID).

创建文件后,文件的所有者就是创建文件的用户。 文件所属的组(“拥有”组)是用户的当前组。 用户和组具有名称,它们也具有数字身份,分别称为用户(或唯一)标识符(UID)和组标识符(GID)。

When you create a file, it is owned by you, and it belongs to your current group. Usually, this is the group you have signed into. By default, this is a group that shares the same name as your user name and was created when you were created as a user on the system.

创建文件时,该文件归您所有,并且属于您当前的组。 通常,这是您登录的组。 默认情况下,这是一个与用户名同名的组,是在系统上以用户身份创建时创建的。

You can use the chown command to can change the ownership values to something else. You can set a new owner, a new group, or a new owner and a new group at the same time. The owner of a file can change the group ownership, but only root can change the user ownership because that involves another user. Without root privileges, you can’t make another user on the system unwittingly “adopt” a file.

您可以使用chown命令将所有权值更改为其他值。 您可以同时设置新的所有者,新的组或新的所有者和新的组。 文件的所有者可以更改组的所有权,但是只有root可以更改用户的所有权,因为这涉及另一个用户。 没有root特权,您将无法使系统上的其他用户无意间“采用”文件。

您为什么要更改所有权? (Why Would You Want To Change Ownership?)

Here are a few examples of situations where you might want to do this:

以下是一些您可能想要执行此操作的示例:

  • If you transfer files between different Linux or Unix-like operating systems, you will need to change the user and group owners to the new user and group owners of the account you wish to use the files under on the new Linux computer.

    如果在不同Linux或类似Unix的操作系统之间传输文件,则需要将用户和组所有者更改为希望在新Linux计算机上使用文件的帐户的新用户和组所有者。
  • A user may leave your organization, and all of his files will be the responsibility of another staff member. You will need to change the owner and group owner to the staff member now responsible for those files.

    用户可以离开您的组织,他的所有文件将由另一位员工负责。 您需要将所有者和组所有者更改为现在负责这些文件的工作人员。
  • You may author a script that is going to be used by a specific user.

    您可以编写将由特定用户使用的脚本。
  • You may create a file or directory logged in as root, but you want it to be accessible to a specific user.

    您可以创建以root用户身份登录的文件或目录,但希望特定用户可以访问它。

查看您的组,UID和GID (Viewing Your Groups, UID, and GID)

To list the groups you are in, you can use the groups command.

列出您所在的组,可以使用groups命令。

groups
groups in a terminal window

To get a list of the groups, their numerical IDs, and your UID and GID, use the id command:

要获取组列表,其数字ID以及您的UID和GID ,请使用id命令:

id
id in a terminal window

You can use some options with ID to refine the output.

您可以使用一些带有ID的选项来优化输出。

  • -u: List your UID.

    -u :列出您的UID。

  • -g: List your effective (current) GID.

    -g :列出您的有效(当前)GID。

  • -nu: List your user name.

    -nu :列出您的用户名。

  • -ng: List your current group name.

    -ng :列出您当前的组名。

id -u
id -g
id -nu
id -ng
id command output in a terminal window

查看文件的用户和组所有权 (Viewing User and Group Ownership of a File)

To see the owners of a file or directory, use the -l (long listing) option with ls.

要查看文件或目录的所有者,请在ls使用-l (长列表)选项。

ls -l

   
   
ls -l in a terminal window

We can see that the name dave appears twice in the listing. The left-most appearance tells us the file owner is a user called dave. The right-most dave tells us the file belongs to a group that is also called dave.

我们可以看到dave这个名字在清单中出现了两次。 最左侧的外观告诉我们文件所有者是名为dave的用户。 最右边的dave告诉我们该文件属于一个组,该组也称为dave

By default, when a Linux user is created, they are added to a private group named for their username. They are the only member of that group.

默认情况下,创建Linux用户时,会将他们添加到以其用户名命名的私有组中。 他们是该组的唯一成员。

This executable file is owned by the user mary and the group the file belongs to is mary's private group.

该可执行文件由用户mary拥有,并且该文件所属的组是mary's私有组。

ls -l
ls -l in a terminal window

This file is owned by the user oscar , but the group that the file belongs to is called researchlab . This means that other members of the researchlab group may access this file, according to the file permissions that have been set for the members of that group.

该文件由用户oscar拥有,但该文件所属的组称为researchlab 。 这意味着,根据为该researchlab组成员设置的文件许可权, researchlab组的其他成员可以访问该文件。


   
   
ls -l in a terminal window

更改用户所有权 (Changing User Ownership)

Let’s work through some examples. This command will change the user ownership of the file while.c to the user mary.

让我们来看一些例子。 此命令会将while.c文件的用户所有权更改为用户mary

sudo chown mary while.c
sudo chown mary while.c in a terminal window

We can use ls to see the changes to the file properties.

我们可以使用ls来查看文件属性的更改。

ls -l while.c
ls -l while.c in a terminal window

You can use chown to change the ownership of several files at once.

您可以使用chown一次更改多个文件的所有权。

sudo chown mary getval.c global.c goto.c
sudo chown mary getval.c global.c goto.c in a terminal window

This changes the user ownership of all three files.

这将更改所有三个文件的用户所有权。

ls -l getval.c global.c goto.c
ls -l getval.c global.c goto.c in a terminal window

You can use wildcards to select groups of files. This command will change the user ownership of all files beginning with the letter “c.”

您可以使用通配符选择文件组。 此命令将更改所有以字母“ c”开头的文件的用户所有权。

sudo chown mary c*.*
sudo chown mary c*.* in a terminal window

All of the files will now have mary as their owner. Note that none of the group ownerships have been changed.

现在,所有文件都将以mary作为其所有者。 请注意,所有组的所有权都没有更改。

ls -l mary c*.*
ls -l mary c*.* in a terminal window

Let’s change the ownership of a directory. We simply pass the directory name to chown instead of a filename.

让我们更改目录的所有权。 我们只需将目录名称传递给chown而不是文件名。

sudo chown mary ./archive/
sudo chown mary ./archive/ in a terminal window

To check the ownership properties of the directory we use ls, but also use the -d (directory) option to it. This lists the properties of the directory, not the files inside it.

要检查目录的所有权属性,我们使用ls ,但也要使用-d (目录)选项。 这列出了目录的属性,而不是其中的文件。

ls -l -d ./archive/
ls -l -d ./archive/ in a terminal window

To change the ownership of all the files in a directory, you can use the -R (recursive) option. This option will change the user ownership of all files within the archive folder.

要更改目录中所有文件的所有权,可以使用-R (递归)选项。 此选项将更改archive文件夹内所有文件的用户所有权。

sudo chown -R mary ./archive/
sudo chown -R mary ./archive/ in a terminal window

Now let’s look at the files in the archive directory.

现在,让我们看一下存档目录中的文件。

ls -l ./archive/
ls -l ./archive/ in a terminal window

As expect, all of the files now belong to mary.

不出所料,现在所有文件都属于mary

更改组所有权 (Changing Group Ownership)

There are different ways to change the group ownership.

有多种更改组所有权的方法。

To change the group ownership at the same time as you change the user ownership, pass the new owner name and the new group name with a colon “:” separating them. The group must already exist.

要在更改用户所有权的同时更改组所有权,请传递新的所有者名称和新的组名称,并用冒号“:”将它们分开。 该组必须已经存在。

sudo chown mary:researchlab charm.c
sudo chown mary:researchlab charm.c  in a terminal window

The user owner and the group that the file belongs have both been changed.

用户所有者和文件所属的组均已更改。

ls -l charm.c
ls -l charm.c in a terminal window

A shorthand way to change the group ownership to the current group of the new owner, just provide the colon and omit the group name.

将组所有权更改为新所有者的当前组的简便方法,只需提供冒号并省略组名即可。

sudo chown mary: caps.c
sudo chown mary: caps.c in a terminal window
ls -l caps.c
ls -l caps.c in a terminal window

Both user ownership and group ownership have been changed to mary.

用户所有权和组所有权都已更改为mary

To change the group ownership only, precede it with a colon and omit the user name. The user owner will not be altered.

要仅更改组所有权,请在其前面加上冒号并忽略用户名。 用户所有者将不会更改。

sudo chown :researchlab at.c
sudo chown :researchlab at.c in a terminal window
ls -l at.c
ls -l at.c in a terminal window

The group ownership has been changed, but the user ownership remains the same.

组所有权已更改,但用户所有权保持不变。

使用具有UID和GID值的Chown (Using Chown with UID and GID Values)

You can use the numerical UID and GID values with the chown command. This command will set the user and the group ownership to mary.

您可以在chown命令中使用数字UID和GID值。 此命令会将用户和组所有权设置为mary

sudo chown 1001:1001 at.c
sudo chown 1001:1001 at.c in a terminal window
ls -l at.c
ls -l at.c in a terminal window

拥有十分之九的法律 (Possession is Nine-Tenths of the Law)

Or so they say. But in Linux, ownership is a massive part of file security, with file permissions providing the remainder of it. Use the chown and chmod commands to secure file access on your system.

或如此,他们说。 但是在Linux中,所有权是文件安全性的重要组成部分,文件许可权提供了其余部分。 使用chownchmod命令可确保系统上文件的访问安全。

翻译自: https://www.howtogeek.com/438435/how-to-use-the-chown-command-on-linux/

linux chown命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值