There are two mistakes in the above. First, by bridging the veth
IF with enp0s3
, you will be sending the pings outside your pc, while your intention is to communicate with another part of your own pc. Second, the syntax of the command establishing the veth
interface is wrong.
You should bridge the veth
interface with another virtual interface, tap0
; then you will see your pings coming back. Try it as follows:
NNS=WhateverYouWantToCallTheNewNetworkNameSpace ip netns add $NNS ip link add veth-a$NNS type veth peer name veth-b$NNS ip link set veth-a$NNS up ip tuntap add tap$NNS mode tap user root ip link set tap$NNS up ip link add br$NNS type bridge ip link set tap$NNS master br$NNS ip link set veth-a$NNS master br$NNS ip addr add 10.0.0.1/24 dev br$NNS ip link set br$NNS up ip link set veth-b$NNS netns $NNS ip netns exec $NNS ip addr add 10.0.0.2/24 dev veth-b$NNS ip netns exec $NNS ip link set veth-b$NNS up ip netns exec $NNS ip link set dev lo up
I have added the lo
interface only because you will find it useful in the future. Now you can ping,
ping -c1 10.0.0.2
and you will see the pings coming back.
EDIT:
how I can access the Internet throw veth-b$NNS and the veth-b$NNS's MAC have to be visible to my ISP
There is no way to expose the two veth
's to your ISP. Still, the following ought to work just fine:
The following, for some reason I do not understand, must be done before all of the previous commands:
NNS=WhateverYouWantToCallTheNewNetworkNameSpace /bin/mkdir -p /etc/netns/$NNS echo "nameserver 8.8.8.8" > /etc/netns/$NNS/resolv.conf echo "nameserver 8.8.4.4" >> /etc/netns/$NNS/resolv.conf
This will give you DNS resolution. But remember, this must be done before the commands above!
On your host, allow IPv4 forwarding and MASQUERADING:
echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o enp0s3 -j MASQUERADE
Add a gateway to your new network namespace:
ip netns exec $NNS ip route add default via 10.0.0.1
Now enter your new network namespace, I do it by creating an
xterm
and then typing within it commands which are necessarily in the new NNS:ip netns exec $NNS su -c xterm YourName &
If you do not like xterm's, you can use whatever kind of terminals you like (if you do not have an xterm, just install it, apt-get install xterm
).
This should work.