管理所有权和文件权限可能是系统管理员最重要的任务。在像Linux(Linux)这样的任何多用户操作系统中,正确分配文件和目录的所有权至关重要。
chown 命令是最有用的工具。不要误认为chmod, chown 可以修改用户对文件的所有权并将它们分配给不同的组。对于任何认真的Linux(Linux)用户
来说,它都是必须掌握的命令。
这是一个帮助您入门的指南。
您如何检查文件(A File)的所有权(Ownership)?
在开始在不同的所有者和组之间转移文件之前,您应该首先了解如何检查文件的当前所有者。过程很简单:在常规 ls 命令中添加 -l 标志,包括查询的文件或目录的所有权信息。
假设(Say)您有一个名为 example.txt 的文件。这是查看其所有权信息的命令的样子:
ls -l 示例.txt
在单个文件上使用 chown
使用 chown 最简单的方法是更改拥有特定文件的用户。语法是 sudo chown username filename,其中 username 是您要将文件提供给的用户的名称,而 filename 是相关文件的名称。这是它在实践中的样子:
sudo chown someone_else example.txt
请注意,这不会更改文件的组所有权,只会更改用户。要更改组所有者,您必须使用不同的语法 - sudo chown :groupname 文件名
在这种特定情况下,这变为:
sudo chown :group2 example.txt
您还可以将这两个命令组合成一行来更改文件的用户和组所有权:
sudo chown me:group1 example.txt
使用Chown更改多个文件(Multiple Files)的所有权(Ownership)
在处理大量文件时,单独更改每个文件的所有权相当繁琐。值得庆幸的是,大多数 Linux 命令(most Linux commands)都允许您在单个命令中将多个以空格分隔的文件名链接在一起。像这样:
sudo chown someone_else:group2 example1.txt example2.txt
使用相同的技巧来检查多个文件的所有权:
ls -l example1.txt example2.txt
即使将多个文件名组合到一个命令中,该过程对于多个文件来说也太不方便了。更好的方法是一次更改目录全部内容的所有权。
这是通过在 chown 命令中添加 -R 标志来实现的。这使得 chown 遍历目录的内容并递归地更改其中每个文件的所有权。这是一个演示:
sudo chown -R someone_else:group2 示例
我们可以再次使用递归标志来检查示例文件夹中文件的所有权。
ls -l -R 例子
使用 UID 修改文件所有权
管理许多用户的系统管理员很快就会厌倦重复输入用户名。任何名称中的一个拼写错误都会在使用 chown 时引发错误,从而大大减慢速度。
更好的替代方法是使用用户 ID。UID是分配给每个创建的用户的四位数字,从 1000 开始,一直向上。这比字符串更容易输入,而且更不容易出错。
要使用此方法,只需将用户名替换为 UID:
sudo chown 1001 示例.txt
如果不知道某个用户的UID,可以通过 id 命令快速查看。只需(Just)输入 id -u username 即可查看该用户的唯一 ID。
此方法也可以扩展到组名。要获取用户登录组和他们所属的其他组
的UID ,请使用不带 -u 标志的 id 命令。(UID)
识别某人_else
如您所见,我们有指定用户所属的各种组 ID。综上所述,我们可以像这样使用 chown 来分配新的所有者并更改文件的组:
sudo chown 1001:1003 example.txt
你还能用 chown命令(Command)做什么?
我们已经演示了 chown 的大部分常见用法。您现在可以通过各种方法更改拥有文件的用户和组。但这还不是指挥部能力的全部限制。
您可以参考官方手册页以获取技术描述和可与该命令一起使用的完整参数列表。只需(Just)在终端输入 man chown 即可查看。
陈好有用吗?
如果您是计算机的唯一用户,那么您将永远不需要使用 chown。但是,如果您在专业环境中使用Linux系统,无论是商业服务器还是大学计算机,那么掌握 chown 命令(除了chmod之外)至关重要。
向用户和组分配和删除文件的能力对于在多用户系统中保持严格的边界至关重要。chown 最好的部分是它的灵活性——您可以使用相同的命令处理单个文件或整个目录。
您还可以在组合语句中分别为用户和组分配所有权。与更方便的UID(UIDs)一起使用,即使是最复杂的用户层次结构也变得轻而易举。
Chown Command in Linux: How to Use It
Managing ownerѕhip and filе permissions is probably the most essential task of a sуstem administrator. In any multi-user operating system like Linux, properly assigning ownership of files and directories is crucial.
The chown command is the most helpful tool for this. Not to be mistaken with chmod, chown can modify user ownership of files and assign them to different groups. It is an essential command to master for any serious Linux user.
Here is a guide to get you started.
How Do You Check the Ownership of A File?
Before you start shifting files around different owners and groups, you should first learn how to check a file’s current owner. The process is simple: adding a -l flag to a regular ls command includes ownership information of the file or directory queried.
Say you have a file called example.txt. This is how the command to view its ownership information will look like:
ls -l example.txt
Using chown On A Single File
The simplest way to use chown is to change the user owning a particular file. The syntax is sudo chown username filename, where username is the name of the user you want to give the file to, and filename is the name of the file in question. This is what it looks like in practice:
sudo chown someone_else example.txt
Note that this does not change the group ownership of the files, only the user. To change the group owner, you have to use a different syntax – sudo chown :groupname filename
In this specific case, this becomes:
sudo chown :group2 example.txt
You can also combine both commands into a single line to change the user as well as group ownership of a file:
sudo chown me:group1 example.txt
Changing Ownership of Multiple Files With Chown
Changing the ownership of every file individually is rather tedious when dealing with a larger number of files. Thankfully, most Linux commands let you chain together multiple space-separated file names within a single command. Like this:
sudo chown someone_else:group2 example1.txt example2.txt
Use the same trick to check the ownership of multiple files as well:
ls -l example1.txt example2.txt
Even for combining multiple file names into a single command, the process is too inconvenient for more than a couple of files. A better approach is to change the ownership of the entire contents of a directory at once.
This is achieved by adding a -R flag to the chown command. This makes chown go through the directory’s contents and recursively change the ownership of every file inside. Here is a demonstration:
sudo chown -R someone_else:group2 examples
We can use the recursive flag again to check the ownership of the files in the examples folder.
ls -l -R examples
Modify File Ownership With UID
System administrators managing many users will quickly get tired of entering user names repeatedly. A single typo in any of the names throws an error in using chown, slowing things down considerably.
A better alternative is to use the user ID instead. The UID is a four-digit number assigned to each user created, starting from 1000 and going up. This is far easier to enter than a string and much less error-prone.
To use this method, just replace the username with the UID:
sudo chown 1001 example.txt
If you don’t know the UID of a user, you can quickly check it with the id command. Just enter id -u username to see the unique ID of that user.
This method can be extended to group names as well. To get the UID of a user’s login group and other groups they belong to, use the id command without the -u flag.
id someone_else
As you can see, we have various group ids to which the specified user belongs. Putting it all together, we can use chown like this to assign a new owner and change the group of a file:
sudo chown 1001:1003 example.txt
What Else Can You Do With the chown Command?
We have already demonstrated most of the common uses of chown. You can now change users and groups that own a file through various methods. But that’s not the complete limit of the command’s abilities.
You can refer to the official man pages for a technical description and a complete list of arguments you can use with the command. Just enter man chown in the terminal to view it.
Is Chown Useful?
If you are the sole user of your computer, then you will never need to use chown. But if you use a Linux system in a professional setting, be it a commercial server or a university computer, then mastering the chown command (in addition to chmod) is of utmost importance.
The ability to assign and remove files to users and groups is essential in maintaining strict boundaries in multi-user systems. The best part about chown is its flexibility – you can work with individual files or whole directories with the same command.
You can also assign ownership to users and groups separately and in a combined statement. Used with the more convenient UIDs, it makes handling even the most complex user hierarchies a breeze.