Linux user account management - modify account
In Linux system, user account management is one of the important aspects of system management. To modify a user account is to change the user's attributes according to the actual situation, such as user ID, home directory, user group, login shell, etc. This article will introduce in detail the method of modifying user accounts in the Linux system, its syntax, practical operation, and the differences between various methods of modifying user accounts.
usermod command
Use the usermod command to modify the information of an existing user, and its syntax is as follows:
usermod [Option] username
Commonly used options include -c, -d, -g, -G, -s, -u, etc. The meaning of these options is the same as the options in the useradd command, which can specify new resource values for users. In addition, some systems can use the option -l new user name, this option specifies a new user name, that is, the original user name is changed to the new user name.
Modify user description information
The user description information is saved in /etc/passwd
the fifth field of the target user information in the file, which can be -c
modified using the option, for example:
$ sudo usermod -c "This is a new description" username
Modify user home directory
The user's home directory is saved in /etc/passwd
the sixth field of the target user information in the file. It should be noted that the home directory must use an absolute path, which can be modified using the -d
option, for example:
$ sudo usermod -d /home/newdir username
Modify User Expiration Date
The user expiration date is saved in /etc/shadow
the 8th field of the target user's password information in the file, the format is "YYYY-MM-DD", and can be -e
modified using the option, for example:
$ sudo usermod -e 2025-12-31 username
Modify the user's initial group
The user's initial group is saved in /etc/passwd
the fourth field (GID) of the target user information in the file, which can be -g
modified using the option, for example:
$ sudo usermod -g newgroup username
Modify user UID
The user UID is saved in /etc/passwd
the third field (UID) of the target user information in the file, which can be -u
modified using the option, for example:
$ sudo usermod -u 1001 username
Modify User Additional Groups
Additional groups of users are saved in the file and can be modified /etc/group
using the option, for example:-G
$ sudo usermod -G group1,group2 username
This command username
joins the user to group1
and group2
groups.
Modify the login shell
The user login Shell is saved in /etc/passwd
the seventh field of the target user information in the file, which can be -s
modified using the option, for example:
$ sudo usermod -s /bin/bash username
modify username
Use -l
the option to modify the username, for example:
$ sudo usermod -l new_username old_username
This command changes the old username old_username
to the new username new_username
.
Temporarily lock and unlock users
A user can be temporarily locked out using -L
the option, which is the same option as the passwd command -l
. To temporarily lock a user is /etc/shadow
to add "!" before the second field (encrypted password) of the target user's password information in the file to make the password invalid.
$ sudo usermod -L username
A user can be unlocked using -U
the option, which is the same as the passwd command -u
.
$ sudo usermod -U username
Table 1: usermod command options
options | describe |
---|---|
-c | Modify user description information |
-d | Modify user home directory |
-e | Modify User Expiration Date |
-g | Modify the user's initial group |
-u | Modify user UID |
-G | Modify User Additional Groups |
-s | Modify the login shell |
-l | modify username |
-L | Temporarily lock users |
-U | unlock user |
Table 2: Format of the /etc/passwd file by default
field | describe |
---|---|
username | A string that uniquely identifies the user in the system |
password | encrypted user password |
UID | User's numeric identifier (User ID) |
GID | user's group id |
user information | Note information |
Main directory | The directory where the user is logged in |
Login Shell | Shell program to be run when the user logs in |
Summarize
This article introduces in detail the method and common options for modifying user accounts in the Linux system, including modifying user description information, user home directory, user expiration date, user initial group, user UID, user additional group, login shell, user name, temporary lock and unlock user etc. It should be noted that when modifying user account information, you must operate carefully to ensure that the normal operation of the system will not be affected. In addition, for running processes and services, you also need to pay attention to whether they need to be restarted to apply the modified user account information.