SSH & Remote — Terminal

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…

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.

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)

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

💡 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

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.

What you will learn