newusers Command in Linux with Examples

«
»

This tutorial explains Linux “newusers” command, options and its usage with examples.

newusers – update and create new users in batch

Description :

advertisement

newusers reads a file of user name and cleartext password pairs and uses this information to update a group of existing users or to create new users. Each line is in the same format as the standard password file with the following exceptions :

pw_passwd
This field will be encrypted and used as the new value of the encrypted password.
pw_age
This field will be ignored for shadow passwords if the user already exists.
pw_gid
This field may be the name of an existing group, in which case the named user will be added as a member. If a non-existent numerical group is given, a new group will be created having this number.
pw_dir
This field will be checked for existence as a directory and a new directory with the same name will be created if it does not already exist. The ownership of the directory will be set to be that of the user being created or updated.

This command is intended to be used in a large system environment where many accounts are updated at a single time.

Usage :

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

newusers <filename>

NOTE:

Content of the file <filename> contains the list of users, one per line. Following is the syntax of the parameters to be provided for each user.

<Username>:<Password>:<UID>:<GID>:<User Info>:<Home Dir>:<Default Shell>

Username: User login name
Password: User password
UID: User Identifier
GID: Group Identifier for user’s primary group
User Info: User information like full name, contact information etc.
Home Dir: User’s home directory
Default Shell: User’s default shell

advertisement

Example:

Creating users from a file containing the details of new users as per above given format :

# cat /root/users.txt
tester1:test1@123:600:1530:Test User1,testuser1@abc.com:/home/tester1:/bin/bash
tester2:test2@123:601:1529:::/bin/bash
tester3:test3@123:::::
tester4:test4@123::::/home/tester4:/bin/tsh

Let’s execute the newusers command by providing file /root/users.txt as input.

# newusers /root/users.txt

Testing whether users are created or not :

advertisement

For each new user a new line is inserted in the /etc/passwd file as shown below.

# cat /etc/passwd | grep tester
tester1:x:600:1530:Test User1,testuser1@abc.com:/home/tester1:/bin/bash
tester2:x:601:1529:::/bin/bash
tester3:x:65537:65538:::
tester4:x:65538:65539::/home/tester4:/bin/tsh

The primary group name for each user is stored in the /etc/group file.

# cat /etc/group | grep tester
devel:x:1529:tester2
tester:x:1530:tester1
tester3:x:65538:tester3
tester4:x:65539:tester4

‘x’ in the password field indicates that the password is shadowed and stored in the /etc/shadow file as shown below.

advertisement
# cat /etc/shadow | grep tester
tester1:$1$NK0LH/kL$.gy3tBXHUlsapiHPlPKs21:15607:0:99999:7:::
tester2:$1$NK0LH/kL$08Y4VdiOY4TTms.UCjjoE1:15607:0:99999:7:::
tester3:$1$NK0LH/kL$OkaRrrsm51tW3j5yheD7q1:15607:0:99999:7:::
tester4:$1$NK0LH/kL$pU2FIZEdGfYcYlMSCN8sI1:15607:0:99999:7:::

Home folders are created for only two users, tester1 and tester2. For other users, home folder was not specified in the /root/users.txt file.

# ls -ld /home/tester*
drwx------ 2 testuser2 tester  4096 Sep 24 14:05 /home/tester1
drwx------ 2 tester4   tester4 4096 Sep 24 14:05 /home/tester4

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.