<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shebang</title>
    <description>The latest articles on Forem by Shebang (@shebang_f7a029a2700eb909c).</description>
    <link>https://forem.com/shebang_f7a029a2700eb909c</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3606400%2F9fd17537-021c-4676-9d2a-969832f02a71.jpg</url>
      <title>Forem: Shebang</title>
      <link>https://forem.com/shebang_f7a029a2700eb909c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shebang_f7a029a2700eb909c"/>
    <language>en</language>
    <item>
      <title>How to Build Your Own VPC Architecture Using Linux Network Namespaces</title>
      <dc:creator>Shebang</dc:creator>
      <pubDate>Wed, 12 Nov 2025 16:26:36 +0000</pubDate>
      <link>https://forem.com/shebang_f7a029a2700eb909c/how-to-build-your-own-vpc-architecture-using-linux-network-namespaces-39i0</link>
      <guid>https://forem.com/shebang_f7a029a2700eb909c/how-to-build-your-own-vpc-architecture-using-linux-network-namespaces-39i0</guid>
      <description>&lt;p&gt;This blog post will guide you through a hands-on project that simulates a Virtual Private Cloud (VPC) using Linux network namespaces. This project is a great way for beginners to understand fundamental networking concepts like VPCs, subnets, routing, NAT, and security groups in a practical, low-level way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview of the project
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;vpcctl.py&lt;/code&gt; script is a command-line tool that allows you to create, manage, and network your own virtual private clouds. Instead of using a cloud provider's complex UI or API, you'll use simple shell commands to build a VPC from scratch on your Linux machine. This provides a transparent and educational way to see how the different components of a VPC work together.&lt;/p&gt;

&lt;p&gt;At its core, the script uses &lt;strong&gt;Linux network namespaces&lt;/strong&gt; to create isolated network environments. Each VPC and subnet runs in its own namespace, preventing interference with your host machine's network or other simulated VPCs.&lt;/p&gt;

&lt;h2&gt;
  
  
  CLI usage examples and explanations
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;vpcctl.sh&lt;/code&gt; script is the easiest way to get started. All commands must be run with &lt;code&gt;sudo&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Create a VPC
&lt;/h3&gt;

&lt;p&gt;First, let's create a new VPC named &lt;code&gt;company-a&lt;/code&gt; with the IP address range &lt;code&gt;10.0.0.0/16&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./vpcctl.sh create-vpc company-a 10.0.0.0/16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Add Subnets
&lt;/h3&gt;

&lt;p&gt;Now, let's add two subnets to our VPC: &lt;code&gt;public-web&lt;/code&gt; and &lt;code&gt;private-db&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./vpcctl.sh add-subnet company-a public-web 10.0.1.0/24
&lt;span class="nb"&gt;sudo&lt;/span&gt; ./vpcctl.sh add-subnet company-a private-db 10.0.2.0/24
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Enable Internet (NAT Gateway)
&lt;/h3&gt;

&lt;p&gt;By default, subnets are isolated. To grant the &lt;code&gt;public-web&lt;/code&gt; subnet internet access, we enable a NAT gateway.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./vpcctl.sh enable-internet company-a public-web
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Architecture diagram showing VPC → bridge → subnets → gateway
&lt;/h2&gt;

&lt;p&gt;The architecture of our simulated VPC is a classic cloud networking setup.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F89z9uh3nlphmsivzv8lf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F89z9uh3nlphmsivzv8lf.png" alt=" " width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;VPC:&lt;/strong&gt; The main container is a &lt;strong&gt;Linux Network Namespace&lt;/strong&gt; that acts as a virtual router.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Bridge:&lt;/strong&gt; Inside the router namespace, a &lt;strong&gt;virtual bridge&lt;/strong&gt; (&lt;code&gt;br-company-a&lt;/code&gt;) acts like a virtual switch, connecting all the subnets.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Subnets:&lt;/strong&gt; Each subnet (&lt;code&gt;public-web&lt;/code&gt;, &lt;code&gt;private-db&lt;/code&gt;) is its own &lt;strong&gt;network namespace&lt;/strong&gt;, providing isolation. They are connected to the bridge via a &lt;strong&gt;veth pair&lt;/strong&gt; (a virtual network cable).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Gateway:&lt;/strong&gt; To provide internet, a separate &lt;strong&gt;veth pair&lt;/strong&gt; connects the VPC's router namespace to the host machine. &lt;strong&gt;iptables&lt;/strong&gt; rules are then used on the host and in the router namespace to perform Network Address Translation (NAT), allowing traffic from the public subnet to go out to the internet.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Testing and validation steps (connectivity, NAT, isolation)
&lt;/h2&gt;

&lt;p&gt;The script includes built-in commands to verify that your virtual network is behaving as expected.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Test Connectivity Between Subnets
&lt;/h3&gt;

&lt;p&gt;First, use the &lt;code&gt;test-connectivity&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./vpcctl.py test-connectivity company-a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This test will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Ping between the &lt;code&gt;public-web&lt;/code&gt; and &lt;code&gt;private-db&lt;/code&gt; subnets to ensure they can communicate with each other within the VPC.&lt;/li&gt;
&lt;li&gt;  Attempt to ping an external address (&lt;code&gt;8.8.8.8&lt;/code&gt;) from each subnet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Expected Outcome:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  You will see a &lt;strong&gt;SUCCESS&lt;/strong&gt; message for communication between subnets.&lt;/li&gt;
&lt;li&gt;  The &lt;code&gt;public-web&lt;/code&gt; subnet will show that it &lt;strong&gt;Has internet access&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  The &lt;code&gt;private-db&lt;/code&gt; subnet will show that it has &lt;strong&gt;No internet access&lt;/strong&gt;, confirming our isolation is working.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Test NAT and Internet Access
&lt;/h3&gt;

&lt;p&gt;To be absolutely sure the NAT gateway is working, run a command inside the &lt;code&gt;public-web&lt;/code&gt; subnet to fetch a webpage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./vpcctl.py run-workload company-a public-web &lt;span class="s2"&gt;"curl -s --head http://www.google.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected Outcome:&lt;/strong&gt; You should see HTTP headers from Google, like &lt;code&gt;HTTP/2 200&lt;/code&gt;, confirming that the &lt;code&gt;public-web&lt;/code&gt; subnet can reach the internet through the NAT gateway.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Test Isolation
&lt;/h3&gt;

&lt;p&gt;Now, run the same command inside the &lt;code&gt;private-db&lt;/code&gt; subnet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./vpcctl.py run-workload company-a private-db &lt;span class="s2"&gt;"curl -s --head http://www.google.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected Outcome:&lt;/strong&gt; The command will hang and eventually time out. This proves that the &lt;code&gt;private-db&lt;/code&gt; subnet is successfully isolated from the internet.&lt;/p&gt;

&lt;h2&gt;
  
  
  A clean up step for deleting resources after creation
&lt;/h2&gt;

&lt;p&gt;After you are done experimenting, it is crucial to clean up the virtual network devices you created. The &lt;code&gt;cleanup&lt;/code&gt; command removes all namespaces, virtual interfaces, and firewall rules created by the script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./vpcctl.sh cleanup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command ensures your system's networking configuration is returned to its original state.&lt;/p&gt;

</description>
      <category>networking</category>
      <category>linux</category>
      <category>tutorial</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
