I have an internal machine , say X (not connected to internet) that is connected to a machine, say Y which is connected to the internet .
How should I configure the machines to be able to route from internal machine X to internet through Y?
The default router Y uses must know that the network X uses is available through Y.
so, if X is at 192.168.1.12/24, Y is at 192.168.1.1/24 and 192.168.0.101/24. Then at the router (let's say at 192.168.0.1/24, connected to Internet) that shares connection with Y you need to add:
ip route add 192.168.1.0/24 via 192.168.0.101 scope global
and appropriate rules to firewall to let traffic through.
The other solution is to use SNAT in Y:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
(assuming that the network between Y and Y's default router is connected to eth0)
In both cases you need to add packet forwarding to Y:
echo 1 > /proc/sys/net/ipv4/ip_forward echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
Set the gateway on machine X as Y. Then make sure host Y allows packet-forwarding. On linux with iptables this would require more or less this:
After that, apply a rule in iptables to allow the traffic.
(if eth0 is the right device)
sysctl -w net.ipv4.ip_forward=1
On machine X, set the default route to be Y.
If that works, permanently set it by adding