Using OpenSSH Certificates for Easier and More Secure Server Access

Introduction to OpenSSH Certificates

I’ve seen many Linux admins struggle with managing SSH keys for their users. OpenSSH certificates are a game-changer here - they offer a more secure and convenient way to manage access to your Linux servers. By using certificates, you can avoid the hassle of managing individual SSH keys for each user and reduce the risk of key compromise.

Generating a Certificate Authority

To get started with OpenSSH certificates, you need to set up a Certificate Authority (CA). The CA will be used to sign and verify the certificates. I usually start with generating a CA key pair using the following command:

ssh-keygen -t rsa -b 4096 -f ca_key

This will generate a 4096-bit RSA key pair in the ca_key file. Don’t bother with smaller key sizes - 4096-bit is a good default.

Generating User Certificates

To generate a user certificate, you’ll need to create a public/private key pair for the user and then sign the public key with the CA key. Here’s an example:

ssh-keygen -t rsa -b 4096 -f user_key
ssh-keygen -s ca_key -I user@example.com -n user@example.com -V +52w user_key.pub

This will generate a 4096-bit RSA key pair in the user_key file and then sign the public key with the CA key, creating a certificate that’s valid for 52 weeks. The real trick is to make sure you’re using the correct CA key and user key.

Configuring the SSH Server

To use OpenSSH certificates, you need to configure the SSH server to trust the CA key. You can do this by adding the following line to your sshd_config file:

TrustedUserCAKeys /path/to/ca_key.pub

Then, restart the SSH service to apply the changes:

sudo systemctl restart sshd

This is where people usually get burned - forgetting to restart the SSH service.

Client Configuration

To use the user certificate, you’ll need to configure the SSH client to use the certificate. Add the following line to your ssh_config file:

IdentityFile /path/to/user_key

You can then connect to the server using the following command:

ssh user@example.com

The SSH client will automatically use the certificate to authenticate with the server. In practice, this makes it much easier to manage access to your servers.

Revoking Certificates

If a user’s certificate is compromised or they leave the organization, you’ll need to revoke the certificate to prevent them from accessing the server. Add the following line to your sshd_config file:

RevokedKeys /path/to/revoked_keys

Then, add the revoked certificate to the revoked_keys file:

ssh-keygen -k -f revoked_keys user@example.com

This will add the revoked certificate to the revoked_keys file, preventing the user from accessing the server.

Security Considerations

Using OpenSSH certificates can significantly improve the security of your server access. However, it’s still important to follow best practices, such as using strong passwords and keeping your CA key secure. You can find more information on OpenSSH certificates in the OpenSSH documentation.

Troubleshooting

If you encounter issues with OpenSSH certificates, you can use the following commands to troubleshoot:

ssh -v user@example.com

This will enable verbose mode, allowing you to see the authentication process in detail. You can also check the SSH server logs to see if there are any errors:

sudo journalctl -u sshd

This will show you the SSH server logs, which can help you identify any issues with the certificate configuration.


See also