Skip to content

Instantly share code, notes, and snippets.

@codefionn
Last active April 5, 2019 09:10
Show Gist options
  • Select an option

  • Save codefionn/98d52bab8c40fc17362ab22efeb933dc to your computer and use it in GitHub Desktop.

Select an option

Save codefionn/98d52bab8c40fc17362ab22efeb933dc to your computer and use it in GitHub Desktop.
Network sharing with iptables
#!/usr/bin/env bash
sysctl -w net.ipv4.ip_forward=1
# Has network (here: a OpenVPN tunnel)
INTERNET=tun0
# wants network (here: LAN-port)
WANTS_NET=enp0s25
NEW_IP=192.168.2.20
ip link set up dev $WANTS_NET
ip addr add 192.168.2.20/24 dev $WANTS_NET
# Setup on client device:
# ip addr add 192.168.2.21 dev mydevice
# ip route add default via 192.168.2.20 dev mydevice
iptables -t nat -A POSTROUTING -o $INTERNET -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $WANTS_NET -o $INTERNET -j ACCEPT
#!/usr/bin/env bash
DEVICE=eno1
# First three bytes the same, last one different from ipforward.
sudo ip addr add 192.168.2.21/24 dev $DEVICE
# Same as in ipforward
sudo ip route add default via 192.168.2.20 dev $DEVICE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment