Skip to content

Instantly share code, notes, and snippets.

@esmaeelE
Last active September 18, 2025 05:55
Show Gist options
  • Save esmaeelE/a97cae63ce2d3f4a46e80fa0cd182c7c to your computer and use it in GitHub Desktop.
Save esmaeelE/a97cae63ce2d3f4a46e80fa0cd182c7c to your computer and use it in GitHub Desktop.
make ipip_tunnel with linux network namespace.
#!/bin/bash
# grab from [IP in IP (IPIP) tunnels using linux network namespaces](https://routemyip.com/posts/linux/networking/ipip-tunnels/)
# great article by Sarath Chandra Mekala.
# Enable IPIP module
modprobe ipip
# Create the network namespaces
ip netns add host1
ip netns add host2
ip netns add internet
# Create the topology and connection between them
ip link add veth0 type veth peer name veth1
ip link add veth2 type veth peer name veth3
ip link set veth0 netns host1
ip link set veth1 netns internet
ip link set veth2 netns internet
ip link set veth3 netns host2
# host1 namespace settings
ip netns exec host1 ip addr add 172.16.10.2/24 dev veth0
ip netns exec host1 ip link set veth0 up
ip netns exec host1 ip link set lo up
# add default route for host1 namespace
ip netns exec host1 ip route add default via 172.16.10.1
# host2 namesapce settings
ip netns exec host2 ip addr add 152.16.10.2/24 dev veth3
ip netns exec host2 ip link set veth3 up
ip netns exec host2 ip link set lo up
# add default route for host1 namespace
ip netns exec host2 ip route add default via 152.16.10.1
# internet namesapce settings
ip netns exec internet ip addr add 172.16.10.1/24 dev veth1
ip netns exec internet ip link set veth1 up
ip netns exec internet ip addr add 152.16.10.1/24 dev veth2
ip netns exec internet ip link set veth2 up
ip netns exec internet ip link set lo up
# enable routing on this namespace
ip netns exec internet sysctl -w net.ipv4.ip_forward=1
# Create ipip tunnel on host1
ip netns exec host1 ip tunnel add tun0 mode ipip local 172.16.10.2 remote 152.16.10.2 ttl 255
# set IP and start interface
ip netns exec host1 ip addr add 192.168.50.1/30 dev tun0
ip netns exec host1 ip link set tun0 up
# Create ipip tunnel on host2
ip netns exec host2 ip tunnel add tun0 mode ipip local 152.16.10.2 remote 172.16.10.2 ttl 255
# set IP and start interface
ip netns exec host2 ip addr add 192.168.50.2/30 dev tun0
ip netns exec host2 ip link set tun0 up
echo "Setup IP IP tunnel done."
# Check connection #
##########################################
## ip netns exec host1 hostname -I
## ip netns exec host1 ping 192.168.50.2
## ip netns exec host2 ping hostname -I
## ip netns exec host2 ping 192.168.50.1
##########################################
@esmaeelE
Copy link
Author

check connection with

# ip netns exec host1 hostname -I
# ip netns exec host1 ping 192.168.50.2

# ip netns exec host2 ping hostname -I
# ip netns exec host2 ping 192.168.50.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment