Use files to bulk add users under CentOS

Source: Internet
Author: User

There are multiple ways to add users to CentOS, such as using Useradd, passwd to add users in bulk, using scripts to add users in batches, and using files to import users in batches. This section describes methods for importing users in batches using files.

Environment Introduction:

[Email protected] ~]# uname-r

3.10.0-327.el7.x86_64

[Email protected] ~]# cat/etc/centos-release

CentOS Linux release 7.2.1511 (Core)

[Email protected] ~]#


    1. Create user files, the file format must be consistent with/etc/passwd format.

[email protected] ~]# cat Useradds.txt

User1:x:1001:1001::/home/user1:/bin/bash

User2:x:1002:1002::/home/user2:/bin/bash

User3:x:1003:1003::/home/user3:/bin/bash

User4:x:1004:1004::/home/user4:/bin/bash

#/etc/passwd file Format Description: Username:passwd:uid:gid:comment:home-directory:shell


2. Use NewUsers to import the user and verify.

[Email protected] ~]# newusers useradds.txt

[Email protected] ~]# tail-5/etc/passwd

Tcpdump:x:72:72::/:/sbin/nologin

User1:x:1001:1001::/home/user1:/bin/bash

User2:x:1002:1002::/home/user2:/bin/bash

User3:x:1003:1003::/home/user3:/bin/bash

User4:x:1004:1004::/home/user4:/bin/bash

[Email protected] ~]#

The user was successfully imported in the/etc/passwd file.

[Email protected] ~]# Ll/home

Total 0

drwx------2 User1 user1 6 May 11:09 user1

DRWX------2 user2 user2 6 May 11:09 User2

DRWX------2 User3 user3 6 May 11:09 User3

DRWX------2 User4 user4 6 May 11:09 User4

[Email protected] ~]#

View/home directory has already established the user's House directory.


3. Import the user password file using CHPASSWD. The password file format is username:passwd

[email protected] ~]# cat Passwd.txt

User1:qwe123

User2:qwe123

User3:qwe123

User4:qwe123

[Email protected] ~]#

As shown above, create a user password file first.


[Email protected] ~]# CHPASSWD < Passwd.txt

[Email protected] ~]#

Import the password file using CHPASSWD. Since the CHPASSWD command can only be read from standard input, a redirect is required to replace the keyboard input with the file contents.


[Email protected] ~]# tail-5/etc/shadow

tcpdump:!! : 16930::::::

User1:$6$j9xvrnk7u$rkuw5pp5ez6uzhe80quf.ooudakhw0m5bi2y42qhy/wxwyxt6qsreki4qbc6qvlt9p2xdxc8czpyz0nnkdhne1 : 16945:0:99999:7:::

user2:$6$z0e6nurnu5/$KQvkQCG 2p2wkpbg8jjn4wdc6hpjewtjav8jo3zue6vgyier34ynmoxdfvz5qylb5vmmrnk7nbaotkfljrvpnt1 : 16945:0:99999:7:::

user3:$6$mdmiblt4i$cw/f/zpecrnfryyxhmr/387tnxfcrsjtwatb5v9l1uwl0hn/rt3prk.ds0dhir2ym0p469z8pgbedvckxpu7q/ : 16945:0:99999:7:::

user4:$6$wf6yg/idssc/$aQCdVqJ 9TKX7OZIJKWYGKE1DE2RZ0.JGBYRIYWT6PUXACIDKCBAARTGHC1YGHONOFA6B2B3ERZ70D5TKDJQ0G1 : 16945:0:99999:7:::

[Email protected] ~]#

To view the/etc/shadow file, the user password was imported successfully.

#/etc/shadow file Format Description:

Username:passwd:change-date:minimum-password-lifetime:maximum-password-lifetime:warning:expire:inactive: Reserve


4. Use Chown to change the copy to the user home directory and the owner and owning group of the home directory itself.

[Email protected] ~]# chown-r user4:user4/home/user4

[Email protected] ~]# ll-d/home/user4

Drwxr-xr-x. 3 User4 user4 9 16:50/home/user4


5. Use Cp-a/etc/skel/. /HOME/USER4 Copy the user Environment profile.

[Email protected] user1]# ls-a/home/user4

. ..

[Email protected] user1]# cp-a/etc/skel/. /home/user4

[Email protected] user1]# ls-a/home/user4

.  .. . bash_logout. Bash_profile. BASHRC. Mozilla

By default, users created with text import do not have an environment profile in their home directory and need to manually copy it from the/etc/skel to the home directory.


6. Verify that the user can successfully log on to the system.

[Email protected] ~]$

[email protected] ~]$ Touch file1

[Email protected] ~]$ ls-l

Total 0

-rw-rw-r--1 user4 user4 0 may 11:47 file1

[Email protected] ~]$

As shown, the user is already able to log on successfully.


This article is from the "Sunhairuirs" blog, make sure to keep this source http://sunhairuirs.blog.51cto.com/745792/1782435

Use files to bulk add users under CentOS

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.