Script to Create a New Linux User with SSH Key
Script to Create a New Linux User with SSH Key
The purpose of this page is to help you create a new Linux user. This is sometimes wanted because the default user is too commonly known (example: ubuntu user in AWS). Here are comments about the below script:- New user will have sudo ability
- New user sudo will not timeout
- New user will have the same bash configuration as your current user
- Notice there are three sections - each section can be copied and pasted as one group of commands at a time
- Be sure to update all variables before you begin - search on the word "VARIABLE" to make sure you see them all
- The below commands assume your local machine is a unix variant (Mac, Linux, etc...)
### SECTION 1 #### #VARIABLES: set variables for your current system (local) CHUBOE_CURRENT_SERVER_IP=52.55.90.70 CHUBOE_CURRENT_SERVER_USER=ubuntu CHUBOE_CURRENT_PEM=chucksteak.pem CHUBOE_USER=NewUserName #NOTE - change below as well!! #connect to remote server - this where where you will create the new user and key details ssh -i ~/.ssh/$CHUBOE_CURRENT_PEM $CHUBOE_CURRENT_SERVER_USER@$CHUBOE_CURRENT_SERVER_IP
#### SECTION 2 #### #VARIABLES: for the remote server CHUBOE_USER=NewUserName #create keys in /tmp directory #skip this step if keys have already been created. Just copy the existing $CHUBOE_USER.pub to the /tmp/ directory cd /tmp/ sudo ssh-keygen -f $CHUBOE_USER -N '' sudo mv $CHUBOE_USER $CHUBOE_USER.pem #make your current user the owner of these files so you can download .pem later via scp sudo chown $USER:$USER /tmp/$CHUBOE_USER* #create $CHUBOE_USER user and give it sudo ability without password sudo useradd -m $CHUBOE_USER sudo usermod -aG sudo $CHUBOE_USER sudo echo "$CHUBOE_USER ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers #copy over current .bashrc to the newly created user sudo rm /home/$CHUBOE_USER/.bashrc cat ~/.bashrc | sudo -u $CHUBOE_USER tee /home/$CHUBOE_USER/.bashrc sudo chsh $CHUBOE_USER -s /bin/bash #create .ssh folder and move files sudo -u $CHUBOE_USER mkdir /home/$CHUBOE_USER/.ssh sudo chmod 700 /home/$CHUBOE_USER/.ssh sudo -u $CHUBOE_USER cat /tmp/$CHUBOE_USER.pub | sudo tee --append /home/$CHUBOE_USER/.ssh/authorized_keys sudo chmod 600 /home/$CHUBOE_USER/.ssh/authorized_keys sudo chown $CHUBOE_USER:$CHUBOE_USER /home/$CHUBOE_USER/.ssh/authorized_keys # remember to remove key data from /tmp after you download it in the below script. # sudo rm /tmp/$CHUBOE_USER* #exit back to your local linux machine exit
#### SECTION 3 #### #copy remote $CHUBOE_USER.pem to your local machine #the below statement assume your remote server's user is named 'ubuntu' scp -i ~/.ssh/$CHUBOE_CURRENT_PEM ubuntu@$CHUBOE_CURRENT_SERVER_IP:/tmp/$CHUBOE_USER.pem ~/.ssh/. chmod 400 ~/.ssh/$CHUBOE_USER.pem #download the $CHUBOE_USER.pub just in case you want to use it in another server scp -i ~/.ssh/$CHUBOE_CURRENT_PEM ubuntu@$CHUBOE_CURRENT_SERVER_IP:/tmp/$CHUBOE_USER.pub ~/Downloads/. #connect to your remote server with your newly generated key ssh -i ~/.ssh/$CHUBOE_USER.pem $CHUBOE_USER@$CHUBOE_CURRENT_SERVER_IP echo "You are now connected as your new user on your remote machine"