DEV Community

Cover image for Mastering SSH Key-Based Authentication: Secure Passwordless Login for Linux and Windows
Richard Chamberlain
Richard Chamberlain

Posted on

Mastering SSH Key-Based Authentication: Secure Passwordless Login for Linux and Windows

When managing remote servers, SSH (Secure Shell) is the standard for encrypted and secure communication. However, relying on passwords for SSH access leaves your systems vulnerable to:

  1. Credential stuffing
  2. Phishing attacks
  3. Keylogging and malware
  4. Man-in-the-middle (MitM) attacks
  5. Password reuse threats
  6. Brute-force dictionary attacks

Table of Contents

  1. Introduction: Why SSH Key-Based Authentication Matters
  2. Why Use SSH Keys?
  3. Generating SSH Keys

If you're looking for a safer, faster, and more reliable way to access your servers, SSH key-based authentication is the solution. In this guide, we’ll show you how to set up passwordless SSH login for Linux and Windows (via PuTTY) and share tips to boost your SSH security posture.


Why Use SSH Keys?

SSH keys are a pair of cryptographic files—a private key and a public key—that authenticate users without a password. This offers several advantages:

  • Enhanced security (especially with stronger keys like 4096-bit RSA or modern Ed25519)
  • Improved convenience (no passwords to remember or type repeatedly)
  • Resilience against common attack vectors

When you connect to a server, your client offers the public key. If the server recognizes it and verifies the corresponding private key, access is granted—no password required.

Let’s walk through setting this up step-by-step.


1. How to Generate SSH Keys

On Linux (OpenSSH)

  1. Open your terminal.
  2. Generate your SSH key pair:
   ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode
  • -t rsa: Specifies the algorithm (RSA).
  • -b 4096: Sets key length for stronger encryption.
  • -C: Optional comment for identifying the key.
  1. Press Enter to accept the default file location (~/.ssh/id_rsa).
  2. Choose an optional passphrase for added protection.
  3. Verify your keys:
   ls -l ~/.ssh/id_rsa*
Enter fullscreen mode Exit fullscreen mode

You’ll see:

  • id_rsa (your private key — keep it safe!)
  • id_rsa.pub (your public key — upload this to servers)
  1. Upload your public key to the server:
   ssh-copy-id user@server_name
Enter fullscreen mode Exit fullscreen mode

This adds the key to the server’s ~/.ssh/authorized_keys file.


On Windows (Using PuTTYgen)

  1. Download PuTTY and open PuTTYgen.
  2. Choose:

    • Key type: RSA
    • Number of bits: 4096
  3. Click Generate and move your mouse to create entropy.

  4. Save your:

    • Private key (id_rsa.ppk) securely (e.g., C:\Users\your_username\ssh)
    • Public key by copying it from the PuTTYgen window
  5. SSH into your server using a password, then:

   echo "your-copied-public-key" >> ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

2. Configuring SSH Clients

Linux: ~/.ssh/config

Simplify server access by creating an SSH config file:

vi ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Add:

Host your_server_alias
    HostName your.server.com
    User your_username
    Port 22
    IdentityFile ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Now connect with:

ssh your_server_alias
Enter fullscreen mode Exit fullscreen mode

Windows: PuTTY

  1. Open PuTTY.
  2. In the Session tab:

    • Host Name: your server’s IP or hostname
    • Port: 22
  3. Go to Connection > SSH > Auth and browse to your .ppk private key.

  4. (Optional) Save the session for quick access later.


3. Testing Your SSH Connection

  • Linux:
   ssh your_server_alias
Enter fullscreen mode Exit fullscreen mode

If configured correctly, you’ll connect without a password prompt.

  • Windows (PuTTY):
    • Load your saved session and click Open.

4. More SSH Features to Explore

SSH does more than log you in—here are a few powerful extras:

Run remote commands:

ssh user@server "uptime"
Enter fullscreen mode Exit fullscreen mode

Secure file transfers with SCP:

  • Upload:
  scp file.txt user@server:/remote/path/
Enter fullscreen mode Exit fullscreen mode
  • Download:
  scp user@server:/remote/path/file.txt .
Enter fullscreen mode Exit fullscreen mode

Create SSH tunnels (port forwarding):

ssh -L 8080:example.com:80 user@server
Enter fullscreen mode Exit fullscreen mode

Use an SSH agent (Linux):

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

5. Troubleshooting SSH Key Issues

  • "Permission denied" errors? Ensure correct file permissions:
   chmod 700 ~/.ssh
   chmod 600 ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode
  • PuTTY not connecting?
    • Make sure you're using the .ppk format
    • Check PuTTY's Event Log for specific errors

6. Bonus Security Tips for SSH Hardening

Want enterprise-grade security? Consider:

  1. Use Ed25519 keys instead of RSA:
   ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode
  1. Disable password authentication: Edit /etc/ssh/sshd_config:
   PasswordAuthentication no
   ChallengeResponseAuthentication no
Enter fullscreen mode Exit fullscreen mode

Then:

   sudo systemctl restart sshd
Enter fullscreen mode Exit fullscreen mode
  1. Restrict SSH access to specific users:
   AllowUsers your_username
Enter fullscreen mode Exit fullscreen mode
  1. Install Fail2Ban or SSHGuard for intrusion detection and brute-force protection.

Final Thoughts

SSH key authentication is an essential skill for anyone working with servers—from home labs to production environments. It significantly improves security, eliminates password fatigue, and opens the door to more advanced remote workflows.

Take the time to set it up properly—your systems (and your future self) will be better protected for it.


Have questions or want to dive deeper into SSH agent forwarding, key rotation strategies, or enterprise hardening? Drop a comment or reach out—we’re here to help.

Postmark Image

20% off for developers shipping features, not fixing email

Build your product without worrying about email infrastructure. Our reliable delivery, detailed analytics, and developer-friendly API let you focus on shipping features that matter.

Start free

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay