SSH Reference

Some handy commands to manage multiple SSH connections, securely, with minimum toil in a lab or recently acquired infrastructure.

Bad AI SSH Cartoon

Generate Keys on host "A"

ssh-keygen -t ed25519

Follow prompts (path, passphrase); you must set a passphrase for further steps.

Key is generated in the path specified with the passphrase.

Copy ID to each remote hosts B,C,D, etc

Easy way:

ssh-copy-id username@remote_host

Hard way (Windows host):

Make sure that the .ssh directory exists in your server's user account home folder and

ssh username@domain1@contoso.com
mkdir C:\Users\username\.ssh\

Use scp to copy the public key file generated previously on your client to the authorized_keys file on your server

scp C:\Users\username\.ssh\id_ed25519.pub user1@domain1@contoso.com:C:\Users\username\.ssh\authorized_keys

Hard way (Linux host)

cat~/.ssh/id_rsa.pub |sshusername@remote_host"mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"

Protip: you can also copy this to the server A so that you can open sessions to itself without passwords.

start the ssh-agent in the background

eval "$(ssh-agent -s)"
> Agent pid 59566
ssh-add ~/.ssh/id_ed25519

Auto-load SSH Agent

In bashrc:

SSH_ENV="$HOME/.ssh/environment" 

function start_agent { 
     echo "Initialising new SSH agent..." 
     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}" 
     echo succeeded 
     chmod 600 "${SSH_ENV}" 
     . "${SSH_ENV}" > /dev/null 
     /usr/bin/ssh-add; 
}

Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then 
     . "${SSH_ENV}" > /dev/null 
     #ps ${SSH_AGENT_PID} doesn't work under cywgin 
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || { 
         start_agent; 
     } 
else 
     start_agent; 
fi

Next Post