Blog
Jan 4, 2025 - 5 MIN READ
SSH Key Authentication Setup Guide

SSH Key Authentication Setup Guide

Learn how to configure SSH key authentication for passwordless remote server login with quick and easy steps.

Jing Zhong

Jing Zhong

SSH key authentication provides a secure and convenient way to access remote servers without entering passwords each time. This guide will walk you through the quick setup process.

Objective

Configure SSH key authentication to enable passwordless login to remote server user@203.0.113.42

Quick Configuration Steps

1. Generate Key Pair

Generate a new SSH key pair using ED25519 algorithm (recommended) or RSA:

ssh-keygen -t ed25519 -C "your_email@example.com"

Options:

  • Use default path: ~/.ssh/id_ed25519
  • Passphrase is optional (leave empty for complete passwordless login)
  • For RSA keys: ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

2. Upload Public Key to Remote Server

Copy your public key to the remote server:

ssh-copy-id user@203.0.113.42

What happens:

  • Enter password once for the initial connection
  • Automatically configures ~/.ssh/authorized_keys
  • Sets correct permissions (700 for .ssh directory, 600 for authorized_keys)

Alternative method (if ssh-copy-id is not available):

cat ~/.ssh/id_ed25519.pub | ssh user@203.0.113.42 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

3. Verify Passwordless Login

Test the configuration:

ssh user@203.0.113.42

You should now be logged in without entering a password!

Understanding SSH Keys

Private Key:

  • Location: ~/.ssh/id_ed25519
  • Purpose: Authentication credential, must be kept secret
  • Permissions: Should be 600 (read/write for owner only)
  • NEVER share this file

Public Key:

  • Location: ~/.ssh/id_ed25519.pub
  • Purpose: Can be freely shared and placed on servers
  • Permissions: Can be 644 (readable by others)
  • Added to ~/.ssh/authorized_keys on remote server

Usage in Deployment Scripts

After configuration, your deployment scripts can run without password prompts:

#!/bin/bash
# scripts/deploy.sh

# Now you can execute commands without password prompts
ssh user@203.0.113.42 "cd /var/www/app && git pull && npm install && npm run build"

# Or copy files without password
scp -r ./dist user@203.0.113.42:/var/www/app/

Security Best Practices

  1. Use a passphrase for your private key (optional but recommended)
  2. Never share your private key (id_ed25519)
  3. Use different keys for different services
  4. Disable password authentication on the server after setting up keys:
    # Edit /etc/ssh/sshd_config
    PasswordAuthentication no
    
  5. Regularly rotate keys for critical systems
  6. Use SSH agent to manage keys:
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
    

Troubleshooting

Permission denied error:

# Fix permissions on local machine
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

# Fix permissions on remote server
ssh user@203.0.113.42 "chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"

Still asks for password:

  • Verify public key is in ~/.ssh/authorized_keys on remote server
  • Check SSH server configuration: /etc/ssh/sshd_config
  • Ensure PubkeyAuthentication yes is set
  • Restart SSH service: sudo systemctl restart sshd

Multiple keys:

# Specify which key to use
ssh -i ~/.ssh/specific_key user@203.0.113.42

Advanced Configuration

SSH Config File

Create ~/.ssh/config for easier connections:

Host myserver
    HostName 203.0.113.42
    User user
    IdentityFile ~/.ssh/id_ed25519
    Port 22

Host production
    HostName 198.51.100.17
    User deploy
    IdentityFile ~/.ssh/work_key
    Port 2222

Now you can connect with:

ssh myserver
ssh production

Conclusion

SSH key authentication provides:

  • ✅ Enhanced security (cryptographic authentication)
  • ✅ Convenience (no password typing)
  • ✅ Automation support (scripts, CI/CD)
  • ✅ Single sign-on experience (with SSH agent)
© 2026 Jing Zhong · zhongjing.hitdic.com