Or: How I Learned to Stop Worrying and Love Network Namespaces
Introduction: The Plot Twist Nobody Saw Coming ๐ฌ
Warning: No AWS bills were harmed in the making of this project! ๐ธ
Hey! I'm Sherifdeen Adebayo, and buckle up because I'm about to tell you how I accidentally became best friends with Linux networking (didn't see THAT coming in 2025! ๐ ).
So there I was, staring at the HNG13 Stage 4 DevOps challenge like it was jollof rice without chicken ๐ญ. The task? Build a complete Virtual Private Cloud system on Linux. My networking knowledge at the time? "IP address goes brrr" ๐คทโโ๏ธ
Spoiler alert: It's not magic. It's just clever use of Linux tools that have been chilling since before TikTok was a thing! (Yes, I'm old enough to remember when we had to ping people to know if they were online ๐ด)
What I Built (While Consuming Unhealthy Amounts of Coffee โ๏ธ)
I created vpcctl - basically AWS VPC's younger, cooler cousin who lives in Lagos and knows all the shortcuts:
- Create isolated virtual networks faster than you can say "subnet mask" ๐ฏ
- Provision public and private subnets (like VIP and regular sections at Detty December ๐)
- Deploy applications without breaking a sweat ๐ช
- Control connectivity like a network traffic warden ๐ฆ
- Apply firewall rules (because we don't trust anybody, not even ourselves ๐)
Basically, I rebuilt a mini AWS VPC from scratch. And honestly? It was like learning to ride a bike - painful at first, then suddenly you're doing wheelies! ๐ดโโ๏ธ
Shopping List: What You'll Need ๐
Before we dive into this beautiful mess, grab these:
- A Linux machine (Ubuntu 20.04+ - or as I call it, "The Reliable Uncle") ๐ป
- Root/sudo access (because we're about to do DANGEROUS things... safely ๐)
- Basic networking knowledge (if you know what an IP address is, you're 80% there!)
- Command line skills (copy-paste counts as skills, right? ๐)
- Patience (LOTS of it - this took me more coffee breaks than I care to admit โ๏ธโ๏ธโ๏ธ)
The Secret Sauce: Linux Networking Unveiled ๐ฉโจ
Like suya and yaji, these concepts are better together!
Network Namespaces: The Private Apartments ๐
Network namespaces are like giving each subnet its own apartment - complete with:
- Its own network interfaces (like having your own Wi-Fi ๐ก)
- Personal IP addresses (no sharing with siblings!)
- Private routing tables (your business is YOUR business ๐คซ)
- Custom firewall rules (because boundaries matter!)
It's like having multiple computers on one machine, but without the electricity bill! ๐ก
veth Pairs: The Virtual Cables ๐
Think of veth pairs as virtual LAN cables, but cooler:
- They come in pairs (like AirPods, but they never get lost!)
- What goes in one end, comes out the other (magic? Nope, just Linux! โจ)
- Perfect for connecting namespaces to bridges
- No tangling required (looking at you, earphones! ๐ค)
Pro tip: These are like WhatsApp groups - messages go in, chaos comes out! ๐ฑ
Linux Bridges: The Virtual Switch ๐๏ธ
Bridges are basically the Dangote of networking - they connect EVERYTHING:
- Act as virtual switches (think of them as traffic controllers ๐ฅ)
- Forward packets between interfaces (like a very efficient delivery service ๐ฆ)
- In our VPC, this bad boy is the central router
- More reliable than NEPA (okay, that's not saying much ๐ )
NAT: The Internet Passport ๐
Network Address Translation is like having a bouncer who lets your private IPs into the internet club:
- Translates private IPs to public IPs
- Keeps your internal network safe (like a digital bodyguard ๐ช)
- Makes the internet think everything is coming from one place
- Basically the VPN your parents wish they understood ๐
The Blueprint: What We're Actually Building ๐
Host System (aka "The Mothership" ๐)
โโโ VPC (10.0.0.0/16) - The whole village ๐๏ธ
โ โโโ Bridge (br-my-vpc) - The town square ๐๏ธ
โ โโโ Public Subnet (10.0.1.0/24) - The market ๐ช
โ โ โโโ Namespace (ns-my-vpc-public) - Individual shops
โ โ โโโ NAT Gateway (iptables) - The security guard ๐ฎ
โ โ โโโ Application - The goods ๐ฆ
โ โโโ Private Subnet (10.0.2.0/24) - The warehouse ๐ญ
โ โโโ Namespace (ns-my-vpc-private) - Storage rooms
โ โโโ Application - The secret stash ๐ค
โโโ Internet Connection - The outside world ๐
Let's Get Our Hands Dirty: Building This Beast ๐ช
Fun fact: This section was written at 2 AM with questionable life choices being made!
Step 1: Project Setup (The Boring Part We Must Endure) ๐ด
First, let's create our project home:
mkdir vpc-control # AKA "The Beginning of Something Beautiful"
cd vpc-control
mkdir -p lib policies tests logs # Creating our empire, one folder at a time
Step 2: The Brain - Core CLI Tool ๐ง
Create the main vpcctl script. This is our Jarvis, our Friday, our... you get it:
#!/usr/bin/env python3
"""
vpcctl - Virtual Private Cloud Control CLI
AKA "The Magic Wand of Networking" ๐ช
"""
import argparse
import sys
import os
# This bad boy handles:
# - create-vpc (Birth of a network ๐ฃ)
# - create-subnet (Subdivision time! ๐๏ธ)
# - deploy-app (App goes brrr ๐)
# - peer-vpcs (Making friends across borders ๐ค)
# - apply-policy (Because we need rules, apparently ๐)
# And more cool stuff that'll make your DevOps heart sing! ๐ต
The full implementation includes these MVPs:
-
vpc_manager.py- The VPC whisperer ๐ฃ๏ธ -
subnet_manager.py- Subnet sensei ๐ฅ -
nat_manager.py- Internet access enabler (the real MVP) ๐ -
peering_manager.py- The matchmaker ๐ -
firewall_manager.py- The bouncer ๐ช
Step 3: Birth of a VPC (It's Alive! โก๏ธ)
Here's what happens when you unleash the beast:
sudo ./vpcctl create-vpc --name my-vpc --cidr 10.0.0.0/16
# Translation: "Let there be network!" ๐
Under the hood (this is where the magic happens โจ):
- Creates a Linux bridge:
ip link add br-my-vpc type bridge- Like building a roundabout in Lagos (but this one actually works!)
- Brings it up:
ip link set br-my-vpc up- "Hello world!" but for networks
- Stores config in state file
- Because elephants aren't the only ones who remember
The bridge is basically your VPC's brain ๐ง . Treat it well!
Step 4: Subnet Mania (Where Things Get Spicy ๐ถ๏ธ)
Subnets = Network namespaces = Mini isolated networks. Mind = Blown ๐คฏ
sudo ./vpcctl create-subnet --vpc my-vpc --name public --cidr 10.0.1.0/24 --type public
# AKA "Creating the VIP section" ๐
Behind the curtains (drum roll please ๐ฅ):
- Create namespace:
ip netns add ns-my-vpc-public- Apartment building, meet your new tenant!
- Create veth pair:
ip link add veth-public type veth peer name eth0- Virtual cables, assemble!
- Move one end to namespace:
ip link set eth0 netns ns-my-vpc-public- "Welcome to your new home!" ๐
- Attach other end to bridge:
ip link set veth-public master br-my-vpc- "Now kiss!" (the bridge and the interface, not you and your computer)
- Configure IP address
- Every subnet needs an identity!
- Set up routing
- GPS for packets ๐บ๏ธ
For public subnets, we add the secret sauce (NAT):
# Enable IP forwarding (because sharing is caring ๐)
sysctl -w net.ipv4.ip_forward=1
# Add NAT rule (the bouncer at the internet club ๐ญ)
iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -o eth0 -j MASQUERADE
Pro tip: This took me 2 hours and 3 cups of coffee to debug. Save yourself the pain - copy paste is not just a skill, it's a lifestyle! โ๏ธ๐
Step 5: Deploy ALL The Apps! ๐
Time to make your subnet actually DO something:
sudo ./vpcctl deploy-app --vpc my-vpc --subnet public --port 8080
# Translation: "Let's get this party started!" ๐
This creates a Python HTTP server that's more reliable than your ISP:
import http.server
import socketserver
PORT = 8080 # The magic number (or any port you fancy)
# Serving web pages like it's hot! ๐ฅ
with socketserver.TCPServer(("", PORT), http.server.SimpleHTTPRequestHandler) as httpd:
httpd.serve_forever() # Forever ever? Forever ever!
Step 6: VPC Isolation (Trust Issues Much? ๐ง)
By default, VPCs don't talk to each other. It's like having separate WhatsApp groups for work and family - necessary for sanity! ๐
sudo ./vpcctl create-vpc --name dev-vpc --cidr 10.1.0.0/16
sudo ./vpcctl create-subnet --vpc dev-vpc --name public --cidr 10.1.1.0/24 --type public
# Creating parallel universes, one VPC at a time! ๐
Try to ping from one VPC to another - DENIED! ๐ซ This is isolation working as intended. Your subnets are safer than money in your mom's purse! ๐ฐ
Step 7: VPC Peering (Now They're BFFs! ๐ฏโโ๏ธ)
Want your VPCs to be friends? Let's introduce them properly:
sudo ./vpcctl peer-vpcs --vpc1 my-vpc --vpc2 dev-vpc
# "Meet cute" but for networks! ๐
What this does (it's actually pretty cool):
- Creates a veth pair connecting both bridges
- Building bridges, literally!
- Adds routes so they can find each other
- Like sharing locations on WhatsApp ๐
Now they can communicate! It's like when your crush finally texts back! ๐ฑโจ
Step 8: Firewall Policies (The Rulebook ๐)
Create a JSON policy file (fancy way of saying "The Law"):
{
"subnet": "10.0.1.0/24",
"ingress": [
{
"port": 80,
"protocol": "tcp",
"action": "allow" // Come on in, HTTP! ๐ช
},
{
"port": 22,
"protocol": "tcp",
"action": "deny" // SSH? We don't know her! ๐
โโ๏ธ
}
]
}
Apply it like a boss:
sudo ./vpcctl apply-policy --vpc my-vpc --subnet public --policy policy.json
# "These are the rules, and yes, I made them up!" ๐ค
This translates to iptables magic (don't worry about understanding it, even I Google it sometimes ๐คซ):
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # Welcome!
iptables -A INPUT -p tcp --dport 22 -j DROP # Nope!
Testing Your Masterpiece ๐งช
If you're not testing, you're just hoping. And hope is NOT a strategy!
Test 1: Intra-VPC Love Connection ๐
Subnets within the same VPC should talk like old friends:
sudo ./vpcctl test-connectivity --vpc my-vpc --from-subnet public --to-subnet private
# Expected: โ Connectivity test PASSED
# If it fails: Time for more coffee! โ๏ธ
Test 2: VPC Cold Shoulder ๐ฅถ
Different VPCs without peering should ignore each other like exes at a party:
# Get the deets
NS1="ns-my-vpc-public"
IP2="10.1.1.2" # IP from dev-vpc (the stranger danger)
# Try to ping (spoiler: it won't work)
sudo ip netns exec $NS1 ping -c 2 $IP2
# Result: Network unreachable โ
# Translation: "New phone, who dis?" ๐ฑ
Test 3: Internet Access Check ๐
Public subnets flexing their internet access:
sudo ip netns exec ns-my-vpc-public ping -c 3 8.8.8.8
# Result: Success โ (Google DNS says hello!)
Private subnets staying humble and offline:
sudo ip netns exec ns-my-vpc-private ping -c 3 8.8.8.8
# Result: Network unreachable โ
# They're focused on the grind, no distractions! ๐ช
Test 4: Peering Power-Up ๐
After peering, VPCs become besties:
sudo ./vpcctl peer-vpcs --vpc1 my-vpc --vpc2 dev-vpc
sudo ip netns exec ns-my-vpc-public ping -c 2 10.1.1.2
# Result: Success โ
# They're texting now! ๐ฑ๐
When Things Go Wrong (And They Will! ๐ )
Debugging is like being a detective in a crime movie where you're also the murderer!
Issue 1: "Operation not permitted" ๐ซ
Problem: Forgot to use sudo (we've ALL been there!)
Solution: Channel your inner admin:
sudo ./vpcctl create-vpc --name test --cidr 10.0.0.0/16
# Remember: With great power comes great sudo! ๐ฆธโโ๏ธ
Issue 2: "Bridge already exists" ๐
Problem: You tried to create what already exists (philosophers hate this trick!)
Solution: Clear the slate:
sudo ./cleanup.sh
# When in doubt, nuke it out! ๐ฅ
Issue 3: "No internet connectivity" ๐ก
Problem: This one haunted my dreams for 2 hours! ๐ญ
Solution: This got me! I spent like 30 minutes debugging before realizing IP forwarding was disabled. Check it:
sysctl net.ipv4.ip_forward
# If it's 0, you need to flip that switch!
sudo sysctl -w net.ipv4.ip_forward=1 # The magic command โจ
Other things to check (because I'm nice like that):
- Interface name might be ens33 instead of eth0 (Linux is creative with naming!)
- iptables rules might be blocking you (check with
sudo iptables -t nat -L) - Your coffee might be empty (this is CRITICAL! โ๏ธ)
Cleanup: Leaving No Trace ๐งน
Always clean up after yourself (your mom taught you this!):
# Delete specific VPC (the gentle approach)
sudo ./vpcctl delete-vpc --name my-vpc
# Delete EVERYTHING (the "I need a fresh start" approach)
sudo ./vpcctl cleanup-all
# Or use the cleanup script (the "I trust this more" approach)
sudo ./cleanup.sh
# Thanos would be proud! ๐ซฐโจ
This removes (THE COMPLETE PURGE):
- All network namespaces ๐๏ธ
- All bridges ๐
- All veth pairs ๐
- All iptables rules ๐ฅ
- State files ๐
- Your mistakes (we all need this sometimes!) ๐
The Victory Lap: Full Test Suite ๐
The project includes tests more comprehensive than your mom's questions when you get home late:
sudo make test
# Sit back, relax, and watch the magic happen! โจ
This validates (EVERYTHING):
- โ VPC creation and deletion (Birth and... well, you know)
- โ Subnet management (The subdivision saga)
- โ Application deployment (App goes live!)
- โ Connectivity (Can we talk?)
- โ Isolation (Stay in your lane!)
- โ Peering (Making friends!)
- โ Firewall policies (The rules of engagement)
- โ NAT gateway (Internet access for all!)
- โ Cleanup (Leaving it better than we found it)
What You Just Became an Expert In ๐
Congratulations! You now understand:
- Linux Network Namespaces: Like apartments for network stacks ๐
- Virtual Networking: The art of making cables out of thin air ๐จ
- Routing: GPS for packets, but actually reliable ๐บ๏ธ
- NAT: The passport office of networking ๐
- iptables: The bouncer who decides who gets in ๐ช
- VPC Architecture: How AWS does it (but now YOU can do it too!) ๐
- Infrastructure as Code: Because clicking buttons is so 2010 ๐ฑ๏ธ
Where This Knowledge Pays Rent ๐ฐ
These concepts show up EVERYWHERE:
- Docker: Uses network namespaces (you're basically a Docker expert now! ๐ณ)
- Kubernetes: Pod networking is just fancy namespace usage (k8s who? We know them! โธ๏ธ)
- Cloud VPCs: AWS VPC, Azure VNet, Google VPC (all doing what you just did!) โ๏ธ
- SD-WAN: Software-defined networking (fancy name, same concepts) ๐
- Job Interviews: "So tell me about network namespaces..." (You: cracks knuckles ๐)
Next Level Moves (When You're Feeling Adventurous) ๐
Want to take this further? Here are some ideas:
- Add custom DHCP (automatic IP assignment, fancy!)
- Implement load balancing (spread the load like butter ๐ง)
- Add VPN gateway (secure connections ftw! ๐)
- Create a web dashboard (because CLI is cool but GUI is prettier ๐จ)
- Support IPv6 (future-proofing like a boss! ๐ฎ)
- Container runtime integration (Docker + Your VPC = ๐)
The Grand Finale: What Did We Learn Today? ๐ฌ
So that's it! I went from "network namespace what?" to building a full VPC system that would make AWS nervous (okay, maybe not nervous, but they'd def give a nod of respect ๐ซก).
Key takeaways (write these down, there might be a test):
- VPCs aren't magical - they're just namespaces + bridges + routing (and A LOT of coffee โ๏ธ)
- The
onlinkflag is a LIFESAVER (seriously, I almost named my firstborn "Onlink") - Always enable IP forwarding BEFORE testing NAT (learn from my pain!)
- Interface names have a 15-character limit (learned THAT one the expensive way - 2 hours I'll never get back! โฐ)
- When in doubt, check the logs (they never lie, unlike my "5 minutes left" estimates)
- Google is your friend (so is Stack Overflow, so is that random blog from 2012)
The complete code is available in the repository. Try it out, break it (you will!), fix it (you can!), and most importantly - understand how every piece works together like a well-oiled machine! ๐ ๏ธ
Additional Resources (For the Overachievers ๐)
Want to go deeper? Check these out:
- Linux Network Namespaces:
man ip-netns(bedtime reading? ๐ด) - iptables Documentation:
man iptables(thriller novel alternative!) - Linux Bridge:
man bridge(surprisingly less boring than expected!) - iproute2 Documentation:
man ip(for when Netflix isn't enough!) - My GitHub: hng13-stage4-devops (shameless plug! ๐)
Installation Speedrun ๐โโ๏ธ๐จ
git clone https://github.com/herdeybayor/hng13-stage4-devops
cd hng13-stage4-devops
make install # One command to rule them all!
sudo ./vpcctl --help # Your journey begins here! ๐บ๏ธ
Quick Start (For the Impatient Ones) โก๏ธ
# Create a VPC (you're basically a cloud provider now!)
sudo ./vpcctl create-vpc --name demo --cidr 10.0.0.0/16
# Add a public subnet (VIP section activated!)
sudo ./vpcctl create-subnet --vpc demo --name web --cidr 10.0.1.0/24 --type public
# Deploy an app (hello world, but make it network-y!)
sudo ./vpcctl deploy-app --vpc demo --subnet web --port 8080
# View your empire
sudo ./vpcctl list-vpcs # Look at what you built! ๐ฐ
# Clean up (responsible developer energy!)
sudo ./vpcctl delete-vpc --name demo
Connect With Me! ๐ค
If you're working on something similar, or if this helped you, or if you just want to chat about networking over virtual jollof rice, hit me up!
- ๐ Portfolio: sherifdeenadebayo.com (check out my other projects!)
- ๐ผ LinkedIn: @herdeybayor (let's connect professionally!)
- ๐ป GitHub: @herdeybayor (where the code lives!)
- โ๏ธ DEV.to: Where I write stuff like this when caffeine levels are optimal โ๏ธ
- ๐ง Questions? Create an issue! I promise I read them (eventually ๐ )
Sherifdeen Adebayo
DevOps Engineer | Professional Coffee Consumer | Network Namespace Whisperer
Built for HNG13 Stage 4 Challenge - November 2025
Powered by determination, late nights, and an unhealthy amount of Stack Overflow! ๐ป
P.S. - If you're doing the HNG challenge too, you got this! ๐ช This stage kicked my butt multiple times, but look - we both made it! If I can build this while Googling "what is a network namespace" every 5 minutes, you can too!
P.P.S. - Special shoutout to coffee, without which this project would still be "TODO: Figure out networking" ๐
P.P.P.S. - Yes, the interface naming bug took me 2 hours. Yes, I cried a little. Yes, it was just a 15-character limit. We don't talk about it. ๐
Remember: In software development, like in life, sometimes you just gotta sudo your way through! ๐โจ
Written while simultaneously debugging, eating chin-chin, and questioning my career choices (in the best way possible!) ๐
If this code works on your machine, you're welcome! If it doesn't... did you try turning it off and on again? ๐
Top comments (0)