🐧 Week 2: Linux + Networking Basics

Day 5: Networking Fundamentals & SSH

Duration: 5 Hours

📚 Learning Objectives

By the end of this session, you will be able to:

  • Understand IP addresses, ports, and protocols
  • Use networking commands (ping, curl, netstat)
  • Connect to remote servers using SSH
  • Set up SSH key authentication
  • Transfer files with SCP

📖 Core Concepts (2 Hours)

Networking Basics

  • IP Address: Unique identifier for a device (192.168.1.100)
  • Port: Virtual channel for specific services (22, 80, 443)
  • Protocol: Rules for communication (TCP, UDP, HTTP, HTTPS)
  • DNS: Translates domain names to IP addresses
Common Ports: 22 - SSH (Secure Shell) 80 - HTTP (Web) 443 - HTTPS (Secure Web) 3306 - MySQL 5432 - PostgreSQL 6379 - Redis 27017 - MongoDB

Networking Commands

# Check network interfaces ip addr # Modern ifconfig # Legacy (net-tools) # Test connectivity ping google.com ping -c 4 192.168.1.1 # 4 packets only # DNS lookup nslookup google.com dig google.com # View routing table ip route route -n # Legacy # Check open ports netstat -tuln # TCP/UDP listening ports ss -tuln # Modern alternative # Test specific port nc -zv hostname 22 # Check if port 22 is open telnet hostname 80 # Connect to port 80

HTTP Requests with curl

# GET request curl http://example.com curl -v http://example.com # Verbose # Follow redirects curl -L http://example.com # Save to file curl -o output.html http://example.com # POST request curl -X POST -d "name=test" http://example.com/api # JSON POST curl -X POST \ -H "Content-Type: application/json" \ -d '{"name":"test"}' \ http://example.com/api # View headers only curl -I http://example.com # With authentication curl -u username:password http://example.com

SSH - Secure Shell

# Basic SSH connection ssh username@hostname ssh user@192.168.1.100 # Specify port ssh -p 2222 user@hostname # Run command remotely ssh user@host "ls -la" ssh user@host "uptime && df -h" # SSH config file (~/.ssh/config) Host myserver HostName 192.168.1.100 User ubuntu Port 22 IdentityFile ~/.ssh/my_key # Then simply: ssh myserver

SSH Key Authentication

# Generate SSH key pair ssh-keygen -t rsa -b 4096 -C "your_email@example.com" # or ssh-keygen -t ed25519 -C "your_email@example.com" # Files created: # ~/.ssh/id_rsa (private key - KEEP SECRET!) # ~/.ssh/id_rsa.pub (public key - share this) # Copy public key to server ssh-copy-id user@hostname # or manually: cat ~/.ssh/id_rsa.pub | ssh user@host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys" # Set correct permissions chmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa chmod 644 ~/.ssh/id_rsa.pub chmod 600 ~/.ssh/authorized_keys

File Transfer (SCP & rsync)

# SCP - Secure Copy # Copy local to remote scp file.txt user@host:/path/to/destination/ scp -r folder/ user@host:/path/ # Recursive # Copy remote to local scp user@host:/path/file.txt ./local/ # rsync - Better for large transfers rsync -avz localdir/ user@host:/remotedir/ rsync -avz --progress localdir/ user@host:/remotedir/ # rsync options: # -a archive mode (preserves permissions, etc.) # -v verbose # -z compress during transfer # --progress show progress # --delete delete files at dest not in source

🔬 Hands-on Lab (2.5 Hours)

Lab 1: Network Exploration

  • Find your IP address
  • Test connectivity to various hosts
  • Check what ports are listening
# Lab 1: Network exploration # Find your IP ip addr | grep inet # Test internet connectivity ping -c 4 google.com ping -c 4 8.8.8.8 # DNS lookup nslookup github.com dig github.com +short # Check listening ports ss -tuln # Test if a remote port is open nc -zv google.com 443 nc -zv google.com 80

Lab 2: HTTP with curl

  • Make various HTTP requests
  • Parse JSON responses
  • Download files
# Lab 2: curl exercises # Get a webpage curl -I https://github.com # Get JSON from API curl https://api.github.com # Parse JSON with jq curl -s https://api.github.com | jq '.current_user_url' # Download a file curl -o test.html https://example.com curl -LO https://example.com/file.zip # Use remote filename # POST request curl -X POST \ -H "Content-Type: application/json" \ -d '{"title":"test","body":"content"}' \ https://jsonplaceholder.typicode.com/posts

Lab 3: SSH Key Setup

  • Generate SSH key pair
  • View public key
  • Configure SSH settings
# Lab 3: SSH key setup # Generate key (if you don't have one) ssh-keygen -t ed25519 -C "your_email@example.com" # View your public key cat ~/.ssh/id_ed25519.pub # OR cat ~/.ssh/id_rsa.pub # Create SSH config cat > ~/.ssh/config << 'EOF' # Default settings for all hosts Host * AddKeysToAgent yes IdentitiesOnly yes # Example server configuration # Host myserver # HostName 192.168.1.100 # User ubuntu # IdentityFile ~/.ssh/id_ed25519 EOF chmod 600 ~/.ssh/config

Lab 4: Bash Scripting - Network Health

  • Create script to check multiple hosts
  • Check if services are responding
  • Generate connectivity report
# Lab 4: Network health check script cat > ~/devops-project/scripts/network-check.sh << 'EOF' #!/bin/bash echo "====== Network Health Check ======" echo "Date: $(date)" echo "" # Hosts to check HOSTS=("google.com" "github.com" "amazon.com") echo "=== Connectivity Test ===" for host in "${HOSTS[@]}"; do if ping -c 1 -W 2 $host > /dev/null 2>&1; then echo "✓ $host - Reachable" else echo "✗ $host - Unreachable" fi done echo "" echo "=== DNS Resolution ===" echo "Google DNS: $(dig +short google.com | head -1)" echo "" echo "=== My Network Info ===" echo "Local IP: $(hostname -I | awk '{print $1}')" echo "Default Gateway: $(ip route | grep default | awk '{print $3}')" echo "" echo "=== Listening Ports ===" ss -tuln | grep LISTEN | head -5 echo "==============================" EOF chmod +x ~/devops-project/scripts/network-check.sh ~/devops-project/scripts/network-check.sh

🎉 Week 2 Complete!

What you've learned this week:

  • Linux file system and navigation
  • File operations and permissions
  • Text processing with grep, sed, awk
  • Process and service management
  • Package management
  • Networking fundamentals and SSH

📝 Week 2 Project

Weekend Project: System Monitoring Script

Build a comprehensive system monitoring script that:

  • Checks disk, memory, CPU usage
  • Lists top resource-consuming processes
  • Verifies critical services are running
  • Pings important hosts for connectivity
  • Saves report to a log file
  • Can be run via cron job

✅ Week 2 Final Checklist

  • Comfortable navigating Linux file system
  • Can perform file operations confidently
  • Can search and process text
  • Can manage processes and services
  • Can install software packages
  • Understand networking basics
  • Can connect via SSH with keys
  • Can transfer files with SCP/rsync