LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-02-2009, 10:49 AM   #1
kchakrak
LQ Newbie
 
Registered: Sep 2008
Location: Kent, UK
Distribution: Ubuntu
Posts: 11

Rep: Reputation: 0
Smile Adding users via a script


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?

Using Ubuntu 8.04 server.
 
Old 09-02-2009, 11:07 AM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Try

man useradd

I don't think it will read users from a file, but it shouldn't be too hard to do what you want w/ a simple script and that command.

HTH

Forrest
 
Old 09-02-2009, 11:08 AM   #3
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
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.

Last edited by MS3FGX; 09-02-2009 at 11:10 AM.
 
Old 09-02-2009, 11:45 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Perl script from the following LQ post: http://www.linuxquestions.org/questi...g-ftp-740675/?

The example script was designed to take a space delimited text file.
 
Old 09-03-2009, 03:27 AM   #5
kchakrak
LQ Newbie
 
Registered: Sep 2008
Location: Kent, UK
Distribution: Ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
Wow, thanks guys. Much appreciated. I'll get to trying various methods out and finding out what suits me best.

Thanks again,
 
Old 09-03-2009, 05:49 AM   #6
netrabhatta
LQ Newbie
 
Registered: Jul 2008
Posts: 1

Rep: Reputation: 0
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.

1. Create File
USERNAME:PASSWORD:UID:GID:COMMENT:HOMEDIR:LOGINSHELL

# vim userlists
netra:redhat:1052:100:I m netra:/home/netra:/bin/bash

# newusers userlists

I think this would help.
 
Old 10-26-2009, 08:48 PM   #7
GaHillBilly
LQ Newbie
 
Registered: Sep 2009
Location: Georgia
Distribution: Slackware & Ubuntu
Posts: 9

Rep: Reputation: 0
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.

GaHillBilly

==============================================================
#!/bin/sh

# 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

# *****************************************************************
# Add matching Samba users.

useradd -m -p $encpass -u $uin -g $group -s $ushell -d $dir $username
[ $? -eq 0 ] && echo "User $username has been added to the system!" \
|| echo "Failed to add user $username!" \


if [ $samba -eq 1 ] ; then
(echo $plainpass; echo $plainpass) | smbpasswd -as $username
echo "User $username added to Samba"
else
echo "Not adding user $username to Samba"
fi
echo ""
echo "**************************************"

done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding new users via Shell script coolfrog Linux - General 10 12-05-2010 10:47 AM
script for adding users information in ldap database aravind1024004 Linux - Server 1 07-16-2008 06:39 AM
Script for adding users BlueStag Linux - General 3 03-14-2006 09:40 PM
bash script for adding multiple users pilipk01 Linux - Newbie 4 01-12-2004 10:05 PM
Script that would automate adding users zyft02 Linux - Newbie 4 02-25-2002 11:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration