Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manage users and user groups in Linux

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Linux in how to achieve user and user group management, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

I. Management of user accounts in Linux system

The management of user accounts mainly involves the addition, modification and deletion of user accounts.

To add a user account is to create a new account in the system, and then assign resources such as user number, user group, home directory and login Shell to the new account. The account you just added is locked and cannot be used.

1. Use the useradd command to add a new user account. The syntax is as follows:

Useradd option user name

The meanings of the options are as follows:

-c comment specifies an annotative description.

The-d directory specifies the user's home directory, and if this directory does not exist, use the-m option at the same time to create a home directory.

-g user group specifies the user group to which the user belongs.

-G user group, which specifies the additional group to which the user belongs.

The-s Shell file specifies the user's login Shell.

The-u user number specifies the user number of the user, and if there is also the-o option, you can reuse the identification number of other users.

The user name specifies the login name of the new account.

2. Example illustration

Example 1:

# useradd-d / usr/sam-m sam

This command creates a user sam

The-d and-m options are used to generate a home directory / usr/sam for the login name sam (/ usr is the parent directory where the default user home directory is located).

Example 2:

# useradd-s / bin/sh-g group-G adm,root gem

This command creates a new user gem whose login Shell is / bin/sh, which belongs to the group user group, as well as to the adm and root user groups, where the group user group is its primary group.

New groups may be created here: # groupadd group and groupadd adm

To add a user account is to add a record to the new user in the / etc/passwd file, while updating other system files such as / etc/shadow, / etc/group, etc.

Linux provides an integrated system management tool userconf, which can be used to manage user accounts in a unified manner.

3. Delete the account

If a user's account is no longer in use, it can be deleted from the system. To delete a user account is to delete the user record in system files such as / etc/passwd and, if necessary, the user's home directory. Delete an existing user account using the userdel command in the following format:

Userdel option user name

The common option is-r, which deletes the user's home directory together.

For example:

# userdel sam

This command deletes the records of the user sam in the system files (mainly / etc/passwd, / etc/shadow, / etc/group, etc.) and deletes the user's home directory.

4. Modify the account

To modify the user account is to change the relevant attributes of the user according to the actual situation, such as user number, home directory, user group, login Shell and so on.

Use the usermod command to modify the information of an existing user in the following format:

Usermod option user name

Common options include-c,-d,-m,-g,-G,-s,-u, and-o, which have the same meaning as the options in the useradd command and can specify new resource values for the user. In addition, some systems can use the following options:

-l New user name

This option specifies a new account, changing the original user name to the new user name.

For example:

# usermod-s / bin/ksh-d / home/z-g developer sam

This command changes the login Shell of user sam to ksh, the home directory to / home/z, and the user group to developer.

5. Management of user password

One of the important contents of user management is the management of user password. The user account was created without a password, but it is locked by the system and cannot be used. It must be assigned a password before it can be used, even if an empty password is specified.

The Shell command to specify and modify a user password is passwd. Superusers can specify passwords for themselves and other users, and ordinary users can only use it to change their own passwords. The format of the command is:

Passwd option user name

Available options:

-l lock password, that is, disable the account.

The-u password is unlocked.

-d make the account without a password.

-f forces the user to change the password the next time he logs in.

If the default user name is used, the password of the current user is modified.

For example, assuming that the current user is sam, the following command modifies the user's own password:

$passwd

Old password:*

New password:*

Re-enter new password:*

If you are a superuser, you can specify the password of any user in the following form:

# passwd sam

New password:*

Re-enter new password:*

When an ordinary user modifies his password, the passwd command asks the original password first, verifies it, and then asks the user to enter the new password twice. If the password entered twice is the same, the password is assigned to the user, while the superuser does not need to know the original password when assigning the password to the user.

For the sake of system security, users should choose more complex passwords, such as 8-digit long passwords, which contain uppercase, lowercase letters and numbers, and should be different from names, birthdays, and so on.

When you specify an empty password for the user, execute the following form of command:

# passwd-d sam

This command removes the password for user sam so that the next time user sam logs in, the system no longer asks for the password.

The passwd command can also lock a user out of login with the-l (lock) option, for example:

# passwd-l sam

II. Management of user groups in Linux system

Each user has a user group, and the system can centrally manage all users in a user group. Different Linux systems have different rules for user groups, for example, users under Linux belong to a user group with the same name, which is created at the same time as the user is created.

The management of user groups involves the addition, deletion and modification of user groups. Group additions, deletions, and modifications are actually updates to the / etc/group file.

1. Add a new user group to use the groupadd command. The format is as follows:

Groupadd option user group

The options you can use are:

-g GID specifies the group identification number (GID) of the new user group.

-o is generally used in conjunction with the-g option to indicate that the GID of a new user group can be the same as the GID of an existing user group on the system.

Example 1:

# groupadd group1

This command adds a new group group1 to the system, and the group identification number of the new group is added 1 to the current largest group identification number.

Example 2:

# groupadd-g 101 group2

This command adds a new group group2 to the system and specifies that the group identification number of the new group is 101.

2. If you want to delete an existing user group, use the groupdel command in the following format:

Groupdel user group

For example:

# groupdel group1

This command removes the group group1 from the system.

3. Use the groupmod command to modify the attributes of a user group. The syntax is as follows:

Groupmod option user group

Common options are:

-g GID specifies a new group identification number for the user group.

Using the-o option with the-g option, the new GID of the user group can be the same as the GID of the existing user group of the system.

-n New user group changes the name of the user group to a new name

Example 1:

# groupmod-g 102 group2

This command changes the group identification number of the group group2 to 102.

Example 2:

# groupmod-g 10000-n group3 group2

This command changes the identification number of the group group2 to 10000 and the group name to group3.

4. If a user belongs to multiple user groups at the same time, the user can switch between user groups in order to have the permissions of other user groups. After logging in, the user can switch to another user group using the command newgrp, whose parameter is the destination user group. For example:

$newgrp root

This command switches the current user to the root user group, provided that the root user group is indeed the primary or additional group of the user. Similar to the management of user accounts, the management of user groups can also be accomplished through integrated system management tools.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report