-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-ubuntu1.sh
More file actions
66 lines (52 loc) · 2.09 KB
/
setup-ubuntu1.sh
File metadata and controls
66 lines (52 loc) · 2.09 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
set -e
#### Variables for Ubuntu1 side ####
NS1="NS1"
NS2="NS2"
NODE_IP="192.168.0.10"
BRIDGE_SUBNET="172.16.0.0/24"
BRIDGE_IP="172.16.0.1"
IP1="172.16.0.2"
IP2="172.16.0.3"
# Point at ubuntu2’s Docker IP on the same Docker bridge
TO_NODE_IP="192.168.0.11"
TO_BRIDGE_SUBNET="172.16.1.0/24"
#### 1) Create network namespaces on ubuntu1 ####
ip netns add $NS1
ip netns add $NS2
#### 2) Create veth pairs: one end in default NS, the peer in each namespace ####
ip link add veth10 type veth peer name veth11
ip link add veth20 type veth peer name veth21
#### 3) Move the “.11” and “.21” ends into their namespaces ####
ip link set veth11 netns $NS1
ip link set veth21 netns $NS2
#### 4) Assign IPs inside each namespace and bring them up ####
ip netns exec $NS1 ip addr add $IP1/24 dev veth11
ip netns exec $NS2 ip addr add $IP2/24 dev veth21
ip netns exec $NS1 ip link set dev veth11 up
ip netns exec $NS2 ip link set dev veth21 up
#### 5) Create a bridge br0 in the default NS of ubuntu1 ####
ip link add br0 type bridge
#### 6) Attach the “.10” and “.20” ends to br0 ####
ip link set dev veth10 master br0
ip link set dev veth20 master br0
#### 7) Assign the bridge IP and bring everything up ####
ip addr add $BRIDGE_IP/24 dev br0
ip link set dev br0 up
ip link set dev veth10 up
ip link set dev veth20 up
#### 8) Bring up loopback inside each namespace ####
ip netns exec $NS1 ip link set lo up
ip netns exec $NS2 ip link set lo up
#### 9) Add default‑routes inside each namespace via br0 ####
ip netns exec $NS1 ip route add default via $BRIDGE_IP dev veth11
ip netns exec $NS2 ip route add default via $BRIDGE_IP dev veth21
#### 10) Enable IPv4 forwarding and allow forwarding in iptables ####
sysctl -w net.ipv4.ip_forward=1
iptables -P FORWARD ACCEPT
#### 11) Add a host‑level route on ubuntu1 → to reach 172.16.1.0/24 via ubuntu2 ####
ip route add $TO_BRIDGE_SUBNET via $TO_NODE_IP dev eth0
echo "== ubuntu1 setup complete =="
echo "Namespaces: $(ip netns list)"
echo "Bridge br0: $(ip addr show br0 | grep inet)"
echo "Ubuntu1 host routes: $(ip route show | grep 172.16.1.0/24)"