Projects

SSH Keys vs Passwords: Secure Server Access

SSH Keys vs Passwords – SSH Keys vs Passwords: Secure Server Access

In this guide: SSH keys vs passwords for secure server access: how SSH authentication works, generating keys, adding them to your server, disabling password login, and…

If you connect to a VPS or hosting account via SSH with a password, your server is exposed to constant brute-force attempts. SSH keys are a much more secure and convenient alternative — and they take only a few minutes to set up.

How SSH authentication works

Password authentication:
You type a password; the server checks it. Anyone who guesses or steals the password can log in. Bots constantly try common passwords against SSH servers.

Key-based authentication:
You create a key pair:

  • Private key: stays on your computer, secret, never shared.
  • Public key: placed on the server in ~/.ssh/authorized_keys.

When you connect, the server uses your public key to create a challenge that only your private key can answer correctly. The private key never leaves your computer.

Benefits:

  • Practically impossible to brute-force with modern key types.
  • No password sent to the server.
  • Convenient: no typing passwords (especially with an SSH agent).
  • Easy to revoke: remove a public key from the server to revoke access.

Generating SSH keys

On macOS, Linux, or Windows 10/11 (PowerShell or Windows Terminal include OpenSSH):

ssh-keygen -t ed25519 -C "your-email@example.com"
  • Ed25519 is a modern, secure, and fast key type. (If a legacy system requires it, use ssh-keygen -t rsa -b 4096.)
  • Press Enter to accept the default location (~/.ssh/id_ed25519).
  • Set a passphrase — it encrypts your private key on disk. If your laptop is stolen, the key can’t be used without it.

You now have:

  • ~/.ssh/id_ed25519 → private key (keep secret)
  • ~/.ssh/id_ed25519.pub → public key (safe to share)

Use an SSH agent so you enter the passphrase once per session:

ssh-add ~/.ssh/id_ed25519

Password managers like 1Password and some others can also store SSH keys and act as an SSH agent.

Adding keys to your server

Option 1 — ssh-copy-id (Mac/Linux):

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-server.com

Option 2 — Manually:

  1. Copy the contents of id_ed25519.pub.
  2. Log in to the server and run:
    bash
    mkdir -p ~/.ssh && chmod 700 ~/.ssh
    nano ~/.ssh/authorized_keys # paste the key on a new line
    chmod 600 ~/.ssh/authorized_keys

Option 3 — Hosting control panel:
cPanel (“SSH Access → Manage SSH Keys → Import Key”), hPanel, Plesk, and cloud providers (when creating a VPS) let you add public keys through the dashboard.

Test the connection in a new terminal window:

ssh user@your-server.com

You should connect without a password prompt (other than your key passphrase if not using an agent).

Disabling password login

Once key login works, disable passwords on your VPS:

  1. Keep your current session open (so you can fix mistakes).
  2. Edit the SSH config:
    bash
    sudo nano /etc/ssh/sshd_config
  3. Set:
    PasswordAuthentication no
    PermitRootLogin prohibit-password

    (Or PermitRootLogin no if you use a separate sudo user — recommended.)
    Some systems also include settings in /etc/ssh/sshd_config.d/ — check those files too.
  4. Restart SSH:
    bash
    sudo systemctl restart ssh # or sshd, depending on the system
  5. Test in a new terminal before closing your original session.

Additional hardening:

  • Use a firewall to allow SSH only from needed IPs (if practical).
  • Install Fail2ban or similar to block repeated failed attempts.
  • Keep the server updated.

On shared hosting, you typically can’t change server-wide SSH settings, but you can still use keys for your account.

Managing keys safely

  • ☐ One key per device (laptop, desktop) — revoke individually if a device is lost.
  • ☐ Always use passphrases.
  • ☐ Never share private keys — not with teammates, not in chat, not in Git repositories.
  • ☐ Give each team member their own key and remove it when they leave.
  • ☐ Audit authorized_keys regularly and remove unknown or old keys.
  • ☐ Back up private keys securely (encrypted, e.g., in a password manager).
  • ☐ Use separate keys for different purposes if needed (e.g., GitHub vs servers), managed via ~/.ssh/config.

Example ~/.ssh/config:

Host myserver
    HostName your-server.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519

Now connect with just ssh myserver. New to the command line? See Terminal Basics Every Website Owner Should Know.

Further reading: OWASP Top 10 web security risks

Key Takeaways: SSH Keys vs Passwords

  • SSH keys are far more secure than passwords.
  • Generate Ed25519 keys with a passphrase.
  • Add public keys to the server via ssh-copy-id, manually, or your control panel.
  • Disable password login on VPS after testing key access.
  • Use one key per device and remove keys when access ends.
Z
zasma171

Writes for Hostreta about the topics covered in this article.

Comments (0)

Your email address will not be published. Required fields are marked *

Recent Comments

No comments to show.
Ad · AdSense
Ad space 300×250
Affiliate links
Tools we recommend
Protect Your Website with Automated Daily Backups
$2.09
Get it
the easiest way to manage all of your social media accounts
$29
Get it
Starter Website Get online, properly
$249
Get it
google workspace Business Starter
$3.75
Get it
Nord Vpn Cybersecurity. Built for Everyday Use
$8.99
Get it

We may earn a commission on purchases made through these links, at no extra cost to you.

Ad · AdSense
Ad space 300×600
Ad · AdSense
Ad space 300×600

$ top hosting picks

affiliate
★★★★★

NovaHost

$2.95/mo,renews at $9.95
  • Free SSL + domain, first year
  • 1-click WordPress install
  • 24/7 chat, sub-5-min response
get this deal →
affiliate link · we may earn a commission
★★★★★

CloudRoot

$4.50/mo,renews at $12.95
  • Built-in CDN + caching
  • Daily backups, 30-day retention
  • Staging environments included
get this deal →
affiliate link · we may earn a commission
★★★★★

AI SiteBuilder

$1.49/mo,renews at $13.99
  • 5 Websites
  • Emails & Security Included
  • AI Website Builder for WordPress
get this deal →
affiliate link · we may earn a commission

Never miss a new article

Join thousands of developers and builders getting a weekly digest of the best tech articles.