How do I share internet with Windows from my Linux box?

You need to set up NAT on the Linux box. There are numerous howtos on the Net when you search for NAT and iptables, maybe including the distro you use. Here is a howto for Debian which should work on other distros as well: http://debianclusters.org/index.php/NAT_with_IPTables

Here are some lines that come from a German Ubuntu howto:

sysctl -w net.ipv4.ip_forward=1
iptables -A FORWARD -o eth0 -s 192.168.0.0/16 -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Put them somewhere, where they are executed at startup (/etc/rc.local or you put "up" in front of every line and put the whole thing into /etc/network/interfaces) and replace eth0 by the network device that connects to the Internet and eth1 by the one that goes to your LAN.

You might also have to tell your Windows box some name servers (DNS) manually if you don't want to set up bind on your Linux box. And I trust you don't need or already have a DHCP server in your LAN.


The question asks,

Is the reverse possible instead (connecting the Internet to the Windows computer and sharing it with Linux)?

Here's how you can share your Internet connection from Windows to Linux.

On Windows,

  • Make sure internet sharing is enabled.

    • Go to Control Panel, find the network connection, then right-click on the internet adapter and click "Properties".
    • Click "Advanced".
    • Right there click the radio button to enable Internet sharing.
  • On Windows 7:

    • Go to Control Panel → "Network and Sharing Center".  (You may need to go through "Network and Internet" to get there.)
    • Clink on "Change adapter settings".
    • Right-click on the network adapter and click "Properties".  Enter an administrator password, if asked.
    • Click on the "Sharing" tab and enable sharing.

On Linux,

With root (sudo or su) privilege,

  1. Type this to set the default gateway:

    route add default gw x.x.x.x

    (Note: replace x.x.x.x by the Windows host's local IP address.  You can find it by typing: ipconfig -all in the Windows Command Prompt.)

  2. Next type this to set the DNS:

    echo "nameserver y.y.y.y" > /etc/resolv.conf

    (Note: replace y.y.y.y by the DNS server address on Windows.  You can find it by typing: ipconfig -all in the Windows Command Prompt.)

In this scenario, Windows is already connected to the Internet.  After all the setup, you can try now to use the Internet from Linux.  The Linux and Windows hosts should be able to use the Internet simultaneously.