Adding users on Ubuntu box via File
I was asked to make a Application server on ubuntu with 100’s of users to setup newly all i had is a .xls file in which student numbers of those users were stated.
I thought it would be as easy to write a bash script . And there would be some script doing such thing however on net i had no luck..! booh. so i started of with writing my own one.
Making such script comes out to be a nightmare if you don’t have your passwd command getting input from –stdin. so there should be some other way around after going through tons of articles i found a useful tool named “expect”
“Expect is a tool for automating interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, etc. Expect really makes this stuff trivial. Expect is also useful for testing these same applications. And by adding Tk, you can also wrap interactive applications in X11 GUIs.” — Taken from here
Installing Expect in ubuntu involves 1 command
apt-get install expect
and you’re done .
Next thing to do is to write a script that will set password from shell prompt
create a file name it setpasswd put this in it
#!/usr/bin/expect -f
spawn passwd [lindex $argv 0]
set pass [lindex $argv 1]
expect {
password: {send "$pass\r" ; exp_continue}
eof exit
}
next
chmod a+x setpasswd
then create a file name it createusers.sh
put this in it
#!/bin/bash
# Add user procedure to add users that have been stored in array!
adduser()
{
USER=$1
PASSWORD=$1
echo "Adding user $USER ..."
useradd $USER
echo "Added user $USER with pass $USER"123" "
passwd -e $USER
echo "User Password will expire as soon as he logins"
#Uncomment these two lines if your passwd command accepts input from --stdin
#echo $USER"123" | passwd --stdin "$USER"
#echo;
#Calling Except script to work!
./setpasswd $USER $USER"123"
mkdir /home/$USER
chown $USER:$USER /home/$USER
}
linecnt=0
# Replace "script" with the file name you have with you which contains list of # users
exec < script
while read line
do
F1[${linecnt}]=${line}
linecnt=`expr $linecnt + 1`
done
echo "total number of Users: $linecnt"
foonum=${#F1[@]}
echo "Initiating Auto add procedure"
for ((i=0;i<$foonum;i++)); do
adduser ${F1[${i}]}
done
Next do chmod a+x createusers.sh
then populate a file named script in this case with usernames
i.e.
user1
user2
user3
user4
then do sudo ./createusers.sh the out put that the script would generate would be some thing like this
webmasters@khi:~$ sudo ./createusers.sh
total number of Users: 4
Initiating Auto add procedure
Adding user user1 ...
Added user user1 with pass user1123
passwd -e user1
User Password will expire as soon as he logins
spawn passwd user1
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Adding user user2 ...
Added user user2 with pass user2123
passwd -e user2
User Password will expire as soon as he logins
spawn passwd user2
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Adding user user3 ...
Added user user3 with pass user3123
passwd -e user3
User Password will expire as soon as he logins
spawn passwd user3
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Adding user user4 ...
Added user user4 with pass user4123
passwd -e user4
User Password will expire as soon as he logins
spawn passwd user4
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
webmasters@khi:~$
now users can login to the system and as soon as they login
they would be asked to change there passwords
Eg:
username : user1
password : user1123
this is the way i have made it to work.




I’ve had this same problem, but I found that I can use chpasswd, which reads a file of usernames and password pairs from standard input.
There can be other workaround no doubt !
echo “passwd” | passwd –stdin username
Same thing, no expect script required.
well i did try this thing out but it didn’t works on all distros as far as i rememba
cool script – helped us save a lot of time creating our student accounts for our school – Thanks
tried adding but unable to login
should the users be given privileges to login
Thank you very much..
Very useful, thank you for sharing!
echo “passwd” | passwd –stdin username
–stdin option does not work with Ubuntu OS so i think need to use expect script.
I am trying to make script using zenity which will prompt the user for changing their password from a graphical utility. When i ll complete it, i ll share it.
My friend,
thank you very much!