How to create number of users and passwords automatically

If you are a network administrator, and need to register a lot of users at once. It takes a lot of time to register one by one. Instead, if you have a list of users, you can register all users at once using an autopasswd command. However, the autopasswd command does not work properly. I checked different version from different distributions, but all of them does not work. It shows errors, so it needs to be modified. I do not know why the useless comannd is widely distributed.

Modify autopasswd

  • Install expect-5.38.0-88.i386.rpm from Red Hat CD or somewhere else.

  • # rpm -ihv expect-5.38.0-88.i386.rpm
    
  • As I said, autopasswd command included in expect package does not work properly. In such case, you need to add "sleep" command in the middle. Edit /usr/bin/autopasswd as below,

  • #!/usr/bin/expect -f
    # wrapper to make passwd(1) be non-interactive
    # username is passed as 1st arg, passwd as 2nd
    
    set password [lindex $argv 1]
    spawn passwd [lindex $argv 0]
    expect {
            -re "password:" {sleep 1; send "$password\r"; exp_continue}
            -re "password:" {sleep 1; send "$password\r";}
    }
    

    Create your own useradd and userdel command

  • create a custom useradd and userdel command to automate necessary arrangement. It creates a user, set a password, create a personal web page directory and copy a disk quota from a model user.

  • /root/bin/myuseradd
    #!/bin/bash
    
    GROUPID=100
    
    # for disk quota. Apply same value of the disk quota to one group
    MODELUSER=quotamodel
    
    useradd $1 -g $GROUPID
    autopasswd $1 $2
    
    # to create a personal web page directory
    mkdir /home/$1/public_html
    chmod 755 /home/$1 -R
    chown $1.$GROUP /home/$1 -R
    
    # Set a user quota to one group (group id = 100)
    edquota -p $MODELUSER `awk -F: '$3 > 499 && $4==100{print $1}' /etc/passwd`
    
  • It deletes a user account and a user home directory.

  • /root/bin/myuserdel
    #!/bin/bash
    # -r option removes both the home directory and the mail spool
    # rm -fr /home/$1
    
    userdel -r $1
    

    Script file to create a number of users

    Using above the command, you can create a number of users automatically as below,
    myuseradd user1 user1
    myuseradd user2 user2
    myuseradd user3 user3
    ...
    

    Back
    Google
    Web www.grape-info.com