Linux basic operation - user operation - user group operation

user operation

Sudo privileges are required to execute the command.

Add new user account

The steps to add a user account in the Linux system are as follows:

1. Open a terminal and log in as the root user.

2. Enter the following command to create a new user account:

useradd username

Among them, username is the username of the new user.

3. Set the password for the new user:

passwd username

Enter the password twice to confirm.

4. If you need to assign sudo permissions to new users, you can add them to the sudo user group:

usermod -aG sudo username

Among them, -aG means adding the user to the specified user group.

5. After completing the above steps, the new user can log in to the system.

Management of user passwords

On Linux systems, you can use the following commands to manage user passwords:

Set user password:

passwd <username>

This command will prompt you for a new password and ask you to confirm it. Passwords will be encrypted and stored in the system.

Force a user to change their password at next login:

passwd -e <username>

This command will expire the user's password, forcing the user to change the password at next login.

Lock user password:

passwd -l <username>

This command will lock the user's password, thereby prohibiting the user from logging into the system with this account. Users will not be able to log in with locked passwords.

Unlock user password:

passwd -u <username>

This command will unlock the locked user password and allow the user to log in to the system with this account.

View user password information:

passwd -S <username>

This command displays status information about the user's password, including whether it is locked, expired, etc. Note that you need to use sudo privileges or log into the system as root user when executing these commands.

delete user account

To delete a user account in a Linux system, you can use the following command:

userdel command: delete user account

For example, to delete a user account named "test", use the following command:

userdel test

rm command: delete the user's home directory

If you want to delete the user's home directory, you can use the following command:

rm -r /home/test

Note: When deleting user accounts and home directories, please operate carefully to avoid deleting important data by mistake.

switch user

In Linux systems, you can use the "su" or "sudo" command to switch users.

The "su" command can be used to switch to other user accounts, requiring the password of the target user.

For example, if the current user is "user1" and you want to switch to the "root" user, you can use the command

su - root

The "sudo" command is used to execute certain commands under the authority of the current user, and the password of the current user is required.

For example, if the current user is "user1" and you want to execute commands with "root" privileges, you can use the command:

sudo [command]

Note: It needs to be configured by the administrator in the /etc/sudoers file before it can be used.

User Group Operations

Add a new user group

To add a new user group in Linux, you can use the following command:

1. Open a terminal and log in as root.

2. Enter the following command to create a new user group:

sudo groupadd <group_name>

where <group_name>is the name of the new user group you want to create.

3. If you need to add users to a new user group, you can use the following command:

sudo usermod -a -G <group_name> <username>

where <username>is the name of the user you want to add to the new user group.

4. Confirm that the new user group has been created successfully, you can use the following command:

cat /etc/group | grep <group_name>

where <group_name>is the name of the new user group you created.

The above is how to add a new user group in Linux.

Modify the properties of a user group

To modify the attributes of a user group in Linux, you can use the chgrpand chmodcommand.

1. Use chgrpthe command to modify the group owner:

chgrp <new_group> <file_or_directory>

where <new_group>is the new group name and <file_or_directory>is the file or directory whose attributes are to be modified.

2. Use chmodthe command to modify group permissions:

chmod g<permission> <file_or_directory>

where g<permission>is the group permission to be modified, <file_or_directory>and is the file or directory whose attributes are to be modified.

For example, to change the group owner of the file "example.txt" to "newgroup", the following command can be used:

chgrp newgroup example.txt

To set the group permission of the directory "mydir" to read-write-execute (rwx), the following command can be used:

chmod g+rwx mydir

3. If you need to modify the group owner and group permissions at the same time, you can use chgrpand chmodcommand in combination:

chgrp <new_group> <file_or_directory> && chmod g<permission> <file_or_directory>

Note that administrator or root privileges may be required to execute the above commands.

User switches between user groups

To switch user groups in Linux, you can use the command "sudo -g <groupname>", where <groupname> is the name of the user group you want to switch to. For example, if you want to switch to the "admin" user group, you can use the command "sudo -g admin". After switching the user group, the current user will have the permissions and access level of the user group. 

delete an existing usergroup

To delete an existing user group in Linux, you can use the following command:

1. Open a terminal and log in as root.

2. Enter the following command to delete a user group:

sudo groupdel <group_name>

where <group_name>is the name of the user group you want to delete.

3. Confirm that the user group has been successfully deleted, you can use the following command:

cat /etc/group | grep <group_name>

If there is no output, it means that the user group has been successfully deleted.

Note that when deleting a user group, be careful not to have any users belonging to it or have been moved to another group. Otherwise, deleting usergroups may cause some permissions and access issues.

Guess you like

Origin blog.csdn.net/feng8403000/article/details/131954149