SSH & Remote Shells
SSH (Secure Shell) gives you a terminal on another computer over an encrypted connection: a cloud server, a Raspberry Pi, a lab machine, a Git hosting service. Whatever OS you sit at, the remote side is usually Linux, and you type bash commands there as if you were sitting in front of it.
your laptop remote server
┌────────────────────┐ encrypted TCP ┌─────────────────────┐
│ ssh client │ ────── port 22 ──►│ sshd (SSH server) │
│ ~/.ssh/id_ed25519 │ │ ~/.ssh/authorized_ │
│ (private key) │ │ keys (public key) │
└────────────────────┘ └─────────────────────┘The good news: all three operating systems ship the same client, OpenSSH, so the commands in this lesson are identical everywhere.
| OS | SSH client | Where keys live |
|---|---|---|
| Windows 10/11 | OpenSSH built in (C:\Windows\System32\OpenSSH\ssh.exe), works in CMD and PowerShell |
C:\Users\ada\.ssh\ |
| macOS | OpenSSH built in | /Users/ada/.ssh/ |
| Linux | OpenSSH (openssh-client package, almost always installed) |
/home/ada/.ssh/ |
ssh -V prints the version. If Windows says 'ssh' is not recognized, add it under Settings → System → Optional features → OpenSSH Client.
The playgrounds can't open network connections, so the sessions below are recorded (host names and fingerprints are samples).
Connecting with a password
The basic form is ssh user@host. The first time you connect, SSH shows the server's fingerprint and asks whether you trust it; the answer is saved in ~/.ssh/known_hosts, and SSH warns loudly if that fingerprint ever changes (which could mean someone is impersonating the server).
PS C:\Users\ada> ssh ada@203.0.113.10
The authenticity of host '203.0.113.10 (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:Qm9vT2Y2NjhzZWNyZXRmaW5nZXJwcmludA0K.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '203.0.113.10' (ED25519) to the list of known hosts.
ada@203.0.113.10's password:
Welcome to Ubuntu 24.04.3 LTS (GNU/Linux 6.8.0-71-generic x86_64)
ada@web1:~$ uptime
14:02:11 up 12 days, 3:41, 1 user, load average: 0.08, 0.05, 0.01
ada@web1:~$ exit
logout
Connection to 203.0.113.10 closed.
PS C:\Users\ada>Notice how the prompt changed: you were in PowerShell on Windows, then in bash on the server, then back. Options you'll use: -p 2222 (another port), -i keyfile (a specific key), and a command after the host to run just that one command: ssh ada@web1 "df -h /".
Key-based login with ssh-keygen
Passwords can be guessed; keys can't. You create a key pair: a private key that never leaves your computer, and a public key you copy to every server. The command is the same in every shell:
ada@macbook ~ % ssh-keygen -t ed25519 -C "ada@macbook"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/ada/.ssh/id_ed25519):
Enter passphrase for "/Users/ada/.ssh/id_ed25519" (empty for no passphrase):
Your identification has been saved in /Users/ada/.ssh/id_ed25519
Your public key has been saved in /Users/ada/.ssh/id_ed25519.pub
ada@macbook ~ % cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGx0bm9ub3RyZWFsa2V5Zm9yZGVtbw ada@macbookUse a passphrase: it encrypts the private key on disk. An SSH agent (ssh-agent, built into macOS Keychain and available as a Windows service) remembers it so you type it once per session.
Next, put the public key into ~/.ssh/authorized_keys on the server:
ada@ubuntu:~$ ssh-copy-id ada@203.0.113.10
Number of key(s) added: 1Windows has no ssh-copy-id, but a pipe does the same job:
PS C:\Users\ada> Get-Content ~\.ssh\id_ed25519.pub | ssh ada@203.0.113.10 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"For GitHub or GitLab, paste the .pub file's content into Settings → SSH keys instead. Test with ssh -T git@github.com.
SSH is strict about permissions: if your private key or ~/.ssh folder can be read by other users, it refuses to use them. On Linux and macOS the right settings are:
mkdir -p .ssh
printf 'FAKE PRIVATE KEY FOR THIS DEMO\n' > .ssh/id_ed25519
printf 'ssh-ed25519 AAAAdemo ada@laptop\n' > .ssh/id_ed25519.pub
chmod 700 .ssh
chmod 600 .ssh/id_ed25519
chmod 644 .ssh/id_ed25519.pub
stat -c '%A %n' .ssh/id_ed25519 .ssh/id_ed25519.pub-rw------- .ssh/id_ed25519
-rw-r--r-- .ssh/id_ed25519.pub
The folder gets 700 (only you may enter it), the private key 600 (only you may read it) and the public key 644 (anyone may read it, which is harmless).
On Windows, files in your profile are private to you by default; if SSH complains UNPROTECTED PRIVATE KEY FILE, remove other users' access with icacls (see Admin Rights & Permissions).
The config file: shortcuts for hosts
Typing ssh -p 2222 -i ~/.ssh/work_key deploy@203.0.113.10 gets old fast. Put it in ~/.ssh/config (C:\Users\ada\.ssh\config on Windows) once:
Host web1
HostName 203.0.113.10
User deploy
Port 2222
IdentityFile ~/.ssh/work_key
Host *
ServerAliveInterval 60Now ssh web1, scp file web1: and git clone web1:repo.git all use those settings. Host * applies to every connection; ServerAliveInterval keeps idle connections from being dropped. ssh -G web1 prints the settings SSH would use, handy for debugging.
Copying files: scp and sftp
scp copies like cp, with host: in front of remote paths. -r copies folders.
C:\Users\ada>scp report.pdf web1:/srv/reports/
report.pdf 100% 248KB 1.2MB/s 00:00
C:\Users\ada>scp -r web1:/var/log/nginx .\logs
access.log 100% 38KB 512.4KB/s 00:00
error.log 100% 1203 33.1KB/s 00:00sftp web1 opens an interactive session (ls, cd, get, put), and rsync -avz (Linux/macOS) copies only what changed, which is much faster for big folders.
Remote shells beyond SSH
- PowerShell remoting (
Enter-PSSession) is the Windows-native way to get a remote shell, and PowerShell 7 can also run it over SSH:Enter-PSSession -HostName web1 -UserName ada. - VS Code Remote - SSH opens a folder on the server in your local editor, using your
~/.ssh/config. - tmux or screen on the server keep your session alive when the connection drops; reconnect and run
tmux attach.
| Task | Command (all OSes) |
|---|---|
| Connect | ssh user@host |
| Other port | ssh -p 2222 user@host |
| Run one command | ssh host "command" |
| Make a key | ssh-keygen -t ed25519 |
| Install public key | ssh-copy-id user@host (Linux/macOS) |
| Copy to server | scp file host:/path/ |
| Copy from server | scp host:/path/file . |
| Show effective config | ssh -G host |
Learn more: SSH & SCP in Bash, PowerShell Remoting & Jobs, Push to GitHub.
Test yourself
Which file must you copy to the server for key-based login?
id_ed25519id_ed25519.pubknown_hostsconfig
What is stored in ~/.ssh/known_hosts?
- Your passwords
- Fingerprints of servers you have trusted
- Your private keys
- Host shortcuts
Which permission should a private key have on Linux and macOS?
- 777
- 755
- 644
- 600