James Fator

Script to enable and configure password-less SSH logins

11 March 2015

This isn’t anything new, there are plenty of sites out there that show you how to do this. Those sites show you step by step what to do, which is great and all, but I want a script, dammit!

#! /bin/bash

if [ $# -ne 5 ]
 then
   echo "Usage: register_host.sh <NAME> <DESCRIPTION> <KEYNAME> <USER> <HOST>"
   exit 1
fi

NAME=$1
DESC=$2
KEYNAME=$3
USERNAME=$4
HOST=$5

ssh-keygen -f ~/.ssh/$KEYNAME -C "$KEYNAME"
cat ~/.ssh/$KEYNAME.pub | ssh $USERNAME@$HOST 'cat >> ~/.ssh/authorized_keys'

echo "" >> ~/.ssh/config
echo "# $DESC" >> ~/.ssh/config
echo "Host $NAME" >> ~/.ssh/config
echo "Hostname $HOST" >> ~/.ssh/config
echo "IdentityFile ~/.ssh/$KEYNAME" >> ~/.ssh/config
echo "user $USERNAME" >> ~/.ssh/config
echo "Port 22" >> ~/.ssh/config

Example usage:

./register_host.sh rbpi "Raspberry Pi" rbpi pi 10.0.1.25

There will be a few prompts that you will need to respond to while it’s running, but when it completes, you’ll be able to simply type ssh rbpi and automatically connect

It may look sloppy to some people… in my defense, I’m not a bash guy.