Table of Contents

Batch Add Samba Users

First, this is a kludge, but it works. I've modified http://samba.2283325.n4.nabble.com/smbpasswd-batch-mode-td2421770.html to do things a little different.

create a user data file

This contains 7 fields separated by a colon (so, your password can not contain colons). The fields are

  1. username
  2. password
  3. uid (empty means let sys generate)
  4. gid (Primary Group ID, empty means create new group with username)
  5. gecos (the thing few people use)
  6. homedir
  7. shell (we're setting to /bin/false so they can not log in from cli)
userdata
mary:maryspw::100::/home/users/mary:/bin/false
john:johnspw::100:/home/users/john:/bin/false

The Script

There are many ways of doing it. The following reads the data file, one line at a time. Each line is formatted exactly as the newusers command expects, so we just send it directly to the newusers command

smbpasswd does not have a batch mode. It always requires you to enter the password from the cli, twice. So, we echo the password twice, then run smbpasswd.

The last command dumps the binary samba database. If you have a system that still uses /etc/samba/smbpasswd to store credentials, it would likely fail.

importusers.sh
#!/bin/bash
datafile=userdata
line_count=`cat $datafile | wc -l`
 
mkdir -p /home/users
 
for data in `seq $line_count`
do
  read data
  # create the user
  ( echo $data ) | newusers
  #/usr/sbin/adduser -m -s /bin/false -p $password $username
  # for smbpasswd
  username=`echo $data | awk -F":" '{print $1}'`
  password=`echo $data | awk -F":" '{print $2}'`
  (echo $password; echo $password) | /usr/bin/smbpasswd -s -a $username
  #/bin/chmod 700 /home/$username
done < "$datafile"
# dump the samba database
pdbedit -L