#!/bin/bash ############################################################################### # ALLOW-SSH SCRIPT # # Author: Mikael Levén # ############################################################################### # What this script basically does: # # 1. Reads your SSH key # # 2. Logins in to the remote server # # 3. Creates a new user using your current username # # 4. Creates the /home/{user}/.ssh/ folder # # 5. Adds your RSA key to the 'authorized_keys' # # 6. Sets required permissions # ############################################################################### USER=$(id -u -n) if [ "$1" = "" ]; then echo "" echo "USAGE: AllowSSH [{username}@]{host}" echo "" echo "This script will SSH into the server and add your SSH RSA key to the 'authorized keys' and thus enable passwordless SSH login automatically." echo "" echo "The username ('{username}@') is optional and if omitted the current username '$USER' will be used." echo "" echo "Some examples:" echo "AllowSSH root@192.168.0.1" echo "AllowSSH root@myhost" echo "AllowSSH 192.168.0.1" echo "" else SERVER=$1 KEY=$(cat ~/.ssh/id_rsa.pub) ssh $SERVER "adduser -q --disabled-password $USER; mkdir -p /home/$USER/.ssh/; touch /home/$USER/.ssh/authorized_keys; chmod 700 /home/$USER/.ssh; chmod 640 /home/$USER/.ssh/authorized_keys; echo $KEY >> /home/$USER/.ssh/authorized_keys; chown $USER:$USER /home/$USER/.ssh; chown $USER:$USER /home/$USER/.ssh/authorized_keys;" echo "Key for local user '$USER' has been added to user '$USER' at the remote host '$SERVER'" fi