Skip to content

Instantly share code, notes, and snippets.

@navin-mohan
Last active July 3, 2018 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save navin-mohan/1806e66fd8f9f83afc91493a8edc343f to your computer and use it in GitHub Desktop.
Save navin-mohan/1806e66fd8f9f83afc91493a8edc343f to your computer and use it in GitHub Desktop.
Shell script to add new user to a remote debian server and set up password less login
#!/bin/bash
# This script adds a new user to the remote server , adds it to sudoers list and enables passwordless login
# follow on my blog https://www.coderew.com/computers/ssh-add-user-remotely-script/
read -p "Enter your Server IP:" serverIP #get server IP address
read -p "Enter a new username:" newusername #username for the new user
read -s -p "Enter the password for $newusername:" password #password
printf "\nEnter the root password of the server when prompted\n"
pass="$(perl -e 'print crypt($ARGV[0],"password")' $password)" #encrypt password
ssh root@$serverIP "useradd -m -p $pass $newusername;apt-get update && apt-get install sudo;usermod -a -G sudo $newusername" #add the new user
if [ ! -d /home/$USER/.ssh ]; then
echo "Seems like you don't have an ssh key pair\nGenerating One..."
ssh-keygen
fi
ssh-copy-id $newusername@$serverIP # enable passwordless login (adds your public key to authorized hosts)
read -p "Do you want to disable root login?[Y/n]" -n 1 -r
printf "\n"
if [[ $REPLY =~ ^[Yy]$ ]];then
ssh root@$serverIP "cat /etc/ssh/sshd_config > /etc/ssh/sshd_config.bak;sed 's/#.*PermitRootLogin*/PermitRootLogin no/' /etc/ssh/sshd_config > /etc/ssh/sshd_config;systemctl restart ssh" #disable root login
fi
printf "Now you can login as $newusername to $serverIP by \n ssh $newusername@$serverIP\n"
@gunnicom
Copy link

gunnicom commented Jul 3, 2018

Just a small question. Is there a reason you use "cat /etc/ssh/sshd_config > /etc/ssh/sshd_config.bak" and not "cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment