I have a script that runs from cron every 15 minutes. The script is meant to ping my DNS server to update a dynamically changing IP. I want this script to ping through the wlan interface and not the eth0.

However, other processes should use eth0 as the primary interface to access the internet.

How do I do this for a specific process only without disturbing other processes? Is there a way to direct traffic through interface like directing stdout? like ls -al > /dev/tty2'. Can chroot help me in this by giving me a sandboxed environment?


The solution is to set the Netfilter packet mark which can be used by advanced routing. There is no way to match by process id, but Netfilter allows to match on process UID or GID. In this case it seems to be the easiest solution to create a new system user especially for this task.

iptables -t mangle -A OUTPUT -m owner --uid-owner wlanping -j MARK --set-mark 42

Create a new routing table (forecewlan) in /etc/iproute2/rt_tables and chose the wlan gateway as default gateway:

ip route add default via $WLAN_GATEWAY

and force the use of this routing table for packets with this mark:

ip rule add fwmark 42 table forcewlan

Maybe you also have to copy this kind of entry from the main routing table: dev wlan0  proto kernel  scope link  src

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.