Stop your SSH agent from forgetting your keys every time the shell closes

SSH & Remote Administration

Stop your SSH agent from forgetting your keys every time the shell closes

🧩 The Challenge

Dealing with the constant password prompts because your SSH agent process keeps dying when you exit your terminal is enough to drive anyone up the wall. I’ve spent way too many mornings re-adding keys after a reboot or a stray terminal exit.

💡 The Fix

You can use a wrapper script in your profile to keep a single agent running persistently across all your shell instances. It saves you the headache of manually managing environment variables every time you open a new tab.

if [ -z "$SSH_AUTH_SOCK" ]; then
eval $(ssh-agent -s) > /dev/null
fi

⚙️ Why It Works

Adding this block to your bashrc or zshrc ensures that whenever a session starts, it checks for an existing socket and spins up the agent if it’s missing. The environment variables persist so your other terminal windows can actually find the running process instead of trying to start their own.

🚀 Pro-Tip: Toss an ssh-add -K in there if you are on macOS to pull those keys directly from the keychain automatically.

Linux Tips & Tricks | © ngelinux.com | 7/24/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted