How to route network traffic of a host via another in the same LAN?

Posted on

Problem :

So host A (mac OS) has ip: 10.1.10.65 and host B has ip: 10.1.10.68, which is a linux Ubuntu in a virtual machine on host A.
I want to route all traffic from host A through host B.

What I have done so far is, I enabled ip forwarding on host B:

sysctl net.ipv4.ip_forward=1

and on host A:

route delete default
route add default 10.1.10.68

After these operations, I can browse Internet fine on host A, but i am really not certain that if all traffic has been routed through host B now.
I read some other posts and they have mentioned to use iptables -t nat -A POSTROUTING -o ethx -j MASQUERADE on host B, I didn’t use this command on host B but I am still able to reach internet fine on host A.
Am I missing something here? How do I tell that I have correctly routed traffic through host B? Thank you.

Solution :

Many people on the internet erronously think they need iptables for routing (which is a pet peeve of mine).

You can check the routing on host A with ip route get a.b.c.d, which will tell you where the next hop of IP a.b.c.d is according to the current rules.

You can also use traceroute, or mtr or similar to see the actual path the packets take (as long as the hosts they go through are not configured to prevent this).

BTW, route is the old command, ip route ... is the newer version, and some routing features of newer kernels will only be accessible with ip route.

Run traceroute 8.8.8.8 on host A. The first hop will tell you what machine the traffic is going through.

Leave a Reply

Your email address will not be published. Required fields are marked *