Sharing an Internet Connection in Linux is easy

This won’t work with real VPN connections, and you may have problems with other oddities like a Play Station 3 or Xbox, but perhaps someone else knows a way around that too?!

Enable IP forwarding

# echo “1″ >> /proc/sys/net/ipv4/ip_foward

Flush the current rules

# iptables -F
# iptables -t nat -F
# iptables -t mangle -F
# service iptables save
# service iptables restart

Share the eth0 interface

This is the interface that’s connected to the router (192.168.1.1)
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Configure eth0

# ifconfig eth0 192.168.1.2 netmask 255.255.255.0 default gw 192.168.1.1

Configure eth1

This is the interface that will connect to a switch to create the LAN (or just directly to another machine via crossover cable)
# ifconfig eth1 192.168.100.1 netmask 255.255.255.0 default gw 192.168.1.1

Save & restart iptables

# service iptables save
# service iptables restart
# iptables -t nat -L POSTROUTING

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all — anywhere anywhere

masquerade-225x225

Configure a host on your new LAN

Try dhcp. If your sharing host is connected to a router handing out dhcp addresses, you’ll get /etc/resolv.conf set up for you too.
# ifup eth0 && dhclient eth0
Manually:
# ifconfig eth0 192.168.100.2 netmask 255.255.255.0 default gw 192.168.100.1

Test it out

# ssh 192.168.100.1 hostname
# ping blog.rootninja.com

That’s it…. This should work step-for-step on CentOS, Fedora, and any Redhat-like distribution. The basic idea will work on any Linux host with iptables.

Posted by admica   @   6 November 2009

Related Posts

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Versatile resource statistics tool for Linux - dstat
Next Post
Fixing slow or broken NFS mounting issues »
Powered by Wordpress   |   Lunated designed by ZenVerse