Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joglomedia/a14e7f6626f422b5a9f6a59325692b63 to your computer and use it in GitHub Desktop.
Save joglomedia/a14e7f6626f422b5a9f6a59325692b63 to your computer and use it in GitHub Desktop.
How to generate an SSH key and add public key to the server for authentication
## Generate a new SSH key
Follow this command, ensure that you change your-identifier with your own, it could be an unique name or email address (recommended)
```
$ ssh-keygen -t rsa -b 4096 -C "your-identifier"
```
An output from those command look like below.
If asked, simply press enter to follow the suggested value (/home/yours/.ssh/id_rsa)
```
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yours/.ssh/id_rsa):
```
In the next step you'll be asked to enter a passphrase, you can enter your passphrase (keep it secret) or empty for no passphrase
```
Enter passphrase (empty for no passphrase):
```
If you decided to protect your SSH key with phassprase, you should re-enter it again for verification
```
Enter same passphrase again:
```
Lastly, you'll see a final message that indicate the key creation is completed, such as below
```
Your identification has been saved in /home/yours/.ssh/id_rsa.
Your public key has been saved in /home/yours/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:5ewGn/33dXEqNpuhIFS/lUmvdxZqvQM8cEQ44BI9x5k rsapub2020
The key's randomart image is:
+---[RSA 4096]----+
| ...o =. |
| oo E . |
| ...+ + |
| ..= o = |
| . S + B .o.|
| . + = =o.+|
| . . * Bo=.=|
| . o o.O +=|
| . o .o+|
+----[SHA256]-----+
```
## Add your key to the ssh-agent
To configure your ssh-agent program to use your SSH key, ensure the ssh-agent is enabled by executing the following command
```
$ eval $(ssh-agent -s)
```
If you see an output like the below line, your ssh-agent is ready
```
Agent pid 12835
```
Then, add your SSH key to the ssh-agent
```
$ ssh-add ~/.ssh/id_rsa
```
If you're decided to use passphrase during the SSH key creation, you'll be asked to enter the passphrase here
```
Enter passphrase for /home/yours/.ssh/id_rsa:
```
You'll see the following message indicating that your SSH key identity has been added
```
Identity added: /home/yours/.ssh/id_rsa (your-identifier)
```
## Add your SSH key to the remote server / VPS / Cloud
To add your public SSH key to the server, you need to copy the public SSH key (generated from the previous step) to the server.
Follow command below and ensure to substitute "username" with your username on the server, and "server.address.com" with the host/domain name, it is recommended to use IP address of your server
```
$ cat ~/.ssh/id_rsa.pub | ssh username@server.address.com 'cat >> ~/.ssh/authorized_keys'
```
The server will then prompt you asking for username's password
```
username@server.address.com's password:
```
Finally, you should now able to connect to the server without having to authenticate (except if you setup your SSH key with passphrase, you'll be asked for your passphrase).
===
@ https://eslabs.id/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment