My aim is to route the default namespace through my vpn, and create a new namespace which does not route through the vpn (so i can selectively launch programs that should not have access to the remote vpn network).
lan address: 10.0.2.15/24 on enp0s3
vpn address: 10.111.0.10/24 on tun1
# enable forwarding
sysctl -w net.ipv4.ip_forward=1
# create the network namespace
ip netns add sample_ns
# create the virtual nic and it's peer
ip link add virt_out type veth peer name virt_in
assign the peer to the network namespace
ip link set virt_in netns sample_ns
bring up interface
ip link set virt_out up
#Create a new bridge and change its state to up:
ip link add name bridge_name type bridge
#To add an interface (e.g. eth0) into the bridge, its state must be up:
ip link set enp0s3 up
#Adding the interface into the bridge is done by setting its master to bridge_name:
ip link set enp0s3 master bridge_name
ip link set virt_out master bridge_name
#To show the existing bridges and associated interfaces, use the bridge utility (also part of iproute2). See bridge(8) for details.
bridge link
assign an ip address
ip addr add 10.0.3.1/24 dev virt_out
#network setup for network namespace
ip netns exec sample_ns ip link set lo up
ip netns exec sample_ns ip addr add 10.0.3.2/24 dev virt_in
ip netns exec sample_ns ip link set virt_in up
ip netns exec sample_ns ip route add default via 10.0.3.2 dev virt_in
allow forwarding and add enable NAT
iptables -I FORWARD -s 10.0.3.0/24 -j ACCEPT
iptables -t nat -I POSTROUTING -s 10.0.3.0/24 -o enp0s3 -j MASQUERADE
pop a shell in the namespace
ip netns exec sample_ns bash
check that you're in the namespace
ip netns identify
run the browser as your local user
runuser -u $USER google-chrome
#to access snap packages in ns run in the ns: (“error: cannot find tracking cgroup”)
sudo mount -t cgroup2 cgroup2 /sys/fs/cgroup
sudo mount -t securityfs securityfs /sys/kernel/security/
The aim is basically the reverse of namespaced-openvpn (https://github.com/slingamn/namespaced-openvpn) so the vpn protected namespace is the default, and the not vpn connected namespace is the new one.
What I am doing currently does not work, I assume I should somehow add ip address to the bridge/the virtual nic or the enp0s3?
Thanks for any help!
Other sources:
https://forums.openvpn.net/viewtopic.php?f=15&t=25690
https://github.com/ausbin/nsdo