Linux [Practice] - User management, linux system root password retrieval method

content

1. User management

1. Add user

2. Set/modify user password

3. Delete user

4. Query user information

5. Switch user

6. View current user information

7. User Groups

8. Log out the user

9. Introduction to related documents of users and user groups

2. How to retrieve the root password (high frequency interview)


1. User management

        The Linux system is a multi-user and multi-task operating system. Every user who wants to use system resources needs to apply for an account with the system administrator, and then enter the system as this account.

1. Add user

Syntax: useradd username , useradd -d specifies the directory username

        After the user is successfully added, the home directory /home/username with the same name as the user will be automatically created, and it will automatically switch to this directory after logging in with the user name; you can also specify a directory for a new user, the command is specified by useradd - d directory username ,

There is only user xiaoma in the initial /home directory as follows, we add a new user xiaowang;

2. Set/modify user password

Syntax: passwd username

Set the password of the account xiaoma;

After the password is set, if you want to modify it, repeat the above operation again.

3. Delete user

Syntax: userdel username , userdel -r username

Delete user xiaowang as follows, you can see that the user's home directory still exists after deletion, but at this time, xiaowang's account can no longer be used to log in.

If you want to delete a user and delete the user's home directory, use the command userdel -r username , all data of the user will be deleted after running this command (operate with caution in specific work).

4. Query user information

Syntax: id username

Querying for a user that does not exist will return "user does not exist";

5. Switch user

Syntax: su - username

When we log in to the Linux operating system, we should try to use the root account as little as possible, because its authority is too high and may cause major mistakes. You can log in to the ordinary user account first, and then use the command su - username to switch to the system administrator. 

The following is an ordinary user xiaoma;

Next, switch the user to root; 

Switching from a user with high authority to a user with low authority does not require a password, and vice versa; when you want to return to the original user, use the command exit/logout

6. View current user information

语法:whoami / who am i

The user information queried is the user who first logged in to the Linux system, that is to say, even if the user is switched during the operation, the user information at the time of login is still displayed when queried again;

7. User Groups

        User groups are similar to roles, and users with commonality (same permissions) can be managed uniformly in the system;

grammar:

  • groupadd group name add group
  • groupdel group name delete group
  • useradd -g user group username directly join the user group when adding a user (when adding a user, if no group is specified, a group with the same name as the user name will be created by default and the user will be added to it)
  • usermod -g user group username Modify the user's group

Create the user taiyuan as follows and join the newly created group shanxi;

Modify the group of user taiyuan;

8. Log out the user

Syntax: logout

If you want to return to the original account after switching accounts, use the logout command  logout   to log out of the current account; if you log out again under the original account, you will directly log out of the Linux system. 

9. Introduction to related documents of users and user groups

  • /etc/passwd file , user's configuration file, record user information (meaning of each line: user name: password: user identification number: comment description: home directory: login shell) ;
  • /etc/shadow file , password configuration file (meaning of each line: login name: encrypted password: last modification time: minimum time interval: maximum time interval: warning time: inactivity time: expiration time: flag) ;
  • /etc/group file , group configuration file, records group information in Linux (meaning of each line: group name: password: group identification number: list of users in the group) ;

To view the specific information in the file, you can use the command vim file to view it.

2. How to retrieve the root password (high frequency interview)

        As the highest authority manager of the system, root's password is naturally crucial, so how should the password of the root account be retrieved? Proceed as follows:

1. Start the Linux system. After entering the boot interface, press " e " to enter the editing interface. Note that this must be fast, and the operation should be performed before the system enters the login page;

2. After entering the editing interface, find the line that starts with "Linux 16", and enter init=/bin/sh at the end of the line ;

3. After the input is completed, press Ctrl+X to enter the single-user mode, the following interface is the single-user mode;

4. Enter mount -o remount,rw / at the position where the cursor is blinking , and press Enter after the input is complete;

5. Enter passwd at the end of the new line and press Enter, then enter the password and confirm the password. After the password is successfully modified, the word passwd... will be displayed;

6. Next, enter touch /.autorelabel at the end of the new line, and press Enter to complete the input;

7. Continue to enter exec /sbin/init at the end of the new line, press Enter after completion and wait for the system to change the password;

8. Wait patiently, the new root password will take effect after the system restarts automatically.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324144160&siteId=291194637