Batch create new users on Linux
Sunday, October 4th, 2015
A while ago I had to create many new users on a Linux machine. Since I’m lazy, I opted to automate this process. The newusers command combined with pwgen (to generate new passwords) was the solution.
First I installed pwgen, a utility to automatically generate passwords:
$ sudo apt-get install pwgen
I created a file with the new user names to create.
$ cat newusers.txt jjohnson ppeterson ccalrson
A simple shell one-liner generates a new file from this in the right format for the newusers tool:
$ for USER in $(cat newusers.txt); do echo "$USER:$(pwgen 12 -n1)::::/home/$USER:/bin/bash" >> newusers.created.txt; done
Finally, we create the new users:
$ sudo newusers newusers.created.txt
The newusers.created.txt file was handed over to the person in charge of notifying the users about their new account.