Skip to content

Instantly share code, notes, and snippets.

@kopwei
Last active November 27, 2018 14:32
Show Gist options
  • Save kopwei/b2d363cda3e89300f82f020f50b22f96 to your computer and use it in GitHub Desktop.
Save kopwei/b2d363cda3e89300f82f020f50b22f96 to your computer and use it in GitHub Desktop.
Ubuntu 16 share internet connection
#!/bin/bash
set -e
INTERNET_INF=$1
SUB_INF=$2
if [ -z "$INTERNET_INF" ] || [ -z "$SUB_INF" ]; then
echo "Please enter 2 interfaces' names"
exit 1
fi
SUB_INF_IP=$(ip -f inet addr show "$SUB_INF" | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*")
if [ -z "$SUB_INF_IP" ]; then
echo "Unable to get IP address of $SUB_INF"
exit 1
fi
sudo iptables -A FORWARD -o "$INTERNET_INF" -i "$SUB_INF" -s $SUB_INF_IP/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment