[Share Experiences] UOS 1060a Sharing Series on User and Group Management
Experiences and Insight 594 views · 0 replies ·
SuperDavid
deepin
2024-08-30 17:50
Author
I. Concepts of Users and Groups
(1) What is a User?
Different services use different users for better security. Using multiple users can enhance system efficiency and improve permission distribution and management.
Within the system, each user is assigned a unique identifier (User ID or UID) to distinguish them, while usernames are created for ease of use. Typically, users are given a password for login authentication.
There are three main types of users:
Super Administrator: Manages the system with UID 0 and has full system access.
System Users: Provide support services and processes. These processes (or daemons) generally do not run as super users. The system assigns non-privileged accounts to ensure their files and resources are unaffected by others. Users cannot log in interactively with system user accounts.
Normal Users: Have limited system access.
Use the id command to display information about the currently logged-in user.
[root@1060a ~]# id
uid=0(root) gid=0(root) groups=0(root)
(2) What is a User Group?
A group is a collection of users. Groups can be used to grant file access permissions to a set of users rather than just a single user. Groups are distinguished by a unique identifier (Group ID or GID) internally, and group names are for user convenience.
For a user, user groups can be divided into primary groups and supplementary groups.
Primary Group (Main Group): If no user group is specified during user creation, the system creates a user group with the same name as the username, which is the user's private group.
Supplementary Group (Secondary Group): A user can belong to multiple supplementary groups besides the primary group.
In the output of the id command, the Group ID represents the primary group, and the Groups field lists the supplementary groups.
(3) Explanation of User and Group Related Files
/etc/passwd: This file contains user account information, storing all users' basic details and is readable by all users. Each line in the file has seven fields separated by colons (:).root:x:0:0:root:/root:/bin/bash
Encrypted Password: The encrypted password. !! indicates no password has been set, and an empty field means the password has been removed.
Last Password Change Date: The number of days since January 1, 1970, when the password was last changed. For example, 19663 represents the 19663rd day after January 1, 1970.
Use the following command to convert it to a standard date:
[root@1060a ~]# date -d '1970-01-01 19663 days'
Thu Nov 2 00:00:00 CST 2023
Password Verification: When a user attempts to log in, the system looks up the user's entry in /etc/shadow, combines the user's salt with the entered password, encrypts it using the specified hash algorithm, and compares the result with the stored hash.
/etc/group: This file contains information about user groups.
root:x:0:
Group Name:Password Placeholder:GID:Group Members
Group Name: The name of the group.
Password Placeholder: Always x.
GID: The group ID.
Group Members: Users belonging to the group.
/etc/gshadow: Stores group password information, accessible only by the root user.
root:::
Group Name:Group Password:Group Administrator:Group Members
Group Name: The name of the group.
Group Password: The encrypted group password.
Group Administrator: Can manage the group using the gpasswd command.
I. Concepts of Users and Groups
(1) What is a User?
Different services use different users for better security. Using multiple users can enhance system efficiency and improve permission distribution and management.
Within the system, each user is assigned a unique identifier (User ID or UID) to distinguish them, while usernames are created for ease of use. Typically, users are given a password for login authentication.
There are three main types of users:
Use the
idcommand to display information about the currently logged-in user.[root@1060a ~]# iduid=0(root) gid=0(root) groups=0(root)(2) What is a User Group?
A group is a collection of users. Groups can be used to grant file access permissions to a set of users rather than just a single user. Groups are distinguished by a unique identifier (Group ID or GID) internally, and group names are for user convenience.
For a user, user groups can be divided into primary groups and supplementary groups.
In the output of the
idcommand, the Group ID represents the primary group, and the Groups field lists the supplementary groups.(3) Explanation of User and Group Related Files
:).root:x:0:0:root:/root:/bin/bashUsername:Password Placeholder:UID:GID:Description:Home Directory:Default Shellxmeans no password is required./sbin/nologin.root:XXXX:19663:0:90:7:::Username:Encrypted Password:Last Password Change Date:Minimum Password Age:Maximum Password Age:Warning Period:Inactive Period:Expiration Date:Reserved!!indicates no password has been set, and an empty field means the password has been removed.Use the following command to convert it to a standard date:
[root@1060a ~]# date -d '1970-01-01 19663 days'Thu Nov 2 00:00:00 CST 2023/etc/shadow, combines the user's salt with the entered password, encrypts it using the specified hash algorithm, and compares the result with the stored hash.root:x:0:Group Name:Password Placeholder:GID:Group Membersx.root:::Group Name:Group Password:Group Administrator:Group Membersgpasswdcommand.II. User and Group Management
(1) User Management
useradduseradd [options] usernameCommon Options:
-u: Specify the user's UIDuseradd -u 1100 user1id user1uid=1100(user1) gid=1100(user1) groups=1100(user1)-g: Specify the user's primary groupuseradd -g user1 user2id user2uid=1101(user2) gid=1100(user1) groups=1100(user1)-G: Specify the user's supplementary groupsuseradd -G user1 user3id user3uid=1102(user3) gid=1102(user3) groups=1102(user3),1100(user1)-c: Specify the user's GECOS informationuseradd -c "web server" user4grep user4 /etc/passwduser4:x:1103:1103:web server:/home/user4:/bin/bash-d: Specify the user's home directoryuseradd -d /opt/user5 user5cd ~user5pwd/opt/user5-s: Specify the user's default shelluseradd -s /sbin/nologin user7grep user7 /etc/passwduser7:x:1106:1106::/home/user7:/sbin/nologinEffect of
useradd** command:**/etc/passwd/etc/group/etc/shadow/etc/gshadow/home/var/spool/mailpasswdpasswd [options] [username]Changing Passwords:
passwd user1passwdCommon Options:
-d: Delete the passwordpasswd -d user1-l: Lock the accountpasswd -l user1-u: Unlock the accountpasswd -u user1--stdin: Read password from standard input (not recommended)echo"password" | passwd --stdin user1usermodusermod [options] usernameCommon Options:
-l: Change the usernameusermod -l new_name old_name-u: Change the UIDusermod -u uid username-g: Change the primary groupusermod -g groupname username-G: Change supplementary groupsusermod -G groupname username-c: Change the user's GECOS informationusermod -c "new comment" username-md: Change the user's home directoryusermod -md /new/home/dir username-s: Change the user's shellusermod -s /bin/sh usernameuserdeluserdel [options] usernameCommon Options:
-r: Delete the user's home directory and mailboxuserdel -r usernamechagechage [options] usernameCommon Options:
chage username(2) Group Management
groupaddgroupadd [options] groupnameCommon Options:
-g: Specify the GIDgroupadd -g gid groupnamegroupmodgroupmod [options] groupnameCommon Options:
-n: Change the group namegroupmod -n new_name old_name-g: Change the GIDgroupmod -g gid groupnamegroupdelgroupdel groupnamegpasswdgpasswd [option] groupnameCommon Options:
-A: Assign group administratorsgpasswd -A username groupname-a: Add a user to the groupgpasswd -a username groupname-d: Remove a user from the groupgpasswd -d username groupname-r: Remove the group passwordgpasswd -r groupnamegroupmemsgroupmems [options]Common Options:
-l: List all group membersgroupmems -l -g groupname-a: Add a user to the groupgroupmems -a username -g groupname-d: Delete a user from the groupgroupmems -d username -g groupname-p: Remove all users from the groupgroupmems -p -g groupname