As IT has become widespread, some of the most common questions of recent years have been "how can I automate this?", "how much will this speed up my work?" and "can it integrate with my devices?". Increasingly, though, questions such as "how do I secure it?" and "could someone steal my data through it?" are joining them. There is no denying that the convenience that came with IT solutions, and with putting more and more data in the cloud, has a consequence: we think differently about protecting our information.

We know that using passwords is not the only way to protect against unauthorized access. What is more, it is not as good a barrier as it might seem. In more sensitive cases, cryptography and what it offers can be very useful or even necessary. The best-known option is the use of keys, commonly called SSH keys. The abbreviation may sound familiar to many people, and rightly so: we have all heard of "logging in over the SSH protocol", and the keys are, well, key to it.

So let's look at what they are.

What are SSH keys used for?

SSH keys (short for Secure Shell) are files that contain strings of characters which let a server identify us as users. Logging in with an SSH key is therefore an alternative way of proving that we are who we say we are, so we do not have to enter a password (unless the key itself is protected by one, which is possible: it is called a passphrase). That convenience is one of the advantages of this method of authentication: there is no need to remember and type a code, and integration with various tools gets easier. An even bigger advantage, though, is better security.

I wrote "files", not "file", for a reason. An SSH key consists of two strings of characters: a private key and a public key. As the names suggest, the first should always stay with us alone, while the second we can (and in fact must) send to the server we want to log in to. The point of the whole scheme is that the keys match each other and make it possible to encrypt and decrypt data: they speak a language only the two of them know. One is used to encode information and the other to decode it, which makes it safe to send values over the network.

SSH keys underlie other authentication methods, the best known of which are SSL certificates. The use of a key is easiest to see in access to servers, but programmers know well that this method is also popular when working with repositories, mainly for convenience. In that case, remember to upload only the public key to services such as GitHub, Bitbucket or Gogs.

What does an SSH key look like?

As I mentioned, an SSH key is two files, which most often have default names:

  • private key: usually the file id_rsa, which we keep on our own disk and never send anywhere.
  • public key: usually the file id_rsa.pub, which we can send to any place where we want to authenticate this way.

The simplest pair of files can look like this. First, a sample private key (I changed a few things inside for safety):

-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAzbQVgfXe985M1/4hOgddtCI+IWNDcyhDGjloBLxY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DWufJ6SgMR9LscPFf7aeG1KJ42wWr0tRo9piSxXBTMRkzt0QxKp6VjkW9gIJHQYe0+R0
svPhVYUizMoOPMyjCaBUm/RTyDQO0nR3zQg5HBy6gqgUAk9Vg5jxES65FqPRQj2J7i+rr/
C3Xj+4cLvu8n9Cj192OAYatwsmf20BnTR7FQs3jGIjeuhhWx+97A+e2PzZ/jOu/g9v5w/E
o+Evt5LBTrA/kAAAAUdXNlckBERWNLVE9QLUoxU0FVUTcBAgMEBQYH
-----END OPENSSH PRIVATE KEY-----

The public key, meanwhile, is a bit shorter and also contains a comment, such as the name of the computer it was generated on:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDNtBWB9d73zkzX/iE6B120Ij4hY0NzKEMaOWgEvFiH5ZpRxsTxPR2fA6FONqwUerQctRViaONl7cFWrUvrI6dfV0b87Z0fuzEFYy9lByLSNl96d5apm4v1KaAkxFc0AOZITCxQ/Si5oj8UcyRg2cuzJ5GGviYWTrQHxghBOGSSIlvOBsOEX0U5c6yORX43rSYAKFSI2sPlV5YggacMILlTPzmAwjJUhJryL/V0fv0iGSlqC3WAJRJ66D54GD8LqxGP+MDkQLIVjnXPJhd/v1cv5X4JAsmMtPuqvBbOg/rVdLf143QdqnbIN2Jd77B32aSI+I+PND0VibeEHBKlwG5HKu9u8kJqaIrSmp0xO/mNG3mPpyeFMkuPCm3GiXf6nve1Bl7iIYN56kmln2eXfHxF8OEjsAmb4PLvIVBMLDQf0Hdfhw2+TL1O2XZmi2m6lppWr8T9Ea0K2V2GKwdffVhPG/QwShs1HXKzlZkP8LGdYUTtXz72HdvYKKM1Mi6zcBU= user@DESKTOP-ABC1234

This is the default version. Nothing prevents you from generating a longer key with more bits, which means better security but also lower performance. Keys are usually stored in the user's home directory, in the .ssh folder. On Linux that is ~/.ssh (or, as a full path, /home/nazwauzytkownika/.ssh), and on Windows the default location is C:/Users/nazwauzytkownika/.ssh.

The string shown above was encoded with the RSA algorithm (named after its creators, Rivest, Shamir and Adleman), which under the hood relies on operations on very large prime numbers. It is the default encryption method available for SSH, although there are others, used less often, such as ECDSA.

How do you generate an SSH key?

A key is most often generated with the ssh-keygen command, which we will cover in a moment. That does not mean we are stuck with it, or that we have to do it on Linux. If you have Git installed on Windows along with Git GUI, it offers an option to generate a key pair with a single click. The even more popular PuTTY can do it too. The command itself is also available in Git Bash or PowerShell after installing the appropriate extension. Let's focus on the basic command, though.

The simplest call of ssh-keygen will:

  • check whether keys already exist in the default directory; if so, the program asks whether to replace them (be very careful here, because you will lose the previous keys for good),
  • ask you to enter and repeat a passphrase, which protects the key additionally (it is very often left empty),
  • show a message that the operation succeeded, along with the key's so-called fingerprint, used to verify that you are connecting to the right machine.

So it is quite simple, but sometimes we want to change something: put the key in a different file, use stronger encryption and so on. Fortunately, the command offers a few options, of which several are especially useful:

  • -t: the encryption method (RSA by default)
  • -b: the number of bits in the key (that is, its length)
  • -f: the location and name of the file the keys will be saved to (~/.ssh/id_rsa by default)

So you can generate a key with parameters other than the defaults like this:

ssh-keygen -t ecdsa -b 384 -f ./my_key

Is an SSH key secure?

Like most recognized methods of protecting data and access to resources, SSH keys are not 100% secure. They are good enough, though (say, 99.8%), that if we have the option, we should use them instead of entering a plain password. So where does the secret lie?

In the complexity of the key itself. Remember that the public key is open and could theoretically be "stolen". But to use it without authorization you also need the contents of the private key, and that is very hard to "guess". It is a bit like losing the lock of a chest but not the key that opens it. Still, with enormous computing power or gigantic luck, breaking a key is possible, so we cannot say that SSH keys are impossible to forge. They give enough assurance, however, that we would have to use really weak and short strings to have any real reason to fear for our security.

For ordinary use of the Internet we usually do not need to know how these keys are created or what they are for. That does not mean the topic is not worth knowing, and for programmers it is simply essential for working comfortably with various services and administrators. After all, the more we know, the better we understand what is going on around us, and when it concerns our security as well, the choice gets even easier.

Best regards and thank you, Jakub Rojek.