Introduction to SSH Keys
Secure Shell (SSH) is a protocol used to securely connect to remote servers. SSH keys provide a more secure and convenient alternative to password-based authentication. This tutorial explains how to access your SSH public key, which is crucial for setting up SSH access to services like GitHub, GitLab, or remote servers.
Understanding SSH Key Pairs
SSH keys come in pairs: a private key and a public key.
- Private Key: This key should never be shared. It’s kept secret on your local machine and used to prove your identity to the server.
- Public Key: This key is safe to share. You add it to the remote server or service to authorize your access. When you connect, the server uses your public key to verify that you possess the corresponding private key.
Locating Your SSH Public Key
By default, SSH keys are stored in the .ssh
directory within your home directory. This directory is often hidden. Let’s explore how to access the public key files:
1. Navigating to the .ssh
Directory
Open your terminal and use the cd
command to navigate to the .ssh
directory:
cd ~/.ssh
2. Listing Public Key Files
Once inside the .ssh
directory, you can list the public key files using the ls
command:
ls *.pub
This will display files like id_rsa.pub
, id_dsa.pub
, or id_ed25519.pub
. The specific file name depends on the type of key you generated. id_rsa.pub
is the most common if you used the default settings when creating the key.
3. Displaying the Public Key
To view the contents of your public key file, use the cat
command:
cat ~/.ssh/id_rsa.pub
Replace id_rsa.pub
with the appropriate file name if you used a different key type. This will output the entire public key string to your terminal.
4. Copying the Public Key
You’ll need to copy this public key to configure SSH access on remote servers or services. The method for copying varies depending on your operating system:
-
macOS:
pbcopy < ~/.ssh/id_rsa.pub
This command copies the key to your clipboard.
-
Linux: You can simply select and copy the output from the
cat
command in your terminal. -
Windows: Use a text editor like Notepad to open the public key file (e.g.,
~/.ssh/id_rsa.pub
). Select all the text and copy it. Be careful to avoid adding any extra spaces or line breaks.
Generating a New SSH Key (If Needed)
If you haven’t already generated an SSH key pair, you can do so using the ssh-keygen
command:
ssh-keygen -t rsa -b 4096
This command generates a new RSA key pair with a key size of 4096 bits (a good security practice). You’ll be prompted for a file name to save the key (the default is id_rsa
) and a passphrase (optional, but recommended for added security).
Utilizing Your Public Key
Once you’ve copied your public key, you can add it to the authorized_keys
file on a remote server or configure it in the settings of a service like GitHub. This allows you to connect to the server or service without entering a password. Refer to the documentation of the specific server or service for detailed instructions.