Overview

This document describes a method for simulating multiple servers belonging to different network domains on a single Linux host. Each simulated server runs inside its own Linux network namespace and is connected to a dedicated VLAN.

  • Training and demonstration environments

Each namespace behaves as an independent server with:

  • Its own IP address
  • Its own MAC address
  • An SSH service
  • A simple HTTP server

Network Topology

The Linux host uses the following interfaces:

InterfacePurpose
ens3Management interface (DHCP)
ens4Bond member
ens5Bond member
bond0LACP bond carrying VLANs
vlan111-vlan132VLAN interfaces mapped to namespaces

Netplan Configuration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
network:
  version: 2
  renderer: networkd

  ethernets:
    ens3:
      dhcp4: true
      match:
        macaddress: 0c:60:9f:d2:00:00
      routing-policy:
        - from: 192.168.122.0/24
          table: 99

    ens4:
      dhcp4: false

    ens5:
      dhcp4: false

  bonds:
    bond0:
      interfaces: [ens4, ens5]
      parameters:
        mode: 802.3ad

  vlans:
    vlan111:
      id: 111
      link: bond0

    vlan112:
      id: 112
      link: bond0

    vlan121:
      id: 121
      link: bond0

    vlan122:
      id: 122
      link: bond0

    vlan131:
      id: 131
      link: bond0

    vlan132:
      id: 132
      link: bond0

Installing Required Packages

Install the required networking tools:

1
2
sudo apt update
sudo apt install ifupdown

Create the namespace initialization script:

1
2
sudo nano /etc/network/if-up.d/namespaces
sudo chmod +x /etc/network/if-up.d/namespaces

Namespace Initialization Script

The following script creates one namespace per VLAN interface, assigns an IP address, starts an HTTP service, and launches an SSH daemon.

Example Namespace Mapping

NamespaceIP AddressGateway
vlan111172.16.111.101/24172.16.111.1
vlan112172.16.112.101/24172.16.112.1
vlan121172.16.121.101/24172.16.121.1
vlan122172.16.122.101/24172.16.122.1
vlan131172.16.131.101/24172.16.131.1
vlan132172.16.132.101/24172.16.132.1

The script performs the following operations:

  1. Creates a network namespace.
  2. Moves the VLAN interface into the namespace.
  3. Assigns IP and MAC addresses.
  4. Configures the default route.
  5. Starts an HTTP server.
  6. Starts an SSH daemon.

HTTP Server

Each namespace runs a lightweight Python HTTP server that returns information about:

  • Namespace name
  • Hostname
  • Client IP address

Example response:

1
Hello from namespace vlan111 on server lab-server to 172.16.111.50

Python Web Server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import http.server
import socketserver
import os

class MyHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        hostname = os.uname()[1]
        namespace = os.getenv("NAMESPACE", "unknown")
        client_ip = self.client_address[0]

        message = (
            f"Hello from namespace {namespace} "
            f"on server {hostname} to {client_ip}\n"
        )

        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write(message.encode())

        print(message)

if __name__ == "__main__":
    port = 80

    with socketserver.TCPServer(("", port), MyHandler) as httpd:
        print(f"Serving on port {port}")
        httpd.serve_forever()

Validation

Verify that the namespaces were created successfully:

1
ip netns list

Access a namespace shell:

1
ip netns exec vlan111 bash

Test connectivity:

1
curl http://172.16.111.101

Test SSH access:


This approach provides a lightweight and scalable platform for emulating servers across multiple isolated network domains while maintaining realistic Layer 2 and Layer 3 behavior.


The content of this post was created by the author; AI was used for editing and restructuring.