You can create multiple network namespaces, each with their own ip address, and connect them together any way you want.
For example, let's say we want to simulate three nodes sharing a
private network. First we create a bridge device that will represent
the network:
ip link add name br-net0 type bridge
ip addr add 192.168.13.1/24 dev br-net0
ip link set br-net0 up
Which gets us:
# ip addr show br-net0
3: br-net0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 2a:06:b9:b4:e9:61 brd ff:ff:ff:ff:ff:ff
inet 192.68.13.1/24 scope global br-net0
valid_lft forever preferred_lft forever
Next, we'll create network namespaces to represent out fake machines:
# ip netns add node0
# ip netns add node1
# ip netns add node2
Next, we create three veth pairs that we'll use to connect our namespaces to the bridge:
ip link add name node0-ext type veth peer name node0-int netns node0
ip link add name node1-ext type veth peer name node1-int netns node1
ip link add name node2-ext type veth peer name node2-int netns node2
Each of these commands creates a veth
pair, and puts one end of the
pair in the named network namespace. We need to connect the "outside"
end of each pair to our bridge:
ip link set master br-net0 dev node0-ext
ip link set master br-net0 dev node1-ext
ip link set master br-net0 dev node2-ext
Finally, we need to (a) assign addresses to our virtual nodes:
ip netns exec node0 ip addr add 192.168.13.10/24 dev node0-int
ip netns exec node1 ip addr add 192.168.13.11/24 dev node1-int
ip netns exec node2 ip addr add 192.168.13.12/24 dev node2-int
The ip netns exec
command lets us run commands inside a network namespace (you can also do this using nsenter
).
And (b) bring up all the interfaces:
for node in 0 1 2; do
ip link set node${node}-ext up
ip netns exec node${node} ip link set node${node}-int up
done
Now, we have three virtual nodes, each with a unique ip address and
MAC address:
# ping -c 1 192.168.13.10
PING 192.168.13.10 (192.168.13.10) 56(84) bytes of data.
64 bytes from 192.168.13.10: icmp_seq=1 ttl=64 time=0.054 ms
--- 192.168.13.10 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.054/0.054/0.054/0.000 ms
# arp -an
? (192.168.122.1) at 52:54:00:90:73:ca [ether] on eth0
? (192.168.13.11) at ba:0b:4b:ed:e4:bb [ether] on br-net0
? (192.168.13.12) at da:38:38:32:29:17 [ether] on br-net0
? (192.168.13.10) at 56:ad:09:af:46:1f [ether] on br-net0
Mininet is a great tool for automating this sort of setup.