Useradd command usage in Linux

Source: Internet
Author: User
Tags comments centos


Command:
Useradd Add Users
AddUser Add Users
passwd Add password
UserMode Modify User Information
Chsh Modify User Shell
Chage Modify Account Date
CHFN Modify User Information
Userdel Delete User
Groupadd Add a group
Groupmde Modify Group
Groupdel Delete Group
NEWGRP Toggle Basic Group

systemd-network:!! : 16982:-::: £ º
Username: Password Placeholder: UID: Base Group ID: User Note information: User home directory: Default Shell

When a user is created, all files under/etc/skel/are copied to the/home/user directory/below

[Root@docker-node1 ~]# Cat/etc/default/useradd
# useradd defaults file
group=100
Home=/home
Inactive=-1 the user to create an inactive state
Expire= User Expiration Period
Shell=/bin/bash User Bash
Skel=/etc/skel Create home Directory
Create_mail_spool=yes Create a mail cache pool. (/var/mail will have the corresponding user's buffer pool)
AddUser: In CentOS, AddUser is linked to Useradd

[Root@docker-node1 ~]# which adduser
/usr/sbin/adduser
[Root@docker-node1 ~]#

[Root@docker-node1 ~]# Ll/usr/sbin/adduser
lrwxrwxrwx. 1 root 7 June 13:17/usr/sbin/adduser-> Useradd
[Root@docker-node1 ~]#
Specify UID: Prerequisite is a UID that is not used repeatedly

Useradd-u 1010 Mark.
[Root@docker-node1 ~]# tail-1/etc/passwd
Mark:x:1010:1010::/home/mark:/bin/bash
[Root@docker-node1 ~]#
If the UID is specified and is created by the next user, the default UID plus 1
-U: Specify UID
-G: Specifies the GID, which specifies the user's basic group, and the GID must exist beforehand

Specify GID: If the group ID must be created or exist
-G: Create and assign a GID to it
-G: Specify user attach group, group must exist beforehand
To create a group, you can add it to a group first
Groupadd Linuxea

[Root@docker-node1 ~]# Groupadd Linuxea
[Root@docker-node1 ~]# Tail-2/etc/group
MARK:X:1010:
linuxea:x:1011:
Specify user to Group

[Root@docker-node1 ~]# useradd-g Linuxea linuxea1
[Root@docker-node1 ~]# tail-1/etc/passwd
Linuxea1:x:1012:1011::/home/linuxea1:/bin/bash
[Root@docker-node1 ~]# Tail-2/etc/group
MARK:X:1010:
linuxea:x:1011:
[Root@docker-node1 ~]#
[Root@docker-node1 ~]# ID Linuxea
uid=1011 (Linuxea) gid=1011 (Linuxea) groups=1011 (Linuxea)
[Root@docker-node1 ~]# ID linuxea1
uid=1012 (LINUXEA1) gid=1011 (Linuxea) groups=1011 (Linuxea)
[Root@docker-node1 ~]#
User types are divided into administrators and ordinary users, in the ordinary users are divided into system users and login users, the administrator for 0, ordinary users from 1-65535, while the system users: 1-499, login user 500 +

Group:
Basic group: A GID field group displayed in/etc/passwd for the user's basic group
Additional groups: Additional groups:/etc/group

[Root@docker-node1 ~]# useradd-g Linuxea linuxea2
[Root@docker-node1 ~]# tail-1/etc/passwd
Linuxea2:x:1013:1013::/home/linuxea2:/bin/bash
[Root@docker-node1 ~]# tail-1/etc/group
linuxea2:x:1013:
[Root@docker-node1 ~]#
Using IDs, you can see

[Root@docker-node1 ~]# ID LINUXEA2
uid=1013 (LINUXEA2) gid=1013 (LINUXEA2) groups=1013 (LINUXEA2), 1011 (LINUXEA)
[Root@docker-node1 ~]#
Group can also see additional groups, of course, you can also directly add additional groups to the file

[Root@docker-node1 ~]# tail-3/etc/group
Linuxea:x:1011:linuxea2
linuxea2:x:1013:
[Root@docker-node1 ~]#
-D: User Directory
User specified directory cannot exist in advance
The specified home directory is not the same as the user name

[Root@docker-node1 ~]# useradd-d/home/xiaoming Xiaohong
[Root@docker-node1 ~]# tail-1/etc/passwd
Xiaohong:x:1015:1015::/home/xiaoming:/bin/bash
[Root@docker-node1 ~]#
Specify home directory Location
Usually at home, of course, you can also customize

[Root@docker-node1 ~]# useradd-d/tmp/daxiong Xiaoxiong
[Root@docker-node1 ~]# tail-1/etc/passwd
Xiaoxiong:x:1016:1016::/tmp/daxiong:/bin/bash
[Root@docker-node1 ~]#
-C Specify Note information

[Root@docker-node1 ~]# useradd-c "Linuxea"-d/tmp/user1 user1
[Root@docker-node1 ~]# tail-1/etc/passwd
User1:x:1017:1017:linuxea:/tmp/user1:/bin/bash
[Root@docker-node1 ~]#
-s Specifies shell

[Root@docker-node1 ~]# useradd-c "linuxea1"-d/tmp/user2 user2-s
[Root@docker-node1 ~]# tail-1/etc/passwd
User2:x:1018:1018:linuxea1:/tmp/user2:/sbin/nologin
[Root@docker-node1 ~]#
If Nologin is not available, the home directory will not be created, and system users will typically use
-G specified group

[Root@docker-node1 ~]# useradd-g linuxea-d/home/user8-s/sbin/nologin
[Root@docker-node1 ~]# ID User8
uid=1024 (User8) gid=1011 (Linuxea) groups=1011 (Linuxea)
Create a user and specify an additional group

[Root@docker-node1 ~]# useradd-g 1018-d/home/user4-s/sbin/nologin
[Root@docker-node1 ~]# Tail-2/etc/group
User2:x:1018:user4
USER4:X:1019:
[Root@docker-node1 ~]#
Create user specified Uid,shell, home directory

[Root@docker-node1 ~]# useradd-u 1021-d/home/user5-s/sbin/nologin
[Root@docker-node1 ~]# tail-1/etc/passwd
User5:x:1021:1021::/home/user5:/sbin/nologin
[Root@docker-node1 ~]#
-m: Force user to create home directory when creating user
-M: Create user is only create user does not create home directory

[Root@docker-node1 ~]# useradd user7-m-s/sbin/nologin
[Root@docker-node1 ~]# tail-1/etc/passwd
User7:x:1023:1023::/home/user7:/sbin/nologin
-D: Change the default value to specify a new default value for the user created by the Useradd command

Delete User:
Delete the user default retention home directory, if you need to delete home directory-R can be: Userdel-r User8

File format:
/ETC/PASSWD:
Username:x:uid:gid:comment:home:shell
/etc/group:
Groupname:x:gid:user:list
Home directory Copy file link:/etc/skel
Default configuration:/etc/default/useradd
In the absence of a password, you cannot log on, you can only use the admin su switch

Set Password: passwd
In which, only admin root can modify its own password, use passwd, if modify other user then passwd user name can
The ordinary user changes the password needs the password complexity, if does not meet will prompt

[Root@docker-node1 ~]# passwd Linuxea
Changing password for user Linuxea.
New Password:
Bad Password:the PASSWORD is shorter than 8 characters
Retype new Password:
Passwd:all authentication tokens updated successfully.
[Root@docker-node1 ~]# Su-linuxea
[Linuxea@docker-node1 ~]$ passwd
Changing password for user Linuxea.
changing password for Linuxea.
(current) UNIX Password: Enter current password
New password: Enter the password, and if the new password is not complex enough, you will be prompted as follows:
Bad Password:the PASSWORD are the same as the old one
New Password:
Bad Password:the PASSWORD are the same as the old one
New Password:
Bad Password:the PASSWORD are the same as the old one
Passwd:have exhausted maximum number of retries for service
[Linuxea@docker-node1 ~]$
[Root@docker-node1 ~]# tail-1/etc/shadow
user8:!! : 16999:0:99999:7::
[Root@docker-node1 ~]#
Shadow File Description:
user8:!! : 16999:0:99999:7::
Username: encryption Password: Last modified time (from January 1, 1970 to password after the number of days): The shortest time limit, 0 unlimited: The maximum length of use, for 99999 is not limited: warning interval, away from the expiration of the number of days to start warning: inactive interval, Account password login need to change Password: account expiration time, expiration date: reserved, not used

Group Password setting: GPASSWD Group name
To modify a user property definition:
Chsh Modify Shell

[Root@docker-node1 ~]# tail-1/etc/passwd
User8:x:1024:1011::/home/user8:/sbin/nologin
[Root@docker-node1 ~]# Chsh User8
changing shell for User8.
New Shell [/sbin/nologin]:/bin/bash
Shell changed.
[Root@docker-node1 ~]# tail-1/etc/passwd
User8:x:1024:1011::/home/user8:/bin/bash
[Root@docker-node1 ~]#
Modify User comments: Chfn
Of course, you can only write a paragraph

[Root@docker-node1 ~]# CHFN User8
Changing finger information for User8.
Name []: User8 Backup
Office []: Manila
Office Phone []: 150 8888 8888
Home Phone []: 119

Finger information changed.
[Root@docker-node1 ~]# tail-1/etc/passwd
User8:x:1024:1011:user8 backup,manila,150 8888 8888,119:/home/user8:/bin/bash
[Root@docker-node1 ~]#
If you want to modify the information directly, you can use Usermod
Usermod:
-U: Modifying UID
-G: Modifying GID
-G: Modify an additional group, if the default has an additional group, the Default-G overrides, if you add a parameter-a option on top of an additional group
[Root@docker-node1 ~]# ID User8
uid=1024 (User8) gid=1011 (Linuxea) groups=1011 (Linuxea)
[Root@docker-node1 ~]# Groupadd Test
[Root@docker-node1 ~]# usermod-a-G test User8
[Root@docker-node1 ~]# ID User8
uid=1024 (User8) gid=1011 (Linuxea) groups=1011 (Linuxea), 1024 (test)
[Root@docker-node1 ~]#
-C: Modifying user comments
-D: Modifying home directory
[Root@docker-node1 ~]# tail-1/etc/passwd
User8:x:1024:1011:user8 backup,manila,150 8888 8888,119:/home/user8:/bin/bash
[Root@docker-node1 ~]# usermod-m-d/tmp/user8 User8

If you do not add-M, you will report su:warning:cannot change directory to/tmp/user8:no such file or directory-d will not migrate home directories

[Root@docker-node1 ~]# tail-1/etc/passwd
User8:x:1024:1011:user8 backup,manila,150 8888 8888,119:/tmp/user8:/bin/bash
[Root@docker-node1 ~]# Su-user8
Last Login:sun June 02:45:03 EDT 2016 on pts/0
[User8@docker-node1 ~]$ ls
-s:shell
[Root@docker-node1 ~]# usermod-s/sbin/nologin User8
[Root@docker-node1 ~]# tail-l/etc/passwd|grep User8
User8:x:1024:1011:user8 backup,manila,150 8888 8888,119:/tmp/user8:/sbin/nologin
[Root@docker-node1 ~]#
-L: Modifying login name
[Root@docker-node1 ~]# usermod-l linuxeacom1 User8
[Root@docker-node1 ~]# tail-l/etc/passwd|grep User8
Linuxeacom1:x:1024:1011:user8 backup,manila,150 8888 8888,119:/tmp/user8:/sbin/nologin
[Root@docker-node1 ~]#
-L: Lock user account
[Root@docker-node1 ~]# passwd-l linuxeacom1
Locking password for user linuxeacom1.
Passwd:success
-U: Unlocking
[Root@docker-node1 ~]# passwd-u linuxeacom1
Unlocking password for user linuxeacom1.
passwd:Warning:unlocked password would be empty.
Passwd:unsafe operation (use-f to force)
[Root@docker-node1 ~]#
Lock and unlock differences to add two additional!! to the configuration file, as follows:

[Root@docker-node1 ~]# tail-1/etc/shadow
Mark1:$6$xn4wjxly$fsumblmfuptongdz0xdsle5okmf39hnejx/knrhqsdhznx/3adx1ldm.. 5mmmb5gzcudmydrsd.vmhee4m9pk.:16999:0:99999:7:::
When using-l, it is added!!

[Root@docker-node1 ~]# passwd-l Mark1
Locking password for user mark1.
Passwd:success
[Root@docker-node1 ~]# tail-1/etc/shadow
mark1:!! $6$xn4wjxly$fsumblmfuptongdz0xdsle5okmf39hnejx/knrhqsdhznx/3adx1ldm.. 5mmmb5gzcudmydrsd.vmhee4m9pk.:16999:0:99999:7:::
-U Cancel

[Root@docker-node1 ~]# passwd-u Mark1
Unlocking password for user mark1.
Passwd:success
[Root@docker-node1 ~]# tail-1/etc/shadow
Mark1:$6$xn4wjxly$fsumblmfuptongdz0xdsle5okmf39hnejx/knrhqsdhznx/3adx1ldm.. 5mmmb5gzcudmydrsd.vmhee4m9pk.:16999:0:99999:7:::
[Root@docker-node1 ~]#
To modify a group property definition:
-N: Modify new group name
[Root@docker-node1 ~]# tail-1/etc/group
user9:x:1025:
[Root@docker-node1 ~]# groupmod-n linuxeacom1 User9
[Root@docker-node1 ~]# tail-1/etc/group
linuxeacom1:x:1025:
[Root@docker-node1 ~]#
-G: Modifying GID
[Root@docker-node1 ~]# groupmod-g 10250 linuxeacom1
[Root@docker-node1 ~]# tail-1/etc/group
LINUXEACOM1:X:10250:
[Root@docker-node1 ~]#

Modify Account Date attribute: chage
-E: Expiration period
-I: Inactive period
-M: Minimum service Age
-M: Maximum service Age
-W: Warning interval
These time passwd can also be defined

Example:
1, create the user is 123, its ID is 3000, the basic group is CentOS (group id3003), the additional group is Linuxea

Groupadd Linuxea
Groupadd-g 3003 CentOS
Useradd u 3000-g centos-g linuxea 123
2, create user for REDHAT1, full name is redhat123, default shell is Nologin

Useradd-c "redhat123"-s/sbin/nologin redhat1
3, modify REDHAT1 ID number is 4000, basic group is CentOS, additional group is LINUXEA and 123

Usermod-u 4000-g centos-g linuxea,123 redhat1
4, add redhat1 password, and set password minimum probation period of 30 days, the maximum is 35 days

passwd REDHAT1
Chage-m 30-m REDHAT1
5, modify the REDHAT1 shell to Bin/bash

Usermod-s/bin/bash REDHAT1
ID use
Show UID

[Root@docker-node1 ~]# id-u linuxeacom1
1024
Show GID

[Root@docker-node1 ~]# id-g linuxeacom1
1011
Show All GID

[Root@docker-node1 ~]# id-g linuxeacom1
1011 1024
Show Basic Group group name

[Root@docker-node1 ~]# id-g-N linuxeacom1
Linuxea
Show additional group group names

[Root@docker-node1 ~]# id-g-N linuxeacom1
Linuxea Test
[Root@docker-node1 ~]#
Invoke command after SU switch

[Root@docker-node1 ~]# su-l-C ' ls/tmp ' mark
-classpath.txt systemd-private-b6e5d402e0764baba1f123fba87fad8a-httpd.service-uczo6x Tomcat.txt
Daxiong Systemd-private-b6e5d402e0764baba1f123fba87fad8a-mariadb.service-cmjoiq User1
Hsperfdata_root tcp-status.txt User2
HttpNUB.txt Tomcat2.txt User8
Lib:lib.txt tomcat3.txt Zabbix_java_gateway_logback.xml. Txt
[Root@docker-node1 ~]#

Related Article

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.