A robust Bash utility script that automatically sets up ssh-agent and loads all SSH private keys from your ~/.ssh
directory. Perfect for development environments and automated workflows.
- Automatic ssh-agent Setup: Starts
ssh-agent
if not already running - Bulk Key Loading: Discovers and loads all SSH private keys from
~/.ssh
- Smart Filtering: Automatically excludes public keys (
.pub
) and configuration files - Duplicate Prevention: Prevents loading the same key multiple times
- Silent Mode: Optional mute flag for script automation
Download the script
wget https://raw.githubusercontent.com/rikkarth/ssh-agent-set-me-up/refs/heads/master/src/ssh-agent-setup.sh
# Source the script to load keys with output
source ssh-agent-setup.sh
# Or execute directly
./ssh-agent-setup.sh
# Load keys without output messages
source ssh-agent-setup.sh --mute
source ssh-agent-setup.sh -m
# Display help
./ssh-agent-setup.sh --help
# Display version
./ssh-agent-setup.sh --version
Option | Description |
---|---|
-m, --mute |
Suppress non-error output messages |
-h, --help |
Display help message and usage information |
-v, --version |
Display version information |
The SSH setup process follows these steps:
- ssh-agent Check: Verifies if
ssh-agent
is running via$SSH_AUTH_SOCK
- Agent Startup: Starts
ssh-agent
if not already running - Key Discovery: Scans
~/.ssh/
directory for potential private key files - Smart Filtering: Excludes:
- Public keys (
.pub
files) - Configuration files (
config
,known_hosts
,authorized_keys
, etc.) - Already processed keys (prevents duplicates)
- Public keys (
- Key Loading: Attempts to load each discovered private key
- Setup Complete: Reports success and loaded key count
The script automatically excludes these common SSH files:
known_hosts
andknown_hosts.old
config
authorized_keys
environment
- Any file with
.pub
extension
$ source src/ssh-agent-setup.sh
Starting ssh-agent...
ssh-agent started successfully
Loading SSH keys from /home/user/.ssh...
✓ Added SSH key: id_ed25519
✓ Added SSH key: id_rsa
✓ Added SSH key: github_deploy_key
Successfully loaded 3 SSH key(s)
ssh-agent setup complete
$ source src/ssh-agent-setup.sh --mute
# No output, but keys are loaded silently
Add to your ~/.bashrc
, ~/.zshrc
, or shell profile:
# Auto-setup SSH keys on shell startup
if [ -f ~/ssh-agent-setup.sh ]; then
source ~/ssh-agent-setup.sh --mute
fi
# In your project's setup script
echo "Setting up SSH environment..."
source path/to/ssh-agent-setup.sh --mute
echo "SSH setup complete, ready for git operations"
git clone [email protected]:rikkarth/ssh-agent-set-me-up.git
cd ssh-agent-set-me-up
source src/ssh-agent-setup.sh
- Bash: Version 4.0 or higher (for associative arrays)
- OpenSSH: Standard
ssh-agent
andssh-add
utilities - Permissions: Read access to
~/.ssh
directory - SSH Keys: At least one SSH private key in
~/.ssh
- Private Key Safety: Only processes files that appear to be private keys
- No Hardcoded Paths: Uses standard SSH directory locations
- Error Suppression: Prevents sensitive key information from appearing in error messages
- Permission Respect: Respects file system permissions
- No Global Pollution: Doesn't leave variables in your shell environment
If no keys are added, check:
-
SSH Directory: Ensure
~/.ssh
exists and contains private keysls -la ~/.ssh/
-
Permissions: Verify read access to key files
chmod 700 ~/.ssh chmod 600 ~/.ssh/id_*
-
Key Format: Ensure keys are in standard OpenSSH format
-
Agent Status: Check if keys are already loaded
ssh-add -l
# Fix SSH directory permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_*
# Ensure you own the files
chown -R $USER:$USER ~/.ssh/
# Make script executable
chmod +x ssh-agent-setup.sh
# Check file exists
ls -la ssh-agent-setup.sh
If you see "ssh-agent already running", this is normal behavior. The script detected an existing agent and used it.
- Development Setup: Automatically load keys when starting development work
- CI/CD Pipelines: Setup SSH access in automated environments
- Remote Server Setup: Bootstrap SSH access on new servers
- Docker Containers: Setup SSH access in containerized environments
- Shell Profiles: Automatic SSH setup on login
This project is licensed under the MIT License - see the LICENSE file for details.
Ricardo Mendes - [email protected]