[linux] learn operation and maintenance step by step - Basic chapter - file permission management

Write before:
Blogger is a rebel who pursues freedom after 80's standard of devoting himself to education and training. His nickname comes from Su Shi's "the height is too cold" in "the head of the tune of water". He always reminds himself to stand the loneliness and suffer the loneliness. On the road of technology, he is brave to go forward.
I have put all the linux operation and maintenance systems on gitee, https://gitee.com/gaosh08/LinuxFamily
Welcome to star, contribute, communicate, and there will be python series and java series in the future.


Article catalog

Basic concept of authority

In this article, we will discuss the general permissions of files in the Linux operating system.
Linux permission is a mechanism used by the operating system to restrict access to resources.
There are three types of authority:

  • read
  • write
  • implement

In the Linux operating system, each process runs as an identity (user). The process permissions are the same as the user permissions. The greater the user permissions, the greater the process permissions.

It needs to be clear here that the file permissions are mainly defined for three types of objects:

Owner: owner, u
Group: genus group, g
Other: other, o

Each file defines three permissions for the above three objects:

jurisdiction Corresponding operation object Authority description
r file The readability of the file means that you can use grep and other similar commands to view it
w file Writable means that this file can be modified or deleted with vim
x file Executable, can be run as a command
r catalog You can use ls for this directory to see everything
w catalog You can create or delete files in the directory
x catalog You can use cd to switch to this directory and view the details in the directory

Here we need to mention the conversion of binary and decimal permissions separately

Next let's look at the command of permission management

Rights management command

1)chmod

Role: modify permission
Usage: chmod mode file
Operator + add permission - reduce permission = directly give a permission

Parameters:

-c: if the file permission has been changed, the change action will be displayed
 -f: do not display error message if the file permission cannot be changed
 -v: display details of permission change
 -R: change all files and subdirectories in the current directory with the same permission (i.e. change one by one in a recursive way)
--Help: show help
 --Version: display version

give an example:

1) Permissions to view files
[root@zmgaosh zip]# ll 
//Total consumption 8
drwxr-xr-x 2 root root 4096 6 20 / 20:26 etc
-rw-r--r-- 1 root root  685 6 20 / 20:25 passwd.zip

In this case, the permissions of the folder etc are
r: 4
w:2
x:1
rwx =7 r_x = 5, so the permissions of etc are 755
And documents passwd.zip Permissions for are 644




2) Grant the user rwx permission to etc directory
[root@zmgaosh zip]# ll  #View current directory
//Total consumption 8
drwxr-xr-x 2 root root 4096 6 20 / 20:26 etc
-rw-r--r-- 1 root root  685 6 20 / 20:25 passwd.zip
//Pay attention to the permissions of the directory
[root@zmgaosh zip]# ll etc/
//Total dosage 4
-rw-r--r-- 1 root root 1317 6 20 15:16 passwd
#The permission to view the files in the directory, currently rw-
[root@zmgaosh zip]# chmod -R u+rwx etc  #Grant rwx permission to the directory and its content owner
[root@zmgaosh zip]# ll
//Total consumption 8
drwxr-xr-x 2 root root 4096 6 20 / 20:26 etc
-rw-r--r-- 1 root root  685 6 20 / 20:25 passwd.zip
[root@zmgaosh zip]# ll etc/
//Total dosage 4
-rwxr--r-- 1 root root 1317 6 20 15:16 passwd #You can see that the owner has changed to rwx
3) Grant rwx permission to the group
[root@zmgaosh zip]# ll
//Total consumption 8
drwxr-xr-x 2 root root 4096 6 20 / 20:26 etc
-rw-r--r-- 1 root root  685 6 20 / 20:25 passwd.zip
[root@zmgaosh zip]# ll etc
//Total dosage 4
-rwxr--r-- 1 root root 1317 6 20 15:16 passwd
[root@zmgaosh zip]# chmod -R g+rwx etc
[root@zmgaosh zip]# ll
//Total consumption 8
drwxrwxr-x 2 root root 4096 6 20 / 20:26 etc
-rw-r--r-- 1 root root  685 6 20 / 20:25 passwd.zip
[root@zmgaosh zip]# ll etc
//Total dosage 4
-rwxrwxr-- 1 root root 1317 6 20 15:16 passwd
[root@zmgaosh zip]# 

Combined with the second example, this example is easy to understand. You can see that the groups of folders and files have become rwx

4) Remove the read and write permissions of the owner of etc directory
[root@zmgaosh zip]# chmod -R u-wx etc
[root@zmgaosh zip]# ll
//Total consumption 8
dr--rwxr-x 2 root root 4096 6 20 / 20:26 etc
-rw-r--r-- 1 root root  685 6 20 / 20:25 passwd.zip
[root@zmgaosh zip]# 

5) Binary method is used to grant rwx permission to etc directory, owner, group and other users
[root@zmgaosh zip]# chmod -R 777 etc
[root@zmgaosh zip]# ll
//Total consumption 8
drwxrwxrwx 2 root root 4096 6 20 / 20:26 etc
-rw-r--r-- 1 root root  685 6 20 / 20:25 passwd.zip
[root@zmgaosh zip]# 

2) chown command

Function: change the owner of the specified file to the specified user or group. The user can be user name or user ID; the group can be group name or group ID

Parameters:

  • -c: display information of changed parts
  • -f: ignore error messages
  • -h: repair symbolic links
  • -v: display detailed processing information
  • -R: process all files in the specified directory and its subdirectories
  • – help: display auxiliary instructions
  • – version: display version

give an example:

1) Change the owner and all groups of passwd to xinsz
[root@zmgaosh etc]# ll
//Total dosage 4
-rwxrwxrwx 1 root root 1317 6 20 15:16 passwd
[root@zmgaosh etc]# chown xinsz:xinsz passwd
[root@zmgaosh etc]# ll
//Total dosage 4
-rwxrwxrwx 1 xinsz xinsz 1317 6 20 15:16 passwd
## You can see that both the user group and the owner become xinsz
[root@zmgaosh etc]# 

2) Another way to put the owners and groups of passwd
[root@zmgaosh etc]# chown root: passwd
[root@zmgaosh etc]# ll
//Total dosage 4
-rwxrwxrwx 1 root root 1317 6 20 15:16 passwd
[root@zmgaosh etc]# 

3) Only change the group of passwd
[root@zmgaosh etc]# chown :xinsz passwd
[root@zmgaosh etc]# ll
//Total dosage 4
-rwxrwxrwx 1 root xinsz 1317 6 20 15:16 passwd
[root@zmgaosh etc]# 

summary

In Linux, everything is a file. Permissions are divided into ordinary permissions, advanced permissions and default permissions. Today, we only discuss ordinary permissions.

You need to keep in mind that permissions are for three objects: owner, group, and other users
There are three permissions for each object: read, write, and execute
Fully understand the three meanings of UGO and the method of setting permissions by letters and numbers.

Shake your hands and praise. Your praise is the biggest driving force of Shenghan's creation. See you next article!

Tags: Linux Python Java vim

Posted on Tue, 23 Jun 2020 03:37:21 -0400 by Fribbles