Understanding SSH and setting up SSH keys

In this article, we’ll see what is SSH, and generate and configure SSH keys for secure authentication
Friday, September 19, 2025

What is SSH

SSH (Secure Shell) is both a protocol and a program that allows you to remotely connect to a server and interact with it. It can also be used to transfer files securely.

When you purchase hosting (such as a server or VPS), you’re typically provided with credentials: hostname, port, username, and password. These details allow you to authenticate via the SSH protocol. However, using a password alone is not considered secure. That’s why we often use an alternative method: SSH keys. This approach enables password-less authentication, offering a more secure and convenient way to connect.

SSH is available by default on Linux and macOS, and can be used on Windows via WSL (Windows Subsystem for Linux).

Generating SSH keys

I recommend following GitHub’s guide to generate SSH keys, as it’s up-to-date and uses modern protocols.

Here’s the basic command to generate a new SSH key:

Terminal window
ssh-keygen -t ed25519 -C "your_email@example.com"
# Or, to specify a custom file name:
# ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/my_key
  • ed25519 is the type of key. It offers strong security today, but key types may evolve over time.
  • -f is optional and lets you choose where to save the key (useful if you want multiple keys). Leave it out if this is your first key.

After running the command, you’ll be asked to set a passphrase. This adds an extra layer of protection—if someone steals your key, they can’t use it without the passphrase.

You’ll now have two new files in your ~/.ssh/ directory:

Terminal window
ls ~/.ssh/ -l
# -rw-------. 1 alex alex 419 Jan 24 2025 id_ed25519
# -rw-r--r--. 1 alex alex 110 Jan 24 2025 id_ed25519.pub
  • id_ed25519: Your private key — never share this!
  • id_ed25519.pub: Your public key — this is what you’ll share with the server.

Copying your public key to the server

To copy your key to a server (so you don’t need to enter your password anymore):

Terminal window
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
# Then enter your password one last time

Now, you can connect like this:

Terminal window
ssh user@server

Or, if you’re using a custom key:

Terminal window
ssh -i ~/.ssh/my_key user@server

Now, you no longer need to enter your password !

This command creates a file called authorized_keys on the server and adds your public key to it:

Terminal window
alex@my_server:~$ ls -l .ssh/authorized_keys
-rw------- 1 alex alex 235 Apr 5 13:57 .ssh/authorized_keys

Advanced SSH config file

You can create a config file to simplify your SSH commands (optional):

Path: ~/.ssh/config

Example content:

Terminal window
Host *
IgnoreUnknown AddKeysToAgent,UseKeychain
AddKeysToAgent yes
UseKeychain yes
Host my_alias
HostName my_hostname
User my_user
IdentitiesOnly yes
IdentityFile ~/.ssh/my_key

Now you can connect using:

Terminal window
ssh my_alias

Recommended articles