W3docs

SSH Key

What are SSH key, what types do they have and how you can set it up on Mac, Linux or Windows? Find all the useful information and see codes.

What are SSH Keys?

SSH Key

SSH keys are an access credential used in the SSH protocol (Secure Shell). A secure shell is a network protocol, which helps to login from one computer to another securely, as well as to manage networks, operating systems, and configurations. Functionally, SSH keys are nearly the same as passwords, but it’s more secure to log into a server with SSH keys than use only passwords because decrypting the key is very difficult, almost impossible.

SSH keys always come in pairs, and each of these pairs is composed of a public key and a private key. These two are types of keys.

  1. Public keys, also known as authorized keys, determine who can access each system.
  2. Private or identity keys identify users and give them access.

How To Set Up SSH Keys?

You need public-key cryptographic algorithms to generate SSH keys, the most commonly used of which are Ed25519 and RSA keys. SSH keys are set up with the help of a key generation tool.

Create an SSH key on Mac and Linux

As these two operating systems have modern terminal applications, that combine with SSH package, the steps of creating an SSH key are the same for both of them.

  1. Open a command terminal and run the following:

set up SSH Key

ssh-keygen -t ed25519 -C "[email protected]"
  1. Then you have to choose the file location.

save SSH Key

> Enter a file in which to save the key (~/.ssh/id_ed25519): [Press enter]
  1. Then you should add a secure passphrase, which will be required any time the SSH key is used.

SSH Key set password

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
  1. Add the new SSH key to the ssh-agent. The ssh-agent is a tool responsible for holding private keys, but it also handles signing requests with private keys for security purposes.

Execute the following command to make sure that the ssh-agent is running:

SSH Key on Linux

eval "$(ssh-agent -s)"
> Agent pid 59566

If the ssh-agent is running, add the new SSH key to the local SSH agent by executing the following command:

SSH Key

ssh-add ~/.ssh/id_ed25519

(Note: On modern macOS (OpenSSH 8.8+), ssh-add -K is deprecated. Instead, add AddKeysToAgent yes to your ~/.ssh/config file to automatically persist keys in the keychain.)

  1. Copy the public key to the remote server. To use the key for authentication, you must add it to the remote server's ~/.ssh/authorized_keys file. Run the following command from your local machine:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_host
  1. Verify the connection. Test that you can log in to the remote server using your new key:
ssh user@remote_host

Create an SSH Key on Windows

You need Git Bash to create an SSH key on the Windows operating system. Open Git Bash and run the following command to generate the key:

ssh-keygen -t ed25519 -C "[email protected]"

Follow the same prompts as above to set the file location and passphrase. After generating the key, copy it to your remote server or Git hosting service using the ssh-copy-id command or by pasting the contents of id_ed25519.pub into the service's SSH key settings.

Practice

Practice

What are important aspects of SSH keys in the context of Git?