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

Optimized record of using IN data volume in Mysql

The MySQL version number is 5.7.28. Table A has 3...

Summary of important components of MySQL InnoDB

Innodb includes the following components 1. innod...

CSS transparent border background-clip magic

This article mainly introduces the wonderful use ...

How to install docker using YUM

As shown in the following figure: If the version ...

Deploy grafana+prometheus configuration using docker

docker-compose-monitor.yml version: '2' n...

Differences between proxy_pass in two modules in nginx

1. The proxy_pass directive of the 1.ngx_stream_p...

Install redis and MySQL on CentOS

1|0MySQL (MariaDB) 1|11. Description MariaDB data...

Vue.js $refs usage case explanation

Despite props and events, sometimes you still nee...

Detailed explanation of how to solve the problem of too long content in CSS

When we write CSS, we sometimes forget about the ...

A little-known JS problem: [] == ![] is true, but {} == !{} is false

console.log( [] == ![] ) // true console.log( {} ...

How to use boost.python to call c++ dynamic library in linux

Preface Recently I started using robot framework ...

CSS Transition expands and collapses elements by changing the Height

A common development need is that we want to coll...

MySQL common backup commands and shell backup scripts sharing

To back up multiple databases, you can use the fo...

Linux kernel device driver virtual file system notes

/******************** * Virtual File System VFS *...