How to create users, set passwords, create groups, bind users to groups, switch between users, and switch from ordinary users to root users in Linux?
Article directory
1. Create user
1.1. Specific operations
New users can be created using useradd
the command. For example, to create a user named "newuser", you can execute the following command:
sudo useradd newuser
This command creates a new user without setting a password.
1.2.Command useradd
usage instructions
useradd
Command is used to create a new user account. Here are common options and examples:
Options:
-c
: Comments from the specified user.-d
: Specify the user's home directory path.-e
: Specify the expiration date of the user account in the format YYYY-MM-DD.-f
: Specify the expiration date of the account, in the format YYYY-MM-DD.-g
: Specify the user's initial login group.-G
: A list of additional groups for the specified user.-m
: Automatically create the user's home directory.-s
:Specifies the user's default shell.-u
: The user ID of the specified user.
Example:
- Create a user named "newuser":
sudo useradd newuser
The above command will create a user named "newuser" but will not set a password.
- Create a user named "newuser" and specify the home directory path and default shell:
sudo useradd -d /home/newuser -s /bin/bash newuser
The above command will create a user named "newuser" and set its home directory path to /home/newuser
and default shell to /bin/bash
.
- Create a user named "newuser" and add it to a new group "newgroup":
sudo useradd -G newgroup newuser
The above command will create a user named "newuser" and add it to a new group "newgroup".
- Create a user named "newuser" and set its password:
sudo useradd newuser
sudo passwd newuser
The above command will create a user named "newuser" and set its password.
2. Set password
2.1. Specific operations
Use passwd
the command to set the user's password. For example, to set a password for the new user "newuser", you can execute the following command:
sudo passwd newuser
This command prompts you to enter a new password and confirm the password.
2.2.Command passwd
usage instructions
passwd
command is the command used to change user password in Linux system. passwd
The command can modify the current user's password or other users' passwords (root privileges are required).
Instructions:
- Modify the current user password: Enter
passwd
the command in the terminal, then enter the new password and confirm the password as prompted.
$ passwd
Changing password for user.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
- Modify the passwords of other users: Enter
sudo passwd 用户名
the command in the terminal, then enter the new password and confirm the password as prompted (root privileges are required).
$ sudo passwd username
[sudo] password for user:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Note: For security reasons, the password should contain uppercase and lowercase letters, numbers and symbols, and be no less than 8 characters in length. At the same time, passwords should be changed regularly to protect system security.
3. Create a group
3.1. Specific operations
New groups can be created using groupadd
the command. For example, to create a group named "newgroup", you can execute the following command:
sudo groupadd newgroup
This command creates a new group.
3.2.Command groupadd
usage instructions
groupadd
command is a command used in Linux systems to create a new user group. groupadd
The command requires root privileges to execute.
Instructions:
- Create a new user group: Enter
sudo groupadd 组名
the command in the terminal, where组名
is the name of the new user group.
$ sudo groupadd mygroup
- Create a new user group and specify the GID: Enter
sudo groupadd -g GID 组名
the command in the terminal, whereGID
is the GID (group identifier) of the new user group and组名
is the name of the new user group.
$ sudo groupadd -g 1000 mygroup
Note: Each user group in the Linux system has a unique GID. If you do not specify a GID, the system will automatically assign an unused GID. You can use grep
the command to view the user groups and GIDs that already exist in the system. For example:
$ grep mygroup /etc/group
mygroup:x:1000:
- Create a new user group and specify the users to whom it belongs: Enter
sudo groupadd -g GID -o -u UID 组名
the command in the terminal, whereGID
is the GID of the new user group,UID
is the UID (user identifier) of the user to which the new user group belongs, and组名
is the name of the new user group.-o
Indicates that user groups with duplicate GIDs are allowed to be created.
$ sudo groupadd -g 1000 -o -u 1000 mygroup
Note: Use grep
the command to view the users and UIDs that already exist in the system. For example:
$ grep myuser /etc/passwd
myuser:x:1000:1000:My User:/home/myuser:/bin/bash
- Create a new user group and specify description information: Enter
sudo groupadd -g GID -o -u UID -r -f -K key=value 组名
the command in the terminal, where-r
means creating a system user group,-f
means forcibly creating a user group, and-K key=value
means specifying the description information of the user group. The meanings of other parameters are the same as above.
$ sudo groupadd -g 1000 -r -K description="My Group" mygroup
Note: Use grep
the command to view the user groups and GIDs that already exist in the system. For example:
$ grep mygroup /etc/group
mygroup:x:1000:
After the user group is successfully created, you can use grep
the command to view the user group information, including group name, GID, users in the group, etc. For example:
$ grep mygroup /etc/group
mygroup:x:1000:myuser
4. User and group binding
4.1. Specific operations
usermod
Users can be added to groups using the command. For example, to add "newuser" to the "newgroup" group, you can execute the following command:
sudo usermod -a -G newgroup newuser
This command will add "newuser" to the "newgroup" group.
4.2.Command usermod
usage instructions
usermod
command is a command used in Linux systems to modify user accounts. usermod
The command requires root privileges to execute.
Instructions:
- Modify the user's username: Enter
sudo usermod -l 新用户名 旧用户名
the command in the terminal, where新用户名
is the new username and旧用户名
is the original username of the user who needs to be modified.
$ sudo usermod -l newname oldname
- Modify the user's home directory: Enter
sudo usermod -d 新主目录 用户名
the command in the terminal, where新主目录
is the new home directory path and用户名
is the user name of the user who needs to be modified.
$ sudo usermod -d /home/newdir username
- Modify the user's default Shell: Enter
sudo usermod -s 新Shell 用户名
the command in the terminal, where新Shell
is the new default Shell path and用户名
is the user name of the user who needs to be modified.
$ sudo usermod -s /bin/bash username
- Modify a user's user group: Enter
sudo usermod -g 用户组名 用户名
the command in the terminal, where用户组名
is the new user group name and用户名
is the user name of the user who needs to be modified. Note that this command removes the user from the original user group and adds them to the new user group.
$ sudo usermod -g newgroup username
- Add a user to another user group: Enter
sudo usermod -aG 用户组名 用户名
the command in the terminal, where用户组名
is the name of the user group that needs to be added, and用户名
is the user name of the user that needs to be modified. Note that this command will not remove the user from the original user group.
$ sudo usermod -aG groupname username
- Modify the user's UID: Enter the command in the terminal
sudo usermod -u 新UID 用户名
, where新UID
is the new UID value and用户名
is the user name of the user who needs to be modified.
$ sudo usermod -u 1001 username
- Enable/disable user account: Enter the command in the terminal
sudo usermod -e 截止日期 用户名
, where is截止日期
the expiration date of the user account in the formatYYYY-MM-DD
. If you need to permanently disable a user account, you can usesudo usermod -e 1 用户名
the command. If you need to enable a user account, you can usesudo usermod -e '' 用户名
the command.
$ sudo usermod -e 2022-12-31 username
Note: Before modifying user account information, you should back up important data to avoid data loss.
5. Switching between users
5.1. Specific operations
Use su
the command to switch to another user in the current terminal session. For example, to switch from the current user to the "newuser" user, you can execute the following command:
su - newuser
This command prompts you for the password of the "newuser" user and switches your terminal session to the "newuser" user. If you need to return to the original user, just execute exit
the command.
5.2.Command su
usage instructions
su
command is a command used to switch user identities in Linux systems. su
The command requires root privileges to execute.
Instructions:
- Switch to the root user: Enter
su
the command in the terminal, and then enter the root user's password as prompted.
$ su
Password:
- Switch to another user: Enter the command in the terminal
su - 用户名
, where用户名
is the user name of the user you want to switch to. Then follow the prompts to enter the user's password.
$ su - username
Password:
Note: su
When using the command to switch user identities, you need to enter the password of the target user. When switching to the root user, you should operate with caution to avoid misoperations that may cause system failure or data loss. It is recommended to add the option when using su
the command -
to switch to the environment variables of the target user. In addition, you can /etc/sudoers
configure the file to allow certain users sudo
to execute specific commands with root privileges using the command to improve system security.
6. Switch from ordinary user to root user
Use sudo
the command to temporarily elevate to root in the current terminal session. For example, to promote the current user to root user, you can execute the following command:
sudo su -
This command prompts you for the current user's password and switches your terminal session to the root user. If you need to return to the original user, just execute exit
the command. Please note that for security reasons, you should use ordinary users for operations whenever possible and avoid performing routine operations under the root user.