5 SSH Agent Best Practices (2024)

The SSH agent (ssh-agent) is an SSH key manager that stores the SSH key in a process memory so that users can log into SSH servers without having to type the key’s passphrase every time they authenticate with the server. In addition to the key management feature, SSH agent supports agent forwarding, which helps to authenticate with servers that sit behind a bastion or jump server. As the agent works as a password manager for SSH keys, incorrect usage or faulty configuration can cause security risks.

In this article, we’ll explore how to avoid potential SSH agent pitfalls and recommend best practices to keep your SSH agent secure.

Security risks of using SSH agent

When you run an SSH agent, it is risky to leave your terminal unattended because anyone with physical access to your terminal can invoke the SSH command and authenticate with the SSH server.

However, the more critical security risk is associated with SSH agent forwarding. When agent forwarding is used to jump between SSH servers, the local SSH agent is forwarded to the jump server. By design, agent forwarding lets you authenticate with the upstream server without copying private keys to the jump server. But it also means that any user with root access to the jump server can access the SSH agent and misuse it to authenticate with SSH servers on your behalf. So it is essential to follow a few best practices to harden usage of SSH agents and minimize the risk of them being compromised.

5 tips for safely using SSH agent

Below are 5 SSH agent hardening tips that will help to minimize risks associated with running SSH agent and SSH agent forwarding.

1. Set a timeout

We should use the ssh-add -t (timeout) argument with the ssh-add command to set a timeout when identifying with a private key. When we run the ssh-add or the ssh-agent command, the process asks for the passphrase of our private key for decryption. Once provided, it stays in memory throughout the active user session. Although this is the intended feature, it poses a security risk because a longer timeout gives an unnecessary time frame for anyone trying to misuse the SSH keys.

By configuring the timeout parameter, we, as Linux admins, can define a period after which the mechanism stops. This period could be a few minutes up to a few hours. Once the session times out, we need to provide the passphrase again for subsequent SSH sessions.

2. Remove unused keys

We can use the ssh-add -D command to remove private keys from the ssh-agent when we no longer need them. The -D argument deletes all added keys from the client. There’s also a -d parameter (lowercase), which allows us to select individual keys.

Removing unused keys is a good security practice because when the key is no longer present on the local machine, the ssh-agent won’t be able to use it. This means it can’t expose any secret information or establish unauthorized SSH sessions on other devices.

3. Always exit sessions

When SSH connections are not required, we should terminate the ssh-agent to avoid leaving it active with our keys open. As mentioned earlier, since we provide a passphrase during the initial SSH session, the ssh-agent won’t ask for it in subsequent sessions. And, when forwarding is enabled, this counts for connections to other remote machines as well.

When an SSH agent is not required, you can kill the active agent with the command eval "$(ssh-agent -k)". You can also update .logout or.bash_logout files to perform this automatically when your terminal session is closed.

To do this, update the file as follows:

 ~/.logout:if ( "$SSH_AGENT_PID" != "" ) then eval `/usr/bin/ssh-agent -k`endif

When we launch an ssh-agent task, it sets SSH_AGENT_PID as a system environment variable. This tiny script checks for the PID and forces a kill action (the -k parameter) on the ssh-agent task.

4. Cautiously use agent forwarding

Never forward your SSH agent on a machine you do not trust. Although the private keys never leave your machine when using the SSH agent, the agent itself is forwarded to the jump server in a forwarding mode. Thus, anyone with root access to that jump server can communicate with the agent, impersonate you as an authentic user, and access any servers where the key is authorized.

5. Use ProxyJump instead of agent forwarding

Available since OpenSSH version 7.3, the ProxyJump feature lets you jump to different SSH servers without agent forwarding. This is a safer alternative to agent forwarding, and we recommend using it over agent forwarding.

As an example, you can reach an SSH server behind a jump server with the following command:

$ ssh -J <jump server address> <server behind jump host>

Conclusion

Although SSH agents make it a lot easier to use SSH keys, they pose risks similar to those that come with password managers. With that in mind, the article reviewed a few common risks using SSH agents and agent forwarding and discussed some hardening tips to keep them secure.

