Setting up a Linux box as a router

If you have a virtual machine in a Linux host which requires Internet and physical LAN connectivity, or you just need to pass all traffic of a LAN through a Linux machine, you may need to set up Linux as a router.

  •     Enable IP forwarding on the Linux box
  •     Create a NAT rule to masquerade the IP
A virtual private IP must be configured on the Linux machine:
ifconfig eth0:1

This will serve as the gateway address for the other machines in the network (or of the virtual machine). In this case the other machines must have an IP in the form of 192.168.1.x. IP forwarding must be enabled:
echo 1 > /proc/sys/net/ipv4/ip_forward

Then we need to create the NAT rule to masquerade the private IP into a public one:
iptables -t nat -A POSTROUTING -s -j MASQUERADE

That's it! Remember that for the other machines you must set the gateway IP as You might also need to enter the DNS details (you can easily obtain it from dig [your ISP]. To list all the NAT rules:
iptables -t nat -L

To delete the NAT rule:
iptables -t nat -L

If you need to access your windows share, you can use smbclient as follows:
smbclient -U r00tb0x_user //
smb: \] dir

Note that you can do all the FTP stuff here..If you want to list shares available on a windows host:
smbclient -U r00tb0x_user -L //

If you want to mount a SAMBA share on your filesystem:
mount -t smbfs -o username=r00tb0x_user // /mount/point

No comments: