Secure Shell (SSH) is a protocol used to securely connect to remote systems over a network. It is commonly used for server administration, file transfers, and remote development.

This guide walks you through generating an SSH key, adding it to a server, and connecting securely.

What You Need Before You Start

  • A remote server with SSH access enabled

  • A username for that server

  • A terminal (Linux/macOS) or an SSH client (Windows)

Step 1: Generate an SSH Key Pair

SSH uses a key pair for authentication:

  • Private key stays on your device (keep this secure)
  • Public key added to the server

Run the following command in your terminal:

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

Press Enter to accept the default file location. Optionally, set a passphrase for added security.

This will create:

  • ~/.ssh/id_ed25519 (private key)

  • ~/.ssh/id_ed25519.pub (public key)

Step 2: Add Your Public Key to the Server

You need to copy your public key to the server, using ssh-copy-id:

ssh-copy-id username@your-server-ip

Step 3: Connect Using SSH

Once your key is added, connect with:

ssh username@your-server-ip

If everything is set up correctly, you should log in without needing a password.

Optional: Create an SSH Config Entry

To simplify connections, you can define a shortcut.

Edit (or create):

nano ~/.ssh/config

Add:

Host myserver HostName your-server-ip User username IdentityFile ~/.ssh/id_ed25519

Then connect using:

ssh myserver

Security Best Practices

  • NEVER share your private key

  • Use a passphrase for your key

  • Disable password authentication on the server (after confirming key access works)

  • Keep your system updated

Troubleshooting

Permission Denied (publickey)

  • Ensure your public key is in ~/.ssh/authorized_keys

  • Check file permissions:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Connection Refused

  • Verify the server is running SSH:
sudo systemctl status ssh
  • Check firewall settings (port 22)

Wrong Key Used

Specify your key manually:

ssh -i ~/.ssh/id_ed25519 username@your-server-ip

Conclusion

SSH provides a secure and efficient way to access remote systems. By using key-based authentication, you significantly improve both security and convenience.

Once configured, connecting to your server becomes fast, secure, and password-free.