Search code examples
linuxsocketsnetwork-programmingwiresharkiptables

How to use Linux Network Namespaces for per processes routing?


I want to crawl webpages through browser and store network traffic per URL (not only HTTP but also udp, rtmp etc.) I came across this solution to use linux network namespace for per process routing. Following are the steps I followed, however unable to browse the webpage.

ip netns add test

create a pair of virtual network interfaces (veth-a and veth-b):

ip link add veth-a type veth peer name veth-b

change the active namespace of the veth-a interface:

ip link set veth-a netns test

configure the IP addresses of the virtual interfaces:

ip netns exec test ifconfig veth-a up 192.168.163.1 netmask 255.255.255.0

ifconfig veth-b up 192.168.163.254 netmask 255.255.255.0

configure the routing in the test namespace:

ip netns exec test route add default gw 192.168.163.254 dev veth-a

sudo bash -c ‘echo 1 > /proc/sys/net/ipv4/ip_forward’

sudo iptables -t nat -A POSTROUTING -s 192.168.163.0/24 -o wlan0 -j MASQUERADE

Open Browser in the namepace and get following:

sudo ip netns exec test /usr/bin/firefox http://google.com

(firefox:15861): GConf-WARNING **: Client failed to connect to the D-BUS daemon: Failed to connect to socket /tmp/dbus-xE8M4KnMPn: Connection refused

(firefox:15861): LIBDBUSMENU-GLIB-WARNING **: Unable to get session bus: Could not connect: Connection refused

In wireshark: sudo ip netns exec test wireshark I can see Only Outgoing DNS requests from 192.168.163 to 127.0.1.1. Kindly let me know what I am missing here?


Solution

  • Got it. I am able to ping 8.8.8.8. The problem was in DNS resolving.

    Update DNS resolver.

    put nameserver 8.8.8.8 in /etc/resolvconf/resolv.conf.d/base and in /etc/resolvconf/resolv.conf.d/head.

    Restart Network.

    sudo service network-manager restart

    Now /etc/resolv.conf looks like.

    # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
    #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
    nameserver 8.8.8.8
    nameserver 127.0.1.1
    

    Finally.

    sudo ip netns exec test /opt/google/chrome/google-chrome --user-data-dir=/tmp/chrome2/ http://yahoo.com