# SSH Stuff

A collection of procedures for doing different things with SSH


# Bash script for installing SSH keys

Add each key as a new line. This script will skip duplicate keys.

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
declare -a keys=("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5KaNzN+dE9ZU83seeXSJb5gX4xRANc7eHNbQuyMXvBGSP942UpHrKgmb2Hd+f1sxH5FeLaE82p4T5Hxa+YbSEKT6VQEiktqRDmTSjiTtl2GEjaDedi0UTXB/D8nZXIbsrfkAwdQ3DgSUi7X7bdPBK43ZFE81qIPzAvl0rKhrAgJcY1HFQhlNmMzxNhPWSG9s0OqmSviC1tGooicpBLr9ibBPiQ5uc5pimYRbd6+zIbFNjNy/SqT+pWVF8J6Qp4GnHIpZZ1TMn3xluYL28zD5B5ItC8gDxfH/uaalRwe0NLEPTwWkT3ipwFaFwH8vZFCdaVU8KqUSpWH59VNHFTbKt" 
				"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC6NfziEHlyzmxiU1ftIMyx8Hh8CbgiawB+wZb02aJd0Uc3lrcm+YGVxCa2jXr9hTO/YT30u4r+X91wGRJblPwyt94OqnwbGmTT+awnwshWfI8h+SP64Y4C+O9vX6b7TCmIsiXBjY35x79QfjFjHkMIZETGrU2M0v8AgfnASPR2VZw89herazZSCcvDZzmg7onGxsNulzp0s6+ldJo27clD9lXfkYtH5oZJZ+60S+lSzcAgE/EkNCAC/XwMj/qoCH2CO/oS+PR+sittw7EwJCMkt7QkQtvTDyPaLA6i2vp7JyEx8KkdsTnZm4yRr9m/I9rk3tR4DS9oj2+MPL0sfkjhMmL0jpuQaQYQczYVNo5hhAFkiVQecXHPr4G9xz4MfUy02oWGqm0hYNuWXaD1VG8Ysg3sIJQOUL7mRpjcpXh3cqP1CYF2oyKwWJh8Mwxr8nnD+F3iFJrcYqA+tgbZ+WMHLTgROK/lGVzuDx73NI9g4K0kfXjxX4AFfLjwVJmOPzzxGnAftCGFrbjENUhiwF9wBh8t825mnlItAPXo7xImmQbjwKyR5m3HKtDbXiFCbtP9DyoocABphK7+9s7kTgJnD0hzW1GqMLlWFrxRWgQniJX/ChbEaZovaLdUncPrun3HFpqQk+Np0q+l8K0CW16PYjUkZNq7warhRZ1YcKHT2Q== ansible@blasteh.uk"
				)

mkdir -p -m 700 ~/.ssh
if [ ! -f ~/.ssh/authorized_keys ]
then
  touch ~/.ssh/authorized_keys
  chmod 600 ~/.ssh/authorized_keys
fi

for i in "${keys[@]}"
do
  search=$(grep "$i" ~/.ssh/authorized_keys)
  if [ -z "$search" ]
  then
    echo "$i" >> ~/.ssh/authorized_keys
  fi

done
echo 'Keys Added'
exit 0

# Match ssh key from logs

If you have multiple keys for single user and want to know which key was used to login.

Dump the keys for the user:

cat /home/<user>/.ssh/authorized_keys | ssh-keygen -lf - >key-list

Match the key found in the log line that contains "Accepted publickey for "

Last Updated: 2024/03/12 11:29+00:00