Skip to content

Instantly share code, notes, and snippets.

@the-moog
Last active February 9, 2024 18:43
Show Gist options
  • Save the-moog/5bf177e62b5e550b442beca802da453b to your computer and use it in GitHub Desktop.
Save the-moog/5bf177e62b5e550b442beca802da453b to your computer and use it in GitHub Desktop.
Got fed up trying to work out which .ssh key maps to which host hence: alias (or function) lskeys
alias lskeys='for kf in ~/.ssh/*.priv; do read -rst 2 size fingerprint userid txt <<<$(ssh-keygen -l -f ${kf});\
printf "KEYFILE: %-50s - BITS: %-6d FP: %s, USER: '%s', INFO: '%s'\n" "$(basename ${kf})" "${size}" "${fingerprint}" "${userid}" "${txt}"; done'
#!/bin/bash
#File: lskeys.bashsrc
# usage:
# source <path_to>/lskeys.bashsrc
# lskeys
# See the 'simpler' version done as a bash alias
# Note this assumes you name all your private keys <whatever>.priv
# as per the GLOB variable
# This works as I use <user>@<domain>.<fqn>.<pub|priv> for my key files
# You may be able to use the 'file' command as this seems to be
# able to tell what a private key file looks like, but for me not all formats.
# You can try that by setting USE_EXT to false.
USE_EXT=true
if ${USE_EXT}; then
GLOB=~/.ssh/*.priv
else
GLOB=~/.ssh/*
fi
function ispriv () {
${USE_EXT} && return 0
# b = brief
# L = Force dereference (or symlinks report just that)
file -bL $1 | grep -q "private key"
return $?
}
function lskeys () {
for kf in ${GLOB}; do
ispriv ${kf} || continue
read -rst 2 size fingerprint userid txt <<< "$(ssh-keygen -l -f ${kf})"
if [ "${userid}" == 'no' ] && [[ "${txt}" =~ comment ]]; then
# Handle where there is no userid in the private key
txt="${userid} ${txt}"
userid="<none>"
fi
printf "KEYFILE: %-50s - BITS: %6d, FP: %s, USER: '%s', INFO: '%s'\n"\
"$(basename ${kf})" "${size}" "${fingerprint}" "${userid}" "${txt}"
done
}
@the-moog
Copy link
Author

the-moog commented Feb 9, 2024

Two versions:
A simple one as a bash alias and a more complicated one as a sourcable bash script, which deals with edge cases and is more adaptable.

@the-moog
Copy link
Author

the-moog commented Feb 9, 2024

I suggest using ssh-keygen like this:

cd .ssh
ssh-keygen -t ed25519 -f username@target.system.domain -C "Comment string, e.g. local_user@local.domain"
mv username@target.system.domain username@target.system.domain.priv

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