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

How to uninstall Linux's native openjdk and install sun jdk

See: https://www.jb51.net/article/112612.htm Chec...

jQuery implements all selection and reverse selection operation case

This article shares the specific code of jQuery t...

Example of using Dockerfile to build an nginx image

Introduction to Dockerfile Docker can automatical...

How to safely shut down MySQL

When shutting down the MySQL server, various prob...

Complete steps to install FFmpeg in CentOS server

Preface The server system environment is: CentOS ...

Sample code for generating QR code using js

Some time ago, the project needed to develop the ...

MySQL sql_mode analysis and setting explanation

When inserting a set of data into the MySQL datab...

How to remotely connect to the cloud server database using Navicat

It is very convenient to connect to a remote serv...

202 Free High Quality XHTML Templates (1)

Here 123WORDPRESS.COM presents the first part of ...

Method for realizing Internet interconnection by VMware virtual machine bridging

After installing VMware and creating a new virtua...

Network management and network isolation implementation of Docker containers

1. Docker network management 1. Docker container ...

7 interview questions about JS this, how many can you answer correctly

Preface In JavaScript, this is the function calli...

Common problems in implementing the progress bar function of vue Nprogress

NProgress is the progress bar that appears at the...