Skip to content

Instantly share code, notes, and snippets.

@nf3
Last active September 15, 2020 16:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nf3/37fbf8916208498b95ffbf7fabfcb26f to your computer and use it in GitHub Desktop.
Save nf3/37fbf8916208498b95ffbf7fabfcb26f to your computer and use it in GitHub Desktop.
#!/bin/bash
#run with:
# wget --no-check-certificate https://gist.githubusercontent.com/nf3/37fbf8916208498b95ffbf7fabfcb26f/raw/addme_ssh.sh
# chmod +x ./addme_ssh.sh
# sudo bash -x addme_ssh.sh
# or
# sudo bash -x echo [PASSWORD] | addme_ssh.sh but this will put the password on the bashhistory
read -s -p "Password: " PASSWORD
USERNAME=nproctor
PUBKEYURL=https://raw.githubusercontent.com/nf3/vagrant-cent-salt/master/vagrant.pub
if [[ -n "$PASSWORD" ]]; then
yes | useradd -p $(openssl passwd -1 $PASSWORD) $USERNAME #creates the user with the password passed in
else
yes | useradd $USERNAME #creates the user with all y answers
chage -d 0 $USERNAME # this expires the password so that the 1st login will force it to be changed
fi
python3 -mplatform | grep -qi CentOS && usermod -aG wheel $USERNAME
python3 -mplatform | grep -qi Ubuntu && gpasswd -a $USERNAME sudo #add the user to sudo group in Ubuntu
python3 -mplatform | grep -qi Debian && gpasswd -a $USERNAME sudo #add the user to sudo group in Debian
mkdir /home/$USERNAME
mkdir /home/$USERNAME/.ssh
wget --no-check-certificate --output-document=/home/$USERNAME/.ssh/authorized_keys $PUBKEYURL
chmod 700 /home/$USERNAME/.ssh
chmod 600 /home/$USERNAME/.ssh/authorized_keys
chown -R $USERNAME:$USERNAME /home/$USERNAME
chsh -s /bin/bash $USERNAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment