Linux user management

Keywords: Linux bash

1. Basic introduction

Linux system is a multi-user and multi task operating system. Any user who wants to use system resources must first apply to the system administrator for an account, and then enter the system as this account.

2. Add user

  • Basic grammar

    useradd username

  • case

    Add a user milan.

    [root@hostName01 ~]# cd /home
    [root@hostName01 home]# ls
    tom
    [root@hostName01 home]# useradd milan
    [root@hostName01 home]# ls
    milan  tom
    
  • be careful

    1. When the user is created successfully, a home directory with the same name as the user will be automatically created. By default, the user's home directory is [/ home/milan]. After logging in with a new user, it will be switched to this directory by default.

    2. You can specify the home directory for the newly created user through [useradd -d].

      [root@hostName01 home]# useradd -d /home/test king
      [root@hostName01 home]# ls
      milan  test  tom
      

3. Specify / modify password

  • Basic grammar

    passwd user name

  • case

    Assign a password to [milan].

    [root@hostName01 home]# passwd milan
     Change user milan Your password.
    New password:
    Invalid password: password is less than 8 characters
     Re enter the new password:
    passwd: All authentication tokens have been successfully updated.
    

    When [invalid password: password less than 8 characters] is reminded, the system only reminds that the password is too simple, but it can still be set successfully. Of course, in practical work, the password should be set more complex.

4. Display the directory of the current user

  • Basic grammar

    pwd

  • case

    Log in to the [milan] user and display the directory of the user.

    [milan@hostName01 ~]$ pwd
    /home/milan
    

5. Delete user

  • Basic grammar

    userdel user name

  • case

    1. Delete the user [milan], but keep the home directory.

      Syntax: userdel milan

      [root@hostName01 ~]# cd /home
      [root@hostName01 home]# ls
      milan  test  tom
      [root@hostName01 home]# userdel milan
      [root@hostName01 home]# ls
      milan  test  tom
      

      [milan] the user cannot log in, but the user's home directory is still there.

    2. Delete the user tom and its home directory.

      Syntax: userdel -r tom

      [root@hostName01 home]# ls
      milan  test  tom
      [root@hostName01 home]# userdel -r tom
      [root@hostName01 home]# ls
      milan  test
      

      This method will delete all work data under the [tom] user, which should be operated with caution.

  • be careful

    1. Deleting a user requires the permission of the [root] user.

    2. Think: do you want to keep the home directory?

      Data are valuable and generally recommended to be retained.

6. Query user information

  • Basic grammar

    id user name

  • case

    Query the information of the [root] user.

    [root@hostName01 home]# id root
    uid=0(root) gid=0(root) group=0(root)
    [root@hostName01 home]# id king
    uid=1002(king) gid=1002(king) group=1002(king)
    [root@hostName01 home]# id milan
    id: milan: no such user
    
  • be careful

    When the user does not exist, return no such user.

7. Switch users

  • introduce

    In Linux operation, if the current user's permission is insufficient, you can switch to a high authority user through the [su -] command, such as [root].

  • Basic grammar

    su - switch user name

  • case

    Create a [jack] user, specify the password, and then switch to [jack].

    [root@hostName01 home]# useradd jack
    [root@hostName01 home]# passwd jack
     Change user jack Your password.
    New password:
    Invalid password: password is less than 8 characters
     Re enter the new password:
    passwd: All authentication tokens have been successfully updated.
    [root@hostName01 home]# su - jack
    [jack@hostName01 ~]$ su - root
     password:
    Last login: October 23, 2006:02:55 CST 2021 From 192.168.233.1pts/0 upper
    [root@hostName01 ~]# logout
    [jack@hostName01 ~]$ exit
     Logout
    
  • be careful

    1. When a user with high permission switches to a user with low permission, it is not necessary to enter a password, otherwise it is required.
    2. When you need to return to the original user, use the [exit/logout] command.

