Complete Guide to Permanently Adding SSH Private Keys on Ubuntu Systems

Nov 10, 2025 · Programming · 9 views · 7.8

Keywords: SSH keys | Ubuntu configuration | Permanent addition | ssh-agent | .ssh/config

Abstract: This article provides a comprehensive exploration of various methods for permanently adding SSH private keys in Ubuntu systems, with detailed analysis of ssh-agent persistence configuration, proper usage of ~/.ssh/config files, and the impact of different configurations on SSH key management. By comparing temporary versus permanent addition approaches, it offers complete operational procedures and best practice recommendations to help users resolve the issue of repeatedly needing to add SSH keys.

Analysis of SSH Key Persistence Issues

When using SSH keys for authentication in Ubuntu systems, users frequently encounter the problem of needing to repeatedly add keys. Based on actual case analysis, this phenomenon typically stems from differences in ssh-agent configuration. As the manager of SSH keys, ssh-agent's default behavior may vary across different systems or configurations.

Configuration Method Using ~/.ssh/config File

The most reliable method for permanently adding SSH private keys is through configuring the ~/.ssh/config file. This file allows users to specify default identity files, ensuring automatic loading of corresponding private keys during each SSH connection.

First, check if the configuration file exists:

ls -al ~/.ssh/config

If the file does not exist, create it and set appropriate permissions:

touch ~/.ssh/config
chmod 600 ~/.ssh/config

Add identity file paths in the configuration file:

IdentityFile ~/.ssh/gitHubKey
IdentityFile ~/.ssh/id_rsa_buhlServer

Host-Specific Configuration

For scenarios requiring different keys for specific hosts, host-specific configuration can be employed:

Host github.com
    User git
    IdentityFile ~/.ssh/githubKey

The advantage of this configuration lies in avoiding identity attempt sequence issues. When a server is configured with multiple keys, only the specified identity file will be attempted, thereby improving connection success rates.

System-Level Configuration

If identical SSH keys need to be configured for all users, edit the system-level configuration file:

/etc/ssh/ssh_config

Add the same IdentityFile directives in this file, while ensuring key files are located in directories accessible to all users.

Persistence Mechanism of ssh-agent

The session persistence of ssh-agent depends on its startup method and environment variable settings. In some desktop environments, ssh-agent may automatically start and remain running as part of the user session, while in other cases manual configuration may be required.

Check if ssh-agent is running:

eval "$(ssh-agent -s)"

Add keys to ssh-agent:

ssh-add ~/.ssh/id_ed25519

Key File Permission Management

Correct file permissions are crucial for secure usage of SSH keys. Private key files should be set to readable only by the owner:

chmod 600 ~/.ssh/id_rsa

Configuration files also require strict permission settings:

chmod 600 ~/.ssh/config

Troubleshooting and Verification

Verify if SSH connection configuration is correct:

ssh -T git@github.com

If connection issues arise, use verbose mode for diagnosis:

ssh -vT git@github.com

Check loaded keys:

ssh-add -l -E sha256

Best Practice Recommendations

By combining ~/.ssh/config file configuration with appropriate ssh-agent management, permanent availability of SSH keys can be achieved. Prioritizing the configuration file method is recommended, as it does not depend on specific session environments and provides a more stable key management solution.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.