So, you’re running a Nextcloud instance and want to batch-create users like a proper sysadmin? Here’s a pure shell-based solution to create users from a CSV file, assign them to groups (even multiple ones), and feel like a boss while doing it.
π¦ Prerequisites
- SSH access to your Nextcloud server
- Command-line access to
occ
(located in the root of your Nextcloud install, e.g./var/www/html/nextcloud
) - Your CSV file ready to roll
π CSV File Format
No headers, just pure data. Format:
username,password,Display Name,Group1;Group2;Group3
Example:
next001,secret123,Max Mustermann,Vorstand;Mitglieder
next002,test456,Lisa Beispiel,Trainer
next003,pass789,Peter Test,Trainer;Mitglieder
π οΈ The Bash Script: create_users_with_groups.sh
<#!/bin/bash
CSV_FILE="benutzer.csv" # Path to your CSV file
cd /var/www/html/nextcloud || exit 1
# Read file line by line
while IFS=',' read -r username password displayname groups_raw; do
# Skip empty or malformed lines
if [[ -z "$username" || -z "$password" || -z "$displayname" ]]; then
echo "β οΈ Skipping invalid or empty line: $username, $displayname"
continue
fi
echo "βΆοΈ Creating user: $username ($displayname)"
# Export password into environment
export OC_PASS="$password"
# Use -E to preserve environment for www-data
sudo -E -u www-data php occ user:add --password-from-env "$username" --display-name "$displayname"
USER_EXISTS=$?
if [ $USER_EXISTS -ne 0 ]; then
echo "β Failed to create user '$username'. Skipping group assignment."
continue
fi
# Split group list on semicolon (;) and trim whitespace
IFS=';' read -ra groups <<< "$groups_raw" for group in "${groups[@]}"; do group=$(echo "$group" | xargs) # Trim spaces if [ -n "$group" ]; then echo " β Adding $username to group '$group'" # Create group if not already existing (ignore stderr) sudo -u www-data php occ group:add "$group" 2>/dev/null
# Add user to group
sudo -u www-data php occ group:adduser "$group" "$username"
fi
done
done < "$CSV_FILE"/code>
βοΈ Usage
chmod +x create_users_with_groups.sh
./create_users_with_groups.sh
Make sure your benutzer.csv
file is in the same directory or update the path accordingly.
π Notes
- Passwords are passed securely using environment variables β not via command line or stdin
- Group names are auto-created if they donβt already exist
- Semicolon (
;
) separates multiple groups per user
π§ Bonus Tip
Want to go full automation mode? Integrate this with user onboarding scripts or provision your users via LDAP and only use this for custom or temporary accounts.
π«‘ Sysadmin Glory Unlocked
Thatβs it β your users are now live, grouped, and ready to Nextcloud like pros. Want to set up Group Folders and apply permission rules? Ping me β Iβll nerd that out too.