Although SSH keys are a secure way to authenticate when compared to passwords, a more secure way of authentication is using SSH certificates. SSH certificates help enforce identity-based access and reduce the risks of stolen credentials with short-lived validity.

Learn more about how to implement certificate-based access for SSH using an open-source solution called Teleport. Along with SSH, Teleport also enables certificate-based access for Kubernetes, databases, HTTP applications, and remote desktops.

5 SSH Agent Best Practices (2024)

FAQs

What are the best practices for SSH keys? ›

It should never be shared or transmitted over insecure networks. Consider encrypting private keys with a passphrase for additional security. Rotate SSH keys. Just like passwords, SSH keys should be changed or rotated regularly to reduce the risk of a key being used maliciously if it's compromised and unnoticed.

What is the ssh-agent? ›

ssh-agent is a key manager for SSH. It holds your keys and certificates in memory, unencrypted, and ready for use by ssh . It saves you from typing a passphrase every time you connect to a server.

How do I ensure ssh-agent is running? ›

Starting Up ssh-agent
  1. Use the following command to execute the ssh-agent commands and enable ssh-agent for your current shell session: eval `ssh-agent` Adding that command as a line to your ~/. ...
  2. Verify that ssh-agent is running by checking for the SSH_AUTH_SOCK environmental variable: echo $SSH_AUTH_SOCK.
Nov 10, 2022

What is the recommended SSH key algorithm? ›

We strongly recommend using only the ed25519 algorithm (an ECDSA variant). It is the most secure SSH key type widely available, and is very well supported in the majority of systems. If you are using an client or server without ed25519 support, you should consider upgrading where possible.

What are the three protocols of SSH? ›

Thanks to QUIC, that uses TLS 1.3 for its protocol handshake, SSH3 offers a significantly faster session establishment than SSHv2. Establishing a new session with SSHv2 can take five to seven network Round-Trip Times (RTT): The TCP handshake. Key exchange and cryptographic algorithm negotiation.

What are the four steps needed to configure SSH? ›

There are four steps required to enable SSH support on a Cisco IOS router:
  • Configure the hostname command.
  • Configure the DNS domain.
  • Generate the SSH key.
  • Enable SSH transport support for the vty.
Aug 9, 2023

How many keys does ssh-agent have? ›

At last for the ssh-agent , the only limit is about the maximum memory available for the user or process. The keys are stored in the linked list which does not have any effective limit and all the constructs.

What is the host key of ssh-agent? ›

A host key is a cryptographic key used for authenticating computers in the SSH protocol. Host keys are key pairs, typically using the RSA, DSA, or ECDSA algorithms. Public host keys are stored on and/or distributed to SSH clients, and private keys are stored on SSH servers.

Why add SSH key to ssh-agent? ›

Whenever you use the key, you must enter the passphrase. If your key has a passphrase and you don't want to enter the passphrase every time you use the key, you can add your key to the SSH agent. The SSH agent manages your SSH keys and remembers your passphrase.

How do I make sure ssh is running? ›

You can try ssh localhost to test if it is running; if it respons with something like Connection refused , then it is not running. These commands must be run as root. If the server does not start automatically, try using the service sshd start command, or just reboot the computer.

How do I check my ssh-agent logs? ›

On most modern systems, journalctl provides a convenient, standardized way to view ssh logs. On other systems, you can find the sshd log at /var/log/auth. log. For quick inspections, you can also use the lastlog command.

How do I make ssh service active? ›

Activate the SSH server
  1. sudo rm -f /etc/ssh/sshd_not_to_be_run sudo systemctl enable ssh sudo systemctl start ssh.
  2. sudo mv /etc/init/ssh.conf.back /etc/init/ssh.conf sudo start ssh.
  3. sudo systemctl stop ssh sudo systemctl disable ssh.
  4. sudo stop ssh sudo mv /etc/init/ssh.conf /etc/init/ssh.conf.back.
Feb 3, 2022

Which tool is best for SSH? ›

Market Share for Top Secure Shell (SSH) Technologies
RankingTechnologyMarket share(Est.)(%)
1WinSCP48.66
2PuTTY20.82
3MobaXTerm9.55
4SecureCRT9.32
12 more rows

