What are SSH Keys?

What are SSH Keys?

What Are SSH Keys?

SSH keys are a pair of cryptographic keys used for secure authentication between a client and a server over the Secure Shell (SSH) protocol. They are commonly used to access remote systems securely without needing a password.

1. How SSH Keys Work

SSH keys use public-key cryptography, where two keys are generated:

🔑 Key Pair Components

  1. Public Key (id_rsa.pub)

    • Can be shared with anyone.
    • Stored on the server to allow secure access.
  2. Private Key (id_rsa)

    • Must be kept secret and stored securely on your local machine.
    • Used to authenticate and decrypt communication.

When you attempt to connect to a remote server, the server checks if your private key matches the public key stored on the server. If they match, you are granted access without entering a password.

2. Why Use SSH Keys?

More Secure than Passwords – SSH keys are harder to crack than passwords.
Convenient for Automation – No need to manually enter passwords every time.
Prevents Brute-Force Attacks – Only the private key holder can access the system.
Essential for Git Authentication – Used for services like GitHub, GitLab, Bitbucket to push/pull code securely.

3. How to Generate SSH Keys?

Step 1: Open the Terminal

On macOS/Linux, open the terminal.
On Windows, use Git Bash or PowerShell.

Step 2: Generate the SSH Key Pair

Run the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

🔹 -t rsa → Specifies RSA key type.
🔹 -b 4096 → Sets a 4096-bit key for extra security.
🔹 -C "your_email@example.com" → Adds a comment (usually your email).

Step 3: Save the SSH Key

  • It will ask for a filename to save the key. Press Enter to save it in the default location (~/.ssh/id_rsa).
  • You can also set a passphrase for additional security (optional).

4. How to Use SSH Keys?

🔹 Add Your SSH Key to the SSH Agent

To ensure your key is used correctly, run:

eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa

🔹 Copy Your Public Key

To copy your public key to use on a server or Git provider:

cat ~/.ssh/id_rsa.pub

Copy the output and add it to your remote server or GitHub/GitLab settings.

🔹 Add SSH Key to GitHub/GitLab

5. Testing Your SSH Connection

To verify that your SSH key is working, run:

ssh -T git@github.com

If successful, you will see a message like:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

This means your SSH key is properly set up for GitHub authentication.

6. Using SSH Keys for Remote Server Access

To connect to a remote server using SSH:

ssh username@remote-server-ip

If your public key is on the remote server, you can log in without entering a password.

7. Deleting or Replacing SSH Keys

To remove an old SSH key:

rm -rf ~/.ssh/id_rsa ~/.ssh/id_rsa.pub

Then, generate a new key and update it in your Git or server settings.

🔹 Conclusion

SSH keys provide a secure, password-free way to authenticate with remote servers and Git repositories. By using SSH keys, you increase security while making authentication faster and more convenient.

🚀 Do you need help setting up SSH keys for GitHub or a remote server? Let me know! 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close