8. View the current login user

  • Basic grammar

    1. who am i

      Query the users who log in to the system.

    2. whoami

      Query valid users of the current system

  • case

    1. who am i

      [root@hostName01 ~]# who am i
      root     pts/0        2021-10-23 10:24 (192.168.233.1)
      [root@hostName01 ~]# su - jack
       Last login: October 23, 2006:20:12 CST 2021pts/0 upper
      [jack@hostName01 ~]$ who am i
      root     pts/0        2021-10-23 10:24 (192.168.233.1)
      
    2. whoami

      [root@hostName01 ~]# whoami
      root
      [root@hostName01 ~]# su - jack
       Last login: October 23, 2006:28:16 CST 2021 From 192.168.233.1pts/0 upper
      [jack@hostName01 ~]$ whoami
      jack
      
  • be careful

    This command queries the logged in user, not the current user, that is, the switched user will not be queried, but the user used when logging in the system for the first time.

9. User group

<1> Introduction

Similar to roles, the system can uniformly manage multiple users with common (common permissions). For example, tom, jack and milan have the same permissions. We can divide them into the same group for unified management.

<2> Add and delete groups

  • New group

    Instruction:

    groupadd group name

    Case:

    [root@hostName01 ~]# groupadd wudang
    
  • delete group

    Instruction:

    groupdel group name

    Case:

    [root@hostName01 ~]# groupdel wudang
    

<3> Add groups directly when adding users

Instruction:

useradd -g user group user name

Case:

Add a user [zwj] and directly assign the user to [wudang]

[root@hostName01 ~]# groupadd wudang
[root@hostName01 ~]# useradd -g wudang zwj
[root@hostName01 ~]# id zwj
uid=1004(zwj) gid=1004(wudang) group=1004(wudang)

be careful:

If a new user does not specify a group, the system will automatically create a group with the same name by default and divide the new user into groups with the same name.

[root@hostName01 ~]# id king
uid=1002(king) gid=1002(king) group=1002(king)

<4> Modify user's group

Instruction:

usermod -g user group user name

Case:

Create a group [mojiao] and put the user [zwj] into the [mojiao] group.

[root@hostName01 ~]# id zwj
uid=1004(zwj) gid=1004(wudang) group=1004(wudang)
[root@hostName01 ~]# groupadd mojiao
[root@hostName01 ~]# usermod -g mojiao zwj
[root@hostName01 ~]# id zwj
uid=1004(zwj) gid=1005(mojiao) group=1005(mojiao)

10. User and group related documents

  • [/ etc/passwd] file

    The user's configuration file records various user information.

    Meaning of each line: user name: Password: user ID: group ID: annotative Description: Home Directory: login shell

    king:x:1002:1002::/home/test:/bin/bash
    jack:x:1003:1003::/home/jack:/bin/bash
    zwj:x:1004:1005::/home/zwj:/bin/bash
    
  • [/ etc/shadow] file

    Password profile.

    Meaning of each line: Login Name: encryption password: last modification time: minimum time interval: maximum time interval: warning time: inactive time: expiration time: Flag

    king:!!:18923:0:99999:7:::
    jack:$6$VjRt0agK$lXdDlSt6bQAh4GT/6xEE2TGK28wWMnHjrBs4lIk64fU6mVwr0lYYLNBksA6eb4t9szs78uPk8AN8KhID73wmP0:18923:0:99999:7:::
    zwj:!!:18923:0:99999:7:::
    

    [!] indicates that no password is set.

  • [/ etc/group] file

    group configuration file, which records the information of groups contained in Linux.

    Meaning of each line: Group Name: Password: group ID number: list of users in the group

    king:x:1002:
    jack:x:1003:
    wudang:x:1004:
    mojiao:x:1005:
    

Reference video: https://www.bilibili.com/video/BV1Sv411r7vd?p=21

Posted by Seraskier on Sat, 23 Oct 2021 05:11:48 -0700