When connecting via SSH from a primary workstation to a remote Linux machine, the remote shell initializes as an isolated environment. By default, it does not inherit your local machine's active ssh-agent session, nor does it automatically spawn a new agent or load custom-named private keys unless explicitly configured to do so.
For example, when I SSH from my MacBook Air (ankaa) into my Lenovo ThinkPad T450 running Fedora Linux (taylor) to work on my Pelican blog repository, running git fetch or git push against GitHub on taylor will fail with Permission denied (publickey) if no active agent socket is available. While running eval "$(ssh-agent -s)" and ssh-add manually on the remote host solves the issue for a single session, OpenSSH provides a much cleaner, permanent workflow via client configuration.
By combining agent forwarding on the client side with native OS credential integration, you can authenticate Git operations and secondary SSH jumps from a remote Linux host without ever copying private keys onto it.
Core SSH Client Directives
To manage SSH key lifecycles automatically on macOS and forward credentials securely to a Linux host, three primary directives in ~/.ssh/config work together:
1. AddKeysToAgent yes
- What it does: Automatically loads your private SSH key into the running ssh-agent memory the first time you use it.
- Why it matters: Instead of manually running ssh-add after every system reboot or new terminal session, OpenSSH silently registers the key the moment you initiate a connection.
2. UseKeychain yes
- What it does: Integrates OpenSSH with native macOS Keychain Services.
- Why it matters: When you unlock an encrypted SSH key with its passphrase on macOS, the passphrase is stored securely in your Keychain. On subsequent logins or reboots, ssh-agent retrieves the passphrase automatically so you do not have to re-enter it.
Note
UseKeychain is a macOS-specific OpenSSH option. If you share your ~/.ssh/config across Linux systems like taylor, OpenSSH on Linux will ignore it or require IgnoreUnknown UseKeychain at the top of the configuration file.
3. ForwardAgent yes
- What it does: Passes your local machine's active ssh-agent socket through the encrypted SSH tunnel to the remote host.
- Why it matters: Remote processes—such as git interacting with GitHub—can request cryptographic signatures directly from your local agent. Your private key never leaves your local machine; only authentication challenges and signatures traverse the encrypted channel.
Warning
Only enable ForwardAgent yes on trusted remote hosts where you control root access, as a privileged user on the remote machine could temporarily access the forwarded socket while your session is active.
Configuring the Client Machine (macOS)
To put this workflow into practice, configure your SSH client on the initiating machine. Using my setup as a working example, I configure ~/.ssh/config on ankaa (macOS) to define connection parameters for taylor (Fedora Linux).
Step 1: Edit ~/.ssh/config
Open your SSH client configuration file on your local machine:
nano ~/.ssh/config
Add a host definition block specifying the target address, user, identity file, and agent directives:
Host taylor
HostName <taylor-ip-or-hostname>
User porfirio
IdentityFile ~/.ssh/id_ed25519
AddKeysToAgent yes
UseKeychain yes
ForwardAgent yes
(Replace ``<taylor-ip-or-hostname>`` and ``porfirio`` with your target host's IP address or local hostname and username).
Step 2: Enforce Strict Configuration Permissions
OpenSSH enforces StrictModes on configuration files as well as keys. Ensure your ~/.ssh/config file is readable and writable only by your user account:
chmod 600 ~/.ssh/config
Verifying the Workflow on the Remote Host
Once the configuration block is saved on ankaa, connecting and authenticating remote Git operations requires zero manual agent management:
Initiate the SSH connection from ankaa:
ssh taylormacOS automatically loads ~/.ssh/id_ed25519 into ssh-agent, unlocks it via Keychain, and forwards the agent socket to taylor.
On taylor, verify that the forwarded identity is visible to the remote shell:
ssh-add -lNavigate to a repository and run remote Git operations directly against GitHub without storing local keys on taylor:
cd ~/gitRepos/github.com/porfiriopaiz/site git fetch origin git switch devel
Finishing Touches
With AddKeysToAgent, UseKeychain, and ForwardAgent properly configured, your primary workstation acts as a single, secure credential vault. You can seamlessly administer remote Linux systems and interact with authenticated Git repositories while keeping your private keys isolated on a single physical device.