Adding, Modifying and Deleting Users and User Groups in Linux

Width is limited to tightness, hard work, and stagnation

1. User:

When creating a user, you need to specify a user group for the new user. If you do not specify the workgroup to which the user belongs, a workgroup with the same name as the user name will be automatically generated. When creating user user1, specify the workgroup users to which it belongs, for example:useradd –g users user1

1. Use the command useradd to create a user

Example: useradd user1 - create user user1
useradd –e 12/30/2009 user2- create user2, specify that the validity period expires on 2009-12-30 The default UID of the
user increases from 500 to the back, and the following is the system reserved account, you can specify the UID, for
example:useradd –u 600 user3

2. Use the passwd command to set a password for the new user

Example: passwd user1
Note: Users who have not set a password cannot use it.

3. Command usermod to modify the user account

Example: Change the login name of user user1 to u1,

usermod –l u1 user1 
  • 1

Example: add user user1 to the users group,

usermod –g users user1 
  • 1

Example: Change the user user1 directory to /users/us1

usermod –d /users/us1 user1 
  • 1

4. Use the command userdel to delete the user account

Example: delete user user2

userdel user2 
  • 1

Example: delete user user3 and delete his working directory at the same time

userdel –r user3 
  • 1

5. View user information

The id command checks the UID and GID of a user, for example: check the id of
user4 id user4
finger command - you can check the user's home directory, startup shell, username, address, phone and other information
Example :finger user4

2. User group:

6. The command groupadd creates a user group

groupadd –g 888 users 
  • 1

Create a group users with GID 888

7. Command gpasswd to add users to the group

Only root and group administrators can change group membership:
Example: add user1 to users group

gpasswd –a user1 users 
  • 1

Example: remove user1 from the users group

gpasswd –d user1 users 
  • 1

8. Command groupmod to modify the group

groupmod –n user usersModify the group name user to users

9, groupdel delete group

groupdel usersdelete group users

3. Command details:

◆ useradd command

1. Function

The useradd command is used to create a user account and create a user's home directory, with the permission of super user.

2. Format

useradd [-d home] [-s shell] [-c comment] [-m [-k template]] [-f inactive] [-e expire ] [-p passwd] [-r] name
  • 1

3. Main parameters

-c: Add the remark text, and the remark text is saved in the remark column of passwd. 
-d: Specifies the starting directory when the user logs in.
-D: Change the default value.
-e: Specifies the validity period of the account. The default is permanent.
-f: Specifies the number of days after the password expires to close the account.
-g: Specifies the group to which the user belongs.
-G: Specifies the additional group to which the user belongs.
-m: Automatically create the user's login directory.
-M: Do not automatically create the user's login directory.
-n: Cancel the creation of a group named after the user name.
-r: Create a system account.
-s: Specifies the shell used by the user after logging in. (After -s, fill in the path of the type of shell used by this user after logging in. Shells generally include /bin/sh, /bin/bash, /bin/ksh, /bin/tcsh, /bin/zsh in the /bin directory; shell It is the interface between the user and the system. Different shells only have different command syntax.)
-u: Specify the user ID number.

4. Description

useradd can be used to create user accounts, it is the same as the adduser command. After the account is created, use passwd to set the password for the account. Accounts created using the useradd command are actually stored in the /etc/passwd text file.

  1. Applications

Create a new user account and set the ID:

#useradd caojh -u 544
  • 1

It should be noted that when setting the ID value, try to be greater than 500 to avoid conflicts. Because some special users will be created after Linux installation, the values ​​between 0 and 499 are generally reserved for system accounts such as bin and mail.

◆ groupadd command

1. Function

The groupadd command is used to add a new group to the system.

2. Format

groupadd [-g gid] [-o]] [-r] [-f] groupname
  • 1

3. Main parameters

-g gid: Specifies the group ID number.
-o: Allow group ID numbers, not necessarily unique.
-r: Join the group ID number, which is lower than the 499 system account.
-f: When joining an existing group, the development program exits.

4. Application Examples

Create a new group and set the group ID to join the system:

#groupadd -g 344 cjh
  • 1

At this point, a group ID (GID) of 344 is generated in the /etc/passwd file.

Standing on the shoulders of giants

The online article reproduced in this blog post
https://my.oschina.net/junn/blog/138939

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324531539&siteId=291194637