Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is it possible to design a matrix (such as a csv) that the users can fill in with all their appropriate details and then can be directly imported? just to make adding endless numbers of users easy.
If this is not possible then is there any way of making adding large numbers of users a quicker task?
I don't know if there is an existing script or program to do what you want, but you could certainly create a Bash script that would feed values from a .csv file into "useradd".
I checked on a Google a bit and found that this is not an uncommon request. It seems there are some scripts out there already that do what you want.
Users can be created by script. First u need to create file containing the list of users with password, uid, gid, home directory, login shell.
Then command newusers is used to read that file and take action as u need.
More ways to create batches of Linux & Samba users by script. . . .
1. The Webmin system management tool allows you to import batches of users, either to create users or to modify existing users. Fields are colon separated. Webmin also has some sort of tool for syncing Linux and Samba users, but I haven't tried it. (In years past, Webmin has hosed my Samba config. It may be fine now, but I'm not inclined to try it.)
2. Also, I have a Bash script, that I've cobbled together from bits and pieces I found, which 'works for me' to simultaneously create Unix and Samba. It creates MD5 encrypted passwords which seems to allow long passwords, rather than the 8 character passwords truncated by "crypt". There may be mistakes or other problems, so I would appreciate feedback from you shell wizards out there. I know there are shorter ways to do it, but it seems to work OK and I can read it, so I'm not real interested in more 'elegant' solutions. But, if there are problems, I'd very much like to correct them.
# Script to add users to Linux and Samba
echo " "
echo "**************************************"
echo " This script takes a list of usernames, groups, and plaintext passwords from"
echo " a file named 'userlist' and located in the same directory as the script, and"
echo " from this file creates Linux and Samba users."
echo ""
echo " It can ONLY be run by root, and will fail if another user attempts to run it."
echo ""
echo " Values in 'userlist' must be separated by a single space character; also, the"
echo " last record (line) in that file must be empty. If it's not, the last user in"
echo " the list will not be added. Note that the script prints only the 1st 4 characters of"
echo " the user plaintext password, to avoid leaving the full password in Bash history. "
echo " For more security, simply comment that line out of the script."
echo ""
echo " The REQUIRED data order is: name group password UIN directory shell samba "
echo " Three lines of SAMPLE data: "
echo "******************************************"
echo "user1 smbgrp user1pass 501 /home/smbusers/user1 /bin/sh 1"
echo "user2 lnxgrp user2pass 521 /home/lnxusers/user2 /bin/sh 0"
echo "user3 lnxgrp user3pass 522 /home/lnxusers/user3 /bin/false 0"
echo "" # empty line!
echo "******************************************"
echo " where samba is '0' or '1'. If 'samba' is '1', then a Samba user and"
echo " password will also be created. "
echo ""
echo "**************************************"
echo ""
# Give ROOT user a chance to read info and make sure he/she wants to run script.
# Comment the line below, and uncomment the next line when you get tired of the pause.
read -p "yes or no?" response
# response="yes"
if echo $response | grep "y" >/dev/null
then
echo ""
echo "Will continue"
echo ""
else
echo ""
echo "Bailing out!"
echo ""
exit 0
fi
# Check user -- must be root!
if [ $(id -u) -ne 0 ]; then
echo "**************************************"
echo "Only root may add a user to the system"
echo "**************************************"
exit 2
fi
# Users to be created.
new_users="userlist"
echo Reading users from file $new_users
echo ""
# *****************************************************************
# Loop through user data
echo "*************************************"
cat ${new_users} | \
while read username group plainpass uin dir ushell samba; do
echo "**************************************"
echo ""
echo "User: $username Group: $group UID: $uin **"
echo "Pass: $plainpass " | head -c10
echo ""
# make random salt for openssl
rand=$(cat /dev/urandom | tr -dc A-Za-z0-9 | head -c8)
# encrypt via openssl using MD5 -- allows long passphrases, but no spaces in this script.
encpass=$(openssl passwd -1 -salt $rand $plainpass)
echo "Home: $dir Shell: $ushell Encrypted password: $encpass Samba?: $samba ***"
echo " "
# *****************************************************************
# Add groups as needed.
#
egrep "^$group" /etc/group >/dev/null
if [ $? -eq 0 ]; then
echo "Group $group exists, and does not need to be created!"
else
groupadd -g $uin $group
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.