Linux: User and Group Management

Linux: User and Group Management

ยท

5 min read

Managing user accounts and groups is a fundamental aspect of system administration, allowing you to control access, privileges, and organizational structure on a computer system. In this guide, we will explore various tasks related to user account creation, group management, password administration, and membership adjustments.

Note: The ones that are highlighted have some explanation.

Tasks

  1. Create user accounts

    • Create the user account "alice" with the password "Pass1234" and the home directory "/home/alice."

    • Create the user account "bob" with the password "Bob@5678" and the home directory "/home/bob."

    • Create a user account with the username "charlie."

  1. Create groups

    • Create the group "developers" with Group ID 1010.

    • Create the group "designers" with Group ID 1011.

  1. Add user as admin of a group

    • Add the user "alice" as the admin of the group "developers."

    • Add the user "bob" as the admin of the group "designers."

  1. Create a new password for user

    • Create a new password for the user account "alice."
  1. Switch to another user

    • Switch to the user account "alice."
  1. Delete user account

    • Delete the user account "bob."
  1. Change the login name of user

    • Change the login name of the user account "alice" to "alicia."
  1. Add new group

    • Add a new group account named "testers."
  1. Delete a group

    • Delete the group account "designers."
  1. Add a single user to a group

    • Add a single member with the username "charlie" to the group "testers."
  1. Add multiple users to a group

    • Add multiple members ("alicia" and "charlie") to the group "developers."
  1. Remove user from a group

    • Remove the group member "alicia" from the group "developers."

Solution

  1. Create user accounts

     ubuntu@~$: sudo useradd -m alice
     ubuntu@~$: ls /home/
     alice  ubuntu
     ubuntu@~$: sudo passwd alice
     New password: 
     Retype new password: 
     passwd: password updated successfully
     ubuntu@~$: sudo grep alice /etc/passwd
     alice:x:1001:1001::/home/alice:/bin/sh
     ubuntu@~$: sudo grep alice /etc/shadow
     alice:$y$j9T$H3Af2FpfPo10bANWWWgWU/$au0KFIMfWHz.oekhL.tJtc616vD4KrlMXQOOL75l589:19565:0:99999:7:::
    
     ubuntu@~$: sudo useradd -m bob
     ubuntu@~$: ls /home/
     alice  bob  ubuntu
     ubuntu@~$: sudo passwd bob
     New password: 
     Retype new password: 
     passwd: password updated successfully
     ubuntu@~$: grep bob /etc/passwd
     bob:x:1002:1002::/home/bob:/bin/sh
     ubuntu@~$: sudo grep bob /etc/shadow
     bob:$y$j9T$JNjgYMHI3S3H1v6UcwU8U.$qeW9qRcPK9ruNSf5/ykvsyz4HCn3cXFLRK741N8gAI6:19565:0:99999:7:::
    
     ubuntu@~$: sudo useradd charlie
     ubuntu@~$: grep charlie /etc/passwd
     charlie:x:1003:1003::/home/charlie:/bin/sh
    

    /etc/passwd file contains the user account details. Each field is separated by :

    alice:x:1001:1001::/home/alice:/bin/sh

    alice - username

    x - denotes that the password is encrypted and present in /etc/shadow file

    1001 - user ID

    1001 - group ID

    /home/alice - user home directory

    /bin/sh - default login shell

    /etc/shadow file contains the secure user details. Each field is separated by :

    alice:$y$j9T$H3Af2FpfPo10bANWWWgWU/$au0KFIMfWHz.oekhL.tJtc616vD4KrlMXQOOL75l589:19565:0:99999:7:::

    alice - username

    $y$j9T$H3Af2FpfPo10bANWWWgWU/$au0KFIMfWHz.oekhL.tJtc616vD4KrlMXQOOL75l589 - encrypted user password

    19565 - The date of the last password change, expressed as the number of days since Jan 1, 1970 (Unix time)

    0 - the number of days left before the user is allowed to change her password again. An empty field and a value of 0 mean that there is no minimum password age

    99999 - The maximum number of days the password is valid, after that user is forced to change her password again

    7 - The number of days before password is to expire that user is warned that his/her password must be changed

  2. Create groups

     ubuntu@~$: sudo groupadd -g 1010 developers
     ubuntu@~$: grep developers /etc/group
     developers:x:1010:
    
     ubuntu@~$: sudo groupadd -g 1011 designers
     ubuntu@~$: grep designers /etc/group
     designers:x:1011:
    

    /etc/group file contains the information about groups. Each field is separated by :

    developers:x:1010:

    developers - group name

    x - denotes whether the group has a password and whether is it present in /etc/gshadow file

    1010 - group ID

    The last field is blank as there are no users that are part of developers group

  3. Add user as admin of a group

     ubuntu@~$: sudo gpasswd -A alice developers
     ubuntu@~$: sudo grep developers /etc/gshadow
     developers:!:alice:
    
     ubuntu@~$: sudo gpasswd -A bob designers 
     ubuntu@~$: sudo grep designers /etc/gshadow
     designers:!:bob:
    

    /etc/gshadow file contains secure information about groups. Each field is separated by :

    developers:!:alice:

    developers - group name

    ! - group password. If the value of this field is !, then no user is allowed to access the group

    alice - group administrator

    last field is blank because there are no users that are part of developers group

    Also, when a user is made admin of the group, it doesn't mean the user is added to the group. alice is made the admin of developers the group but she is not part of the group.

  4. Create a new password for user

     ubuntu@~$: sudo passwd alice
     New password: 
     Retype new password: 
     passwd: password updated successfully
    
  5. Switch to another user

     ubuntu@~$: su - alice
     Password: 
     $ whoami
     alice
     $ pwd
     /home/alice
     $ exit
     ubuntu@~$: 
    
     ubuntu@~$: su alice
     Password: 
     $ whoami
     alice
     $ pwd
     /home/ubuntu
     $ exit
     ubuntu@~$:
    

    There are two ways to switch to another user.

    su - alice changes the current directory to the home directory of the target user i.e. /home/alice

    su alice the current directory remains the same as of the previous user i.e. /home/ubuntu

  6. Delete user account

     ubuntu@~$: sudo userdel bob
     ubuntu@~$: grep bob /etc/passwd
     ubuntu@~$:
    
  7. Change the login name of user

     ubuntu@~$: sudo usermod -l alicia alice
     ubuntu@~$: grep alicia /etc/passwd
     alicia:x:1001:1001::/home/alice:/bin/sh
    
  8. Add new group

     ubuntu@~$: sudo groupadd testers
     ubuntu@~$: grep testers /etc/group
     testers:x:1012:
    
  9. Delete a group

     ubuntu@~$: sudo groupdel designers 
     ubuntu@~$: grep designers /etc/group
    
  10. Add a single user to a group

    ubuntu@~$: sudo usermod -aG testers charlie
    ubuntu@~$: grep testers /etc/group
    testers:x:1012:charlie
    
  11. Add multiple users to a group

    ubuntu@~$: sudo gpasswd -M alicia,charlie developers
    ubuntu@~$: grep developers /etc/group
    developers:x:1010:alicia,charlie
    
  12. Remove user from a group

    ubuntu@~$: sudo gpasswd -d alicia developers 
    Removing user alicia from group developers
    

Go back to the main page

"๐ŸŒฑ Keep learning, and spread the knowledge to inspire others. ๐Ÿš€๐Ÿ’ก"

ย