Introduction
The usermod
command is one of the several Linux commands system administrators have at their disposal for user management. It is used to modify existing user account details, such as username, password, home directory location, default shell, and more.
In this tutorial, you will learn how to use the usermod
command in Linux to change user login information.
Prerequisites
- A system running Linux
- Access to the command line
usermod Linux Syntax
The basic syntax for the usermod
command is:
usermod [options] [username]
The usermod
command modifies configuration files containing user account information. Those files are:
/etc/passwd
– information regarding users’ accounts/etc/shadow
– user security-related information/etc/group
– information about groups/etc/gshadow
– group security-related information/etc/login.defs
– shadow password suite configuration
It is possible to edit the above-mentioned files directly, using a text editor such as nano or vim. However, usermod
makes the process faster and more straightforward.
usermod Command Examples
Below is a list of usermod
options, along with the option-specific syntax and examples.
Add Information to a User
Use usermod
with the -c
option to add a piece of information about a user to the /etc/passwd
file. This helps identify the user and provides space for temporary user-related comments.
sudo usermod -c "[information]" [username]
Use the getent
command to check the user-related entry in the /etc/passwd
file.
The entry now contains the comment added with usermod
.
Set User’s Home Directory
When you create a user in Linux, the system automatically creates a home folder for them in /home/[username]
. To change the location of the user’s home folder, use the -d
option:
sudo usermod -d [directory-location] [username]
The example above changed the user’s home folder from /home/mike
to /var/mike
. The getent
command confirms the successful change.
The -d
option does not move the home folder’s content to the new location. If the user has previously utilized the home folder to store their files, add the -m
option to move the content of the user’s home directory:
sudo usermod -d [location] -m [username]
In the case of the user from the example above, the command looks like this:
sudo usermod -d /var/mike -m mike
Note: You cannot use usermod
to change the name of the currently logged in user. Furthermore, when changing the user’s ID (UID), make sure the user is not currently executing any processes.
Set User’s Account Expiry Date
User accounts do not expire in Linux by default. Use the chage -l
command to check account aging information for a user:
sudo chage -l [username]
As the example above shows, the account is set not to expire. Change this by using usermod
with the -e
option. It allows you to specify the year, month, and day of the account expiry:
sudo usermod -e [YYYY-MM-DD] [username]
Confirm the successful change by checking the account with the chage -l
command again:
Set User’s Shell
Use usermod -s
to change the default shell for a single user:
sudo usermod -s [shell] [username]
If the change was successful, getent
shows the new default shell at the end of the entry.
Change User’s UID
A UID (user identifier) is the unique number assigned to the user upon account creation with the useradd
command. The /etc/login.defs
file defines the range of UID values. Change a user’s UID with the usermod -u
command:
sudo usermod -u [new-UID] [username]
In the passwd
entry, find UID right after the username and password:
Change User’s Login Name
Change a username by adding the -l
option. The syntax is as follows:
sudo usermod -l [newname] [oldname]
As the example above shows, using the old username to check the /etc/passwd
file no longer returns data. However, the same data is now available under the new name.
Note: NAME_REGEX
value in the /etc/adduser.conf
file defines the standards for new username creation. However, usermod
does not enforce the same standards for modifying usernames. While any string of characters is accepted as a new username with usermod -l
, it is still advisable to adhere to standard naming conventions.
Lock and Unlock a User
Lock a user account by using the -L
option.
sudo usermod -L [username]
Once the account has been locked with –L
, the user’s login attempts fail upon typing the password:
To unlock the account you previously locked, type usermod -U
followed by the account name:
sudo usermod -U [username]
As you see in the example above, the password now works again, and the zsh shell initiates.
Set Password for a User
The most common way to set a password for a user is by using the passwd command. usermod
also has the dedicated -p
option for creating a password:
sudo usermod -p [password] [username]
However, this method of creating a password is not recommended since the password is visible in the /etc/shadow
file. If you use the grep command to look for a username in the /etc/shadow
file, the entry contains the plain-text version of the password created with usermod
:
Change User’s Primary Group
Change the primary group of a user with usermod -g
:
sudo usermod -g [group] [username]
The id
command confirms the successful change of the primary group:
Add a User to a Supplementary Group
Aside from their primary group, users can be members of any number of supplementary groups. The -G
option adds the user to a supplementary group:
sudo usermod -G [group] [username]
However, if the user already belongs to some supplementary groups, usermod -G
removes them from those groups and adds only to the ones specified after the command.
In the example below, the user is already a member of a supplementary group. The combination of the -a
and -G
options adds the user to the specified group and leaves them in the supplementary group they were already a member of.
sudo usermod -a -G [group] [username]
Change User’s Account Using Multiple Options
Use multiple options in one command for a more convenient way to edit a user. The example below shows a usermod
command that:
- changes the home folder and the shell,
- sets an account expiry date,
- adds a comment,
- changes the UID, and
- adds the user to a supplementary group.
sudo usermod -d [home-folder] -s [shell] -e [YYYY-MM-DD] -c "[comment]" -u [UID] -aG [group] [username]
Display All usermod Commands and Arguments
To read usermod
help, use the --help
argument:
usermod --help
For a more detailed list of options and functions, use the man command:
man usermod
Conclusion
This guide explained the use of the usermod
command, along with its numerous options. After reading it, you should know how to use the command to modify user account settings and login information.
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/223862.html