I have an IP address on my server, say, 192.168.0.3, I want to share in a net namespace, so to run apps there which will only be able to communicate to the internet using that 192.168.0.3.
I can "kind" of get it working with the macvlan
device type... except, for having two mac addresses, the gateway will often favor my root or my namespace-bound interface.
Is there a way I could just mirror that IP inside a netns without affecting the external IP?
Here's what I've been trying:
ip netns add myns
ip link add link eth0 name en0 netns myns type macvlan
ip netns exec myns ip link set en0 up
ip netns exec myns ip addr add 192.168.0.3/24 dev en0
ip netns exec myns ip route add default via 192.168.0.1
And then I test with variations of netcat
; either listening in a remote host and connecting from inside and outside the namespace, or listening inside and outside the namespace and connecting from the remote host.
With macvlan
it is unreliable just because the immediate router will send packets to a mac address or the other, I need to reset the cached mac in the router (by sending a packet) before I can switch between the physical or macvlan
interface. While this is understandable, I am missing a way to let the IPs be "shared" instead of "fought over".
My motivation for that is that I have just a few IPs in my server, and each already listen to services (that properly allows you to bind to specific IP/ports). And then I'm trying to create a chroot jail which would expose only one of these few IPs to apps running therein. So these jailed apps could bind to any (free) port of these IPs, but won't stand a chance of taking over any other IP addresses in the same server.
(I can think of other options like using LD_PRELOAD, or iptables NAT, but I really think I'm missing just a little to have it working with net namespaces)