Skip to content
Greg Flynn edited this page Jul 14, 2020 · 5 revisions

SSH keygen

ssh-keygen -t rsa -b 4096 -C "gregf36665@gmail.com"

SSH agent autostart

This script should be added to a .bashrc or .bash_profile to automatically add ssh keys to the ssh agent. This means that any passwords only need to be typed in on the first launch of a bash session.

env=~/.ssh/agent.env
keyName='gregf_id_rsa'

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add ~/.ssh/$keyName
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add ~/.ssh/$keyName
else
	echo "At least 1 key already added"
	echo `ssh-add -l`
fi

Delete a key

If it is desired to remove a key from the ssh-agent use the following command:

ssh-add -D

Note that this will remove ALL keys

SSH connection config file

The config file allows for unique settings to be specified for specific hosts. Note many more features can be specified. The most common ones to use would be the key and username

Host hostname
    IdentityFile ~/.ssh/id_rsa
    Port 22
    User username

Clone this wiki locally