(LINUX) General User Create Script

Script to create user and add their public keys. How to use the script:

  1. vim generalusercreate.sh

  2. copy the below given script

  3. In vim editor, press colon(:) and type set paste

  4. Press i to enter insert mode in set paste command

  5. paste the script

  6. Press escape and colon(:), type set nopaste

  7. Again press escape and type wq.

  8. This will close the editor, now type: chmod +x generalusercreate.sh

  9. Script is executable now, run it by ./generalusercreate.sh

Parameters: This script asks following input:

  1. Username: Ensure to give exact convention of username which will be used by user to login, this script does not convert full names to conventions.

  2. Choice to add public ssh keys: If you have user's public ssh keys, enter y and paste them without word wrap. If you don't want to add, simply press n

  3. Choice to add more users: If you have a list of multiple users to add, press y and continue same process till list ends, if not, simply press n

action(){ 
        echo provide username to create user 
        read name 
        sudo adduser --disabled-password --disabled-login $name 
        sudo mkdir /home/$name/.ssh 
        sudo chmod -c 750 /home/$name/.ssh 
        sudo touch /home/$name/.ssh/authorized_keys 
        sudo chown -cR $name. /home/$name/.ssh 
        echo $name created, would you like to add ssh keys y/n? 
        read ans 
        echo your choice is $ans 
        var2="y" 
        if [ "$ans" = "y" ]; then 
                echo write public key to add in user $name 
                read sshkey 
                sudo chown -cR ubuntu. /home/$name/.ssh 
                destdir=/home/$name/.ssh/authorized_keys 
                echo "$sshkey" > "$destdir" 
                echo key added 
                sudo chown -cR $name. /home/$name/.ssh 
        else : 
        fi
} 

choice="y" 
while [ "$choice" = "y" ]; do 
           action 
           echo $name created, want to create more y/n? 
           read choice 
done 
Previous
Previous

(LINUX)User Account Expiry

Next
Next

(LINUX)Copy Files: Server from/to Remote