A simple guide to connect to a server using SSH key-based authentication with a custom .ssh/config file.
Prerequisites
You should have:
- A server with SSH enabled (e.g., cloud instance, VPS, dedicated server).
- Access to an account on the server (e.g.,
admin, oruser). - A local machine with OpenSSH installed (Linux, macOS, or Windows with WSL/PowerShell).
- Permission to add your SSH public key to the server.
1. SSH Authentication Types
SSH supports multiple authentication methods:
-
Password-based
Enter a username and password when connecting. Simple, but less secure. -
Key-based (recommended)
Use a cryptographic key pair for authentication. No password sent over the network. -
Other methods
Includes GSSAPI/Kerberos, certificate-based, and hardware tokens.
This guide focuses on key-based authentication.
2. Generate an SSH Key Pair
Run this command on your local machine:
ssh-keygen -t ed25519 -C "your_email@example.com"
-t ed25519— modern, secure key type.-C— optional comment (usually your email).- Press Enter to accept default location (
~/.ssh/id_ed25519). - You can set a passphrase for extra protection (optional).
If you prefer RSA (for compatibility with older systems):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
3. Add Your Public Key to the Server
Option A — You have server access and can edit authorized_keys directly:
-
Copy your public key to the server:
ssh-copy-id user@example.com(Replace
user@example.comwith your server username and address.) -
Or manually append your public key to:
~/.ssh/authorized_keys
Option B — You send your public key to the server administrator:
- Provide the contents of your public key file (e.g.,
~/.ssh/id_ed25519.pub). - The admin will add it to
~/.ssh/authorized_keysfor your account.
4. Configure .ssh/config for Easier Connections
Instead of typing a long SSH command with username, hostname, and key path each time, create a shortcut.
Edit (or create) your SSH config file:
nano ~/.ssh/config
or for vim users:
vi ~/.ssh/config
Example:
Host myserver
HostName server.example.com
User myuser
IdentityFile ~/.ssh/myserver-key.pem
Host— nickname for the connection.HostName— server’s public IP or domain.User— username on the server.IdentityFile— path to your private key.
Save the file, then:
chmod 600 ~/.ssh/config
5. Connect to the Server
Now you can connect with just:
ssh myserver
Instead of:
ssh -i ~/.ssh/myserver-key.pem myuser@server.example.com
6. Troubleshooting
-
Permission denied (publickey)
- Check that the public key is in the correct
~/.ssh/authorized_keysfile. - Ensure
~/.ssh(700) andauthorized_keys(600) permissions are correct.
- Check that the public key is in the correct
-
.ssh/config not applied
- Ensure file permissions are
chmod 600. - Confirm that
Hostname matches the shortcut you’re using.
- Ensure file permissions are
Best Practices
- Use ed25519 keys for stronger security and faster authentication.
- Never share your private key.
- Keep a backup of your private key in a safe location.
- Disable password authentication on the server for better security (optional).