Jan 3, 2017

Part 4. Demo: LAN inside a Linux machine (static IP address)

Leave a Comment

Topology



Expected result

  • All PCs can see each other
  • All PCs can access the Internet

Configuration

#Create namespaces and virtual switch
sudo ip netns add pc1
sudo ip netns add pc2
sudo ip netns add pc3
sudo ip netns add pc4
sudo brctl addbr vSwitch
  
#Create vEth interface pairs
sudo ip link add eth1 type veth peer name eth5
sudo ip link add eth2 type veth peer name eth6
sudo ip link add eth3 type veth peer name eth7
sudo ip link add eth4 type veth peer name eth8
   
#Add interfaces to namespaces
sudo ip link set eth1 netns pc1
sudo ip link set eth2 netns pc2
sudo ip link set eth3 netns pc3
sudo ip link set eth4 netns pc4
  
#Add interfaces to switch
sudo brctl addif vSwitch eth5
sudo brctl addif vSwitch eth6
sudo brctl addif vSwitch eth7
sudo brctl addif vSwitch eth8
   
#Set IP address
sudo ip netns exec pc1 ip addr add 10.0.0.1/25 dev eth1
sudo ip netns exec pc2 ip addr add 10.0.0.2/25 dev eth2
sudo ip netns exec pc3 ip addr add 10.0.0.3/25 dev eth3
sudo ip netns exec pc4 ip addr add 10.0.0.4/25 dev eth4
sudo ip addr add 10.0.0.10/24 dev vSwitch
  
  
#Turn on interfaces
sudo ip netns exec pc1 ip link set eth1 up
sudo ip netns exec pc2 ip link set eth2 up
sudo ip netns exec pc3 ip link set eth3 up
sudo ip netns exec pc4 ip link set eth4 up
sudo ip link set vSwitch up
sudo ip link set eth5 up
sudo ip link set eth6 up
sudo ip link set eth7 up
sudo ip link set eth8 up
    
#Set default gateway for namespaces
sudo ip netns exec pc1 route add default gw 10.0.0.10 eth1
sudo ip netns exec pc2 route add default gw 10.0.0.10 eth2
sudo ip netns exec pc3 route add default gw 10.0.0.10 eth3
sudo ip netns exec pc4 route add default gw 10.0.0.10 eth4
  
#Set forwarding route for host machine
sudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
sudo iptables -t nat -A POSTROUTING -o eth0 -j  MASQUERADE
  
#Check result
ifconfig
sudo ip netns exec pc1 ifconfig
sudo ip netns exec pc1 ping 10.0.0.2
sudo ip netns exec pc1 ping 10.0.0.3
sudo ip netns exec pc1 ping 10.0.0.4
sudo ip netns exec pc1 ping 8.8.8.8
sudo ip netns exec pc2 ping 8.8.8.8
sudo ip netns exec pc3 ping 8.8.8.8
sudo ip netns exec pc4 ping 8.8.8.8
sudo ip netns exec pc1 traceroute 8.8.8.8
sudo ip netns exec pc1 traceroute 10.0.0.4

Result

vdkmai@Ubuntu32bit:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:7a:94:3d 
          inet addr:192.168.0.36  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe7a:943d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12489 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12634 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1106428 (1.1 MB)  TX bytes:2808519 (2.8 MB)
eth5      Link encap:Ethernet  HWaddr 4a:8a:dd:6d:4b:b4 
          inet6 addr: fe80::488a:ddff:fe6d:4bb4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:196 errors:0 dropped:0 overruns:0 frame:0
          TX packets:229 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:11678 (11.6 KB)  TX bytes:19553 (19.5 KB)
eth6      Link encap:Ethernet  HWaddr 52:59:de:5b:18:1e 
          inet6 addr: fe80::5059:deff:fe5b:181e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:70 errors:0 dropped:0 overruns:0 frame:0
          TX packets:289 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6792 (6.7 KB)  TX bytes:19519 (19.5 KB)
