Skip to content

Instantly share code, notes, and snippets.

@x10an14
Last active July 11, 2020 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x10an14/c6d67091f74f2704c5c1e7b5e998e0c2 to your computer and use it in GitHub Desktop.
Save x10an14/c6d67091f74f2704c5c1e7b5e998e0c2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
USER_NAME_TO_RUN_STEAM="x10an14"
IP_SUBNET="10.10.10"
LOCAL_DNS="<X.Y.Z.W>"
echo 'Setting up steam network namespace...'
sudo ip netns add steam_ns
sudo ip netns exec steam_ns ip addr add 127.0.0.1/8 dev lo
sudo ip netns exec steam_ns ip link set lo up
sudo ip link add steam_ns0 type veth peer name steam_ns1
sudo ip link set steam_ns0 up
sudo ip link set steam_ns1 netns steam_ns up
sudo ip addr add $IP_SUBNET.1/24 dev steam_ns0
sudo ip netns exec steam_ns ip addr add $IP_SUBNET.2/24 dev steam_ns1
sudo ip netns exec steam_ns ip route add default via $IP_SUBNET.1 dev steam_ns1
echo -e '\tSteam network namespace set-up complete!'
echo 'Setting up iptables configuration for Steam network namespace...'
sudo iptables -A INPUT ! -i steam_ns0 -s $IP_SUBNET.0/24 -j DROP
sudo iptables -t nat -A POSTROUTING -s $IP_SUBNET.0/24 -o en+ -j MASQUERADE
# sudo sysctl -q net.ipv4.ip_forward=1 # My system already had this set - not sure why, but hence commenting out
sudo mkdir -p /etc/netns/steam_ns
echo "nameserver $LOCAL_DNS" | sudo tee /etc/netns/steam_ns/resolv.conf
echo "Starting Steam in Steam network namespace..."
sudo ip netns exec steam_ns su - $USER_NAME_TO_RUN_STEAM -c "DISPLAY=:0 /usr/games/steam"
echo "Killing Steam running in Steam network namespace..."
sudo ip netns pids steam_ns | xargs -rd'\n' sudo kill
echo -e '\tSteam processes running in Steam network namespace killed!'
sudo rm -rf /etc/netns/steam_ns
# sudo sysctl -q net.ipv4.ip_forward=0
sudo iptables -D INPUT ! -i steam_ns0 -s $IP_SUBNET.0/24 -j DROP
sudo iptables -t nat -D POSTROUTING -s $IP_SUBNET.0/24 -o en+ -j MASQUERADE
sudo ip link del steam_ns0
sudo ip netns delete steam_ns
echo 'Deleted steam network namespace.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment