Command Library
0%
Beginner ~25 min

SSH Keys From Scratch: Your Machine to Your Servers

Create a key, put it on a server, name your servers in ~/.ssh/config, and connect with ssh prod instead of an IP address. Ends with a machine that logs in without passwords and a config you can copy to the next one.

0 / 13 done

Prerequisites

  • A terminal on your own machine (macOS, Linux, or Windows with WSL or Git Bash)
  • A server you can currently reach, with a password or an existing key
  • No prior SSH knowledge needed
1
Step 1 Snippet

If a key exists, do not generate over it. Anything that trusts it, servers and GitHub alike, would stop letting you in.

Check Whether You Already Have an SSH Key


                            

A pair named id_ed25519 and id_ed25519.pub means you already have a key and can skip the next step.

2
Step 2 Snippet

Generate ed25519 SSH key


                            
Yes. It encrypts the private key on disk, so a stolen laptop is not a stolen server. The agent, two steps down, means you only type it once per session.
3
Step 3 Snippet

Fix ~/.ssh Permissions


                            

Run this even on a fresh key. It is the fix for most "it keeps asking for my password" reports.

4
Step 4 Snippet

Load a Key into the SSH Agent


                            

ssh-add -l should now list your key fingerprint. An empty list means the agent is not running.

5
Step 5 Command

Two ways to put the key on the server

# الأسهل، إن كان لديك دخول بكلمة مرور:  الخطوة التالية
# وإن كان الدخول بكلمة المرور معطلاً، أو تلصق عبر لوحة تحكم:  الخطوة التي تليها

ssh-copy-id does it in one command when password login still works. The manual step after it is for servers where it does not, or for pasting into a provider panel.

6
Step 6 Snippet

Copy SSH Key to Remote Server


                            

It asks for your password one last time. After this, that password is no longer part of your workflow.

7
Step 7 Snippet

Only the .pub file is ever shared. The file without .pub is the private key and never leaves your machine.

Print Your Public Key to Paste It Somewhere


                            

The alternative route: copy this line and paste it into GitHub, cPanel, or your provider dashboard.

8
Step 8 Snippet

Test an SSH Connection Without Opening a Shell


                            

It should print OK and the hostname without asking for anything. If it asks for a password, go back to the permissions step.

9
Step 9 Snippet

Set Up ~/.ssh/config for Several Servers


                            

This is the step that changes your day: ssh prod now replaces the address, the user and the port.

Yes. Anything built on ssh reads the same file, so scp file.txt prod:/tmp/ and rsync -avz ./dist prod:/var/www/ work straight away.
10
Step 10 Snippet

Use a Different Key per Account or Host


                            

Optional, and the reason most people meet ~/.ssh/config in the first place: a work GitHub account and a personal one on the same laptop.

11
Step 11 Snippet

SSH via Jump Host (Bastion)


                            

For a server with no public address: reach it through a bastion in one command.

Yes, add ProxyJump bastion to that host block and plain ssh db1 routes through it every time.
12
Step 12 Snippet

Debug a Refused SSH Login (Verbose Mode)


                            

Keep this one. When a login is refused, it names the cause instead of leaving you guessing.

13
Step 13 Snippet

Only after the previous steps succeed, and keep your current session open while you test a second one.

Disable Root SSH Login


                            

The payoff: once key login is proven, turning off passwords and root login removes the attack surface brute force relies on.