Jses.ioby Shaojiang

How to use ssh-keygen to generate a new SSH key and use it?

Shaojiang

Date:

How to use ssh-keygen to generate a new SSH key and use it?

1. TL;DR

Command to generate SSH key: $ ssh-keygen -t ed25519 -C "your-email@emailsite.com".

2. Introduction

SSH (Secure Shell) key is a cryptographic authentication method that allows secure and authenticated connections between different systems over a network. It is primarily used for remote login or remote command execution on servers, providing a secure alternative to traditional password-based authentication.

An SSH key consists of a pair of cryptographic keys: a public key and a private key. The public key, as the name suggests, is shared with the systems you want to authenticate with. It is stored on the remote server or system you wish to access. The private key, on the other hand, is kept securely on the client system from which you initiate the SSH connection.

When you establish an SSH connection, the client system uses its private key to encrypt a message, and the server system uses the corresponding public key to decrypt and verify the authenticity of the message. This process ensures secure communication and prevents unauthorized access to the server.

SSH keys offer several advantages over traditional password-based authentication. They eliminate the need to transmit and store passwords, reducing the risk of password theft and brute-force attacks. SSH keys are also generally longer and more complex than passwords, making them harder to guess or crack. Furthermore, SSH keys are not tied to any specific user, allowing multiple users to authenticate with the same key pair if necessary.

Overall, SSH keys provide a secure and efficient way to authenticate and establish secure connections between systems, making them a popular choice for system administrators and developers working with remote servers.

3. Generate SSH key using ssh-keygen

ssh-keygen is a tool for creating new authentication key pairs for SSH. Such key pairs are used for automating logins, single sign-on, and for authenticating hosts.
1root@localhost:~# ssh-keygen -t ed25519 -C "your-email@email-site.com"
2Generating public/private ed25519 key pair.
3Enter file in which to save the key (/root/.ssh/id_ed25519):
4Enter passphrase (empty for no passphrase):
5Enter same passphrase again:
6Your identification has been saved in /root/.ssh/id_ed25519
7Your public key has been saved in /root/.ssh/id_ed25519.pub
8The key fingerprint is:
9SHA256:9iEFgQvSEqqq..........WqVDEu90hc1ik your-email@email-site.com
10The key's randomart image is:
11+--[ED25519 256]--+
12| .o .o. .o==B+|
13| .o o . . .+E===|
14|. o . . .So==. |
15|. . ....=oo |
16|. .S.o... . |
17|. .. o o |
18|. . . . |
19|. o. + . |
20|..+..+ . |
21+----[SHA256]-----+
22root@localhost:~# cat ~/.ssh/id_ed25519.pub
23ssh-ed25519 AAAA...................VL7xrix caishaojiang@gmail.com
24

4. Use SSH key on GitHub

GitHub, a popular web-based platform for version control and collaboration, leverages SSH keys to provide secure and authenticated access to its services. GitHub allows users to generate and manage SSH keys for their GitHub accounts, enabling them to securely interact with repositories and perform various Git operations.

When a user generates an SSH key, they obtain a public key that needs to be added to their GitHub account settings. GitHub then associates this public key with the user's account. When the user wants to interact with their repositories, they can use the corresponding private key stored on their local system.

By using SSH keys, GitHub ensures secure and authenticated connections between users' local systems and their repositories hosted on GitHub's servers. This authentication method eliminates the need for passwords and provides an extra layer of security. It also allows for convenient and seamless integration with Git, making it easier for developers to clone, push, and pull repositories without repeatedly entering passwords.

From the step above, we have generated the SSH key, now run cat /root/.ssh/id_ed25519.pub to read content of the public key. Remember to copy the .pub file content, which should be like:

1root@localhost:~# cat /root/.ssh/id_ed25519.pub
2ssh-ed25519 AAAAC3...................VL7meExrix your-email@email-site.com

Go to your GitHub settings page: Settings -> SSH and GPG keys -> New SSH key:

Give it a readable name, then paste the text content of /root/.ssh/id_ed25519.pub, click Add SSH key button. Now you should be able to access to GitHub from you machine, such as cloning some private repo: git clone git@github.com:yourgithubusername/yourprivaterepo.git.

Done.