Monday, June 10, 2019

How to Create Multiple User Accounts in Linux




Method-1: "newusers" linux command

, First create a user list with passwords, id, groupid, firstname , lastname, home directory, shell
$ nano userslist.txt
user1:user1pass:1001:1001:User1 Lastname:/home/user1:/bin/bash
user2:user2pass:1002:1002:User2 Lastname:/home/user2:/bin/bash
user3:user3pass:1003:1003:User3 Lastname:/home/user3:/bin/bash
user4:user4pass:1004:1004:User4 Lastname:/home/user4:/bin/bash
user5:user5pass:1005:1005:User5 Lastname:/home/user5:/bin/bash

, Save the file and set the required permissions on it.
$ sudo chmod 0600 users.txt

, Now run the newusers command with the input file to add the above user accounts at once.
$ sudo newusers users.txt


Method-2: shell script to create users with passwords 

, First create a user list file
$ nano userlist.txt
user1
user2
user3
user4
user5

, create shell script that add users with passwords "username@123"

$ nano user-add.sh
#!/bin/sh
for user in `more userlist.txt`
do
echo "$user"
useradd $user -m -s /bin/bash
echo "$user:$user@123" | chpasswd
chage -d 0 $user
done

, Set an executable permission to user-add.sh file.
$ chmod +x user-add.sh

, Finally run the script to achieve this.
$ sudo bash user-add.sh

, Verify user creation
$ grep "user1\|user2\|user3" /etc/passwd


No comments:

Post a Comment