Configure SSH Agent Forwarding in Ubuntu
This guide explains how to configure your Ubuntu Linux SSH client to automatically forward your local SSH agent to remote servers. By enabling SSH agent forwarding, you can securely use your local SSH keys on remote hosts to authenticate further connections (such as accessing private Git repositories or hopping to other servers) without copying your private keys to the remote machines.
Step 1: Ensure Your SSH Agent is Running and Keys are Added
Before configuring forwarding, your local SSH agent must be active and holding your SSH keys.
Open your terminal on Ubuntu.
Verify the SSH agent is running and load your SSH keys by running:
eval $(ssh-agent -s) ssh-add ~/.ssh/id_rsa(Replace
~/.ssh/id_rsawith the path to your specific private key if it differs).
Step 2: Configure SSH Agent Forwarding
You can configure agent forwarding temporarily for a single session, or permanently for specific hosts or all hosts.
Option A: Permanent Configuration via SSH Config (Recommended)
To automatically forward the agent without typing extra commands every time, edit your local SSH configuration file.
Open or create your user-specific SSH configuration file using a text editor:
nano ~/.ssh/configTo enable forwarding for all remote hosts, add the following block:
Host * ForwardAgent yesTo enable forwarding only for a specific remote host (more secure), add its details like this:
Host target-server-alias HostName server.example.com User ubuntu ForwardAgent yesSave the file and exit the editor (in nano, press
Ctrl+O,Enter, thenCtrl+X).Secure the configuration file permissions:
chmod 600 ~/.ssh/config
Option B: Temporary Forwarding via Command Line
If you only want to forward your agent for a single session, use the
-A flag when connecting:
ssh -A user@remote-hostStep 3: Verify the Configuration
To confirm that the SSH agent has been successfully forwarded to the remote host:
Connect to your remote server:
ssh user@remote-hostOnce logged into the remote machine, check if the SSH authentication socket environment variable is present:
echo $SSH_AUTH_SOCKIf configured correctly, this command will output a path to a socket file (e.g.,
/tmp/ssh-XXXXXX/agent.XXXX).Test if your forwarded keys are accessible by running:
ssh-add -lThis should list the fingerprints of the keys loaded on your local machine.