Archive for: June, 2006

Linux connection sharing

Jun 28 2006 Published by under Linux,Networking

Ever been stuck with a friend (or a few of them) with only one of you being able to connect to the Internet and both needing the connection? And at least one of you has more then one network interface? Problem solved.

All you need is iptables, appropriate kernel modules and in case you want to share your wired connection over the wireless one, support for Master mode for your wireless adapter. Intel Wireless Pro 2200BG, a rather popular adapter, has this support on a basic, but developing level. And of course, nobody prevents you from doing the same thing on an Ad-Hoc network, a mode that is supported on a much wider range of hardware.

This assumes your outputting interface is set up and your inputting interface will have an IP address unless other is specified as a third argument. You can save the script below as /usr/local/sbin/router, make it executable, and you will be able to invoke it as just router in_iface out_iface.

if [ "$2" != "" ]; then


if [ "$3" != "" ]; then
IP=`echo $3 | cut -f 1,2,3 -d .`

iptables -F
ifconfig $IN up
ifconfig $OUT up

ifconfig $IN $ADDR
echo "$IN IP: $ADDR"

iptables -t nat -A POSTROUTING -o $OUT -j MASQUERADE
iptables -A INPUT -s $IP.0/24 -i $IN -j ACCEPT
iptables -A FORWARD -s $IP.0/24 -i $IN -o $OUT -j ACCEPT
iptables -A FORWARD -d $IP.0/24 -i $OUT -o $IN -j ACCEPT

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


echo "Usage: router in_iface out_iface [local_ip]"


So an example command would be:

router eth0 eth1

This way I route all the traffic coming to eth0 onto the network that I connected using eth1.

No responses yet

Older posts »