(LINUX) General User Create Script
Script to create user and add their public keys. How to use the script:
vim generalusercreate.sh
copy the below given script
In vim editor, press colon(:) and type set paste
Press i to enter insert mode in set paste command
paste the script
Press escape and colon(:), type set nopaste
Again press escape and type wq.
This will close the editor, now type: chmod +x generalusercreate.sh
Script is executable now, run it by ./generalusercreate.sh
Parameters: This script asks following input:
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.
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
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