DEV Community

jun liu
jun liu

Posted on

How to Connect to Remote Servers Without Typing a Password

How to Connect to Remote Servers Without Typing a Password

As an operations engineer, I've spent years SSHing into remote servers. The frustration of typing passwords on cold terminals at 3 AM is real. Then I discovered SSH key authentication — and never looked back.

Today I'll show you how to set it up in under 5 minutes. No GUI tools, no expensive software. Just the command line.

Why SSH Keys Beat Passwords

Before we dive in, here's why this matters:

  • Brute-force resistant: A 4096-bit RSA key has more combinations than atoms in the observable universe (well, close enough)
  • Convenient: One ssh user@host and you're in — no typing passwords
  • Audit-friendly: Each key is traceable to a specific person/machine
  • Automation-ready: Scripts, CI/CD pipelines, Ansible playbooks — all love key auth

And yes, password auth still has its place (temporary access, shared machines). But for anything you touch regularly, keys are the way to go.

Step 1: Generate Your Key Pair

Open your terminal and run:

ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Why Ed25519? It's faster and more secure than the old RSA default. If you're working with legacy systems that don't support it, fall back to ssh-keygen -t rsa -b 4096.

You'll be asked three questions:

  1. File location: Just press Enter to use the default (~/.ssh/id_ed25519)
  2. Passphrase: Strongly recommended. This encrypts your private key on disk. Use a password manager if you need help remembering one.
  3. Confirm passphrase: Type it again

You now have two files:

  • ~/.ssh/id_ed25519 — your private key (keep this secret)
  • ~/.ssh/id_ed25519.pub — your public key (share this freely)

Step 2: Copy the Public Key to the Server

The easiest method uses ssh-copy-id:

ssh-copy-id username@server_ip
Enter fullscreen mode Exit fullscreen mode

You'll be prompted for the server password one last time. That's it — the public key gets appended to ~/.ssh/authorized_keys on the remote machine.

Manual Method (if ssh-copy-id isn't available)

# Show your public key
cat ~/.ssh/id_ed25519.pub

# SSH into the server and append it manually
ssh username@server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# Then paste the output from the previous command
Enter fullscreen mode Exit fullscreen mode

Step 3: Test and Lock It Down

Before disabling password auth, test your new key:

ssh username@server_ip
Enter fullscreen mode Exit fullscreen mode

If it logs you in without a password prompt, you're golden. If not, check:

  • File permissions: chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
  • The public key is on one line in authorized_keys
  • You're using the right private key: ssh -i ~/.ssh/id_ed25519 username@server_ip

Once confirmed, harden the server:

sudo nano /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

Set:

PasswordAuthentication no
PubkeyAuthentication yes
Enter fullscreen mode Exit fullscreen mode

Then restart: sudo systemctl restart sshd

Warning: Keep your current session open while testing! If something goes wrong, you can fix it before locking yourself out.

Bonus: Managing Multiple Keys

As your infrastructure grows, you'll accumulate keys. Here's how to stay sane:

~/.ssh/config file:

Host production
  HostName 192.168.1.100
  User deploy
  IdentityFile ~/.ssh/id_production

Host staging
  HostName staging.example.com
  User admin
  IdentityFile ~/.ssh/id_staging
Enter fullscreen mode Exit fullscreen mode

Then just ssh production or ssh staging.

Key rotation: Every 90 days, generate new keys and rotate them. Old keys go in a revoked_keys file on servers (check before authorized_keys).

Troubleshooting Cheat Sheet

Symptom Fix
"Permission denied (publickey)" Check permissions: chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
"Agent admitted failure to sign" ssh-add ~/.ssh/id_ed25519
Key works locally but not from script Use ssh-agent or IdentityFile in config
"Too many authentication failures" Use -o IdentitiesOnly=yes flag

The Bottom Line

SSH key authentication takes 5 minutes to set up and pays dividends for years. No more password fatigue, no more brute-force worries, just clean secure access.

Full guide with screenshots and edge cases: https://ops.jiunotes.top/ssh-key-auth-setup

More DevOps notes: ops.jiunotes.top

Top comments (0)