Skip to content

Instantly share code, notes, and snippets.

@rohan-molloy
Last active June 9, 2022 06:00
Show Gist options
  • Save rohan-molloy/d33103481c6a301d9d6fbca99483bb50 to your computer and use it in GitHub Desktop.
Save rohan-molloy/d33103481c6a301d9d6fbca99483bb50 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Variables
LINK='enp0s20f0u4u3'
ADDR='192.168.8.100/24'
NETNS='testns'
BROWSER='chromium-browser'
# Create the network namespace
echo "Creating ${NETNS}" 1>&2
sudo ip netns add $NETNS
# Wait for link to come online
while ! ip link show $LINK >/dev/null 2>&1;
do
echo "Waiting for ${LINK}..." 1>&2;
sleep 5;
done
# Configure the interface inside the network namespace
echo "Binding ${LINK} to ${NETNS}" 1>&2
sudo ip link set dev $LINK netns $NETNS
echo "Bringing up ${LINK} in ${NETNS}"
sudo ip netns exec $NETNS \
ip link set up dev $LINK
echo "Setting ${ADDR} on ${LINK} in ${NETNS}" 1>&2
sudo ip netns exec $NETNS \
ip addr add $ADDR dev $LINK
# Start the browser inside the network namespace
echo "Starting ${BROWSER} in ${NETNS}" 1>&2
sudo ip netns exec $NETNS \
su $USER -c ${BROWSER}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment