SSH and Remote Access
Summary
This chapter covers secure remote access to Linux systems using SSH. You'll learn about the SSH protocol, key-based authentication (far more secure than passwords), configuring SSH clients, and transferring files with SCP and rsync. You'll also learn firewall basics with UFW and understand network security principles.
Concepts Covered
This chapter covers the following 21 concepts from the learning graph:
- SSH Protocol
- SSH Command
- SSH Keys
- SSH Config
- SCP Command
- Rsync Command
- Firewall Basics
- UFW Firewall
- Firewall Rules
- Linux Security
- User Authentication
- Password Security
- SSH Security
- Key-Based Auth
- Port Security
- Secure File Transfer
- Security Updates
- Vulnerability Scanning
- Log Files
- Audit Logs
- Security Best Practices
Prerequisites
This chapter builds on concepts from:
Welcome to the Secret Tunnel!
Imagine being able to control a computer on the other side of the world, as if you were sitting right in front of it. That's what SSH gives you—a secure, encrypted tunnel through the wild internet, letting you work remotely without anyone eavesdropping on your commands.
In this chapter, you'll learn to connect to remote systems like a pro, transfer files securely, and lock down your systems so the bad guys stay out. Security isn't just for paranoid people—it's for SMART people. And you're about to get very smart!
The SSH Protocol: Secure Shell
The SSH protocol (Secure Shell) is the standard way to securely access remote Linux systems. It encrypts everything—your commands, your passwords, your files—so nobody can snoop on your connection.
SSH replaced older, insecure protocols like Telnet (which sent passwords in plain text—yikes!) and has been the gold standard since 1995.
What SSH Does
- Encrypted remote terminal: Run commands on remote computers
- Secure file transfer: Copy files safely between systems
- Port forwarding: Tunnel other connections through SSH
- Key-based authentication: Login without passwords (more secure!)
How SSH Works (Simplified)
- Client connects to server on port 22
- Server and client agree on encryption method
- Client authenticates (password or key)
- Encrypted session begins
- Everything you type is encrypted before sending
1 2 3 4 | |
The SSH Command: Connect to Remote Systems
The ssh command is your gateway to remote servers. It's simple to use but incredibly powerful.
Basic SSH Connection
1 2 3 4 5 6 7 8 9 10 11 | |
First Connection: The Fingerprint Warning
The first time you connect to a server, you'll see something like:
1 2 3 | |
This is SSH protecting you from "man-in-the-middle" attacks. The fingerprint is the server's identity. Type yes if you trust the server—SSH will remember it for next time.
Fingerprint Changes = Red Flag!
If you've connected before and suddenly see a different fingerprint, STOP! Someone might be intercepting your connection. Verify with the server administrator before proceeding.
Running Remote Commands
1 2 3 4 5 6 7 8 9 10 | |
SSH Options You'll Use
| Option | Purpose |
|---|---|
-p port |
Connect to different port |
-i keyfile |
Use specific private key |
-v |
Verbose (debugging) |
-X |
Forward X11 graphics |
-L local:host:remote |
Local port forwarding |
-R remote:host:local |
Remote port forwarding |
-N |
No command (just tunnel) |
SSH Keys: Passwords Are So Yesterday
SSH keys are the secure alternative to passwords. Instead of typing a password every time, you use cryptographic key pairs. It's more secure AND more convenient—the best of both worlds!
How Key Authentication Works
- You generate a key pair: private key (secret!) and public key (shareable)
- You put your public key on the remote server
- When you connect, SSH does cryptographic magic to prove you have the private key
- If it matches, you're in—no password needed!
Generating SSH Keys
1 2 3 4 5 | |
You'll be asked:
- Where to save: Press Enter for default (~/.ssh/id_ed25519)
- Passphrase: Optional but HIGHLY recommended (protects your key if stolen)
This creates two files:
- ~/.ssh/id_ed25519 - Your private key (NEVER share this!)
- ~/.ssh/id_ed25519.pub - Your public key (safe to share)
Installing Your Public Key
1 2 3 4 5 6 7 8 9 | |
Key Security Best Practices
1 2 3 4 5 | |
Protect Your Private Key!
Your private key is like your house key. If someone gets it (and knows your passphrase if you have one), they can access all servers where you've installed the public key. NEVER:
- Email your private key
- Put it in a Git repository
- Store it on shared systems
- Share it with anyone
Key-Based Auth: Why It's Better
Key-based authentication is superior to passwords for several reasons:
| Feature | Password | SSH Key |
|---|---|---|
| Can be guessed | Yes | Practically impossible |
| Can be brute-forced | Yes (slowly) | No (too long) |
| Can be phished | Yes | No |
| Reused across sites | Often | Never (unique per key) |
| Length | 8-20 characters | 4096+ bits |
| Convenience | Type every time | Automatic |
Once you set up SSH keys, you can (and should!) disable password authentication entirely:
1 2 3 4 5 6 | |
SSH Config: Your Connection Shortcuts
The SSH config file (~/.ssh/config) lets you create shortcuts for servers you connect to frequently. Instead of typing long commands, you define aliases!
Creating SSH Config
1 2 | |
Example Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
Now you can just type:
1 2 3 | |
Useful Config Options
| Option | Purpose |
|---|---|
HostName |
Real hostname or IP |
User |
Default username |
Port |
SSH port |
IdentityFile |
Which private key to use |
ProxyJump |
Jump through another host |
ServerAliveInterval |
Keep connection alive |
ForwardAgent |
Allow key forwarding |
LocalForward |
Set up port forwarding |
The SCP Command: Secure Copy
The scp command (secure copy) transfers files over SSH. It's like cp, but works across networks securely.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
SCP Between Two Remote Servers
1 2 | |
Common SCP Options
| Option | Purpose |
|---|---|
-r |
Recursive (directories) |
-P port |
Specify port (capital P!) |
-p |
Preserve modification times |
-C |
Compress during transfer |
-v |
Verbose output |
-i key |
Use specific identity file |
The Rsync Command: Smart Synchronization
The rsync command is the power tool for file synchronization. Unlike SCP, it only transfers what's changed—making it MUCH faster for large directories or repeated transfers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
The Trailing Slash Matters!
1 2 3 4 5 | |
Rsync is Idempotent
You can run the same rsync command repeatedly—it only transfers changes. This makes it perfect for backups and keeping servers in sync!
Rsync vs SCP
| Feature | SCP | Rsync |
|---|---|---|
| Speed (first copy) | Similar | Similar |
| Speed (updates) | Copies everything | Only changes |
| Resume interrupted | No | Yes |
| Exclude patterns | No | Yes |
| Delete removed files | No | Yes |
| Compression | Basic | Better |
| Bandwidth limiting | No | Yes |
Secure File Transfer: Best Practices
Secure file transfer is crucial when moving sensitive data. Here are the secure options:
The Good (Encrypted)
- SCP: Simple, secure, good for quick copies
- SFTP: Interactive file browser over SSH
- Rsync over SSH: Best for syncing large amounts
- HTTPS uploads: Encrypted web transfers
The Bad (Avoid These)
- FTP: Passwords sent in plain text!
- HTTP uploads: Anyone can see the data
- Telnet file transfers: Everything exposed
Using SFTP
1 2 3 4 5 6 7 8 9 10 11 | |
Firewall Basics: Your Digital Fortress
Firewall basics are essential for any Linux administrator. A firewall controls what network traffic can enter and leave your system—think of it as a security guard checking IDs at the door.
What Firewalls Do
- Block unwanted incoming connections (hackers, port scanners)
- Allow legitimate traffic (SSH, web servers)
- Control outgoing connections (optional but useful)
- Log suspicious activity (for investigation)
Firewall Types in Linux
| Type | Description |
|---|---|
iptables |
Low-level, powerful, complex |
nftables |
Modern replacement for iptables |
ufw |
User-friendly frontend |
firewalld |
Zone-based, used by RHEL/Fedora |
The UFW Firewall: Uncomplicated Firewall
The UFW firewall (Uncomplicated Firewall) is exactly what the name says—a simple way to manage firewall rules on Ubuntu and Debian systems.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
UFW Default Policies
1 2 3 4 5 | |
Rate Limiting with UFW
1 2 3 4 | |
Firewall Rules: Building Your Policy
Firewall rules determine what traffic is allowed. Rules are processed in order, and the first match wins.
Example Server Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Rule Order Example
1 2 3 4 | |
Port Security: Minimizing Attack Surface
Port security means only opening the ports you actually need. Every open port is a potential entry point for attackers.
Checking Open Ports
1 2 3 4 5 6 7 | |
Best Practices
- Default deny: Block everything, then allow specific ports
- Principle of least privilege: Only open what's necessary
- Use non-standard ports: Move SSH from 22 to something obscure
- Rate limiting: Slow down brute force attacks
- Geoblocking: Block countries you never connect from (advanced)
1 2 3 4 5 6 | |
Security Through Obscurity?
Changing the SSH port isn't TRUE security—a determined attacker will find it. But it stops 99% of automated bots that only try port 22. Use it AS WELL AS other security measures, not instead of them!
Linux Security: The Big Picture
Linux security is a layered approach—no single measure is enough, but together they create strong protection.
The Security Onion (Layers of Defense)
- Physical security: Lock the server room
- Network security: Firewalls, VPNs
- Host security: Updates, minimal services
- Application security: Secure configurations
- Data security: Encryption, backups
- User security: Strong passwords, limited privileges
- Monitoring: Logs, alerts, auditing
Quick Security Checklist
- [ ] Firewall enabled with minimal ports open
- [ ] SSH key authentication (passwords disabled)
- [ ] Regular system updates
- [ ] Non-root user accounts
- [ ] Sudo for administrative tasks
- [ ] Log monitoring configured
- [ ] Unnecessary services disabled
- [ ] File permissions properly set
User Authentication: Who Are You?
User authentication verifies that users are who they claim to be. Linux supports multiple authentication methods:
Authentication Methods
| Method | Security | Convenience |
|---|---|---|
| Password | Medium | High |
| SSH Keys | High | High |
| Two-Factor (2FA) | Very High | Medium |
| Smart Cards | Very High | Low |
| Biometrics | High | Medium |
Two-Factor Authentication for SSH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Now you need both your key/password AND a code from your phone!
Password Security: If You Must Use Them
Password security matters even if you prefer SSH keys—you still need passwords for sudo and local logins.
Strong Password Rules
- Length: Minimum 12 characters (longer is better)
- Complexity: Mix of upper, lower, numbers, symbols
- Uniqueness: Different password for every system
- No patterns: Avoid
password123,qwerty, birthdays
Password Policies
1 2 3 4 5 6 7 8 9 | |
Password Aging
1 2 3 4 5 6 | |
SSH Security: Hardening Your SSH Server
SSH security involves configuring the SSH daemon to be as secure as possible.
Essential SSH Hardening
Edit /etc/ssh/sshd_config:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
After changes:
1 2 3 4 5 | |
Don't Lock Yourself Out!
Before disabling password authentication, make sure your SSH key works! Keep a second terminal connected while testing.
Fail2Ban: Automatic Banning
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Fail2ban watches your logs and automatically blocks IPs that fail too many login attempts!
Security Updates: Stay Patched!
Security updates are your first line of defense against known vulnerabilities. Hackers actively scan for unpatched systems.
1 2 3 4 5 6 7 8 9 10 | |
How Fast Should You Update?
| Update Type | Urgency | When |
|---|---|---|
| Critical security | ASAP | Same day |
| Security patches | High | Within a week |
| Regular updates | Normal | Regular schedule |
| Major upgrades | Plan carefully | Test first |
Set It and Forget It
Enable automatic security updates on all servers. The risk of an unpatched vulnerability is almost always greater than the risk of an update breaking something.
Vulnerability Scanning: Know Your Weaknesses
Vulnerability scanning helps you find security holes before attackers do.
Basic Scanning Tools
1 2 3 4 5 6 7 8 9 10 | |
What Scanners Look For
- Open ports that shouldn't be
- Outdated software with known vulnerabilities
- Misconfigured services
- Weak file permissions
- Rootkits and malware
- Password policy issues
Regular Scanning Schedule
1 2 | |
Log Files: Your Security Diary
Log files record everything that happens on your system. When something goes wrong (or someone breaks in), logs tell the story.
Important Log Files
| Log | Location | Contents |
|---|---|---|
| Auth log | /var/log/auth.log |
Login attempts, sudo usage |
| Syslog | /var/log/syslog |
System events |
| Kernel | /var/log/kern.log |
Kernel messages |
| SSH | /var/log/auth.log |
SSH connections |
| Firewall | /var/log/ufw.log |
UFW firewall events |
| Fail2ban | /var/log/fail2ban.log |
Banned IPs |
Reading Logs
1 2 3 4 5 6 7 8 9 10 11 12 | |
Log Analysis Examples
1 2 3 4 5 6 7 8 9 10 | |
Audit Logs: Detailed Tracking
Audit logs provide even more detailed tracking using the Linux audit system (auditd).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Persistent Audit Rules
1 2 3 4 5 6 7 8 | |
Security Best Practices: The Golden Rules
Security best practices are the habits that keep systems safe:
The Top 10 Security Habits
- Update regularly: Patch early, patch often
- Use SSH keys: Disable password authentication
- Firewall everything: Default deny, explicit allow
- Principle of least privilege: Minimal permissions
- Monitor logs: Know what's happening
- Strong passwords: Where you must use them
- Disable unused services: Smaller attack surface
- Use sudo, not root: Accountability and safety
- Backup regularly: You will need them someday
- Test your security: Scan and audit yourself
The Security Mindset
"The only truly secure system is one that is powered off, cast in a block of concrete, and sealed in a lead-lined room with armed guards." — Gene Spafford
You can't achieve perfect security, but you CAN:
- Make attacks difficult
- Detect attacks quickly
- Recover from attacks smoothly
RealVNC: Remote Desktop for Support
Sometimes SSH isn't enough—you need to see the actual desktop to help someone with a technical issue. RealVNC lets you share and control remote Linux desktops, perfect for remote technical support.
What is VNC?
VNC (Virtual Network Computing) is a protocol for sharing graphical desktops remotely. Unlike SSH (which is text-only), VNC shows you the entire screen—windows, menus, everything!
Installing RealVNC Server
On the remote Linux system you want to control:
1 2 3 4 5 6 7 8 9 10 | |
Installing RealVNC Viewer
On your computer (the one you're using for support):
1 2 3 4 5 | |
Setting Up for Remote Support
Method 1: Direct Connection (Same Network)
- On the remote system, find its IP address:
hostname -I - On your computer, open VNC Viewer
- Enter the IP address
- Enter the remote user's password when prompted
1 2 | |
Method 2: RealVNC Cloud Connect (Internet)
For helping someone over the internet (no network configuration needed):
- Create a free RealVNC account at realvnc.com
- Sign in to VNC Server on the remote system
- Sign in to VNC Viewer on your computer
- The remote system appears in your team—just click to connect!
This is MUCH easier than configuring port forwarding!
Using VNC for Tech Support
Here's a typical remote support workflow:
- User reports problem: "My program won't open!"
- Connect via VNC: See exactly what they see
- Investigate: Watch their screen, check settings
- Fix or demonstrate: Show them how to fix it
- Disconnect: They're back on their own
VNC Security Tips
1 2 3 4 5 6 7 8 9 10 | |
VNC vs SSH for Support
Use SSH when you can fix things from the command line. Use VNC when you need to see the graphical interface—like helping someone configure desktop applications, debug GUI issues, or train them visually.
Alternative: X11 Forwarding
For running single graphical applications remotely (not the whole desktop):
1 2 3 4 5 6 | |
This is lighter than VNC for single applications, but requires X11 on your local system.
Putting It All Together: Securing a New Server
Here's a checklist for securing a fresh Linux server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
Key Takeaways
You're now a security-conscious Linux user!
- SSH: The secure way to access remote systems
- SSH keys: Far more secure than passwords
- SCP/Rsync: Secure file transfer methods
- Firewalls: Control what traffic enters your system
- UFW: The user-friendly firewall tool
- Security updates: Your first line of defense
- Logs: Your security diary for investigations
- VNC: Remote desktop for graphical support
Security is a Journey!
You won't implement everything at once, and that's okay. Start with the basics (keys, firewall, updates) and add more security measures over time. Every layer helps!
What's Next?
Congratulations—you've completed the core Linux curriculum! The next chapters cover advanced topics like container orchestration and system administration for those who want to go even deeper.
Quick Quiz: SSH and Security
- Why are SSH keys more secure than passwords?
- What command copies a file to a remote server over SSH?
- What UFW command blocks all incoming traffic except SSH?
- What file do you edit to configure SSH key aliases?
- Where do you look to see failed login attempts?
- What tool automatically bans IPs with too many failed logins?
- What's the difference between SCP and Rsync?
Quiz Answers
- Keys can't be guessed or brute-forced (they're 4096+ bits), can't be phished, and don't need to be typed
scp file.txt user@server:/path/orrsync -av file.txt user@server:/path/sudo ufw default deny incomingthensudo ufw allow ssh~/.ssh/config/var/log/auth.logorjournalctl -u sshd- Fail2ban
- SCP copies everything; Rsync only copies what changed (and can resume interrupted transfers)
References
- OpenSSH Manual - Official OpenSSH documentation covering all SSH features
- SSH Key Authentication Guide - DigitalOcean's comprehensive SSH key setup tutorial
- SSH Config File Examples - How to create and use SSH client configuration files
- UFW Firewall Guide - Ubuntu community guide to Uncomplicated Firewall
- Fail2ban Tutorial - Protecting SSH with automatic IP banning
- rsync Tutorial - Complete guide to file synchronization with rsync
- SSH Security Best Practices - Industry best practices for securing SSH
- Two-Factor Authentication for SSH - Adding 2FA with Google Authenticator
- Understanding Public Key Cryptography - How SSH key pairs work
- SCP Command Examples - Secure copy command tutorial with examples
- Linux Security Fundamentals - Red Hat's top security practices for Linux
- SSH Port Forwarding - Using SSH tunnels for secure connections
- Hardening SSH Configuration - Detailed SSH hardening guides
- VNC Server Setup - RealVNC official Raspberry Pi documentation
- Linux Audit System Guide - Using auditd for security auditing
- Password Policy Configuration - Setting up strong password requirements
- iptables vs UFW Comparison - Understanding Linux firewall options
- SSH Agent Forwarding - Using ssh-agent for key management
- Log Monitoring with journalctl - Viewing and analyzing system logs
- CIS Linux Benchmark - Industry-standard security configuration benchmarks