Created
May 13, 2016 14:27
-
-
Save lilydjwg/b50559213ab5d636536cb8821aa50d83 to your computer and use it in GitHub Desktop.
mynetns_run: Run a program in a seperate network namespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
NETNS_FILE=/var/run/netns/mynet | |
MNTNS_FILE=/var/run/ns/mynet_mnt | |
if [[ ! -f $NETNS_FILE ]]; then | |
ip netns add mynet | |
ip link add mynet0 type veth peer name mynet1 | |
ip link set mynet0 up | |
ip link set mynet1 netns mynet | |
brctl addif br0 mynet0 | |
ip netns exec mynet ip link set mynet1 up | |
ip netns exec mynet ip address add dev mynet1 192.168.57.101/24 | |
ip netns exec mynet ip route add default via 192.168.57.1 | |
fi | |
if [[ ! -f $MNTNS_FILE ]]; then | |
mnt_dir=$(dirname $MNTNS_FILE) | |
mkdir -p "$mnt_dir" | |
mount --bind "$mnt_dir" "$mnt_dir" | |
mount --make-private "$mnt_dir" | |
echo 'nameserver 192.168.57.1' > $MNTNS_FILE | |
unshare --mount=$MNTNS_FILE mount --bind $MNTNS_FILE /etc/resolv.conf | |
fi | |
exec nsenter --mount=$MNTNS_FILE --net=$NETNS_FILE sudo -u lilydjwg DISPLAY=:0 LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh_TW GTK_IM_MODULE=fcitx QT_IM_MODULE=fcitx XMODIFIERS=@im=fcitx DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus TMUX=1 "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
详见博客文章:一个系统,两套网络 - 依云's Blog。