Jump to Tutorial<\/a><\/p>\n\n\n\n
Secure Shell, or Secure Socket Shell \u2014 commonly abbreviated as SSH \u2014 is a secure network protocol that allows users to securely authenticate to remote devices. SSH leverages a pair of SSH keys<\/a> to encrypt communication with a remote system. The key pair is cryptographic in nature and made up of a public<\/em> and private<\/em> key. The keys work in tandem to provide authentication between the client and the remote system. <\/p>\n\n\n\n
Before we proceed further, let us draw a distinction between public and private keys.<\/p>\n\n\n\n
As the name suggests, the private key is only meant for the person who created it, and therefore strictly resides on the client system. It allows users to securely authenticate with the remote server, and should always be kept secret and never disclosed to anyone. In the wrong hands, the private key could be compromised and malicious users can use it to breach your systems. We cannot emphasize this enough: the private key should never<\/em> be revealed to anyone else.<\/p>\n\n\n\n
On the other hand, the public key can be freely shared with any server you wish to connect to without compromising your identity. It is used for encrypting data exchanged between the server and the client. The private key decrypts the messages sent from the remote server and a connection is established. On the remote system, the public key is saved in the authorized_keys<\/kbd> <\/code>file.<\/p>\n\n\n\n
Let\u2019s now switch gears and check out how SSH authentication works. <\/p>\n\n\n\n
How SSH Authentication Works<\/strong><\/h2>\n\n\n\nSSH authentication is broadly categorized into two types: password-based and public key authentication.<\/p>\n\n\n\n
1. Password-based authentication<\/h3>\n\n\n\n
In password-based authentication, the client sends an authentication request to the server which includes the encrypted username and password for the remote server. Upon receipt, the server decrypts the request and validates the credentials in plain text. Once verified, the client is notified of the authentication outcome.<\/p>\n\n\n\n
2. Public key authentication<\/h3>\n\n\n\n
Public key authentication \u2014 also known as asymmetric encryption \u2014 uses a cryptographic key pair which comprises a private and public key on the client system. After generating the SSH key pair, both the public and private keys are saved on the client. The client then copies the public key to the remote server. <\/p>\n\n\n\n
So, how does the authentication really work?\u00a0<\/p>\n\n\n\n
During authentication, the client system sends an authentication request to the remote server which includes a public key. The remote server then receives the request and checks if the public key matches the one copied to it. If the key is valid and matches the one on the server, the server then creates a secret message and encrypts it with the public key from the client. The message is then sent back to the client whereupon the private key decrypts the message. Because this is asymmetric encryption, only the client system can decrypt the message with the private key. Once the message is decrypted, the server acknowledges it and the authentication is successful.<\/p>\n\n\n\n
Public key authentication is the more preferred authentication of the two. It is more convenient and secure than password-based authentication \u2014 and for good reasons. SSH keys are complex and difficult to crack thanks to the strong encryption algorithms used. In addition, only the user with the private key can access the remote system. <\/p>\n\n\n\n
When public key authentication is enabled, password authentication should be turned off so that only the private key alone can be used to authenticate with the remote system.<\/p>\n\n\n\n
To leverage public key authentication, the first step is to generate the SSH key pair. To do so, launch your terminal on the client and run the following command:<\/p>\n\n\n\n
$ ssh-keygen -t rsa<\/code><\/p>\n\n\n\n
The -t<\/kbd> flag specifies the type of the SSH key to be created. In this case, the RSA key pair. <\/p>\n\n\n\n
There are two possible values of RSA: \u201crsa1<\/kbd>\u201d for RSA version 1 and \u201crsa<\/kbd>\u201d for RSA version 2. Since the first option is now deprecated and considered weak, here we are going with the latter which is considerably stronger.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
Specify the location that you wish to save the key pair. Typically, the default path is the user\u2019s home directory or simply the <\/code>~\/.ssh<\/kbd> folder (for example, \/user\/home\/.ssh<\/kbd>). If you wish to save the keys in this location, simply press \u201cENTER.\u201d<\/p>\n\n\n\n
Next, you will be required to provide a passphrase (optional). This adds an extra layer of security in the rare event that a hacker gets a hold of the key. You can leave it blank or specify a key phrase that will be required on each login attempt. To leave it blank, just hit \u201cENTER.\u201d<\/p>\n\n\n\n
To confirm the keypair has been successfully generated, list the contents of the ~\/.ssh<\/kbd>
<\/code>directory.<\/p>\n\n\n\n
$ ls -l ~\/.ssh<\/code><\/p>\n\n\n\n
The id_rsa<\/kbd>
<\/code>is the private key which, as mentioned, should be kept top-secret on the client system to prevent potential compromise.<\/p>\n\n\n\n
The id_rsa.pub<\/kbd>
<\/code>is the public key that can be freely shared with the server you intend to connect to.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
Once the SSH keys are generated, the next step is to copy the public key to the remote server. You can achieve this using the simple ssh-copy-id<\/kbd> <\/code>command:<\/p>\n\n\n\n
$ ssh-copy-id username@remote-server-IP<\/code><\/p>\n\n\n\n
Provide the remote server\u2019s password and hit \u201cENTER\u201d to copy the public key. You should get an acknowledgment from the remote server that the key was successfully added. The public key is saved to the ~\/.ssh\/authorized_keys<\/kbd>
<\/code>file on the remote system.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
The next time you attempt to log in, the SSH-key challenge response will take place and you will be automatically logged in without password authentication. This is what is popularly known as SSH passwordless authentication since authentication is fully reliant on the SSH-key pair.<\/p>\n\n\n\n
$ ssh remote-server-ip<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\nTroubleshooting Common Issues<\/h2>\n\n\n\n
Permission Denied Errors<\/h3>\n\n\n\n
Permission denied errors are one of the most common issues encountered when using SSH keys. These errors typically indicate a problem with file permissions or misconfigurations in the authorized_keys<\/kbd> file. Common causes and solutions entail:<\/strong><\/p>\n\n\n\n
\n- Incorrect File Permissions<\/strong>: SSH requires strict permissions on key files to ensure security.\n
\n- Ensure the private key file is only accessible by the owner:
chmod 600 ~\/.ssh\/id_rsa<\/code><\/li>\n\n\n\n
Verify the permissions of the ~\/.ssh<\/kbd> directory:
chmod 700 ~\/.ssh<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n
Misconfigured authorized_keys<\/kbd> File<\/strong>: This file on the server side must have the correct public key and appropriate permissions.\n\n- Check that your public key is correctly copied into ~\/.ssh\/authorized_keys<\/kbd> on the server.<\/li>\n\n\n\n
- Ensure the permissions are set properly:
chmod 600 ~\/.ssh\/authorized_keys<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n
Mismatch Between Public and Private Keys<\/strong>: Double-check that the private key on your client matches the public key in the server\u2019s authorized_keys<\/kbd> file.\n\n- Use the ssh-keygen<\/kbd> command to verify keys:
ssh-keygen -lf ~\/.ssh\/id_rsa.pub<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\nssh-agent<\/kbd> Not Running<\/h3>\n\n\n\nThe ssh-agent<\/kbd> is essential for managing SSH keys in memory during a session. If it is not running, you may encounter repeated prompts for your SSH key passphrase or failure to connect.<\/p>\n\n\n\n
How to Start and Add Keys Again:<\/strong><\/p>\n\n\n\n
\n- Start the ssh-agent<\/kbd> manually:
eval $(ssh-agent -s)<\/code><\/li>\n\n\n\n
Add your SSH key to the agent:
ssh-add ~\/.ssh\/id_rsa<\/code>\n\n- If you encounter errors, ensure the correct private key path is specified.<\/li>\n<\/ul>\n<\/li>\n\n\n\n
- To confirm the key has been added:
ssh-add -l<\/code><\/li>\n<\/ol>\n\n\n\nIncorrect ~\/.ssh\/config<\/kbd><\/h3>\n\n\n\nThe ~\/.ssh\/config<\/kbd> file can simplify the use of multiple SSH keys but must be configured correctly to avoid connection issues.<\/p>\n\n\n\n
How to Validate and Correct Config Files:<\/strong><\/p>\n\n\n\n
\n- Check for Syntax Errors<\/strong>: Use the following command to quickly validate the file:
ssh -F ~\/.ssh\/config -T<\/code>\n\n- This will identify any invalid syntax or unsupported options.<\/li>\n<\/ul>\n<\/li>\n\n\n\n
- Correct Configurations for Multiple Keys<\/strong>:\n
\n- Ensure each host configuration block is defined clearly. For example:
Host github.com
HostName github.com
User git
IdentityFile ~\/.ssh\/id_rsa_github
Host server.example.com
HostName server.example.com
User your_username
IdentityFile ~\/.ssh\/id_rsa_server<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n
Apply Changes<\/strong>: Restart the SSH session or manually test the connection:
ssh -v your_username@server.example.com<\/code><\/li>\n<\/ol>\n\n\n\nHow to Manage Linux SSH Keys<\/strong><\/h2>\n\n\n\nGiven how critical SSH keys are in granting access to highly sensitive and mission-critical systems, having full visibility of your keys is essential to ensure they are in the right hands. Lack of proper SSH management can leave the organization susceptible to insider attacks orchestrated by disgruntled employees, or even external attacks where hackers steal the SSH keys and have a foothold of your resources. It could also lead to non-compliance by industry regulations such as PCI DSS and HIPAA.<\/p>\n\n\n\n
We will take a high-level approach to the best practices you can implement to effectively manage your organization\u2019s SSH keys and protect valuable business assets.<\/p>\n\n\n\n
1. Use an SSH key manager to automate the handling of SSH keys<\/h3>\n\n\n\n
The initial step in proper management of SSH keys is to take an inventory of the existing keys in your network and consolidate them in a central database. Doing this manually is a daunting and time-consuming task which is often prone to errors. An SSH manager helps you to automatically discover SSH keys within your IT environment and determine which systems they have access to. <\/p>\n\n\n\n
Automation alleviates the cumbersome task of manually combing through each system in search of the keys. Further, a key manager helps identify orphaned SSH or idle SSH keys. Orphaned keys are public keys whose private keys\u2019 whereabouts are unknown. <\/p>\n\n\n\n
Ideally, you do not need to add on another point solution to your already complex IT environment in order to automate SSH key management. A good cloud directory platform<\/a> can be leveraged instead to gain visibility and control of who has access to what IT resource, no matter where they exist. <\/p>\n\n\n\n
2. Apply the principle of least privilege when using SSH keys<\/h3>\n\n\n\nThe concept of least privilege in this case implies that only authorized users should be granted access to SSH keys. <\/p>\n\n\n\n
To enforce this, you need to generate keys and associate them with the authorized users\u2019 accounts. In addition, best practices also recommends the removal of old keys every time an employee exits your organization, which leads us to the next point.<\/p>\n\n\n\n
3. Remove idle and orphaned SSH keys<\/h3>\n\n\n\nUnaudited keys including forgotten, idle, and orphaned keys can be used as backdoors by hackers or disgruntled employees to gain access and sabotage your IT resources. As a systems administrator, this is something you clearly don\u2019t want to risk.<\/p>\n\n\n\n
It\u2019s prudent, therefore, to implement policies that ensure SSH keys associated with employees who have off-boarded are purged \u2014 much the same way you would disable email accounts of employees leaving the company. Otherwise, you will have old and orphaned keys lying around that can be exploited by unauthorized parties for nefarious reasons.<\/p>\n\n\n\n
The best SSH key lifecycle automation tool<\/a> will be tied directly to a comprehensive identity and access management solution. This allows IT admins to securely manage users, systems, and access privileges from a single place, and automate employee offboarding with ease. <\/p>\n\n\n\n
4. Use different keys for different users and environments<\/h3>\n\n\n\nGood SSH key management demands that you have unique SSH keys for specific users and IT environments or servers. <\/p>\n\n\n\n
For example, SSH keys assigned to IT administrators with access to production servers should be different from those assigned to developers who should only have access to the staging server. It\u2019s not hard to imagine what the outcome could be if a malicious user accessed the SSH keys of a user in one environment who also happens to use the same keys to access several other IT systems.<\/p>\n\n\n\n
5. Periodically rotate your SSH keys <\/h3>\n\n\n\n
As your IT team grows over time and different members take on different projects, constant changes in access privileges become the norm. These constant changes can present vulnerabilities to your infrastructure when users access keys that should be a reserve for other users or left behind by off-boarded users.<\/p>\n\n\n\n
To minimize such risk, it’s always recommended to periodically rotate SSH keys. The cycle of rotation may vary from one organization to another and according to your organization\u2019s audit policies. During SSH key rotation, new keys are generated to replace the old ones. Along the way, idle keys are also purged. By doing so, the chances of your systems being compromised are minimized to the lowest possible margin.<\/p>\n\n\n\n
Your SSH key management strategy<\/a> is only as effective as the policies you have put in place to ensure the safety of the SSH keys and that they are in the right hands. Policies that merely appear on paper count for nothing. It behooves you, as the system administrator, to enforce strict policies that govern the usage of SSH keys and ensure accountability by those handling them. <\/p>\n\n\n\n
Using alternative SSH key types can enhance security and compatibility. Common key types include:<\/p>\n\n\n\n
ssh-keygen -t ecdsa -b 521<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n
Ed25519<\/strong>:\n
ssh-keygen -t ed25519<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n
When to Use Different Key Types<\/strong>:\n
Restricting key usage adds an additional layer of security by limiting the actions a key can perform on the server.<\/p>\n\n\n\n
Configuration Options in authorized_keys<\/kbd>:<\/strong><\/p>\n\n\n\n
command=”\/usr\/bin\/your_script”,no-port-forwarding ssh-rsa AAAAB3… user@host<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n
no-agent-forwarding<\/strong>:\n\n- Disables agent forwarding to prevent the key from being used elsewhere.<\/li>\n\n\n\n
- Example:
no-agent-forwarding ssh-rsa AAAAB3… user@host<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n
no-pty<\/strong>:\n\n- Disallows the allocation of a pseudo-terminal.<\/li>\n\n\n\n
- This is useful for automated tasks where interactive sessions are not needed.<\/li>\n\n\n\n
- Example:
no-pty ssh-rsa AAAAB3… user@host<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\nApplying Restrictions<\/strong>:<\/p>\n\n\n\n
\n- Open the authorized_keys file on the server:
nano ~\/.ssh\/authorized_keys<\/code><\/li>\n\n\n\n
Add the desired restrictions before the public key entry.<\/li>\n<\/ul>\n\n\n\nThese restrictions help mitigate risks by ensuring that keys are only used for their intended purposes.<\/p>\n\n\n\n
Secure Linux SSH Key Management With JumpCloud<\/strong><\/h2>\n\n\n\nSSH keys provide a more secure and convenient way of authenticating to remote systems compared to the traditional username\/password authentication approach. However, for this authentication to continue to provide secure access to your resources, stringent management of the SSH keys is required, along with sound policies that ensure proper visibility of all the keys, what authorization each key has, and who gets to use which keys. <\/p>\n\n\n\n
SSH key management has historically been challenging, due to the sheer number of SSH keys and the types of access many organizations must manage. Fortunately, hassle-free SSH key management<\/a> can be a reality in today\u2019s IT world thanks to the JumpCloud Directory Platform<\/a>.<\/p>\n\n\n\n
Ready to test our open-protocol, vendor-agnostic directory platform with SSH support for yourself? Sign up for JumpCloud Free<\/a> today, no credit card required, and start managing your first 10 users and 10 devices for free as long as you need until you scale to more.<\/p>\n","protected":false},"excerpt":{"rendered":"