SECTION 05 · 25 MIN READ & EXPLORE
Connect and transfer files with SSH
Understand keys, host fingerprints, permissions, and remote file transfer.
Module 1: SSH (Secure Shell) Essentials
SSH is the industry standard for secure remote login. It uses Public Key Cryptography: you keep the Private Key (secret) and place the Public Key on any server you want to access.
1. Critical Files & Locations
On Your Local Machine (The Client)
~/.ssh/id_rsa(orid_ed25519): Your Private Key. 🛑 NEVER SHARE THIS.~/.ssh/id_rsa.pub: Your Public Key. You copy this to servers.~/.ssh/known_hosts: Fingerprints of servers you have connected to (prevents Man-in-the-Middle attacks).~/.ssh/config: Your personal shortcuts (e.g., aliasssh myserverinstead of typing the full IP/User).
On the Remote Server
~/.ssh/authorized_keys: Contains a list of Public Keys allowed to log in as this user./etc/ssh/sshd_config: The main configuration file for the SSH daemon (change ports, disable passwords, etc.).
2. The Essential Command Belt
| Action | Command | Note |
|---|---|---|
| Connect | ssh user@192.168.1.50 |
Default port 22. |
| Connect (Custom Port) | ssh -p 2222 user@192.168.1.50 |
If the server uses a non-standard port. |
| Generate Keys | ssh-keygen -t ed25519 -C "email@example.com" |
ed25519 is faster/safer than RSA. |
| Copy Key to Server | ssh-copy-id user@192.168.1.50 |
Automates adding your key to authorized_keys. |
| Debug Connection | ssh -v user@host |
“Verbose” mode. Shows exactly where the connection fails. |
3. Critical Permissions (The “Strict” Rule)
SSH is paranoid about file permissions. If your files are too open, SSH will refuse to work.
~/.ssh/Directory: Must be 700 (drwx------). Only you can read/write/execute.- Private Key (
id_rsa): Must be 600 (-rw-------). Only you can read/write. - Public Key (
.pub): Usually 644 (-rw-r--r--). Anyone can read (safe).
4. Quick Hardening (Best Practices)
Edit /etc/ssh/sshd_config on the server:
- Disable Root Login:
PermitRootLogin no(Force login as a regular user, thensudo). - Disable Passwords:
PasswordAuthentication no(Force key-only authentication). - Restart Service: Always run
sudo systemctl restart sshafter config changes.
5. SCP (Secure Copy) Commands
SCP uses SSH to transfer files. It is the “Copy and Paste” of the Linux server world.
Basic Syntax: scp [source] [destination]
| Scenario | Command | Breakdown |
|---|---|---|
| Upload File | scp myfile.txt user@192.168.1.50:/home/user/ |
Copies local myfile.txt to the remote server. |
| Download File | scp user@192.168.1.50:/var/log/syslog ./ |
Copies remote syslog to your current local directory (./). |
| Upload Directory | scp -r my_folder/ user@192.168.1.50:/opt/ |
-r (Recursive) is required for folders. |
| Custom Port | scp -P 2222 myfile.txt user@localhost:/tmp/ |
Note: SCP uses capital -P, while SSH uses lowercase -p. |
| Use Specific Key | scp -i ~/.ssh/lab_key myfile.txt user@host:/tmp |
Uses the custom key you generated above. |
Example 1
ssh-keygen -t rsa -b 4096 -f lab_key_rsa -N ""
ssh-copy-id -i lab_key_rsa ubuntu@192.168.1.50
Pause and predict
Which key is copied to the server?
Try your own explanation first. These notes stay on this page and are not saved or graded.
Show explanation
The public key goes in authorized_keys. The private key stays on your client and is never committed or shared.
Take a moment to explain the idea in your own words.