This lesson joins the pieces that sit between a visitor and your application: the firewall that decides what may pass, NAT that lets private machines reach the internet, VPNs that connect networks safely, load balancers and reverse proxies that share the work, TLS that encrypts it, and the CDN that puts copies near the visitor. Then it ends with a simple method for finding network problems. Every cloud lesson later uses these ideas under different names.
Why do we need it?
Between your users and your app sit many network parts that protect it and share the work. If you do not understand them, you cannot secure a service or find why traffic is blocked.
A real-life example
Think of a stadium. Security guards check tickets at the gate (firewall). The ticket office hides the real seat numbers and sells tickets through one window (NAT). Staff guide people to the least crowded entrance (load balancer). A tunnel for players only (VPN). The CDN is like selling snacks at stands near every seat instead of one kitchen.
Firewalls
A firewall allows or blocks traffic using rules. A rule usually names a direction (in or out), a protocol, a port and a source or destination address. The safest approach is to block everything by default and open only what you need. On Ubuntu, the simple tool is ufw.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp # SSH: allow this BEFORE you enable the firewall
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
The Red Hat family uses firewalld with firewall-cmd, and underneath both are built on the kernel feature called nftables or iptables. Cloud providers add their own firewalls at the network edge, called security groups and network ACLs in AWS. A request must pass every firewall on its path, so if a port is open in ufw but blocked in the cloud security group, it is still blocked. Always check both. This commands block was not run for this course. Be very careful with remote servers: if you enable a firewall without allowing SSH first, you lock yourself out.
NAT
NAT (Network Address Translation) lets many devices with private addresses share one public address. When a private machine opens a connection to the internet, the NAT device swaps the private source address for its own public one, remembers the conversation, and swaps back when the answer returns. Outside machines cannot start a connection to the private machine, which is a handy side effect. In the cloud, a NAT gateway gives private servers a way out (for updates, for example) without giving them a public address.
VPN
A VPN (Virtual Private Network) creates an encrypted tunnel across the internet. A site-to-site VPN joins two networks, for example an office and a cloud network, so they behave like one network. A remote-access VPN lets one person connect to a private network from home. Tools you will meet include WireGuard and OpenVPN, and every big cloud has managed VPN services. A VPN protects the traffic in the tunnel. It does not make a weak server safe.
Load balancers and reverse proxies
- A reverse proxy sits in front of one or more servers and forwards requests to them. Nginx is the common example. It can serve static files, terminate TLS, compress, cache and hide the real servers.
- A load balancer spreads requests over several copies of an application, and stops sending to a copy that fails its health check. Common ways to pick a server are round robin (take turns) and least connections.
- Layer 4 balancers work with addresses and ports only. Layer 7 balancers read the HTTP request and can route by path or host name, for example /api to one group and /blog to another.
A load balancer is what makes a web service both scalable (add more copies) and available (a failed copy is skipped). It needs a health check endpoint in your app, such as /health, that answers 200 only when the app is truly working. The health check script from the Bash lesson is a tiny version of what a load balancer does every few seconds.
TLS and certificates
TLS encrypts the connection and proves who the server is. A certificate says: this public key belongs to this domain, signed by an authority your browser trusts. Certificates expire, and an expired one gives every visitor a scary warning, so renewal must be automatic. Let’s Encrypt is a free authority that issues certificates through the ACME protocol, and tools like Certbot automate it. Cloud providers offer managed certificates that renew by themselves. You can look at a server’s certificate with openssl s_client -connect example.com:443 or with curl -vI https://example.com, and the expiry date is in the output.
Routing, switching and CDN
- A switch connects devices inside one network. A router connects different networks and decides the next hop for each packet using a routing table. The default route (0.0.0.0/0) means: for anything I do not know, send it this way, normally to the internet gateway.
- A CDN (content delivery network) keeps copies of your files on servers in many places, so visitors get them from a nearby server. This makes pages load faster, takes load off your server and can absorb some attacks. It works best for images, scripts and pages that do not change per user.
Network configuration on Linux
Before you can troubleshoot, you need to see how a Linux machine is set up. These commands only read information, so they are safe to try. They were not run for this course on a real server, and the output differs on every machine.
ip addr # interfaces and their IP addresses (older systems: ifconfig)
ip route # the routing table, including the default gateway
hostname -I # just the addresses
cat /etc/resolv.conf # which DNS servers this machine asks
cat /etc/hosts # fixed name-to-address entries, checked before DNS
hostnamectl # the machine name
To make a static address permanent you edit the network configuration of your distribution: Ubuntu uses Netplan files in /etc/netplan/, Red Hat style systems use NetworkManager (nmcli). In the cloud you almost never edit this by hand, because the provider’s DHCP gives the machine its address, so change addresses in the cloud console or in code instead. A wrong network change on a remote server can cut your own connection, so keep a second way in, such as the provider’s console.
A troubleshooting method
When something cannot connect, do not guess. Go from the bottom up and from near to far, and change only one thing at a time.
- Is the name right? Check DNS with
digornslookup. - Can I reach the machine? Try
ping(some networks block it, so a failed ping is not proof). - Is the port open? Test from the client with the health script,
curlornc -vz host port. - Is the service listening? On the server,
ss -tlnpshows what listens and on which address. - Is a firewall in the way? Check the local firewall, the cloud security group and any network ACL.
- Does the application answer? Use
curl -vand read the status code. Then read the application logs.
This small script is the port and URL test from the Bash lesson. It tells you at once whether the failure is at the port (nothing is listening or a firewall drops it) or in the application (a status code other than 200). It was run in the course lab against a local test server. The script in this lesson was run on Linux in the course lab (bash 5, Python 3.13). The output shown is real. Your numbers (memory, disk, dates) will be different.
#!/usr/bin/env bash
# healthcheck.sh - is a TCP port open, and does a URL answer 200? Exit code 0 = healthy.
# Usage: ./healthcheck.sh <host> <port> [path]
set -uo pipefail
HOST="${1:?host}"; PORT="${2:?port}"; PATH_="${3:-/}"
if ! (exec 3<>"/dev/tcp/$HOST/$PORT") 2>/dev/null; then
echo "FAIL: $HOST:$PORT is not accepting connections"; exit 1
fi
CODE=$(curl -s -o /dev/null -m 3 -w '%{http_code}' "http://$HOST:$PORT$PATH_" || true)
if [ "$CODE" = "200" ]; then
echo "OK: $HOST:$PORT$PATH_ answered 200"; exit 0
fi
echo "FAIL: $HOST:$PORT$PATH_ answered ${CODE:-nothing}"; exit 1
$ ./healthcheck.sh 127.0.0.1 8765 /
OK: 127.0.0.1:8765/ answered 200
$ ./healthcheck.sh 127.0.0.1 8765 /missing
FAIL: 127.0.0.1:8765/missing answered 404
$ ./healthcheck.sh 127.0.0.1 8765
FAIL: 127.0.0.1:8765 is not accepting connections
Architecture diagram
Here is the idea as a simple picture. You can use the same diagram in your own notes.
flowchart LR
U((Users)) --> CDN[CDN]
CDN --> FW[Firewall]
FW --> LB[Load balancer]
LB --> A1[App copy 1]
LB --> A2[App copy 2]
A1 --> DB[(Database in private subnet)]
A2 --> DB
How to check it worked
- You can explain what NAT does and why a private server cannot be reached from the internet directly.
- You can say the difference between a reverse proxy and a load balancer.
- Your health check shows the three different results when you test OK, 404 and a stopped server.
How companies use it
Production systems sit behind a load balancer with HTTPS, private application servers, a NAT gateway for outbound traffic and strict firewall rules. Engineers use VPNs for admin access and CDNs for speed and protection.
Common errors and how to fix them
- Locked out after enabling ufw. Allow SSH before enabling. Use the provider console to recover.
- Port open locally but blocked from outside. Check the cloud security group and network ACL too.
- Certificate expired. Set up automatic renewal and an expiry alert.
- Load balancer marks all targets unhealthy. The health check path or port is wrong, or the app is down.
- Cannot reach the internet from a private server. Add a NAT gateway and a route to it.
Best practices
- Deny by default and open only needed ports.
- Automate certificate renewal.
- Put databases in private subnets.
- Use health checks that test the real app.
- Log and monitor blocked traffic.
Interview questions with simple answers
- What is a firewall? A set of rules that allows or blocks traffic.
- What is NAT? Lets private addresses share a public address by translating them.
- What is a VPN? An encrypted tunnel across the internet joining devices or networks.
- Reverse proxy vs load balancer? A reverse proxy forwards to servers. A load balancer spreads traffic and checks health.
- Layer 4 vs layer 7 balancing? Layer 4 uses addresses and ports. Layer 7 reads the HTTP request.
- What does TLS do? Encrypts the connection and proves who the server is.
- What is Let’s Encrypt? A free certificate authority with automatic issuing.
- What is a CDN? A network of servers that caches content near users.
- How do you troubleshoot a connection? Name, reachability, port, listening service, firewalls, application.
- Why can a failed ping be misleading? Many systems block ping on purpose.
Practical exercises
Try each one yourself before you look at the hint. Struggling for a few minutes is how the skill sticks.
- Test a local web server with the health script, then stop it and test again. Hint:
python3 -m http.server. - Check the expiry date of a site certificate. Hint:
curl -vIoropenssl s_client. - Draw a request path through CDN, firewall and load balancer. Hint: Use the diagram.
- Write a ufw plan that allows only SSH, HTTP and HTTPS. Hint: Default deny incoming.
- Explain NAT to a friend with the stadium example. Hint: One window sells to many.
Mini project: Secure small web server
On a practice Ubuntu machine, install nginx, allow only ports 22, 80 and 443 with ufw, and test each rule from another machine. Record the commands and what you saw.
Knowledge test
Answer the ten questions in the quiz at the end of this lesson. You pass with 60 percent. If an answer is wrong, read the explanation, then reread the part of the lesson it points to.