# Getting NAT to work

## dE_logics

------I gave this thread a facelift----------

I'm trying to make a nat out of by box. The internet is connected to eth0 (which's already routed, thus I need to perform a double routing), and the connection sharing should be established with the vboxnet0 virtual interface.

I've referred to lot of iptables tutorials, but none are seeming to work (including Gentoo's which contains a lot of additional stuff).

So these are the parameters - 

eth0 -- 192.168.1.3, having internet connection through a router which lies on 192.168.1.1

vboxnet0 -- The virtual interface.

What I've done -- 

Set vboxnet0 to 192.168.2.1/255.255.255.0

iptables -t nat -I PREROUTING -i vboxnet0 -j ACCEPT

iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE

In the virtual machine, I've set the default gateway to 192.168.2.1 (vboxnet0), ip is 192.168.2.2, netmask is /24.

However I cant access the network connected to eth0, i.e I cant even ping the router (at 192.168.1.1) (forget access to the internet). However I can ping 192.168.1.3, i.e the address of eth0; BUT that also happens when iptables is completely empty... so essentially iptables is absolutely NOT working.

----------

## dE_logics

Question rewritten.

----------

## chiefbag

I would just try bridging the virtual box interface and use the following rule on the gentoo box

iptables -A SNAT_PROXY -o eth0 -j SNAT --to-source 192.168.1.3

This would NAT all outgoing traffic to 192.168.1.3

----------

## gentoo_ram

What are your forwarding rules set to?  By default, no forwarding is done between interfaces unless you enable it.

To open it up completely to test if that's the issue:

echo '1' > /proc/sys/net/ipv4/ip_forward

iptables -P FORWARD ACCEPT

Take out this rule:

iptables -t nat -I PREROUTING -i vboxnet0 -j ACCEPT

Change the other rule to this:

iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth0 -j MASQUERADE

Also, show us your routing tables and output from ifconfig -a.

Just so you know, I do have masquerading working with a vboxnet0 interface, so it is possible.

----------

## dE_logics

 *gentoo_ram wrote:*   

> What are your forwarding rules set to?  By default, no forwarding is done between interfaces unless you enable it.
> 
> To open it up completely to test if that's the issue:
> 
> echo '1' > /proc/sys/net/ipv4/ip_forward
> ...

 

Ok, got it. DONE. thanks.

----------

## dE_logics

Surprisingly it also works without the -s option, i.e in the same way I was doing it first hand.

Maybe I'd forgotten to set /proc/sys/net/ipv4/ip_forward to 1.

----------