eth7      Link encap:Ethernet  HWaddr 4a:d4:12:f1:2d:d2 
          inet6 addr: fe80::48d4:12ff:fef1:2dd2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11 errors:0 dropped:0 overruns:0 frame:0
          TX packets:231 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:830 (830.0 B)  TX bytes:14999 (14.9 KB)
eth8      Link encap:Ethernet  HWaddr 8a:a5:17:c7:55:74 
          inet6 addr: fe80::88a5:17ff:fec7:5574/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11 errors:0 dropped:0 overruns:0 frame:0
          TX packets:230 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:830 (830.0 B)  TX bytes:14921 (14.9 KB)
lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:352 errors:0 dropped:0 overruns:0 frame:0
          TX packets:352 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:28096 (28.0 KB)  TX bytes:28096 (28.0 KB)
vSwitch   Link encap:Ethernet  HWaddr 4a:8a:dd:6d:4b:b4 
          inet addr:10.0.0.10  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::488a:ddff:fe6d:4bb4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:207 errors:0 dropped:0 overruns:0 frame:0
          TX packets:196 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:9950 (9.9 KB)  TX bytes:16011 (16.0 KB)
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ifconfig
eth1      Link encap:Ethernet  HWaddr 12:81:d0:d7:96:9e 
          inet addr:10.0.0.1  Bcast:0.0.0.0  Mask:255.255.255.128
          inet6 addr: fe80::1081:d0ff:fed7:969e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:231 errors:0 dropped:0 overruns:0 frame:0
          TX packets:196 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:20084 (20.0 KB)  TX bytes:11678 (11.6 KB)
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_req=1 ttl=64 time=0.022 ms
^C
--- 10.0.0.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.022/0.022/0.022/0.000 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 10.0.0.3
PING 10.0.0.3 (10.0.0.3) 56(84) bytes of data.
64 bytes from 10.0.0.3: icmp_req=1 ttl=64 time=0.076 ms
^C
--- 10.0.0.3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.076/0.076/0.076/0.000 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 10.0.0.4
PING 10.0.0.4 (10.0.0.4) 56(84) bytes of data.
64 bytes from 10.0.0.4: icmp_req=1 ttl=64 time=0.079 ms
64 bytes from 10.0.0.4: icmp_req=2 ttl=64 time=0.055 ms
^C
--- 10.0.0.4 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.055/0.067/0.079/0.012 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=32.4 ms
^C
--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 32.449/32.449/32.449/0.000 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc2 ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=32.5 ms
^C
--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 32.505/32.505/32.505/0.000 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc3 ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=33.5 ms
^C
--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 33.520/33.520/33.520/0.000 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc4 ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=32.0 ms
^C
--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 32.048/32.048/32.048/0.000 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 traceroute 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  10.0.0.10 (10.0.0.10)  0.031 ms  0.009 ms  0.008 ms
 2  192.168.0.1 (192.168.0.1)  0.513 ms  0.783 ms  0.767 ms
 3  10.0.12.2 (10.0.12.2)  1.535 ms  1.524 ms  1.488 ms
 4  10.0.5.1 (10.0.5.1)  2.947 ms  2.910 ms  2.898 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 traceroute 10.0.0.4
traceroute to 10.0.0.4 (10.0.0.4), 30 hops max, 60 byte packets
 1  10.0.0.4 (10.0.0.4)  0.334 ms  0.072 ms  0.050 ms
Ping to local address
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ip link set lo up
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 10.0.0.1
PING 10.0.1.11 (10.0.1.11) 56(84) bytes of data.
64 bytes from 10.0.1.11: icmp_req=1 ttl=64 time=0.044 ms
^C
--- 10.0.1.11 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.044/0.044/0.044/0.000 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ip link set lo up
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 10.0.0.1
PING 10.0.1.11 (10.0.1.11) 56(84) bytes of data.
64 bytes from 10.0.1.11: icmp_req=1 ttl=64 time=0.044 ms
^C
--- 10.0.1.11 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.044/0.044/0.044/0.000 ms
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment