Permission management under Linux (required for beginners)

content

1. Users under Linux

2. File permissions

file type:

Basic permissions

The representation method of file weights:

Three permission settings and modifications

chmod function:

chown function

umask

  4. Directory permissions

  5. Sticky bit


1. Users under Linux

There are two kinds of users under linux: super user (root) and ordinary user .

1. Super user: You can do anything under the linux system, and you can do limited things under linux without restriction. The command prompt is "#".
2. Ordinary users: Ordinary user permissions are not as high as super users and will be restricted. The command prompt of ordinary users is "$".

As shown in the picture: ordinary users

 root:

             

 Switching between superuser and normal user can use su or su-intern to switch between normal user and superuser. Switching from a super user to a normal user does not require a password, and switching from a normal user to a super user requires the super user's password.

See the demo below:                       

Hit enter:

 Let's demonstrate switching from normal user to super user:

 When switching from super user to ordinary user above, we use su to switch and we find that the working directory is still root? Why is this? Let's talk about the difference between su and su-:

1. The difference between su and su- is: su only switches the identity of root, but the shell environment is still the shell of the root identity, and the latter switches the shell and the user environment together to the identity of ordinary users.

2. After su is switched to the root user, pwd it and find that the working directory is still the working directory of the ordinary user; after switching with the su - command, the working directory becomes the working directory of the root. Use the echo $PATH command to see how the environment variables differ between su and su -after. And so on, to switch from the current user to another user, you should use the su - command.

If we just want to briefly elevate privileges we can use the sudo command.

Command: sudo 

Function: Temporarily elevate the privileges of ordinary users

Description: Allows ordinary users to do things that they couldn't do before

2. File permissions

Permissions under linux = people + attributes of things

One: Classification of file visitors (people):
1. The owner of the file and file directory: u---User (Chinese civilian legal issues)
2. The user of the group where the owner of the file and file directory belongs: g-- -Group (not much to say)
3. Other users: o---Others (foreigners)

Two: File Types and Access Rights

file type:

d folder (directory file)
- normal file
l Soft connection (equivalent to a shortcut under Windows)
b block device file
p pipe file
c character device file
s socket file

Summary: Under linux, everything is a file, and under linux, there is no file suffix distinction (gcc g++ is to distinguish file suffixes, and the two are commands on the system).

Basic permissions:

i. Read (r/4): Read has the right to read the contents of the file for a file; for a directory, it has the right to browse the information in the directory
ii. Write (w/2): Write for a file, It has the right to modify the content of the file; for the directory, it has the right to delete the file in the mobile directory.
iii. Execute (x/1): execute has the right to execute the file for the file;
iv. "—" means not having the permission

The representation method of file weights:

There are two ways to express file weights:

1. Character representation:

Express illustrate Express illustrate
rwx Readable, Writable and Executable -wx Writable and executable
r_x Readable, not writable, executable rw- read and write
r__ read only -w_ writable only
--x executable only --- permission denied

2. Character representation:

character representation Octal binary
rwx  7 111
rw_ 6 110
r-- 4 100
r_x 5 101
--x 1 001
_w_ 2 010
-wr 3 011
--- 0 000

Three permission settings and modifications

chmod
function: set the
file access                                                                       rights +/-=authority characters +:Add the authority indicated by the authority code to the authority scope-   :Cancel the authority indicated by the authority code to the authority scope=:Give the authority indicated by the authority code to the authority scope User symbol: u: owner g : owner of the same group use o: other users a: all users












 

 In linux, a file has its owner, its group and others: the following demonstrates adding permissions to the owner of the file:

 

 We found that the group to which the file belongs at this time has already added the x permission. Others are similar.

Method 2: Add in octal format

chown
function: modify the owner of the
file Format: chown [parameter] user name file name
Example:


c) chgrp
function: modify the group of a file or directory
Format: chgrp [parameter] User group name file name
Common options: -R recursively modify the group of a file or directory
Example:

 

umask

umask
function:
1. View or modify the file mask 2. The default permission of the new folder = 0666 3. The default permission of the new directory = 0777
But in fact, the permissions you see for the files and directories you create are often not the above values. The reason is that it is also affected by
umask when creating a file or directory. Assuming that the default permission is mask, the actually created file permission is: mask & ~umask
Format: umask Permission value description: After subtracting the permission mask from the existing access permission, the default permission when creating a file can be generated . The default mask value of super user is 0022, and
the default value of common user is 0002.

1. View the permission mask:

2. Modify the permission mask:

 However, it is recommended that we generally do not modify it.

4. Directory permissions

1. Executable permission (x): If the directory does not have executable permission, you cannot cd to the directory.
2. Readable permission (r): If the directory does not have readable permission, you cannot use commands such as ls to view the files in the directory Content.
3. Writable permission (w): If the directory does not have writable permission, you cannot create files in the directory, and you cannot delete files in the directory.

Example demonstration: If a directory does not have x permission, can we still enter this directory?

 Example demonstration: If we remove the read permission of a directory, can we read the file?

 We found that this is not possible at this time, we cannot read the contents of this file.

Example demonstration: If we remove the writable permission of this file, can we delete this file or create a file in this directory?

5. Sticky bit

When a directory is set to "sticky bit" (with chmod +t), the files in the directory can only be
deleted by 1. Super administrator
2. The owner of the directory deletes
3. The owner of the file deletes

Example demonstration:

 Finally, assign the user sudo privileges:

1. First switch to root

2. Use vim to open the file /etc/sudoers

 

 Find the corresponding position of the picture and add the corresponding user.

Guess you like

Origin blog.csdn.net/qq_56999918/article/details/123647390