Creating SSH Keys
How to generate a public/private key pair for GitHub or for remote SSH access.
Key Location
List existing keys under ~/.ssh
. If any are present, you will see an output similar to this:
ls -l ~/.ssh
total 12
-rw------- 1 user user 3243 jun 1 15:05 id_rsa
-rw------- 1 user user 742 jun 1 15:05 id_rsa.pub
-rw-r--r-- 1 user user 1326 Jun 1 15:05 known_hosts
Create a New Key Pair
In this example we will generate the id_rsa
key pair.
Generate a new SSH key.
ssh-keygen -t rsa -b 4096 -C "user@example.com"
Enter file in which to save the key (/home/user/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user/.ssh/id_rsa. Your public key has been saved in /home/user/.ssh/id_rsa.pub. The key fingerprint is: SHA256:xCYt9XPANH0jk76Td96cU1m1D2htEPamHfu8lWce5Kk user@example.com The key's randomart image is: +---[RSA 4096]----+ | .o+.oo | | + ..+*.o .| | o = o..B+.o| | = o++++.| | S ..+oo+| | + ++*| | o OX| | .=O| | E .o| +----[SHA256]-----+
Start the SSH agent.
eval "$(ssh-agent -s)"
Agent pid 19690
Add the private key to the agent.
ssh-add ~/.ssh/id_rsa
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)
Usage
Copy the public key id_rsa.pub
.
cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAAF...MLZ94Nq2F4ToHR3U6Q== user@example.com
Paste the public key wherever it is required.
For GitHub, go to Settings > SSH and GPG keys.
To allow remote access on another server, append the key to the
authorized_keys
file inside~/.ssh
.cd /home/server/.ssh
nano authorized_keys
Permissions for the Key Directory
Secure the permissions for the directory and its files.
sudo chmod 600 -R ~/.ssh/*
sudo chmod 644 ~/.ssh/known_hosts
sudo chmod 755 ~/.ssh
References
Published: June 1, 2020