Linux User and Group Management
The Linux system is a multi-user, multi-tasking time-sharing operating system. Any user who wants to use system resources must first request an account from the system administrator, and then enter the system as that account.
On one hand, a user account helps the system administrator track users of the system and control their access to system resources; on the other hand, it also helps users organize files and provides them with security protection.
Each user account has a unique username and its own password.
After entering the correct username and password at login, a user can enter the system and their own home directory.
To implement user account management, the main tasks involve the following aspects:
- Adding, deleting, and modifying user accounts.
- Management of user passwords.
- Management of user groups.
I. Linux System User Account Management
User account management mainly involves adding, modifying, and deleting user accounts.
Adding a user account means creating a new account in the system, then assigning resources such as a user ID, user group, home directory, and login Shell to the new account. A newly added account is locked and cannot be used.
1. To add a new user account, use the useradd command. Its syntax is as follows:
useradd 选项 用户名
Parameter Description:
-
Options:
- -c comment specifies a descriptive comment.
- -d directory specifies the user's home directory. If this directory does not exist, use the -m option together to create the home directory.
- -g group specifies the user group to which the user belongs.
- -G group,group specifies the supplementary groups to which the user belongs.
- -s shell-file specifies the user's login Shell.
- -u uid specifies the user's ID. If the -o option is also used, the ID of another user can be reused.
-
Username:
Specifies the login name of the new account.
Example 1
# useradd –d /home/sam -m sam
This command creates a user sam, where the -d and -m options are used to generate a home directory /home/sam for the login name sam (/home is the default parent directory where user home directories reside).
Example 2
# useradd -s /bin/sh -g group –G adm,root gem
This command creates a new user gem whose login Shell is /bin/sh, which belongs to the group user group and also to the adm and root user groups, with group being its primary group.
A new group may be created here:#groupadd group及groupadd adm
Adding a user account means adding a record for the new user in the /etc/passwd file, while also updating other system files such as /etc/shadow, /etc/group, and so on.
Linux provides an integrated system administration tool, userconf, which can be used for unified management of user accounts.
2. Deleting Accounts
If a user's account is no longer in use, it can be deleted from the system. Deleting a user account means removing that user's record from system files such as /etc/passwd, and deleting the user's home directory when necessary.
To delete an existing user account, use theuserdelcommand, whose format is as follows:
userdel 选项 用户名
The commonly used option is -r, which deletes the user's home directory along with the account.
For example:
# userdel -r sam
This command deletes the record of user sam from system files (mainly /etc/passwd, /etc/shadow, /etc/group, etc.) and also deletes the user's home directory.
3. Modifying Accounts
Modifying a user account means changing the user's relevant attributes as needed, such as user ID, home directory, user group, login Shell, and so on.
To modify an existing user's information, use theusermodcommand, whose format is as follows:
usermod 选项 用户名
Common options include-c, -d, -m, -g, -G, -s, -u以及-o等, the meanings of these options are the same as those in theuseraddcommand, and can be used to assign new resource values to the user.
In addition, some systems support the option: -l new-username
This option specifies a new account, that is, changing the original username to a new one.
For example:
# usermod -s /bin/ksh -d /home/z –g developer sam
This command changes user sam's login Shell to ksh, home directory to /home/z, and user group to developer.
4. User Password Management
An important part of user management is password management. A user account has no password when first created and is locked by the system, so it cannot be used until a password is assigned to it, even an empty one.
The Shell command for specifying and modifying a user password ispasswd. A superuser can specify passwords for themselves and other users, while ordinary users can only use it to change their own password. The command format is:
passwd 选项 用户名
Available options:
- -l locks the password, that is, disables the account.
- -u unlocks the password.
- -d makes the account passwordless.
- -f forces the user to change the password at the next login.
If the username is omitted, the current user's password is changed.
For example, if the current user is sam, the following command changes that user's own password:
$ passwd
Old password:******
New password:*******
Re-enter new password:*******
If you are a superuser, you can specify any user's password in the following form:
# passwd sam
New password:*******
Re-enter new password:*******
When an ordinary user changes their own password, the passwd command first asks for the old password and, after verification, asks the user to enter the new password twice. If the two entries match, the password is assigned to the user. When a superuser assigns a password for a user, the old password is not needed.
For system security, users should choose relatively complex passwords, such as an 8-character password containing uppercase letters, lowercase letters, and digits, and different from their name, birthday, and so on.
To assign an empty password to a user, execute a command of the following form:
# passwd -d sam
This command deletes user sam's password, so the next time sam logs in, the system will no longer allow that user to log in.
The passwd command can also use the -l (lock) option to lock a user so they cannot log in, for example:
# passwd -l sam
II. Linux System User Group Management
Every user has a user group, and the system can centrally manage all users in a group. Different Linux systems have different rules for user groups. For example, on Linux a user belongs to a user group with the same name as the user, and this group is created at the same time the user is created.
User group management involves adding, deleting, and modifying groups. Adding, deleting, and modifying groups is actually updating the /etc/group file.
1. To add a new user group, use the groupadd command. Its format is as follows:
groupadd 选项 用户组
The available options are:
- -g GID specifies the group ID (GID) of the new user group.
- -o is generally used together with the -g option, indicating that the new group's GID can be the same as an existing group's GID.
Example 1:
# groupadd group1
This command adds a new group group1 to the system, whose group ID is the current maximum group ID plus one.
Example 2:
# groupadd -g 101 group2
This command adds a new group group2 to the system and specifies its group ID as 101.
2. To delete an existing user group, use the groupdel command. Its format is as follows:
groupdel 用户组
For example:
# groupdel group1
This command deletes group group1 from the system.
3. To modify a user group's attributes, use the groupmod command. Its syntax is as follows:
groupmod 选项 用户组
Common options are:
- -g GID assigns a new group ID to the user group.
- -o used together with -g allows the group's new GID to be the same as an existing group's GID.
- -n new-group changes the group's name to a new one
Example 1:
# groupmod -g 102 group2
This command changes group group2's group ID to 102.
Example 2:
# groupmod –g 10000 -n group3 group2
This command changes group group2's ID to 10000 and its name to group3.
4. If a user belongs to multiple user groups at the same time, the user can switch between groups to gain the permissions of other groups.
After logging in, a user can use the newgrp command to switch to another user group, whose argument is the target group. For example:
$ newgrp root
This command switches the current user to the root group, provided that the root group is indeed the user's primary or supplementary group. Similar to user account management, group management can also be done through integrated system administration tools.
III. System Files Related to User Accounts
There are many ways to accomplish user management, but each method actually modifies the relevant system files.
Information about users and groups is stored in several system files, including /etc/passwd, /etc/shadow, /etc/group, and so on.
The contents of these files are described below.
1. The /etc/passwd file is the most important file involved in user management.
Every user in the Linux system has a corresponding record line in /etc/passwd, which records some basic attributes of that user.
This file is readable by all users. Its content is similar to the following example:
# cat /etc/passwd
root:x:0:0:Superuser:/:
daemon:x:1:1:System daemons:/etc:
bin:x:2:2:Owner of system commands:/bin:
sys:x:3:3:Owner of system files:/usr/sys:
adm:x:4:4:System accounting:/usr/adm:
uucp:x:5:5:UUCP administrator:/usr/lib/uucp:
auth:x:7:21:Authentication administrator:/tcb/files/auth:
cron:x:9:16:Cron daemon:/usr/spool/cron:
listen:x:37:4:Network daemon:/usr/net/nls:
lp:x:71:18:Printer administrator:/usr/spool/lp:
sam:x:200:50:Sam san:/home/sam:/bin/sh
From the example above we can see that one record line in /etc/passwd corresponds to one user, and each record is separated by colons (:) into 7 fields, whose format and meanings are as follows:
用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell
1) "Username" is the string representing the user account.
It is usually no more than 8 characters long and consists of uppercase and lowercase letters and/or digits. A login name cannot contain a colon (:), because the colon is a separator here.
For compatibility, a login name should preferably not contain a dot (.) and should not start with a hyphen (-) or a plus sign (+).
2) "Password": In some systems, this stores the encrypted user password.
Although this field stores only the encrypted string of the user password, not plaintext, since /etc/passwd is readable by all users, this is still a security risk. Therefore, many Linux systems (such as SVR4) now use shadow technology, storing the real encrypted password in /etc/shadow and placing only a special character, such as "x" or "*", in the password field of /etc/passwd.
3) "User ID" is an integer used internally by the system to identify a user.
Normally it corresponds one-to-one with the username. If several usernames have the same user ID, the system treats them internally as the same user, but they can have different passwords, different home directories, and different login Shells.
The user ID typically ranges from 0 to 65535. 0 is the ID of the superuser root, 1 to 99 are reserved by the system for administrative accounts, and ordinary user IDs start at 100. In Linux systems, this boundary is 500.
4) The "Group ID" field records the user group to which the user belongs.
It corresponds to a record in the /etc/group file.
5) The "Comment" field records some personal information about the user.
For example, the user's real name, phone number, address, and so on. This field has no practical use. In different Linux systems, the format of this field is not standardized. In many Linux systems, this field stores an arbitrary descriptive text, used as the output of the finger command.
6) "Home directory", that is, the user's starting working directory.
It is the directory a user is placed in after logging into the system. In most systems, all users' home directories are organized under the same specific directory, and the name of a user's home directory is that user's login name. Each user has read, write, and execute (search) permissions on their own home directory, while other users' access permissions to this directory are set according to the specific situation.
7) After a user logs in, a process is started to pass the user's operations to the kernel. This process is the command interpreter or a specific program that runs after the user logs into the system, namely the Shell.
The Shell is the interface between the user and the Linux system. There are many kinds of Shells in Linux, each with different features. Commonly used ones include sh (Bourne Shell), csh (C Shell), ksh (Korn Shell), tcsh (TENEX/TOPS-20 type C Shell), bash (Bourne Again Shell), and so on.
The system administrator can assign a specific Shell to a user based on the system situation and user habits. If no Shell is specified, the system uses sh as the default login Shell, that is, the value of this field is /bin/sh.
A user's login Shell can also be specified as a specific program (a program that is not a command interpreter).
Taking advantage of this feature, we can restrict a user to running only a specified application, and when that application finishes, the user automatically exits the system. Some Linux systems require that only programs registered in the system can appear in this field.
8) There is a class of users in the system called pseudo users.
These users also have a record in /etc/passwd, but cannot log in because their login Shell is empty. They exist mainly to facilitate system administration and to satisfy the requirement of corresponding system processes for a file owner.
Common pseudo users are as follows:
伪 用 户 含 义
bin 拥有可执行的用户命令文件
sys 拥有系统文件
adm 拥有帐户文件
uucp UUCP使用
lp lp或lpd子系统使用
nobody NFS使用
Owning Account Files
1. In addition to the pseudo users listed above, there are many standard pseudo users, such as audit, cron, mail, usenet, and so on, each of which is needed by its corresponding processes and files.
Since /etc/passwd is readable by all users, if a user's password is too simple or has an obvious pattern, an ordinary computer can easily crack it. Therefore, Linux systems with high security requirements separate the encrypted password and store it in a separate file, which is /etc/shadow. Only the superuser has read permission on this file, which ensures the security of user passwords.
2. The record lines in /etc/shadow correspond one-to-one with those in /etc/passwd, and it is generated automatically by the pwconv command from the data in /etc/passwd.
Its file format is similar to /etc/passwd, consisting of several fields separated by ":". These fields are:
登录名:加密口令:最后一次修改时间:最小时间间隔:最大时间间隔:警告时间:不活动时间:失效时间:标志
- "Login name" is the user account consistent with the login name in /etc/passwd
- "Password" field stores the encrypted user password, 13 characters long. If empty, the corresponding user has no password and no password is needed at login; if it contains characters not in the set { ./0-9A-Za-z }, the corresponding user cannot log in.
- "Last change time" indicates the number of days from a certain moment to the last time the user changed the password. The starting point may differ across systems. For example, in SCO Linux, this starting point is January 1, 1970.
- "Minimum interval" refers to the minimum number of days required between two password changes.
- "Maximum interval" refers to the maximum number of days a password remains valid.
- "Warning time" field indicates the number of days from when the system starts warning the user until the password officially expires.
- "Inactivity time" indicates the maximum number of days a user can have no login activity while the account remains valid.
- "Expiration time" field gives an absolute number of days. If this field is used, it gives the lifetime of the corresponding account. After expiration, the account is no longer valid and can no longer be used to log in.
The following is an example of /etc/shadow:
# cat /etc/shadow
root:Dnakfw28zf38w:8764:0:168:7:::
daemon:*::0:0::::
bin:*::0:0::::
sys:*::0:0::::
adm:*::0:0::::
uucp:*::0:0::::
nuucp:*::0:0::::
auth:*::0:0::::
cron:*::0:0::::
listen:*::0:0::::
lp:*::0:0::::
sam:EkdiSECLWPdSa:9740:0:0::::
3. All user group information is stored in the /etc/group file.
Grouping users is a means of managing users and controlling access permissions in Linux systems.
Every user belongs to a certain user group; a group can have multiple users, and a user can also belong to different groups.
When a user is a member of multiple groups at the same time, the /etc/passwd file records the user's primary group, that is, the default group at login, while the other groups are called supplementary groups.
When a user wants to access a file belonging to a supplementary group, they must first use the newgrp command to make themselves a member of the group they want to access.
All user group information is stored in the /etc/group file. The format of this file is also similar to /etc/passwd, with several fields separated by colons (:). These fields are:
组名:口令:组标识号:组内用户列表
- "Group name" is the name of the user group, consisting of letters or digits. Like the login names in /etc/passwd, group names should not be duplicated.
- "Password" field stores the encrypted password of the user group. Generally, user groups in Linux systems have no password, that is, this field is usually empty or contains *.
- "Group ID" is similar to the user ID, also an integer, used internally by the system to identify a group.
- "Group user list" is a list of all users belonging to this group, with different users separated by commas (,). This user group may be a user's primary group or a supplementary group.
An example of the /etc/group file is as follows:
root::0:root
bin::2:root,bin
sys::3:root,uucp
adm::4:root,adm
daemon::5:root,daemon
lp::7:root,lp
users::20:root,sam
IV. Adding Users in Bulk
Adding and deleting users is easy for every Linux system administrator. The tricky part is when you need to add dozens, hundreds, or even thousands of users, it is impractical to keep using useradd one by one, so you must find a simple way to create many users at once. Linux provides tools for creating many users, allowing you to create a large number of users immediately, as follows:
(1) First, edit a text user file.
Each column follows the/etc/passwdformat of the password file. Note that each user's username, UID, and home directory must be unique, and the password field can be left blank or filled with x. An example file user.txt has the following content:
user001::600:100:user:/home/user001:/bin/bash
user002::601:100:user:/home/user002:/bin/bash
user003::602:100:user:/home/user003:/bin/bash
user004::603:100:user:/home/user004:/bin/bash
user005::604:100:user:/home/user005:/bin/bash
user006::605:100:user:/home/user006:/bin/bash
(2) As root, execute the command /usr/sbin/newusersto import data from the newly created user fileuser.txtand create users:
# newusers < user.txt
Then you can execute the command vipw or vi /etc/passwd Checks /etc/passwd to check whether the data for these users has appeared in the file and whether the users' home directories have been created.
(3) Execute the command /usr/sbin/pwunconv.
data to the child component via /etc/shadow decodes the shadow password generated and writes it back to /etc/passwd , and removes the/etc/shadow'sshadowpassword field. This is to facilitate the next step of password conversion, that is, first canceling the shadow password function.
# pwunconv
(4) Edit each user's password mapping file.
The format is:
用户名:密码
Example file passwd.txt has the following content:
user001:123456
user002:123456
user003:123456
user004:123456
user005:123456
user006:123456
(5) As root, execute the command /usr/sbin/chpasswd.
to create user passwords.chpasswd will write the password encoded by the /usr/bin/passwd command into the /etc/passwd password field.
# chpasswd < passwd.txt
(6) After confirming that the passwords have been encoded and written into the password field of /etc/passwd.
Execute the command /usr/sbin/pwconv to encode the passwords as shadow passwordand write the results to /etc/shadow.
# pwconv
This completes the creation of a large number of users. Afterward, you can go to /home to check whether the permission settings of these users' home directories are all correct, and log in to verify whether the user passwords are correct.