Advertisement
Guest User

Untitled

a guest
Apr 1st, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #!/bin/bash
  2. # This script adds a new user to the remote server , adds it to sudoers list and enables passwordless login
  3.  
  4. read -p "Enter your Server IP:" serverIP #get server IP address
  5.  
  6. read -p "Enter a new username:" newusername #username for the new user
  7. read -s -p "Enter the password for $newusername:" password #password
  8.  
  9. printf "\nEnter the root password of the server when prompted\n"
  10.  
  11. pass="$(perl -e 'print crypt($ARGV[0],"password")' $password)" #encrypt password
  12.  
  13. ssh root@$serverIP "useradd -m -p $pass $newusername;apt-get update && apt-get install sudo;usermod -a -G sudo $newusername" #add the new user
  14.  
  15.  
  16. if [ ! -d /home/$USER/.ssh ]; then
  17. echo "Seems like you don't have an ssh key pair\nGenerating One..."
  18. ssh-keygen
  19. fi
  20.  
  21. ssh-copy-id $newusername@$serverIP # enable passwordless login (adds your public key to authorized hosts)
  22.  
  23. read -p "Do you want to disable root login?[Y/n]" -n 1 -r
  24. printf "\n"
  25. if [[ $REPLY =~ ^[Yy]$ ]];then
  26. 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
  27. fi
  28.  
  29. printf "Now you can login as $newusername to $serverIP by \n ssh $newusername@$serverIP\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement