Take you to learn comprehensive Linux account and permission management

1. User account and group account

1.1 User account

Super user: root account UID=0, GID=0
Program user: UID range is 1-999
Ordinary user: UID range is 1000+

1.2 Group accounts

Basic group (private group)
additional group (public group)

1.3 UID and GID

UID (User IDentity) user identification number
GID (Group IDentity) group identification number

1.4 User account file /etc/passwd

Save user name, home directory, login shell and other information.

1.4.1 File location

[root@server1 ~]#head -2 /etc/passwd

Insert picture description here
The meaning of the displayed information:

Insert picture description here

1.5 User account file /etc/shadow

Save the user's password, effective account information and other information

1.5.1 File location

[root@server1 ~]#head -2 /etc/shadow

Insert picture description here

2. Manage user accounts

2.1 Add user account

useradd command

2.1.1 Code format

[root@server1 ~]#useradd [选项]...用户名

2.1.2 Common options

Insert picture description here
Example: Set uid to 1010, host directory to wangwu, basic group to wheel, nearby group to zhangsan, lisi account that cannot be logged inInsert picture description here

2.2 Set user password

passwd command

2.2.1 Code format

[root@server1 ~]#passwd [选项] 用户名

2.2.2 Common options

Insert picture description here
When passwd does not specify a user name, modify the password of the current account.

2.2.3 Initialize the password for the user as 123456

 echo "123456" >passwd --stdin 用户名 

2.3 Modify the attributes of a user account

usermod command

2.3.1 Code format

[root@server1 ~]#usermod [选项] 用户名

2.3.2 Common options

Insert picture description here

2.4 Delete user

userdel command

2.4.1 Code format

[root@server1 ~]#userdel [选项] 用户名

2.4.2 Common options

Insert picture description here

Three, management group account

3.1 Group account file

Similar to user account file,
save basic information of group account

/etc/group

Save the password information of the group account

/etc/gshadow

3.2 Add and delete group members

gpasswd command

3.2.1 Code format

[root@server1 ~]#gpasswd [选项]...组账户名

3.2.2 Common options

Insert picture description here

3.3 Query group account information

3.3.1 groups command

Query the group to which the user belongs:

 groups [用户名]

3.3.2 id command

Query user ID

id [用户名]

3.3.3 finger command

Query user account details

finger [用户名]

3.3.4 w、who、users命令

Query the information of the logged-in host

直接输入w、who、users

4. Permission and ownership of files/directories

4.1 Access rights

Insert picture description here

4.2 Ownership (ownership)

Insert picture description here

4.3 View permissions and ownership of files/directories

Insert picture description here
So the default file permissions are: 644 The
default directory permissions are: 755

4.4 Set permissions for files and directories

chmod command

4.4.1 Code format

chmod [ugoa] [+-=] [rwx] 文件或目录

Among them, u, g, o, a represent owner, group, other users and all users respectively.

chmod nnn 文件或目录

n is the digital representation of the aforementioned authority.

4.4.2 Common options

-R: recursively modify the permissions of all sub-items in the specified directory

4.5 Set the ownership of files and directories

chown command

4.5.1 Code format

chown 属主 文件或目录
chown :属组 文件或目录
chown 属主:属组 文件或目录

4.5.2 Common options

-R: recursively modify the permissions of all sub-files and directories in the specified directory

4.6 Permission mask

The umask command
controls the permissions of new files or directories. The
default permissions are removed. The permissions of umask are newly created files or directories.

4.6.1 Code format

umask settings

umask 022

umask view

umask

Cancel umask setting

unset umask

Five, the initial configuration file of environment variables and user accounts

5.1 Environment variables

Environment variables are closely related to Shell. After the user logs in to the system, a Shell is started. For Linux, it is generally bash, but it can also be reset or switched to another Shell. For UNIX, it may be CShelll. Environment variables are set through Shell commands, and the set environment variables can be used by all programs running by the current user. For the Shell program bash, the corresponding environment variables can be accessed through variable names, and environment variables can be set through export.

5.2 Initial configuration file of user account

5.2.1 etc/profile:

This file sets environmental information for each user of the system, and the file is executed when the user logs in for the first time.
And collect the shell settings from the configuration file in the /etc/profile.d directory.
Note: Here we set global variables that can be used by all users.

5.2.2 /etc/bashrc

Execute this file for every user running the bash shell. When the bash shell is opened, the file is read.

5.2.3 ~/.bash_profile

Each user can use this file to input the shell information dedicated to their own use. When the user logs in, the file is executed only once!
By default, he sets some environment variables and executes the user's .bashrc file.
Note: ~ Under LINUX represents the variable HOME.
In addition, under different LINUX operating systems, this file may be different, it may be ~/.bash_profile;
one or more of ~/.bash_login or ~/.profile , if there are several, then the order of execution They are: ~/.bash_profile, ~/.bash_login, ~/.profile.

5.2.4 ~/.bashrc

This file contains bash information specific to your bash shell, which is read when you log in and every time you open a new shell.
(Note: This file is at the beginning, so it is hidden in the folder)
So how do we add our own environment variables?
Open this file with Notepad, then write: xiaokang=kangkang at the end
and save it, so that every time we open a new terminal, our variable will take effect. Remember, if you have opened a terminal and then you modify this file, it will not take effect under this terminal. In general, it is best for users to modify here, but sometimes it will overwrite the parent variable, such as PATH is set by ROOT, but if you write PATH=xx in this file, then all PATHs in the future will become xx , So we should write in this file as:

PATH=$PATH:xx

Then add the original and your own. And pay attention to use under LINUX system: split means parallel, not windo;
3 and 4 are both in the user directory, and their only difference is: .bash_profile can only be started once when logging in. There seems to be no 3 files in my Ubuntu.

5.2.5 ~/.bash_logout

Each time you exit the system (exit the bash shell), execute the file. In addition, the variables (global) set in /etc/profile can act on any user, while the variables (local) set in ~/.bashrc etc. can only inherit the variables in /etc/profile, they are /" Father-son/"Relationship.
~/.bash_profile is an interactive, login mode to enter bash operation
~/.bashrc is an interactive non-login mode to enter bash operation
usually the two settings are roughly the same, so usually the former will call the latter.

5.3 Summary

When you log in and the login shell is bash, bash first executes the commands in the /etc/profile file (if the file exists), and then it searches for ~/.bash_profile, ~/.profile, ~/.bash_login or files in order, and Execute the command in the first readable file found. When you log in to bash and exit, it will execute the command in the ~/.bash_logout file.
When starting an interactive bash, it will execute the commands in the ~/.bashrc file (if the file exists and is readable). When starting non-interactively to run a shell script, bash will look for the bash_env environment variable and determine the execution The name of the file.

Guess you like

Origin blog.csdn.net/weixin_50344820/article/details/109061222