User Tools

Site Tools


linux:netns

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
linux:netns [2025/08/15 16:25] – created jonathanlinux:netns [2025/08/15 16:55] (current) jonathan
Line 1: Line 1:
 ===== Linux - NETNS ===== ===== Linux - NETNS =====
- +This Bash script creates three separate Linux network namespaces (instance1, instance2, and instance3), assigns a specific physical interface to each, configures their IPv4/IPv6 addresses and default routes, and sets custom DNS resolvers for each namespace. 
-<code - instances-netns.sh>+<code - /usr/local/share/instances-netns.sh>
 #!/bin/bash #!/bin/bash
 ip netns add instance1 ip netns add instance1
Line 22: Line 22:
 mkdir -p /etc/netns/instance2 mkdir -p /etc/netns/instance2
 echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" | sudo tee /etc/netns/instance2/resolv.conf > /dev/null echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" | sudo tee /etc/netns/instance2/resolv.conf > /dev/null
 +
  
 ip netns add instance3 ip netns add instance3
 ip link set enp7s0 netns instance3 ip link set enp7s0 netns instance3
 ip netns exec instance3 ip link set enp7s0 up ip netns exec instance3 ip link set enp7s0 up
-ip netns exec instance2 ip -4 addr add 192.168.1.67/31 dev enp7s0 +ip netns exec instance3 ip -4 addr add 192.168.1.67/31 dev enp7s0 
-ip netns exec instance2 ip -4 route add 0.0.0.0/0 via 192.168.1.66 dev enp7s0 +ip netns exec instance3 ip -4 route add 0.0.0.0/0 via 192.168.1.66 dev enp7s0 
-ip netns exec instance2 ip -6 addr add 2001:db8:742:c01:153:8888:0:a/96 dev enp7s0 +ip netns exec instance3 ip -6 addr add 2001:db8:742:c01:153:8888:0:a/96 dev enp7s0 
-ip netns exec instance2 ip -6 route add ::/0 via 2001:db8:742:c01:153:8888:0:1 dev enp7s0+ip netns exec instance3 ip -6 route add ::/0 via 2001:db8:742:c01:153:8888:0:1 dev enp7s0
 mkdir -p /etc/netns/instance3 mkdir -p /etc/netns/instance3
 echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" | sudo tee /etc/netns/instance3/resolv.conf > /dev/null echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" | sudo tee /etc/netns/instance3/resolv.conf > /dev/null
 </code> </code>
 +\\ 
 +This Bash script displays the network interface details, IPv4/IPv6 addresses, and routing tables for three network namespaces (instance1, instance2, and instance3) in a formatted, sectioned output.
 <code - show-netns-settings.sh> <code - show-netns-settings.sh>
 #!/bin/bash #!/bin/bash
Line 75: Line 77:
 echo "################################################" echo "################################################"
 </code> </code>
 +\\ 
 +This script opens firefox-esr in three different network namespaces (instance1, instance2, instance3), each using its own browser profile and with proxy settings disabled.
 <code - netns-firefox.sh> <code - netns-firefox.sh>
 #####Note that it isn't compatible with Firefox snap version. The firefox-esr must be installed.##### #####Note that it isn't compatible with Firefox snap version. The firefox-esr must be installed.#####
Line 90: Line 93:
 env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY firefox-esr --new-instance --profile $HOME/.mozilla/instance3 env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY firefox-esr --new-instance --profile $HOME/.mozilla/instance3
 </code> </code>
 +\\ 
 +This script runs speedtest-cli inside three network namespaces (instance1, instance2, instance3) with proxy variables cleared, allowing separate network speed tests for each namespace.
 <code - netns-speedtest.sh> <code - netns-speedtest.sh>
 #!/bin/bash #!/bin/bash
Line 101: Line 105:
 sudo /usr/sbin/ip netns exec instance3 env http_proxy="" https_proxy="" \ sudo /usr/sbin/ip netns exec instance3 env http_proxy="" https_proxy="" \
 sudo -u $USER \ speedtest-cli sudo -u $USER \ speedtest-cli
 +</code>
 +\\
 +This systemd unit file defines a one-shot service that runs the /usr/local/share/instances-netns.sh script at boot after the network is up and remains marked active after execution.
 +<code - /etc/systemd/system/instances-netns.service>
 +[Unit]
 +Description=Run CPE Netns Script at Boot
 +After=network.target
 +
 +[Service]
 +Type=oneshot
 +ExecStart=/usr/local/share/instances-netns.sh
 +RemainAfterExit=yes
 +
 +[Install]
 +WantedBy=multi-user.target
 +</code>
 +\\
 +These commands reload systemd to recognize new or changed units, enable the instances-netns.service to start at boot, and start the service immediately.
 +<code - Activate the new service>
 +sudo systemctl daemon-reload
 +sudo systemctl enable instances-netns.service
 +sudo systemctl start instances-netns.service
 </code> </code>
linux/netns.1755275141.txt.gz · Last modified: by jonathan