To link SSH key B to host C, you must configure the SSH client to use a specific identity file for that target. This is achieved by adding an entry to your
~/.ssh/config file that maps the hostname of server C to the private key located at the path of key B. Once configured, any standard SSH command directed at host C will automatically present key B for authentication, eliminating the need for manual agent forwarding or explicit
-i flags during every session.
Understanding the SSH Agent and Key Chain
The SSH agent acts as a secure credential manager that holds decrypted private keys in memory. When you start an
ssh-agent and add your keys using
ssh-add, the agent handles the cryptographic signing of authentication challenges on your behalf. This means your private keys never leave your machine in plaintext form during network transmission. However, the agent itself does not inherently know which key belongs to which remote server; it simply provides whatever key the SSH client requests.
Linking a specific key to a specific host requires instructing the SSH client on how to behave when connecting to certain destinations. Without this configuration, the client might try all available keys in the agent, leading to "Too many authentication failures" errors if none match, or it might default to a different key entirely. By explicitly defining the relationship between a host and a key, you streamline the connection process and ensure consistent access to server C using key B.
Configuring the SSH Client Config File
The most robust way to manage these mappings is through the user-specific SSH configuration file. This file allows you to define aliases, specify ports, set usernames, and bind specific keys to specific hosts. To begin, locate or create the file at
~/.ssh/config. If the file does not exist, you can create it using a text editor such as
nano or
vim.
You need to add a new block that targets the hostname of server C. Inside this block, you will specify the
HostName, the
User (if it differs from your local username), and crucially, the
IdentityFile pointing to the location of key B. Here is the structure you should follow:
Host c-server
HostName 192.168.1.50
User admin
IdentityFile ~/.ssh/key_b
IdentitiesOnly yes
In this example,
c-server is an alias you can use instead of the full IP address or domain name. The
IdentitiesOnly yes directive is particularly important because it forces the SSH client to use only the specified identity file for this host, ignoring other keys loaded in the agent. This prevents conflicts where the agent might try an incorrect key first, causing delays or authentication failures. After saving the file, ensure its permissions are strict enough to prevent unauthorized reading by running
chmod 600 ~/.ssh/config.
For more details on managing SSH connections and troubleshooting common identification errors, you may find
Troubleshooting kex_exchange_identification Error in Linux SSH Connection helpful if you encounter handshake issues after configuring your keys.
Verifying the Configuration
Once the configuration is saved, you can test the setup by attempting to connect to server C using the alias defined in the config file. Run the command
ssh c-server. If the configuration is correct and key B is added to your SSH agent, the connection should establish without prompting for a password. You can also use the verbose flag to see exactly which key is being used:
ssh -v c-server. Look for lines indicating "Offering public key" and confirm that the path matches your key B location.
If you experience permission denied errors despite correct configuration, check that the private key file itself has restrictive permissions. Private keys must typically have permissions set to
600 or
400. You can verify this with
ls -l ~/.ssh/key_b. If the permissions are too open, the SSH client will refuse to use the key for security reasons.
Managing Keys with ssh-add
Even with a correctly configured
config file, the private key must be available to the SSH agent. If you have recently rebooted or started a new terminal session, you may need to reload your keys. Use the
ssh-add command to load key B into the agent. For example:
ssh-add ~/.ssh/key_b. If the key has a passphrase, you will be prompted to enter it. Once added, the agent will hold the decrypted key in memory until the agent terminates or you explicitly remove it.
It is good practice to periodically review which keys are currently loaded in the agent. You can list them using
ssh-add -l. This output shows the fingerprints of all keys currently managed by the agent. Ensuring that key B appears in this list confirms that the agent is ready to authenticate with server C. If key B is missing, the SSH client cannot use it, even if the
config file points to it.
For users who frequently switch between multiple servers and keys, understanding how to cleanly exit and reset connections can be valuable. See
Closing SSH Connection with Keygen for tips on managing session lifecycles and securely removing keys from the agent environment.
Handling Multiple Keys
In complex environments, you might have multiple keys for different servers. The
config file excels here by allowing distinct blocks for each host. Each block can specify its own
IdentityFile. This modular approach ensures that changing the key for one server does not affect the authentication logic for others. Always use
IdentitiesOnly yes in these blocks to maintain strict control over which key is presented, avoiding the ambiguity of the agent trying every loaded key sequentially.
Troubleshooting Common Issues
Despite careful configuration, issues may arise. One common problem is the SSH client ignoring the
config file due to syntax errors. Ensure there are no typos in the directive names and that indentation is consistent, though spaces vs tabs generally do not matter in SSH config files. Another issue is network-related, such as firewall rules blocking the connection. If you suspect a network issue rather than an authentication one, refer to
Ubuntu 22.4.4 LTS: UFW Firewall Allows SSH but Prevents Connection to Another Server to diagnose potential intermediate network restrictions.
Another frequent error occurs when the remote server's
authorized_keys file does not contain the public counterpart of key B. You must ensure that the public key corresponding to
key_b is appended to the
~/.ssh/authorized_keys file on server C. You can extract the public key from the private key file using
ssh-keygen -y -f ~/.ssh/key_b > ~/.ssh/key_b.pub and then copy the content to the remote server. Without this step, the authentication will fail regardless of local configuration.
Frequently Asked Questions
Why does SSH still ask for a password after configuring the key?
This usually happens because the key is not added to the SSH agent or the permissions on the key file are too open. Verify that
ssh-add -l lists your key and that the file permissions are
600. Additionally, ensure
IdentitiesOnly yes is set in your config if other keys are causing interference.
Can I use the same key for multiple servers?
Yes, you can point multiple
Host blocks in your
~/.ssh/config to the same
IdentityFile. This is useful for managing a group of servers with identical access requirements. Just ensure that the public key is distributed to all those servers'
authorized_keys files.
What is the difference between IdentityFile and IdentitiesOnly?
IdentityFile specifies which key to use, while
IdentitiesOnly yes restricts the SSH client to use only that key, ignoring others in the agent. Without
IdentitiesOnly, the client may try other keys first, potentially leading to "too many authentication failures" before reaching the correct one.
How do I remove a key from the SSH agent?
You can remove a specific key using
ssh-add -d ~/.ssh/key_b or clear all keys with
ssh-add -D. This is useful for security purposes when switching contexts or before shutting down your computer to ensure no sensitive keys remain in memory.
Is it safe to store private keys in ~/.ssh/?
Yes, provided you set the correct permissions. The directory
~/.ssh should be
700 and private keys should be
600 or
400. This prevents other users on the same system from reading your private keys. Never share private keys via email or unencrypted channels.