Monday, April 22, 2013

Create Multiple User Accounts on Linux Systems

adduser is a system command on FreeBSD that allows root to create new user accounts in interactive or batch modes.

useradd is a system command on Linux systems that enable root to create a new user account; to create multiple user accounts on Linux systems in batch mode, we may use the newusers system command.

Each input line for newusers is in the same format as the standard password file with some exceptions. To use command newusers, we need to specify the password for user or leave it blank as for null password. There are times that we need to create multiple user accounts with random passwords. The following bash script allows us to accomplish this with few more options:

#!/bin/bash
function mkpw()
{
    head /dev/urandom | uuencode -m - | sed -n 2p | cut -c1-10;
}

SAVEFILE=newAccounts
chmod 0400 $SAVEFILE
IFS=':'
while  read USER PASSWORD uUID GID FULLNAME HOMEDIR SHELL
do
    PASSWORD=${PASSWORD:-`mkpw`}
    GID=${GID:-"users"}
    SHELL=${SHELL:-"/bin/bash"}

    echo "Adding user '$USER, $FULLNAME'"
    useradd -m $USER -p `mkpasswd -p $PASSWORD` \
        -c "$FULLNAME" -s $SHELL -g $GID
    echo "$USER:$PASSWORD" >> $SAVEFILE
done


where mkpasswd is the command that encrypts the given password. The input file for this script is in the same format as for command newusers. The output file, named newAccounts, contains all newly created account ids and passwords in plain text; you need to take proper action on it.

No comments:

Post a Comment