How to modify the user and group of a file in Linux

How to modify 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 creates 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.

To modify the user and group to which a file belongs, you need to use the chown command.

The chown command, which can be considered as the abbreviation of "change owner", is mainly used to change the owner of a file (or directory). In addition, this command can also change the group to which a file (or directory) belongs.

When you only need to change the owner, you can use the following basic format of the chown command:

chown [-R] owner of a file or directory
  • The -R (note the uppercase) option means changing the owner of all files in the subdirectories as well.

When you only need to change the group, you can use the following basic format of the chown command:

chown [-R]: group file or directory

If you need to change both the owner and the group, the basic format of the chown command is:

chown [-R] owner:group file or directory

Note that in the chown command, a dot (.) can be used between the owner and the group, but this will cause a problem. If the user adds a decimal point when setting the account (for example, zhangsan.temp), the system will misjudge it. Therefore, it is recommended that you use a colon to connect the owner and the group.

Of course, the chown command also supports simply modifying the group to which a file or directory belongs. For example, chown :group install.log means modifying the group to which the install.log file belongs. However, the chgrp command is usually used to modify the group to which the file belongs, so it is not recommended to use the chown command.

Another point to note is that when using the chown command to modify the owner (or owner) of a file or directory, you must ensure that the user (or user group) exists. Otherwise, the command cannot be executed correctly and will prompt "invalid user" or "invaild group".

Examples of using the chown command

1. Change the owner of the file

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

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

Next, 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-rr-- 1 root family 0 2019-03-30 11:04 tmpfile

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

2. Change the file group

The group (the group to which the file belongs) can also be changed through the chown command.

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

#chown :root tmpfile

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

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

You can also change the owner and group of the file to root at one time by using the following command:

#chown root:root tmpfile

This is the end of this article about the practical method of modifying the user and group to which a file belongs in Linux. For more information on how to modify the user and group to which a file belongs in Linux, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed analysis of the chmod command to modify file permissions under Linux
  • Modify file permissions (ownership) under Linux
  • Linux common commands chmod to modify file permissions 777 and 754
  • Linux server programming utime() function to modify file access time

<<:  Metadata Extraction Example Analysis of MySQL and Oracle

>>:  Tips for implementing multiple borders in CSS

Recommend

Vue implements simple calculator function

This article example shares the specific code of ...

CSS mimics remote control buttons

Note: This demo is tested in the mini program env...

7 Best VSCode Extensions for Vue Developers

Adding the right VS Code extension to Visual Stud...

Detailed explanation of MLSQL compile-time permission control example

Preface The simple understanding of MySQL permiss...

How to quickly query 10 million records in Mysql

Table of contents Normal paging query How to opti...

Example code for implementing a QR code scanning box with CSS

We usually have a scanning box when we open the c...

WeChat applet realizes horizontal and vertical scrolling

This article example shares the specific code for...

Simple steps to configure Nginx reverse proxy with SSL

Preface A reverse proxy is a server that receives...

Detailed explanation of MySQL file storage

What is a file system We know that storage engine...

MySQL kill command usage guide

KILL [CONNECTION | QUERY] processlist_id In MySQL...