Lesson 18: Networking & SSH

Goal: Learn to inspect network settings, transfer files, and connect to remote systems securely.


Table of Contents

  1. Network Info — ip, hostname
  2. Connectivity — ping, traceroute
  3. DNS — dig, nslookup
  4. HTTP Requests — curl, wget
  5. Ports & Connections — ss, netstat
  6. Firewall — ufw
  7. SSH — Secure Shell
  8. File Transfer — scp, rsync
  9. Exercises

Network Info — ip, hostname

Show IP addresses

# Show all interfaces
ip a

# Show only IPv4 addresses
ip -4 a

# Short format
hostname -I

Show routing table

ip r
# or
ip route

Output:

default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100

Show your public IP

curl -s ifconfig.me
curl -s ipinfo.io
curl -s checkip.amazonaws.com

Connectivity — ping, traceroute

ping — Check if a host is reachable

# Continuous ping
ping google.com

# Send 5 pings only
ping -c 5 google.com

# Set timeout (1 second)
ping -W 1 -c 3 192.168.1.1

# Quick check (1 ping, silent)
ping -c 1 -W 2 google.com > /dev/null 2>&1 && echo "Online" || echo "Offline"

traceroute — Trace the path to a host

# Install
sudo apt install traceroute

# Trace route
traceroute google.com

# Using ICMP instead of UDP
sudo traceroute -I google.com

mtr — Combines ping + traceroute

sudo apt install mtr
mtr google.com

DNS — dig, nslookup

dig — DNS lookup

# Install
sudo apt install dnsutils

# Basic lookup
dig example.com

# Short answer only
dig +short example.com

# Specific record types
dig example.com A         # IPv4 address
dig example.com AAAA      # IPv6 address
dig example.com MX        # Mail servers
dig example.com NS        # Name servers
dig example.com TXT       # TXT records
dig example.com CNAME     # Canonical name

# Reverse DNS lookup
dig -x 8.8.8.8

# Use a specific DNS server
dig @8.8.8.8 example.com

nslookup — Interactive DNS

nslookup example.com
nslookup -type=MX example.com

HTTP Requests — curl, wget

curl — Transfer data

# GET request
curl https://api.example.com/data

# Show response headers
curl -I https://example.com

# Include headers in output
curl -i https://example.com

# Follow redirects
curl -L https://example.com

# Download a file
curl -O https://example.com/file.zip

# Download with custom filename
curl -o myfile.zip https://example.com/file.zip

# POST request with data
curl -X POST -d "name=John" https://api.example.com/users

# POST JSON
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"name": "John", "email": "john@example.com"}' \
  https://api.example.com/users

# With authentication
curl -u username:password https://api.example.com/

# Bearer token
curl -H "Authorization: Bearer TOKEN" https://api.example.com/

# Silent mode (no progress bar)
curl -s https://api.example.com/data

# Save cookies
curl -c cookies.txt https://example.com

# Send cookies
curl -b cookies.txt https://example.com/dashboard

wget — Download files

# Download a file
wget https://example.com/file.zip

# Download with custom filename
wget -O myfile.zip https://example.com/file.zip

# Download in background
wget -b https://example.com/large-file.iso

# Continue interrupted download
wget -c https://example.com/large-file.iso

# Download entire website (mirror)
wget --mirror --convert-links --page-requisites https://example.com

# Limit download speed
wget --limit-rate=1M https://example.com/file.zip

# Download silently
wget -q https://example.com/file.zip

Ports & Connections — ss, netstat

ss — Socket Statistics (modern)

# Show all listening TCP/UDP ports
ss -tulnp

# Show only TCP
ss -tlnp

# Show only UDP
ss -ulnp

# Show all connections
ss -a

# Show established connections
ss -t state established

# Filter by port
ss -tlnp | grep :80
ss -tlnp | grep :443

Understanding ss output

State    Recv-Q  Send-Q  Local Address:Port   Peer Address:Port  Process
LISTEN   0       128     0.0.0.0:22           0.0.0.0:*          users:(("sshd",pid=1234))
LISTEN   0       511     0.0.0.0:80           0.0.0.0:*          users:(("nginx",pid=5678))

netstat (legacy, but widely known)

# Same as ss -tulnp
netstat -tulnp

Firewall — ufw

ufw (Uncomplicated Firewall) is a simple frontend for iptables.

# Enable firewall
sudo ufw enable

# Check status
sudo ufw status
sudo ufw status verbose

# Allow SSH (IMPORTANT — do this before enabling!)
sudo ufw allow ssh
sudo ufw allow 22

# Allow HTTP and HTTPS
sudo ufw allow 80
sudo ufw allow 443

# Allow a specific port
sudo ufw allow 3000

# Allow from a specific IP
sudo ufw allow from 192.168.1.100

# Allow from subnet to specific port
sudo ufw allow from 192.168.1.0/24 to any port 22

# Deny a port
sudo ufw deny 8080

# Delete a rule
sudo ufw delete allow 8080

# Reset all rules
sudo ufw reset

# Disable firewall
sudo ufw disable

Always allow ssh BEFORE enabling ufw on a remote server, or you will lock yourself out!


SSH — Secure Shell

Connect to a remote server

# Basic connection
ssh user@hostname

# Specific port
ssh -p 2222 user@hostname

# With verbose output (debugging)
ssh -v user@hostname
# 1. Generate a key pair
ssh-keygen -t ed25519 -C "your@email.com"
# Press Enter for default location (~/.ssh/id_ed25519)
# Optionally set a passphrase

# 2. Copy public key to server
ssh-copy-id user@hostname

# 3. Now you can connect without a password
ssh user@hostname

SSH Config File

Create ~/.ssh/config for shortcuts:

cat > ~/.ssh/config << 'EOF'
Host myserver
    HostName 192.168.1.50
    User benjamin
    Port 22
    IdentityFile ~/.ssh/id_ed25519

Host production
    HostName prod.example.com
    User deploy
    Port 2222
    IdentityFile ~/.ssh/prod_key

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
EOF

# Set correct permissions
chmod 600 ~/.ssh/config

Now connect with just:

ssh myserver
ssh production

Run a command remotely

# Single command
ssh user@host "uptime"

# Multiple commands
ssh user@host "cd /var/log && tail -20 syslog"

# Run a local script on a remote server
ssh user@host < local-script.sh

SSH Tunneling

# Local port forwarding (access remote service locally)
# Access remote:5432 (PostgreSQL) via localhost:5432
ssh -L 5432:localhost:5432 user@host

# Remote port forwarding (expose local service remotely)
ssh -R 8080:localhost:3000 user@host

# Dynamic SOCKS proxy
ssh -D 1080 user@host

File Transfer — scp, rsync

scp — Secure Copy

# Copy file TO server
scp file.txt user@host:/path/

# Copy file FROM server
scp user@host:/path/file.txt ./

# Copy directory (recursive)
scp -r folder/ user@host:/path/

# Custom port
scp -P 2222 file.txt user@host:/path/

rsync only transfers changed files, making it much faster for repeated transfers.

# Basic sync
rsync -avz source/ user@host:/destination/

# Dry run (preview what would be transferred)
rsync -avzn source/ user@host:/destination/

# Delete files on destination that don't exist on source
rsync -avz --delete source/ user@host:/destination/

# Exclude files
rsync -avz --exclude='node_modules' --exclude='.git' source/ user@host:/dest/

# Show progress
rsync -avz --progress source/ user@host:/destination/

# Local sync (also useful!)
rsync -avz /source/folder/ /backup/folder/

# With SSH on custom port
rsync -avz -e "ssh -p 2222" source/ user@host:/dest/

rsync flags explained

Flag Meaning
-a Archive mode (preserves permissions, timestamps, etc.)
-v Verbose
-z Compress during transfer
-n Dry run
--delete Delete extraneous files on destination
--progress Show transfer progress
--exclude Exclude files/patterns

Exercises

Exercise 1: Network inspection

# 1. Show your IP address
ip -4 a | grep inet

# 2. Show your public IP
curl -s ifconfig.me && echo

# 3. Ping Google (3 times)
ping -c 3 google.com

# 4. DNS lookup
dig +short google.com

# 5. Check open ports on your machine
ss -tulnp

Exercise 2: curl practice

# 1. Get HTTP headers from a website
curl -I https://example.com

# 2. Download a file
curl -O https://example.com/index.html

# 3. Make an API call
curl -s https://api.github.com | head -20

# 4. Clean up
rm -f index.html

Exercise 3: SSH keys

# 1. Generate an SSH key (if you don't have one)
ls ~/.ssh/id_ed25519 2>/dev/null || ssh-keygen -t ed25519

# 2. View your public key
cat ~/.ssh/id_ed25519.pub

# 3. Check SSH config
cat ~/.ssh/config 2>/dev/null || echo "No config file yet"

Key Takeaways

  • ip a — show network interfaces and IPs
  • ping -c 3 host — test connectivity
  • dig +short domain — DNS lookup
  • curl — HTTP requests and API calls
  • wget — download files
  • ss -tulnp — show open ports
  • ufw allow ssh — always allow SSH before enabling firewall
  • ssh-keygen -t ed25519 — generate SSH keys
  • rsync -avz — efficient file sync (better than scp)
  • ~/.ssh/config — SSH shortcuts for your servers

Next Lesson: Lesson 19: Advanced Tools & Power User →