Friday, October 01, 2010

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.

Prerequisites:
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 192.168.1.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 192.168.1.0/24 -j MASQUERADE

That's it! Remember that for the other machines you must set the gateway IP as 192.168.1.1. 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 //192.168.1.2/public
smb: \] dir
file1.txt
file2.txt

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 //192.168.1.2/
If you want to mount a SAMBA share on your filesystem:
mount -t smbfs -o username=r00tb0x_user //192.168.1.2/public /mount/point
Post a Comment