Securing Infrastructure Access at Scale in Large Enterprises
Dec 12
Virtual
Register Now
Teleport logoTry For Free
Home > Teleport Academy > Infrastructure Access

Mastering SSH Keygen

Posted 7th Aug 2024 by Ben Arent

Secure Shell (SSH) is the backbone of secure remote access in the world of Linux, Unix, and increasingly, Windows. While passwords offer a basic level of authentication, SSH keys provide a far more robust and secure approach. At the heart of this secure access lies the ssh-keygen command, a powerful tool for generating and managing your SSH key pairs. This tutorial walks you through the ins and outs of ssh-keygen, empowering you to take control of your SSH security.

Understanding SSH Key Pairs and Public Key Authentication

Before diving into the ssh-keygen command, let's grasp the core concepts:

  • Public-Key Authentication: Unlike password-based authentication, which relies on something you know (your password), public key authentication utilizes a pair of keys - a public key and a private key.
  • Public Key: This key can be shared freely and is placed on the SSH server you want to access.
  • Private Key: This key is your secret, kept secure on your client machine. It's used to prove your identity to the server possessing the corresponding public key.
  • SSH Key Pair: Think of it like a lock and key system. The server has the lock (public key), and you have the key (private key) to unlock it.

ssh-keygen: Your Command-line Keymaster

The ssh-keygen command, available on Linux, macOS, and Windows (via OpenSSH), is your command-line tool for generating these SSH key pairs. Let's explore its usage:

1. Generating a New SSH Key:

The following command generates a new SSH key pair:

ssh-keygen -t <key_type> -b <key_length> -f <filename>
  • -t <key_type>: Specifies the type of SSH key to generate. While RSA keys were once common (ssh-rsa), ECDSA (ecdsa) is now the recommended choice for its balance of security and performance. Learn more about Comparing SSH Keys - RSA, DSA, ECDSA, or EdDSA?.
  • -b <key_length>: Defines the length of the key in bits. The default and recommended length for ECDSA is 256 bits (-b 256).
  • -f : Specifies the filename for your SSH key pair. If omitted, the default location is ~/.ssh/id_rsa or ~/.ssh/id_ecdsa depending on the chosen key type.

Example:

ssh-keygen -t ecdsa -b 256 -f ~/.ssh/my_new_key

2. Setting a Passphrase (Highly Recommended):

During key generation, ssh-keygen prompts you to enter a passphrase. A strong passphrase adds an extra layer of security, ensuring that even if someone steals your private key file, they cannot use it without the passphrase.

3. Output Files:

ssh-keygen creates two files:

  • Private Key File: Contains your private key; keep this file secure! In the example above, it would be ~/.ssh/my_new_key.
  • Public Key File: Contains your public key. This file can be shared and will be placed on the server you want to access. Its filename will be the same as the private key file with .pub appended (e.g., ~/.ssh/my_new_key.pub).

Managing Your SSH Keys with ssh-agent

To avoid typing your passphrase every time you use your SSH key, you can utilize the ssh-agent. This program securely stores your private key in memory, allowing SSH clients to access it without repeated prompts.

1. Start the SSH Agent:

Most operating systems have the ssh-agent running by default. If not, you can start it manually.

2. Add Your Key to the Agent:

ssh-add <private_key_file>

Example:

ssh-add ~/.ssh/my_new_key

You'll be prompted for your passphrase the first time you add a key.

Placing Your Public Key on the SSH Server

Once you have your SSH key pair, you need to place your public key on the server you wish to access. This is done by adding it to the authorized_keys file within the .ssh directory of your user account on the server. Several methods exist, but a common one is using ssh-copy-id:

ssh-copy-id -i <public_key_file> <username>@<hostname>

Example:

ssh-copy-id -i ~/.ssh/my_new_key.pub [email protected]

This command securely copies your public key to the specified server.

OpenSSH Key Formats and Security Considerations

OpenSSH supports various key formats, including RSA, DSA, and ECDSA. As mentioned earlier, ECDSA is the preferred choice for modern implementations.

  • Key Length: Ensure you use a strong key length for your chosen key type.
  • Passphrase: Always set a strong passphrase for your private key.
  • Secure Storage: Protect your private key files with appropriate permissions, limiting access to only authorized users.

Additional Notes and Tools

  • Key Fingerprint: You can view your public key's fingerprint using ssh-keygen -lf <public_key_file>. This fingerprint can be used to verify the authenticity of a host key when connecting for the first time.
  • Puttygen: If you're using PuTTY as your SSH client on Windows, the Puttygen tool allows you to generate SSH keys compatible with OpenSSH.

