Detailed explanation of Linux file permissions and group modification commands

Detailed explanation of Linux file permissions and group modification commands

In Linux, everything is a file (directories are also files), and each file has readable (read), writable (write), and executable (execute) permissions for users. The execution operation of a directory indicates whether you have permission to enter the directory, and the executable operation of a file indicates whether you can run the file. Each file belongs to a user and a user group, and each file has specific permissions for the file owner, the group to which it belongs, and other user groups.

As shown in the figure above, except for the first character indicating the file type, the following characters are grouped in three, which is a combination of the three parameters of "rwx". [r] stands for readable (read), [w] stands for writable (write), and [x] stands for executable (execute). At the same time, they will also correspond to a number respectively, [r] corresponds to 4, [w] corresponds to 2, and [x] corresponds to 1. These numbers can be used when modifying file permissions. If there is no permission in [rwx], a minus sign [-] will be used instead.

The first group is the file owner's operating permissions for the file, the second group is the file's group's operating permissions for the file, and the third group is the operating permissions for users in other groups. For example: If the permission data of a file is "rwxr-xr--", the first three characters indicate that the owner of the file can read, write, and execute the file. The middle three characters indicate that the group to which the file belongs can read and execute the file. The last three characters indicate that users of other groups can only read the file.

Change file permissions: chmod
To change file permissions, use the command chmod. The permissions of a file correspond to three types of read, write, and execute permissions for three users (yourself, group, and others). There are a total of 9 permissions, three in a group. Add up the permissions of each group to get a number. For example, the number corresponding to [rwxrwx---] is:

owner = rwx = 4+2+1 = 7
group = rwx = 4+2+1 = 7
others= --- = 0+0+0 = 0

As long as the user has write permission to the file, the user can update other users' operation permissions on the file. The command is:

chmod [-R] xyz file or directory
  • xyz: corresponds to the sum of the three types of user permission values.
  • -R: Perform recursive changes, that is, all files in the subdirectory will be changed

For example:

[root@www ~]# ls -al .bashrc
-rw-r--r-- 1 root root 395 Jul 4 11:45 .bashrc
[root@www ~]# chmod 777 .bashrc # Change to allow all users to read, write and execute [root@www ~]# ls -al .bashrc
-rwxrwxrwx 1 root root 395 Jul 4 11:45 .bashrc

In addition to using numbers to change file permissions, chmod can also use symbols to change file permissions. u, g, o, and a are abbreviations for user, group, others, and all (all users), respectively. r, w, and x are abbreviations for read, write, and execute, respectively. chmod can accept these abbreviations to change file permissions.

ug +(increase) r
chmod o -(remove) w file or directory a =(assign value) x

For example:

# Give yourself read, write and execute permissions, and give the user group and others read and execute operations chmod u=rwx,go=rx .bashrc
#Add write permission to all users chmod a+w .bashrc
# Remove write permission for all users chmod aw .bashrc

Change the group chgrp

To change the group of a file, use the chgrp command, which is the abbreviation of change group. The command format is:

chgrp [-R] groupname dirname/filename

-R recursively changes directory and subdirectory files. groupname must be a group that exists in the /etc/group file on the system.

# Update the group of all files in the current directory and its subdirectories to the mysql group chgrp -R mysql .

Change the file owner chown

To change the owner of a file, use the chown command, which is the abbreviation of change owner. The command format is:

chown [-R] owner dirname/filename

or

chown [-R] owner:group dirname/filename

The chown command can not only change the owner of a file, but also the group of the file. Just add the name of the group after the owner.

# Change install.log to mysql user chown mysql install.log
# Change install.log to root user and root group chown root:root install.log

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed Analysis of the chmod Command to Change File Permissions in Linux
  • Modify Linux file permissions command: chmod command detailed explanation
  • View and modify directory file permissions (commands) under Linux
  • Simple command explanation for modifying directory and file permissions in Linux
  • How to view and modify file read and write permissions in Linux system

<<:  MySQL 8 new features: Invisible Indexes

>>:  SMS verification code login function based on antd pro (process analysis)

Recommend

How to use nginx to build a static resource server

Taking Windows as an example, Linux is actually t...

Nginx prohibits direct access via IP and redirects to a custom 500 page

Directly to the configuration file server { liste...

Win10 configuration tomcat environment variables tutorial diagram

Before configuration, we need to do the following...

Key points for writing content of HTML web page META tags

The META tag is an auxiliary tag in the head area...

Steps to create a CentOS container through Docker

Table of contents Preface Create a bridge network...

About 3 common packages of rem adaptation

Preface I wrote an article about rem adaptation b...

Solution to the problem of MySQL data delay jump

Today we analyzed another typical problem about d...

A simple way to implement Vue's drag screenshot function

Drag the mouse to take a screenshot of the page (...

Solve the problem of IDEA configuring tomcat startup error

The following two errors were encountered when co...

Tutorial diagram of installing centos7.3 on vmware virtual machine

VMware Preparation CentOS preparation, here is Ce...

Two ways to configure Vue global methods

Table of contents 1. Introduction 2. The first me...

Detailed process of installing nginx1.9.1 on centos8

1.17.9 More delicious, really Nginx download addr...

Detailed explanation of Vue3 sandbox mechanism

Table of contents Preface Browser compiled versio...