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?

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?

1. Create user

1.1. Specific operations

New users can be created using useraddthe 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 useraddusage instructions

useraddCommand 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:

  1. Create a user named "newuser":
sudo useradd newuser

The above command will create a user named "newuser" but will not set a password.

  1. 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/newuserand default shell to /bin/bash.

  1. 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".

  1. 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 passwdthe 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 passwdusage instructions

passwdcommand is the command used to change user password in Linux system. passwdThe command can modify the current user's password or other users' passwords (root privileges are required).

Instructions:

  1. Modify the current user password: Enter passwdthe 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.
  1. 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 groupaddthe 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 groupaddusage instructions

groupaddcommand is a command used in Linux systems to create a new user group. groupaddThe command requires root privileges to execute.

Instructions:

  1. 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
  1. Create a new user group and specify the GID: Enter sudo groupadd -g GID 组名the command in the terminal, where GIDis 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 grepthe command to view the user groups and GIDs that already exist in the system. For example:

$ grep mygroup /etc/group
mygroup:x:1000:
  1. 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, where GIDis the GID of the new user group, UIDis the UID (user identifier) ​​of the user to which the new user group belongs, and 组名is the name of the new user group. -oIndicates that user groups with duplicate GIDs are allowed to be created.
$ sudo groupadd -g 1000 -o -u 1000 mygroup

Note: Use grepthe 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
  1. 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 -rmeans creating a system user group, -fmeans forcibly creating a user group, and -K key=valuemeans 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 grepthe 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 grepthe 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

usermodUsers 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 usermodusage instructions

usermodcommand is a command used in Linux systems to modify user accounts. usermodThe command requires root privileges to execute.

Instructions:

  1. 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
  1. 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
  1. Modify the user's default Shell: Enter sudo usermod -s 新Shell 用户名the command in the terminal, where 新Shellis the new default Shell path and 用户名is the user name of the user who needs to be modified.
$ sudo usermod -s /bin/bash username
  1. 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
  1. 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
  1. Modify the user's UID: Enter the command in the terminal sudo usermod -u 新UID 用户名, where 新UIDis the new UID value and 用户名is the user name of the user who needs to be modified.
$ sudo usermod -u 1001 username
  1. Enable/disable user account: Enter the command in the terminal sudo usermod -e 截止日期 用户名, where is 截止日期the expiration date of the user account in the format YYYY-MM-DD. If you need to permanently disable a user account, you can use sudo usermod -e 1 用户名the command. If you need to enable a user account, you can use sudo 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 suthe 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 exitthe command.

5.2.Command suusage instructions

sucommand is a command used to switch user identities in Linux systems. suThe command requires root privileges to execute.

Instructions:

  1. Switch to the root user: Enter suthe command in the terminal, and then enter the root user's password as prompted.
$ su
Password: 
  1. 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: suWhen 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 suthe command -to switch to the environment variables of the target user. In addition, you can /etc/sudoersconfigure the file to allow certain users sudoto execute specific commands with root privileges using the command to improve system security.

6. Switch from ordinary user to root user

Use sudothe 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 exitthe 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.

Guess you like

Origin blog.csdn.net/qq_36462452/article/details/131119209