Beyond the Basics: SSH Certificates

While SSH keys offer significant security enhancements, SSH certificates take it a step further by providing centralized key management and additional security features like automatic expiration and granular access controls.

Teleport, an open-source platform, simplifies the implementation of certificate-based authentication, enabling you to manage SSH access, Kubernetes clusters, databases, and more with enhanced security and control. Learn More https://goteleport.com/blog/how-to-ssh-properly/

Conclusion

Mastering ssh-keygen is the first step towards a more secure SSH experience. By understanding key types, passphrases, and best practices, you can significantly reduce the risks associated with password-based authentication and safeguard your remote access. As you delve deeper into the world of SSH, remember that tools like Teleport offer even more advanced security features to protect your valuable infrastructure.

Mastering ssh-keygen: Best Practices and Future Trends

While the basics of ssh-keygen are essential, mastering its nuances can elevate your security posture and streamline your workflow. Let's explore best practices, common pitfalls, and how ssh-keygen fits into the evolving landscape of secure access.

Best Practices for ssh-keygen

  • Choose Strong Host Keys: When setting up an ssh server, use ssh-keygen to generate strong host keys. Opt for Ed25519 for its security and performance advantages. Regularly rotate your host keys to minimize the impact of potential compromise.
  • Securely Store Private Keys: Treat your private keys as highly confidential information. Store them in a secure location, ideally with strong access controls and encryption. Never share your private keys with anyone.
  • Limit Key Usage: Instead of using a single key for everything, consider generating separate keys for different purposes (e.g., one for personal use, one for work, one for specific servers). This limits the potential damage from a compromised key.
  • Utilize ssh-copy-id Carefully: While ssh-copy-id simplifies copying your SSH public key to a server, ensure you trust the target machine before using this command. A compromised server could potentially steal your public key.

Common Pitfalls and How to Avoid Them

  • Weak Passphrases: A weak or easily guessable passphrase renders your SSH key vulnerable. Choose a strong, unique passphrase that is not found in dictionaries or common password lists. Consider using a passphrase manager to securely store and manage your passphrases.
  • Forgetting Your Passphrase: Losing your passphrase effectively locks you out of your SSH key. Store your passphrase securely, whether using a password manager or a trusted offline method.
  • Incorrect File Permissions: If your private key file has overly permissive permissions, other users on your system could potentially access it. Use the chmod command to set appropriate permissions, typically 600 to restrict access only to the owner.
  • Blindly Trusting known_hosts: While the known_hosts file helps prevent man-in-the-middle attacks, ensure you validate host key fingerprints carefully before adding them to this file.

Frequently Asked Questions

What is ssh-keygen?

ssh-keygen is a command-line tool used to generate and manage SSH key pairs. These key pairs are the foundation of secure, passwordless authentication for accessing remote servers via SSH. It's a core component of the OpenSSH suite and is available on Linux, macOS, and Windows.

How does ssh-keygen work?

ssh-keygen utilizes asymmetric cryptography to generate a pair of mathematically linked keys - a public key and a private key. The public key can be shared freely, while the private key must be kept secret. Authentication occurs when the client, possessing the private key, successfully responds to a challenge from the server holding the corresponding public key.


What does ssh-keygen do?

ssh-keygen creates a pair of cryptographic keys - a public key and a private key. These keys are used for secure authentication when connecting to SSH servers, replacing the need for passwords. The public key is placed on the server, while the private key is kept secure on the client machine.

How to generate an SSH key pair using ssh-keygen?

To generate an SSH key pair, open your terminal and run the command ssh-keygen -t ecdsa -b 256 -f ~/.ssh/my_new_key. This will create a new ECDSA key pair with a length of 256 bits, stored in the file ~/.ssh/my_new_key for the private key and ~/.ssh/my_new_key.pub for the public key.

How to use ssh-keygen?

ssh-keygen is used primarily for generating new SSH key pairs, but it also offers options for managing keys, such as viewing key fingerprints, converting key formats, and changing passphrases. You can explore these options by running ssh-keygen --help in your terminal.


Where does ssh-keygen store keys?

By default, ssh-keygen stores SSH keys in the ~/.ssh directory within your user's home directory. The private key is typically named id_rsa or id_ecdsa (depending on the key type), and the public key has the same name with a .pub extension.

How to install ssh-keygen on Ubuntu?

ssh-keygen is usually installed by default on Ubuntu as part of the OpenSSH client package. If it's not installed, you can install it by running sudo apt update followed by sudo apt install openssh-client in your terminal.