Report this

What is the reason for this report?

How do I add to an Ubuntu system many users at once?

Posted on January 5, 2023
erel

By erel

I have a list of over 40 students, that I want to add as users to my Ubuntu server. I do not wantto do “adduser” for each of them separately. Is there an easy way to add all of them at once? For example, can I create a CSV file with usernames and passwords, and create a username for each row of the file?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
0

Accepted Answer

Hi @erel,

You can create a list of users and passwords and then use a small bash script to go through them.

Let’s assume the list looks like this:

  1. # File named studentusers
  2. user1-password1
  3. user2-password2
  4. user3-password3

Your bash script will look like:

#!/bin/bash
# Run script with SUDO
for user in $(cat /root/studentusers); do
  STUDENT=$(echo $user | awk -F"-" '{print $1}')
  STUDENT_PASSWORD=$(echo $user | awk -F"-" '{print $2}')
  useradd $STUDENT
  echo "${STUDENT}:${STUDENT_PASSWORD}" | chpasswd
done

To create the script, ssh to your Droplet, create a file called createusers.sh and put the above contents in it. Additionally, don’t forget to create the studentusers file in your /root directory.

Once that is done, just execute the following sudo bash createusers.sh

That should do the trick.

Hello @erel

What I can add as an idea is to use random passwords for the usernames and not re-using the same password or just changing the last digit, as in my experience students usually tend to abuse a system during exams and are often able to find the pattern in the username/password generation.

Regards

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.