Setting up SSH access with keys is a secure and convenient way to connect to a server without a password. Below is a step-by-step guide on how to set everything up.

🔧 Step 1: Generating SSH Keys with a Name

Open the terminal and execute the command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/my_custom_key

 

  • -t rsa — key type
  • -b 4096 — key length
  • -C — comment (usually email)
  • -f — path and file name

After execution, two files will appear:

  • ~/.ssh/my_custom_key — private key
  • ~/.ssh/my_custom_key.pub — public key

📤 Step 2: Copying the Public Key to the Server

The easiest way is to use ssh-copy-id:

ssh-copy-id -i ~/.ssh/my_custom_key.pub user@server_ip

 

If ssh-copy-id is unavailable:

cat ~/.ssh/my_custom_key.pub | ssh user@server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

 

⚙️ Step 3: Configuring the SSH Server on Ubuntu

Make sure OpenSSH is installed and running:

sudo apt update 
sudo apt install openssh-server 
sudo systemctl enable ssh --now

 

Open the configuration file:

sudo nano /etc/ssh/sshd_config

 

Ensure the following parameters are enabled:

PubkeyAuthentication yes 
PasswordAuthentication no 
PermitRootLogin no

 

Restart SSH:

sudo systemctl restart ssh

 

🧠 Step 4: Simplifying Connection through Configuration

Create or edit the file ~/.ssh/config:

Host myserver 
HostName server_ip 
User username 
IdentityFile ~/.ssh/my_custom_key

 

Now you can connect easily:

ssh myserver

🛡️ Security Recommendations

  • Use a non-standard port (e.g., 2222)
  • Set up Fail2Ban to protect against brute force attacks
  • Restrict access by IP using iptables or ufw

✅ Conclusion

Now you can connect to the Ubuntu server using SSH keys without a password. This is not only convenient but also significantly more secure. Use unique names for keys, especially if you are working with multiple servers or projects.