What are the strong SSH algorithms? ›

Valid host key algorithms are:
  • ecdsa-sha2-nistp256.
  • ecdsa-sha2-nistp384.
  • ecdsa-sha2-nistp521.
  • rsa-sha2-256.
  • rsa-sha2-512.
  • ssh-ed25519.
  • ssh-rsa.

How to optimize SSH? ›

How can you optimize network performance for SSH?
  1. Choose the right encryption algorithm.
  2. Enable compression.
  3. Use multiplexing.
  4. Adjust TCP settings.
  5. Use a proxy or a VPN.
  6. Here's what else to consider. Be the first to add your personal experience.
Feb 29, 2024

What are the best permissions for SSH key? ›

ssh directory permissions should be 700 (drwx------). The public key (. pub file) should be 644 (-rw-r--r--). The private key (id_rsa) on the client host, and the authorized_keys file on the server, should be 600 (-rw-------).

What are the best keys for SSH? ›

ssh-keygen defaults to RSA therefore there is no need to specify it with the -t option. It provides the best compatibility of all algorithms but requires the key size to be larger to provide sufficient security. Minimum key size is 1024 bits, default is 3072 (see ssh-keygen(1)) and maximum is 16384.

How to store SSH keys safely? ›

That being said, you should always password-protect your SSH private keys, or store them on a hardware token for added security.

Is Ed25519 better than RSA? ›

Compared to traditional RSA keys, Ed25519 key generation is significantly faster. This advantage is especially valuable when dealing with systems that require frequent key generation, such as in large-scale infrastructure setups or automated processes.

Top Articles
The Impact of a Vegan Diet on Many Aspects of Health: The Overlooked Side of Veganism
What Is a Credit Tradeline?
Palm Coast Permits Online
Fat Hog Prices Today
Did 9Anime Rebrand
South Carolina defeats Caitlin Clark and Iowa to win national championship and complete perfect season
Poplar | Genus, Description, Major Species, & Facts
What Was D-Day Weegy
Best Cav Commanders Rok
What is a basic financial statement?
Cranberry sauce, canned, sweetened, 1 slice (1/2" thick, approx 8 slices per can) - Health Encyclopedia
Bestellung Ahrefs
National Office Liquidators Llc
Nhl Wikia
Rams vs. Lions highlights: Detroit defeats Los Angeles 26-20 in overtime thriller
Gemita Alvarez Desnuda
Chelactiv Max Cream
Craigslist Portland Oregon Motorcycles
Msu 247 Football
Scout Shop Massapequa
Invitation Homes plans to spend $1 billion buying houses in an already overheated market. Here's its presentation to investors setting out its playbook.
Craigslist Houses For Rent In Milan Tennessee
Discord Nuker Bot Invite
Snohomish Hairmasters
FAQ's - KidCheck
Wku Lpn To Rn
Hrconnect Kp Login
Abga Gestation Calculator
Askhistorians Book List
Loopnet Properties For Sale
Rogold Extension
Ucm Black Board
Acuity Eye Group - La Quinta Photos
Fridley Tsa Precheck
A Small Traveling Suitcase Figgerits
Yoshidakins
Suspect may have staked out Trump's golf course for 12 hours before the apparent assassination attempt
M3Gan Showtimes Near Cinemark North Hills And Xd
Lichen - 1.17.0 - Gemsbok! Antler Windchimes! Shoji Screens!
Craigslist In Myrtle Beach
How to Play the G Chord on Guitar: A Comprehensive Guide - Breakthrough Guitar | Online Guitar Lessons
Kvoa Tv Schedule
Retire Early Wsbtv.com Free Book
Gold Nugget at the Golden Nugget
John M. Oakey & Son Funeral Home And Crematory Obituaries
Hawkview Retreat Pa Cost
Bridgeport Police Blotter Today
Twizzlers Strawberry - 6 x 70 gram | bol
Vcuapi
Palmyra Authentic Mediterranean Cuisine مطعم أبو سمرة
Swissport Timecard
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 6233

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.