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

20,709

Solution 1

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.

Solution 2

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.

Share:
20,709
fanaticTwin
Author by

fanaticTwin

Updated on September 18, 2022

Comments

  • fanaticTwin
    fanaticTwin over 1 year

    I have a Linux (Ubuntu 12.04) PC connected to the internet with a Greenpacket WiMax USB modem. I want to share the Internet connection with another computer running Windows 7 Home Premium, connected to the Linux PC over a LAN. Is this possible? How? Is the reverse possible instead (connecting the internet to the Windows computer and sharing it with Linux)?

    • Christian
      Christian almost 12 years
      Did it work? Do you need any more help?
  • Renan
    Renan over 11 years
    -1: he wants to do the opposite (share the internet from the Linux box to the Windows box).
  • rnlsgh
    rnlsgh over 11 years
    but he also interested to do the reverse. here is i am quote his question: Is the reverse possible instead (connecting the internet to the Windows computer and sharing it with Linux)?
  • rnlsgh
    rnlsgh over 11 years
    if you want to share internet from Linux to Windows, I think the answer above given by Christian is basically correct after replacing this: iptables -A FORWARD -o eth0 -s 192.168.0.0/16 -m conntrack --ctstate NEW -j ACCEPT with this: iptables -A FORWARD -i eth1 -s 192.168.0.0/16 -m conntrack --ctstate NEW -j ACCEPT (note:I am only correcting the typos: -o eth0, replace by -i eth1). plus on Windows side you have to set the gateway with Linux IP and DNS address with the ISP's DNS.
  • rnlsgh
    rnlsgh over 11 years
    but if it doesn't work maybe there is conflict on iptables rules. in this case before those 4 command lines maybe you need to erase all previous iptables rules by type this: (1)iptables -F (2) iptables -t nat -F (3)iptables -X (4)iptables -t nat -X
  • rnlsgh
    rnlsgh over 11 years
    after all 8 command lines you need to save the settings by type: iptables-save.
  • themadmax
    themadmax almost 6 years
    Sometime this method did not work. Windows 7 Host internet ok. But I have change IP tomshardware.co.uk/faq/id-1925829/…. Linux can ping Windows, but cannot ping 8.8.8.8. Any Idea ?