Linux User and Group management commands

Source: Internet
Author: User

One, user management commands

Useradd

The Useradd command is used for new system users created in Linux. Useradd can be used to create user accounts. After the account is built, then use passwd to set the password of the account. You can delete an account by using Userdel . The account number created by using the USERADD directive is actually saved in a /etc/passwd text file.

In Slackware, the adduser instruction is a script program, using the way of conversation to get input user account information, and then to the real establishment of the account Useradd command to establish a new user, so that the administrator can establish user account. In Red Hat Linux, the AddUser command is a symbolic connection to the Useradd command, which is actually the same instruction.

Grammar

Useradd (option) (parameter)

Options

-c< Remarks: Add notes text. Note text is saved in the comment field of passwd;

Useradd-c Nihao hancat/etc/passwd |tail-n 1han:x:503:505:nihao:/home/han:/bin/bash

-d< login Directory;: Specifies the starting directory at which the user logged in; (the directory must not exist)

useradd-d/home/yingyu hancat/etc/passwd |tail-n 1 Han:x:503:505::/home/yingyu:/bin/bash

-D: Change the preset value;

Use with other parameters to display or modify default values

Useradd-d

Useradd-d-S SHELL

Useradd-d-B Base_dir

Useradd-d-G GROUP

-e< validity period;: Specify the expiry date of the account;

USERADD-E 3 Hancat/etc/shadow |tail-n 1han:!! : 17548:0:99999:7::3:

Account expires after three days

-f< buffer days;: Specify how many days after the password expires to close the account;

Useradd-f 4 Hancat/etc/shadow |tail-n 1han:!! : 17548:0:99999:7:4::

Close the account after 4 days of expiration

-g< Group;: Specifies the group to which the user belongs, either as a set name or as a GID; (this group must exist)

Useradd-g Yingyu Hanid hanuid=503 (han) gid=506 (Yingyu) groups=506 (Yingyu)

-g< Group;: Specifies the additional group to which the user belongs;

Useradd-g Yingyu hancat/etc/group |tail-n 2yingyu:x:506:hanhan:x:507:

Adding Han in the Yingyu group

-M: Automatically set up the user's log in directory;

Useradd adds the-m parameter by default, so we use Useradd to create a directory where the user is automatically created with the same name under/home

-M: Do not automatically set up the user's log in directory;

-N: Cancels the creation of a group named after the user name;

Useradd-n Hanid hanuid=503 (han) gid=100 (users) groups=100 (users)

-R: Establish the system account number;

Useradd-r hancat/etc/passwd |tail-n 1han:x:496:493::/home/han:/bin/bash

(CentOS 6 and earlier versions <, CentOS 7 < 1000)

-s<shell>: Specifies the shell used by the user when logging in;

Useradd-s/bin/sh hancat/etc/passwd |tail-n 1han:x:503:507::/home/han:/bin/sh

Useradd-s/sbin/nologin Test Create a shell for non-interactive logon users

-u<uid>: Specifies the user ID.

Useradd-u 2018 HANCAT/ETC/PASSWD |tail-n 1han:x:2018:2018::/home/han:/bin/bash
Parameters

User name: The user name to create.


Usermod

The Usermod command is used to modify the user's basic information. The Usermod command does not allow you to change the user account name that is being online. When the Usermod command is used to change the user ID, it must be confirmed that the user is not executing any programs on the computer.

Usermod (option) (parameter)

Options

-c< Note: Modify the user account notes text;

Useradd-c Nihao hancat/etc/passwd |tail-1han:x:2019:2019:nihao:/home/han:/bin/bash usermod-c Hello HANCAT/ETC/PASSW D |tail-1han:x:2019:2019:hello:/home/han:/bin/bash

Change the Han user's note information Nihao to Hello

-d< log in Directory;: Modify the user login directory;

usermod-d/home/qwe hancat/etc/passwd |tail-1han:x:2019:2019::/home/qwe:/bin/bash

-e< validity period;: Modify the validity period of the account;

[Email protected] ~]# Cat/etc/shadow |tail-1han:!! : 17549:0:99999:7:::[[email protected] ~]# usermod-e 3 han[[email protected] ~]# Cat/etc/shadow |tail-1han:!! : 17549:0:99999:7::3:

-f< buffer days;: Modify the number of days after the password expires to close the account;

[[email protected] ~]# usermod-f 4 han[[email protected] ~]# Cat/etc/shadow |tail-1han:!! : 17549:0:99999:7:4:3:

-g< Group;: Modify the group to which the user belongs;

[[email protected] ~]# usermod-g Yingyu han[[email protected] ~]# ID hanuid=2019 (han) gid=506 (Yingyu) groups=506 (Yingyu)

-g< Group >; Modify the additional groups to which the user belongs;

[Email protected] ~]# usermod-g shuxue han[[email protected] ~]# ID hanuid=2019 (han) gid=506 (Yingyu) groups=506 (Yingyu), 2020 (Shuxue)

-l< account name;: Modify user account name;

