SSH
Connect to remote servers securely, manage SSH keys, and transfer files
Free tier: read the explanation here. Upgrade to Pro for Drills, Speed challenges & Mastery badges.
UpgradeKnowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
SSH (Secure Shell) is the standard way to connect to and control remote Linux servers. Every deployment, every cloud instance, every CI/CD system uses SSH.
Connecting to a server:
bash ssh user@host # connect (prompts for password if no key) ssh bill@192.168.1.10 # by IP ssh bill@example.com # by hostname ssh -p 2222 bill@host # non-default port (default is 22)
SSH key authentication (the right way):
bash # 1. Generate a key pair on your local machine ssh-keygen -t ed25519 -C "bill@example.com" # Creates: ~/.ssh/id_ed25519 (private key) + ~/.ssh/id_ed25519.pub (public key) # 2. Copy your public key to the server ssh-copy-id bill@server.com # This appends your pub key to ~/.ssh/authorized_keys on the server # 3. Connect without a password ssh bill@server.com # now uses key automatically
Key security rules:
bash chmod 700 ~/.ssh # directory: only you can access chmod 600 ~/.ssh/id_ed25519 # private key: only you can read chmod 644 ~/.ssh/id_ed25519.pub # public key: readable by all (it is public) chmod 600 ~/.ssh/authorized_keys # on the server
SSH config file — shortcuts for common servers:
# ~/.ssh/config Host myserver HostName 203.0.113.42 User bill IdentityFile ~/.ssh/id_ed25519 Port 22 Host prod HostName prod.example.com User deploy IdentityFile ~/.ssh/deploy_key bash ssh myserver # uses the config above — no need to type full command ssh prod
Transferring files:
bash # scp — secure copy (like cp, but over SSH) scp file.txt bill@server.com:/home/bill/ # upload scp bill@server.com:/var/log/app.log ./ # download scp -r my_dir/ bill@server.com:/opt/app/ # upload directory # rsync — more efficient for large transfers rsync -avz ./dist/ bill@server.com:/var/www/app/ # -a: archive (preserves permissions, timestamps), -v: verbose, -z: compress
Tunneling and port forwarding:
bash # Local forward: access remote DB on localhost:5432 ssh -L 5432:localhost:5432 bill@server.com # Jump hosts (bastion pattern) ssh -J bastion.example.com bill@internal-server.com
Examples
Setting up key-based SSH access to a new server
ed25519 keys are more secure and shorter than the older RSA. Use them for all new setups.
# On your LOCAL machine:
# Generate a key (skip if you already have one)
ssh-keygen -t ed25519 -C "bill@email.com" -f ~/.ssh/id_ed25519
# Prompts for passphrase — use one for security, or leave empty
# See the public key you will share with servers
cat ~/.ssh/id_ed25519.pub
# ssh-ed25519 AAAAC3... bill@email.com
# Copy it to the server (requires password this one time)
ssh-copy-id bill@203.0.113.42
# Now connect without password
ssh bill@203.0.113.42
# Welcome to Ubuntu 22.04.3 LTS
# Verify key permissions are correct
ls -la ~/.ssh/
# drwx------ (700) .ssh/
# -rw------- (600) id_ed25519 — private key
# -rw-r--r-- (644) id_ed25519.pub — public keySSH config and file transfer
The SSH config file saves you from memorizing IP addresses and key paths — set it up once and use short names.
# ~/.ssh/config — avoids typing full command every time
cat ~/.ssh/config
# Host staging
# HostName staging.myapp.com
# User deploy
# IdentityFile ~/.ssh/deploy_key
# Port 22
# Now just:
ssh staging
# connects as deploy@staging.myapp.com with deploy_key
# Upload a build artifact
scp ./dist.tar.gz staging:/opt/app/
# Or use rsync for a directory (skips unchanged files)
rsync -avz --delete ./dist/ staging:/var/www/app/
# --delete removes files on server that no longer exist locally
# Download logs for analysis
scp staging:/var/log/app/error.log ./local-errors.logHow well did you understand this?
Next in Linux & Command Line
Environment Variables