Home  >  Article  >  Operation and Maintenance  >  How to change the user and group of a file in Linux?

How to change the user and group of a file in Linux?

青灯夜游
青灯夜游Original
2019-03-30 11:49:1335259browse

In Linux we can change the owner and group of a file by using the Chown command.

How to change the user and group of a file in Linux?

In Linux, when a file is created, the owner of the file is the user who created the file. The file user can modify the owner and user group of the file; or under the root user, the owner and user group of any file can be modified.

The chown command has many uses. You can use the chown command to change the file owner, and you can also directly modify the name of the user group.

Note: The user must already exist in the system, that is, it can only be changed to a user name recorded in the /etc/passwd file.

Let’s take a look at how to use the Chown command to change the user and group to which a file belongs.

1. Change the owner of the file

First we use the ls -l command to check the owner of the file, for example:

#ls -l tmpfile
-rw-r-r-- 1 himanshu family 0 2019-03-30 11:03 tmpfile

Below we use the chown command to change the owner of the tmpfile file

#chown root tmpfile

Then use the ls -l command to check the owner of the tmpfile file

#ls -l tmpfile
-rw-r-r-- 1 root family 0 2019-03-30 11:04 tmpfile

It can be seen that: The owner of the tmpfile file "himanshu" has changed to "root"

2. Change the file group

Through the chown command , you can also change the group (the group the file belongs to).

Use the following command to change the group to which the file belongs:

#chown :root tmpfile

Then use the ls -l command to check the group to which the tmpfile file belongs.

#ls -l tmpfile
-rw-r-r-- 1 root root 0 2019-03-30 11:04 tmpfile

You can also directly do it all at once To change the owner and group of the file to root, you need to use the following command:

#chown root:root tmpfile

Recommended related video tutorials: "Linux Tutorial"

The above is the summary of this article All content, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to change the user and group of a file in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn