Understanding SSH and setting up SSH keys
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:
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:
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):
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server# Then enter your password one last time
Now, you can connect like this:
ssh user@server
Or, if you’re using a custom key:
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:
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:
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:
ssh my_alias
Understanding XSS vulnerabilities
In this article, we will explore how XSS vulnerabilities work and how to prevent them
Understanding CSRF vulnerabilities
In this article, we will explore how CSRF vulnerabilities work and how to prevent them
Understanding SQL injection vulnerabilities
In this article, we will explore how SQL injection vulnerabilities work and how to prevent them
Practice code with the "Quick Sort" algorithm
Enhance your coding skills by learning how the Quick Sort algorithm works!
The SOLID/STUPID principles
Learn what are the SOLID and STUPID principles with examples
Create a Docker Swarm playground
Let's create Docker Swarm playground on your local machine
Create an Ansible playground with Docker
Let's create an Ansible playground with Docker
Setup a Kubernetes cluster with K3S, Traefik, CertManager and Kubernetes Dashboard
Let's setup step by step our own K3S cluster !