Overview
ssh-agent securely stores your SSH private keys and provides them to SSH clients (ssh) when needed, streamlining the authentication process. Once you enter a passphrase, you don't need to enter it again as long as the agent is running, making it convenient. This is particularly useful in environments where you frequently use multiple SSH connections.
Key Features
- SSH key management and storage
- Prevents repetitive passphrase entry
- Secure communication with SSH clients
- Session-based key persistence
Key Options
ssh-agent primarily runs in the background and sets shell environment variables to allow SSH clients to find the agent.
Execution and Control
Generated command:
Try combining the commands.
Description:
`ssh-agent` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Start ssh-agent (bash/sh)
eval "$(ssh-agent -s)"
Starts ssh-agent in the background and sets up the necessary environment variables for the current shell session.
Add SSH Key
ssh-add ~/.ssh/id_rsa
Adds a private key to the ssh-agent. If the key has a passphrase, you will be prompted to enter it once.
Terminate Agent
ssh-agent -k
Terminates the ssh-agent process connected to the current shell session.
Start Agent with 1-Hour Key Lifetime
eval "$(ssh-agent -s -t 3600)"
Starts the agent with a maximum lifetime of 1 hour (3600 seconds) for all keys added.
List Keys Loaded in Current Agent
ssh-add -l
Checks the list of SSH keys currently loaded into the ssh-agent.
Tips & Precautions
Tips for effectively using ssh-agent and maintaining security.
Tips
- **Automatic Start**: You can add `eval "$(ssh-agent -s)"` to your `.bashrc` or `.zshrc` file to automatically start the agent when your shell starts. It's recommended to add logic to check if an agent is already running.
- **Check Key List**: You can use the `ssh-add -l` command to view the list of SSH keys currently loaded into your ssh-agent.
- **Remove Keys**: You can remove specific keys with `ssh-add -d ~/.ssh/id_rsa` or remove all keys with `ssh-add -D`.
Precautions
- **Agent Forwarding**: When using agent forwarding with the `ssh -A` option, you allow remote servers to access your local agent. While convenient, this poses a risk of exposing your local keys if the remote server is compromised. Therefore, use it only on trusted servers.
- **Agent Lifetime**: You can set the maximum lifetime for keys using the `ssh-agent -t` option. For security reasons, it's advisable to avoid excessively long lifetimes.