Jan 4, 2017

5. Demo: LAN inside a Linux machine (IP address via DHCP server)

1 comment

Topology


Expected result:
  • All PCs can get an IP address from DHCP server
  • All PCs can see each other
  • All PCs can access Internet 

Configuration

sudo ip netns add pc1
sudo ip netns add pc2
sudo ip netns add pc3
sudo ip netns add pc4
sudo brctl addbr vSwitch
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
  
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
sudo brctl addif vSwitch eth5
sudo brctl addif vSwitch eth6
sudo brctl addif vSwitch eth7
sudo brctl addif vSwitch eth8
#Set IP address for vSwitch interface
sudo ip link set vSwitch up
sudo ip addr add 10.0.0.1/24 dev vSwitch
sudo ip link set eth5 up
sudo ip link set eth6 up
sudo ip link set eth7 up
sudo ip link set eth8 up
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
#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
#Setup and configure DHCP server
sudo apt-get install isc-dhcp-server
#Edit these files as the below result
sudo vi /etc/default/isc-dhcp-server
sudo vi cat /etc/dhcp/dhcpd.conf
sudo service isc-dhcp-server restart
#Get IP address from DHCP server
sudo ip netns exec pc1 dhclient -nw -v eth1
sudo ip netns exec pc2 dhclient -nw -v eth2
sudo ip netns exec pc3 dhclient -nw -v eth3
sudo ip netns exec pc4 dhclient -nw -v eth4
#Check Result
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

Result

vdkmai@Ubuntu32bit:~$ cat /etc/default/isc-dhcp-server
# Defaults for dhcp initscript
# sourced by /etc/init.d/dhcp
# installed at /etc/default/isc-dhcp-server by the maintainer scripts
#
# This is a POSIX shell fragment
#
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
#       Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="vSwitch"
vdkmai@Ubuntu32bit:~$ cat /etc/dhcp/dhcpd.conf
log-facility local7;
default-lease-time 600;
max-lease-time 7200;
subnet 10.0.0.0 netmask 255.255.255.0 {
  range 10.0.0.101 10.0.0.253;
  option subnet-mask 255.255.255.0;
  option routers 10.0.0.1;
  option domain-name-servers 8.8.8.8;
}
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc4 dhclient -nw -v eth4
Internet Systems Consortium DHCP Client 4.1-ESV-R4
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eth4/7a:56:5c:45:0f:7e
Sending on   LPF/eth4/7a:56:5c:45:0f:7e
Sending on   Socket/fallback
DHCPREQUEST of 10.0.0.104 on eth4 to 255.255.255.255 port 67
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=33.1 ms
--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 33.184/33.184/33.184/0.000 ms
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc4 ifconfig
eth4      Link encap:Ethernet  HWaddr 7a:56:5c:45:0f:7e 
          inet addr:10.0.0.104  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::7856:5cff:fe45:f7e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:138 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:17281 (17.2 KB)  TX bytes:1960 (1.9 KB)
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc4 ip route
default via 10.0.0.1 dev eth4
10.0.0.0/24 dev eth4  proto kernel  scope link  src 10.0.0.104
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc4 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.1 (10.0.0.1)  0.055 ms  0.012 ms  0.011 ms
 2  * * *
 3  * * *
 4  * * *
 5  * 59.12.187.1 (59.12.187.1)  5.185 ms  5.170 ms
If You Enjoyed This, Take 5 Seconds To Share It

1 comment: