Your server sits in a data centre (a building where servers are kept) in one city, and you're in a rented room in another city. How do you run commands on that server? With SSH (Secure Shell). SSH opens a terminal on another computer over the network, and all the data that travels is encrypted (scrambled so that others can't read it). You type on your laptop, and the command runs on the server. To prove who you are, SSH can use a password, or the safer option, an SSH key. It's like the signature card at a bank: you leave a sample of your signature with the bank. Every visit you sign a fresh slip with your own hand, and the clerk compares it with the sample. In SSH, the bank and its clerk are the server. The signature sample is the public key you leave on the server, and your hand is the private key that stays on your laptop. The fresh slip is data that is unique to each connection. The difference from a real bank: nobody can forge your signature from the public key.
First connection: ssh user@host
This is Budi's first connection to his server, from logging in to leaving:
$ ssh [email protected]
The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established.
ED25519 key fingerprint is SHA256:sIJQN9EZQ3roLfkGIEqUJAtSfNxhTBRoG/FLq8q73xs.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.100' (ED25519) to the list of known hosts.
[email protected]'s password:
Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-45-generic x86_64)
...
budi@server:~$ exit
logout
Connection to 192.168.1.100 closed.
- The format is
ssh user@host:budiis the user name on the server, and192.168.1.100is the server's address (a domain name works too). SSH uses port 22. If the server uses another port, add-p, such asssh -p 2222 [email protected]. - The first connection to a new server always asks about the server's fingerprint, a short code that identifies that server. Answer
yes, and the server is recorded in~/.ssh/known_hosts. Later connections don't ask again. - The password you type doesn't appear at all, not even as stars. That's normal.
- Once you're in, the prompt changes to
budi@server:~$: every command now runs on the server.exitcloses the connection.
Logging in with an SSH key
This is how Budi makes an SSH key and puts it on the server:
$ ssh-keygen -t ed25519 -C "budi@laptop"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/budi/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/budi/.ssh/id_ed25519
Your public key has been saved in /home/budi/.ssh/id_ed25519.pub
...
$ ssh-copy-id [email protected]
...
Number of key(s) added: 1
...
$ ssh [email protected]
Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-45-generic x86_64)
ssh-keygen -t ed25519makes a pair of keys with the Ed25519 algorithm (a method of calculation).-C "budi@laptop"is just a label so you remember whose key it is. The passphrase (a password for the key) may stay empty, but it's safer to set one, because the passphrase locks the private key file.- The result is two files.
id_ed25519is the private key: it stays on your laptop and is never shared.id_ed25519.pubis the public key: you may give it to any server, and to GitHub. ssh-copy-idlogs in once with your password, then adds the contents ofid_ed25519.pubto the file~/.ssh/authorized_keys(the list of public keys that may log in) on the server.- If you set a passphrase, ssh asks for it every time the key is used. To type it only once, run
ssh-add ~/.ssh/id_ed25519and enter the passphrase.ssh-addgives the unlocked key to ssh-agent, a program that holds the key while ssh-agent is running. If you seeCould not open a connection to your authentication agent., your shell is not connected to an ssh-agent. Start ssh-agent first witheval "$(ssh-agent -s)". On macOS, add--apple-use-keychainto thessh-addcommand so the passphrase is also saved in the Keychain (the macOS password store). - PowerShell on Windows has no
ssh-copy-id, so do its steps yourself. Copy the contents ofid_ed25519.pub, log in to the server with your password, then add those contents as one new line in~/.ssh/authorized_keyson the server. In WSL,ssh-copy-idis there as on Linux.
From now on logging in doesn't ask for a password. Guess first: does your private key travel to the server or not? This is what happens during the login:
| Step | Who | What |
|---|---|---|
| 1 | laptop | "I'm budi, I want to log in with this key", showing its public key |
| 2 | server | That key is in authorized_keys, so the server is willing to check it |
| 3 | laptop | Signs data that is unique to this connection with the private key |
| 4 | server | Checks the signature with the public key. It matches, come in |
That unique data includes the session identifier. This is a code that the laptop and the server calculate together at the start of the connection, so it is different for every connection. Because of that, a signature from one connection can't be used again in another one. Only the public key and a signature travel. The private key never leaves your laptop.
File permissions in ~/.ssh
SSH is strict about the file permissions from the Permission & Ownership lesson. The ~/.ssh folder must be 700, the private key 600, and the public key may be 644. If others can read the private key, ssh refuses to use it:
$ chmod 644 ~/.ssh/id_ed25519
$ ssh [email protected]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/budi/.ssh/id_ed25519' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/home/budi/.ssh/id_ed25519": bad permissions
[email protected]'s password:
The key is ignored, so ssh goes back to asking for the password. If the server doesn't accept password logins, you get [email protected]: Permission denied (publickey). There's one cure: chmod 600 ~/.ssh/id_ed25519.
Shortcut: ~/.ssh/config
Tired of typing the user, address and port every time? Write them once in the file ~/.ssh/config:
Host server
HostName 192.168.1.100
User budi
Port 22
IdentityFile ~/.ssh/id_ed25519
Host is a short name you choose, HostName the real address, and IdentityFile the private key to use. Now ssh server is the same as ssh [email protected]. The name server works in scp and rsync too, the commands for sending files in the next section.
Sending files: scp and rsync
Four examples of copying files between the laptop and the server:
scp app.zip [email protected]:/home/budi/ # laptop to server
scp [email protected]:/var/log/app.log ./ # server to laptop
scp -r dist/ [email protected]:/var/www/html/ # a whole folder, -r = recursive
rsync -avz dist/ [email protected]:/var/www/html/ # only sends what changed
scp(secure copy) copies files over SSH:scp source destination. The server side is writtenuser@host:path, and that colon is what marks "this is on the server".-rcopies a folder with everything in it.rsyncfirst compares the files on the laptop and on the server, then only sends the files that changed. If only 3 of the 2,000 files in a folder changed, uploading the folder again is quick, because only those 3 files are sent.-akeeps file permissions and times,-vshows what's sent,-zcompresses the data on the way, and-nonly tries without sending anything.
💡 Bonus: an SSH tunnel
This command makes a tunnel through SSH:
ssh -L 5432:localhost:5432 [email protected]
While this connection is open, port 5432 on your laptop is forwarded through SSH to port 5432 on the server. A database app on your laptop just connects to localhost:5432 (localhost means the computer itself, here your laptop), even though the database is on the server. The localhost inside the -L command is read by the server, so there it means the server itself. And that database port never has to be opened to the internet.
⚠️ Four habits that cause SSH trouble
- Sharing the wrong file. What goes to a server or GitHub is the file ending in
.pub.id_ed25519without.pubis the private key: don't send it, don't commit it. - Private key permissions too loose. You get
UNPROTECTED PRIVATE KEY FILE, and the key is ignored. Fix it withchmod 600 ~/.ssh/id_ed25519. - Forgetting the colon in scp.
scp app.zip [email protected]without:sends nothing to the server. That command only copies to a local file (on your laptop) named[email protected]. - Typing yes without thinking. The yes/no question only appears for a server you connect to for the first time. That's when you need to check its fingerprint: compare it with the one from whoever runs the server. If you see
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!instead, the server's fingerprint differs from the one recorded inknown_hosts, and ssh refuses right away (Host key verification failed.) without asking. The cause may be a reinstalled server, or someone spying on your connection. Ask whoever runs the server first, then remove the old record withssh-keygen -R 192.168.1.100.
TL;DR: ssh user@host opens a terminal on the server over an encrypted connection (scrambled so others can't read it), on port 22 (-p for another port). The first connection asks about the server's fingerprint (a code that identifies the server), then records it in ~/.ssh/known_hosts. ssh-keygen -t ed25519 makes a private key (id_ed25519, secret, stays on the laptop) and a public key (id_ed25519.pub, fine to share). ssh-copy-id installs the public key in ~/.ssh/authorized_keys on the server. When you log in, the laptop signs data unique to that connection with the private key, the server checks the signature with the public key, and the private key is never sent. The private key must be 600, the ~/.ssh folder 700. ~/.ssh/config stores shortcuts, scp source user@host:path copies files, and rsync -avz only sends what changed.
⚡ Log in to the server
Write:
ssh [email protected]
Meaning: ssh opens a terminal on another computer over the network. budi is the user name on the server, and 192.168.1.100 its address. Once you're in, the prompt becomes budi@server:~$ and every command runs on the server. exit closes the connection.
⚡ Make an SSH key
Write:
ssh-keygen -t ed25519 -C "budi@laptop"
ssh-copy-id [email protected]
Meaning: An SSH key is a pair of files that replaces the password. ssh-keygen makes them (-t picks the type of key, -C is only a label). id_ed25519 is the private key: it stays on the laptop, never share it. id_ed25519.pub is the public key, and ssh-copy-id installs it on the server.
⚡ Logging in without a password
Guess first: when you log in with a key, is your private key sent to the server?
Never. The laptop signs data that is unique to that connection (different for every connection) with the private key, then the server checks that signature with the public key.
Meaning: The private key must have permission 600 (only the owner may read and change it). If its permission is too open, ssh ignores it. The fix is chmod 600 ~/.ssh/id_ed25519.
⚡ Send a file
Write:
scp app.zip [email protected]:/home/budi/
Meaning: scp source target copies a file over SSH, here app.zip from the laptop to the folder /home/budi/ on the server. The colon after the server's address marks "this is on the server", so don't forget it. rsync -avz only sends the files that changed.