[Email protected] ~]# usermod-l xiaoming han[[email protected] ~]# cat/etc/passwd |TAIL-1XIAOMING:X:2019:506::/HOME/QW E:/bin/bash[[email protected] ~]# ID xiaominguid=2019 (xiaoming) gid=506 (Yingyu) groups=506 (Yingyu), 2020 (Shuxue)

-L: Lock the user password to invalidate the password;

[[email protected] ~]# usermod-l xiaoming[[email protected] ~]# su lilei[[email protected] root]$ su xiaomingpassword:su : Incorrect password

-s<shell>: Modify the shell used by the user when logging in;

[Email protected] ~]# usermod-s/bin/sh xiaoming[[email protected] ~]# cat/etc/passwd |tail-1xiaoming:x:2019:506::/hom E/qwe:/bin/sh

-u<uid>: Modify user ID;

[Email protected] ~]# usermod-u 1995 Xiaoming[[email protected] ~]# cat/etc/passwd |tail-1xiaoming:x:1995:506::/home/q We:/bin/sh

-u: Unlock the password.

[[email protected]/home]# usermod-u xiaoming[[email protected]/home]# su lilei[[email protected] home]$ su xiaomingpass Word: [[email protected] home]$


Userdel


The userdel command is used to delete a given user, as well as files related to the user. Without the option, only the user account will be deleted, not the relevant files.

Grammar


Userdel (option) (parameter)


Option-F: Force the deletion of the user, even if the user is currently logged in;
[Email protected] ~]# Userdel wangcaiuserdel:user Wangcai is currently used by process 2727[[email protected] ~]# cat/e Tc/passwd|tail-1wangcai:x:2019:2021::/home/wangcai:/bin/bash[[email protected] ~]# userdel-f Wangcaiuserdel:user Wangcai is currently used by process 2727[[email protected] ~]# cat/etc/passwd|tail-1han:x:2018:2018::/home/han:/bin/ba Sh

-r: Deletes all files associated with the user while deleting the user.

[[email protected] ~]# ls/home/lilei wangcai[[email protected] ~]# userdel-r lilei[[email protected] ~]# LS/HOME/WANGC Ai
just deleted Wangcai This user did not add the-R parameter so also save the home directory, add-R user Lilei home directory is gone
Parameters

User name: The user name to delete.


Please do not easily use the-r option; He will delete the user all the files and directories, remember that if there are important files in the user directory, please back up before deleting.

In fact, there is the simplest way, but this approach is a bit insecure, that is, directly in the/etc/passwd delete the user you want to delete the record, but it is best not to do so,/etc/passwd is a very important file, you may inadvertently operation error.


Second, group account Maintenance command


Groupadd


The Groupadd command is used to create a new workgroup, and the new workgroup information is added to the system files.


Grammar

groupadd (option) (parameter)


Options

-G: Specifies the ID of the new workgroup;

[Email protected] ~]# groupadd-g 1243 yuwen[[email protected] ~]# cat/etc/group|tail-1yuwen:x:1243:

-r: Create system Workgroup, System Workgroup Group ID is less than 500;

[Email protected] ~]# groupadd-r tiyu[[email protected] ~]# cat/etc/group|tail-1tiyu:x:493:


Parameters


Group Name: Specifies the group name of the new workgroup.


Groupmod


The Groupmod command changes the group identifier or name. When you need to change the identification code or name of the group, you can use the GROUPMOD directive to do this.


Grammar


Groupmod (option) (parameter)


Options


-g< group identification Code;: Set the group identifier to use;

Tiyu:x:493:[[email protected] ~]# groupmod-g 510 tiyu[[email protected] ~]# cat/etc/group|tail-1tiyu:x:510:

-n< New group Name: Set the name of the group you want to use.

[[email protected] ~]# cat/etc/group|tail-1tiyu:x:510:[[email protected] ~]# groupmod-n Lishi tiyu[[email protected] ~] # cat/etc/group|tail-1lishi:x:510:
-O: Repeated use of the group identification Code;
[Email protected] ~]# cat/etc/group|tail-1lishi:x:510:[[email protected] ~]# groupmod-g 1243 Lishi-o[[email Protect Ed] ~]# Cat/etc/group|tail stapdev:x:158:tcpdump:x:72:screen:x:84:admins:x:502:sysadmins:x:504:yingyu:x:506:han:x : 2019:shuxue:x:2020:yuwen:x:1243:lishi:x:1243:


Parameters


Group Name: Specifies the group name of the work to be modified.


Groupdel


The Groupdel command is used to delete the specified workgroup, and the system files to be modified by this command include/ect/group and/ect/gshadow. If some users are still included in the group, you must delete the users before you can delete the groups.


Grammar


Groupdel (parametric)


Parameters


Group: The workgroup name to delete.


[[email protected] ~]# cat/etc/group|tail-1lishi:x:1243:[[email protected] ~]# Groupdel lishi[[email protected] ~]# cat /etc/group|tail-1yuwen:x:1243:



Linux User and Group management commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.