Question

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

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?


Submit an answer


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!

Sign In or Sign Up to Answer

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.

KFSys
Site Moderator
Site Moderator badge
January 5, 2023
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.

alexdo
Site Moderator
Site Moderator badge
January 10, 2023

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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel