Networking Commands
978 words
5 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Networking Commands ## 🎯 Learning Objectives - Diagnose network connectivity with ping, traceroute - Transfer data with curl and wget - Connect securely with SSH - Copy files with SCP and rsync - Inspect network connections with netstat, ss, lsof ## 1. Network Diagnostics ## 2.

Networking Commands
🎯 Learning Objectives
- Diagnose network connectivity with ping, traceroute
- Transfer data with curl and wget
- Connect securely with SSH
- Copy files with SCP and rsync
- Inspect network connections with netstat, ss, lsof
1. Network Diagnostics
bash# Ping — test connectivity ping google.com # Continuous ping ping -c 4 google.com # 4 pings only ping -c 1 -W 2 192.168.1.1 # 1 ping, 2 second timeout # Traceroute — show path to host traceroute google.com # Show each hop traceroute -n google.com # Skip DNS lookup (faster) # DNS lookup nslookup google.com # Query DNS records dig google.com # Detailed DNS information host google.com # Simple DNS lookup # IP configuration ip addr # Show all IP addresses (replaces ifconfig) ip route # Show routing table ip link # Show network interfaces
2. Data Transfer — curl
curl (Client URL) transfers data to/from servers. It's the swiss-army knife of HTTP:
bash# Basic HTTP GET curl https://api.github.com/users/octocat curl -O https://example.com/file.zip # Download file (keeps filename) curl -o myfile.zip https://example.com/file.zip # Download with custom name # HTTP headers curl -I https://google.com # Show response headers only curl -v https://example.com # Verbose (request + response headers) # POST with data curl -X POST https://api.example.com/login \ -H "Content-Type: application/json" \ -d '{"username":"alice","password":"secret"}' # Authentication curl -u username:password https://api.example.com/secure curl -H "Authorization: Bearer TOKEN" https://api.example.com # Follow redirects curl -L https://bit.ly/example # Follow redirects
3. Downloading — wget
bash# Basic download wget https://example.com/file.zip # Resume interrupted download wget -c https://example.com/largefile.zip # Download to specific directory wget -P /downloads/ https://example.com/file.zip # Recursive download (website mirroring) wget -r -l 2 -np https://example.com/docs/ # -r: recursive, -l 2: 2 levels deep, -np: no parent directories
4. Secure Shell — ssh
bash# Basic connection ssh user@hostname # Connect to remote host ssh user@192.168.1.100 # Connect by IP ssh -p 2222 user@hostname # Custom port (default: 22) # Key-based authentication ssh-keygen -t ed25519 # Generate SSH key pair ssh-copy-id user@hostname # Copy public key to server # Now: ssh user@hostname (no password!) # Execute remote command ssh user@hostname "ls -la /var/log" ssh user@hostname "df -h" # Tunnel (port forwarding) ssh -L 8080:localhost:80 user@hostname # Forward local 8080 → remote 80 # SCP — secure copy over SSH scp file.txt user@hostname:/remote/path/ # Copy file TO remote scp user@hostname:/remote/file.txt ./ # Copy file FROM remote scp -r dir/ user@hostname:/remote/path/ # Copy directory recursively # rsync — efficient file sync (only transfers differences) rsync -avz source/ user@hostname:/dest/ # Sync TO remote rsync -avz user@hostname:/source/ ./dest/ # Sync FROM remote # -a: archive (preserve permissions), -v: verbose, -z: compress
5. Network Connections — netstat and ss
bash# Show active connections netstat -tuln # Listening ports (-t: TCP, -u: UDP, -l: listen, -n: numeric) ss -tuln # Modern replacement for netstat (faster) # Show all connections netstat -an # All connections ss -an # Modern version # Show process using port lsof -i :80 # What's running on port 80? lsof -i :3000 # What's on port 3000? fuser 80/tcp # Process using port 80 # Check if port is open nc -zv hostname 80 # Check if port 80 is open nc -zv hostname 22 80 443 # Check multiple ports
6. Firewall
bash# ufw (Uncomplicated Firewall) — Ubuntu sudo ufw status # Show firewall rules sudo ufw allow 80/tcp # Allow HTTP sudo ufw allow 22 # Allow SSH sudo ufw enable # Enable firewall
7. Practice Questions
Q1: How do you test if a remote server is reachable?Answer:ping -c 4 server.com. Sends 4 ICMP packets and shows response times. If the server is unreachable, you'll see "Destination Host Unreachable" or 100% packet loss. Q2: What doescurl -Ido?Answer: Sends an HTTP HEAD request, which retrieves only the response headers (not the body). Useful for checking content type, file size, status code, or last modified date without downloading the entire file. Q3: How do you securely copy a file to a remote server?Answer:scp localfile.txt user@server:/remote/path/. For directories:scp -r localdir/ user@server:/remote/path/. For large/syncing:rsync -avz source/ user@server:/dest/. Q4: How do you find what process is using port 3000?Answer:lsof -i :3000orfuser 3000/tcp. Shows the PID and process name. If nothing is listening, there's no output. Q5: What doesssh-keygendo?Answer: Generates a public/private key pair for SSH authentication. By default creates~/.ssh/id_rsa(private key) and~/.ssh/id_rsa.pub(public key). After copying the public key to a server withssh-copy-id, you can log in without a password. Q6: What is the difference between curl and wget?Answer:curlis more versatile: supports more protocols, more options for sending data (POST, PUT), better for API interaction.wgetis simpler for downloading files: supports recursive download, resuming, and works better for large file downloads. Both can download files; use curl for API testing, wget for bulk downloads. Q7: How do you check which ports are open on your machine?Answer:ss -tulnornetstat -tuln. Shows listening TCP and UDP ports with their numeric addresses.-t=TCP,-u=UDP,-l=listening,-n=numeric (no DNS). Q8: What doesssh -L 8080:localhost:3000 user@serverdo?Answer: Creates a local port forward: connections to localhost:8080 are tunneled through SSH to server, which forwards to localhost:3000 (from server's perspective). This allows accessing a service running on the server's port 3000 via your machine's port 8080.
📐 Key Concepts
| Command | Purpose | Common Usage |
|---|---|---|
ping | Test connectivity | ping -c 4 host |
curl | HTTP data transfer | curl -O url |
wget | File download | wget url |
ssh | Secure shell | ssh user@host |
scp | Secure copy | scp file user@host:/path |
rsync | Efficient sync | rsync -avz src/ dest/ |
ss | Socket status | ss -tuln |
lsof | Open files/ports | lsof -i :port |