Use the useradd command to create a new account in Linux

Source: Internet
Author: User
A long time ago, I wrote two articles about some user-related files in Linux. Now I want to use some time to introduce the operations related to Linux Account Management. To manage accounts, let's take a look at the command to create a new account named useradd in Linux.In another day, we will provide you with commands to set user passwords and delete accounts.

This article still references "laruence's Linux private house dish", so if the column is in place, you can think that I just want to sort out some of laruence's stuff. Well, it's for your convenience. Let's go ~

Files related to user management:

Although I have introduced it before, I would like to give a brief review here to learn more about it. In Linux, user management-related files include:/etc/passwd,/etc/shadow,/etc/gfoup, and/etc/gshadow. They are related to the user's account, password, user group, and user group password.

Therefore, the process of creating a new account is dealing with these files, but do not be afraid,Use the useradd command to create a new account. The system will automatically perform operations with these files without manual changes, so you do not have to worry about errors.Haha.

Useradd syntax structure:

Finding a "man" is the most effective method in Linux. Let's take a look at the syntax structure of useradd, because I am using the fedora11 system, and its man statement uses Chinese directly, although it is easy to read, it is inevitable that there will be errors, but it is basically okay. Do not blame the following Chinese characters:

Useradd-Account creation or updating of new user information

Syntax useradd [-C comment] [-D home_dir]
[-E expire_date] [-F inactive_time]
[-G initial_group] [-G group [,...]
[-M [-K skeleton_dir] |-M] [-s shell]
[-U uid [-O] [-N] [-R] Username

Useradd-d [-G default_group] [-B default_home]
[-F default_inactive] [-e default_expire_date]
[-S default_shell]

First, let's take a look at this-D parameter. If this parameter is added, some default values for creating a new account are displayed when useradd is used, or the default value is updated by specifying the parameter content.Let's discuss it later.

It seems that there are no options for the-D parameter. The following is a brief description of the first and second items. I will not feel dizzy when I look at the example I gave the column. Haha.

Parameter Name Description
-U UID followed by a group of numbers. Specify a specific uid for this account.
-G The user group name that follows is the initial user group mentioned earlier.
The Group ID (GID) is placed in the fourth field of/etc/passwd.
-G The user group name that follows is the user group that this account can belong.
This parameter modifies the data in/etc/group.
-M Do not create a home directory.
-M You must create a home directory.
-C It is the user description in the Fifth Column of/etc/passwd. You can set it as needed.
-D Use the path specified after this parameter as the Home Directory of the new account, without using the default value.
-R The created account is a system account. The UID of this account is limited (/etc/login. defs)
-S Specify the default shell for the new account

Useradd example:

After talking about this for a long time, we also started to use the useradd command to create several accounts to improve our hands-on capabilities. In addition, let's make a demonstration. After all, we have a good idea. Oh, but I forgot what a great man said. Well.

# Create a new user chongpig by default without adding any parameters
[Simaopig @ xiaoxiaozi ~] $ Useradd chongpig

Bash:/usr/sbin/useradd: insufficient Permissions
[Simaopig @ xiaoxiaozi ~] $ Su

Password:
[Root @ xiaoxiaozi simaopig] #
Useradd chongpig
[Root @ xiaoxiaozi simaopig] #
Ls-L/home/

Total 24

Drwx ------. 4 chongpig 4096 07-21 21:58 chongpig

Drwx ------. 2 root Root 16384 07-12 13:08
Lost + found

Drwxr-XR-X. 44 simaopig 4096 07-21 simaopig

# Search for the Account chongpig in several files associated with the user

[Root @ xiaoxiaozi simaopig] #
Grep chongpig/etc/passwd/etc/shadow/etc/group
/Etc/passwd: chongpig: X: 501: 501:/home/chongpig:/bin/bash
/Etc/shadow: chongpig :!! : 14446: 0: 99999: 7 :::
/Etc/group: chongpig: X: 501: # create a new account simaopig2 and check that uid 888 does not exist. specify this account to simaopig2,
# And add it to the MySQL Group
[Root @ xiaoxiaozi simaopig] #
Grep 888/etc/passwd
[Root @ xiaoxiaozi simaopig] #
Useradd-u 888-G MySQL simaopig2
[Root @ xiaoxiaozi simaopig] #
Grep simaopig2/etc/passwd/etc/shadow/etc/group
/Etc/passwd: simaopig2: X: 888: 27:/home/simaopig2:/bin/bash
/Etc/shadow: simaopig2 :!! : 14446: 0: 99999: 7: # create a system account simaopig3 and check whether the system account is different.
[Root @ xiaoxiaozi simaopig] #
Useradd-r simaopig3
[Root @ xiaoxiaozi simaopig] #
Grep simaopig3/etc/passwd/etc/shadow/etc/group
/Etc/passwd: simaopig3: X: 490: 484:/home/simaopig3:/bin/bash
/Etc/shadow: simaopig3 :!! : 14446 ::::::
/Etc/group: simaopig3: X: 484:

The uid of the system account is different from the GID, And it corresponds to/etc/group.

Careful students will surely find that,The account created with useradd does not have the logon function because it cannot be logged on in the/etc/shadow password column! Start. So we will discuss how to change the password for users in Linux recently.

Useradd default value:

The above example shows that,In fact, the system has already standardized some parameters for new users. For example, I have not specified the user's home directory or the user's UID and GID, but the system will add them to the user, at the same time, the default shell is set to/bin/bash.And the useradd command must have the root permission. What is the default value of useradd when users are added?

The-D parameter of useradd above can display its default value. You can see that the value is exactly the same as that in the/etc/default/useradd file. OriginalIn Linux, when useradd is used to add users, some values in/etc/passwd will refer to the/etc/default/useradd file.. The file content is basically as follows:

# Useradd defaults File
# Default User Group
Group = 100
# Home Directory address
Home =/home
# Password Expiration grace time, corresponding to the seventh column of/etc/shadow
Inactive =-1
# Account expiration date, corresponding to the eighth column of/etc/shadow
Expire =
# Shell used by default
Shell =/bin/bash

# Reference files in the user's home directory (basically all of them are hidden files --> files starting)
SKEL =/etc/skel
# Create a user's mailbox
Create_mail_spool = Yes

Conclusion and comment:

This is a simple introduction. It is said that the adduser command can also be used to create an account in Linux. In fact, this is different from the useradd command. Because man adduser is used to access the introduction page of useradd. Of course there are simpler ways to create users through the graphic interface, but I will not teach you this.

I quietly told you that I tried these examples when I typed this article. Now there are many more users in my system. Haha. But in order to ensure the accuracy of the program, this is a must, after all, this is my own learning process.

PS: It's a little fun that the traffic volume is over 40. Thank you.

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.