7

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)?

2
  • Did it work? Do you need any more help?
    – Christian
    Commented Jun 15, 2012 at 9:20
  • Thanks. You are so helpful. But sorry, I have no time to do this right now. I will comment as soon as possible. Commented Jun 20, 2012 at 12:07

4 Answers 4

6

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.

3

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.

6
  • 2
    -1: he wants to do the opposite (share the internet from the Linux box to the Windows box).
    – Renan
    Commented Nov 30, 2012 at 22:11
  • 2
    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
    Commented Dec 2, 2012 at 20:17
  • 1
    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
    Commented Dec 2, 2012 at 20:51
  • 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
    Commented Dec 3, 2012 at 7:05
  • 1
    after all 8 command lines you need to save the settings by type: iptables-save.
    – rnlsgh
    Commented Dec 3, 2012 at 7:16
0

To connect a Linux box to a Windows box (to get internet), you need a working LAN (Ethernet) connection - as said in my first post here. So basically, you need to assign static private IPv4 addresses yourself, on both machines. You might need to set the connection to "private" or "trusted" as well. Once both PC's are connected, go to "Network and Sharing Center"->"change adapter settings", and pick your network interface that corresponds to your internet connection. Right click for Properties and look for a tab related to sharing the connection. There you should be able to select your LAN (Ethernet) connection, to share with your other PC.

You might need to specify the DNS on the "internal" LAN PC, like I said in my first post here, on this thread. So you will need to run ipconfig on Windows in a command prompt to find out what DNS you are using, or use Google's DNS 8.8.8.8 and/or 8.8.4.4.

0

There is a simple guide at https://oracle-base.com/articles/linux/use-iptables-to-implement-packet-filtering-and-configure-nat. But I found out, that in Windows, the DNS address that the Linux PC is using, must also be given in Windows.

I have a 3G USB modem connected to my Linux PC, and from that I have two Windows PC's that get internet. It's called (kernel) packet forwarding, and it's quite simple and quick to do - when you understand it finally.

You will need to find out your network interfaces names by running the command "ifconfig" on Linux (in the terminal), and "ipconfig" on Windows (in the command prompt). The interface names, on Linux, are at the leftmost side, like: enp2s0, enp3s0, enp0s18f2u6, lo. Now you set up packed forwarding:

  1. First you need to enable packet forwarding on your Linux PC (see that guide I linked from oracle-base.com).

  2. Next, you can run these commands, as that oracle-base.com guide says (but wait, and read on):

    sudo iptables -I FORWARD -i my_lan_interface -o my_modem_interface -j ACCEPT sudo iptables -I FORWARD -i my_modem_interface -o my_lan_interface -j ACCEPT sudo iptables -t nat -I POSTROUTING -o my_modem_interface -j MASQUERADE

    But there is a more robust way, that I found on the net:

    sudo iptables -t nat -A POSTROUTING -o my_modem_interface -j MASQUERADE --random sudo iptables -A FORWARD -i my_lan_interface -o my_modem_interface -j ACCEPT sudo iptables -A FORWARD -i my_modem_interface -o my_lan_interface -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -j DROP

    Here's an explanation for the commands just above (the "more robust" example), in the same order:

    • First command: enable masquerading on my_modem_interface so the source address is rewritten on outgoing packets. The --random flag is to have symmetric NAT.

    Now we are going to configure forwarding rules. iptables, by default, will unconditionally forward all traffic. Here we prefer to restrict inbound traffic from the internet and allow all outgoing:

    • Second command: Allow traffic from my_lan_interface to my_modem_interface.
    • Third command: Allow returning traffic from my_modem_interface to my_lan_interface.
    • Fourth command: Drop all other traffic that shouldn't be forwarded.

    When you reboot your Linux PC, you will have to reenter these commands - I have a script that runs them at each boot. You can make these changes permanent with the appropriate "service" call (again, see the oracle-base.com guide given above), but I recommend running them at each boot (in a script), as you might want to experiment and change things, and undoing what you saved is another challenge. If something goes wrong, or you want to change things, or you just don't want this feature for your current session (or at all), then just reboot your PC and they're gone - but you must disable packet forwarding yourself (before rebooting write "net.ipv4.ip_forward = 0" in /etc/sysctl.conf) to fully disable this technology.

  3. Now you have to create a network connection (Ethernet connection) between your Linux PC and your other computers. For example:

    • A) On the Linux PC connected to the internet (I use Fedora), with the network connections manager, I create an Ethernet connection, select the appropriate interface (network card name, enp3s0 in my case) that will connect this Linux PC to my LAN. Be sure to select the right firewall zone or your LAN will not get internet. So since this is your internal LAN interface, set the firewall zone to "trusted", but the modem connection interface should be set to "public". Next, in the IPv4 settings tab, set that IPv4 must be used for the connection (IPv6 can be ignored), and select manual address setup. Now add the address 192.168.2.100 - this will be the address of this Linux PC on your LAN. The net mask will be set automatically (255.255.255.0, because this is a "C class" (private) address). Apply your settings and connect.

    • B) On the second PC (with Linux or Windows, connected by Ethernet cable to the first one above), also create a network connection (if it's Windows, look below for better details), select the appropriate network interface (the device/card which is connecting to the first PC), use manual IPv4 setup as above for the first PC, but here set the address 192.168.2.101 - this will be the address of this second PC on your LAN (these are "private" addresses, meaning that they will not be visible outside your LAN).

    • B1) If you want to connect Windows (operating system), go to "Network and Sharing Center"->"change adapter settings", and look for your network interface that corresponds to your LAN network card on this Windows PC, which is also connected by Ethernet cable to the first PC. Now, select that interface, right click for Properties. You will see a list. Here unselect "Internet Protocol Version 6" and double click on "Internet Protocol Version 4". Now you enter the address 192.168.2.101 - this will be the address of this Windows PC on that interface (on that [LAN/Ethernet] connection)). Tab down so netmask is filled out automatically (255.255.255.0). (The "Network Profile" can be set to "public".)

    • B2) It might be necessary to set the gateway address to the first Linux PC, so you would need to enter as gateway address 192.168.2.100.

    • C) Now, you must enter the DNS address that your first Linux PC is using. For example, my modem uses DNS address 192.168.1.1 (which I can find with the command "nmcli device show | grep IP4.DNS", where interfacename is the name of your Linux interface connected to the internet). (If you connect to the internet with Windows, run "ipconfig" in the terminal/command prompt, look for a DNS address). And that's it. The DNS information is critical to getting an internet connection.

So putting it short:

My modem address: 192.168.1.100, DNS 192.168.1.1.

Linux PC address on LAN: 192.168.2.100, packet forwarding enabled with iptables redirecting traffic, as explained above, firewall zone set to "trusted".

Windows PC 1 address: 192.168.2.101 with DNS set to 192.168.1.1, network profile set to public. (Default gateway set to Linux PC: 192.168.2.100).

Windows PC 2 address: 192.168.2.102 with DNS set to 192.168.1.1, network profile set to public. (Default gateway set to Linux PC: 192.168.2.100).

(All computers connected together by a simple Ethernet switch with plain Ethernet cables (also called "patch cables" or "patch cords"), not crossed, because "modern" network cards do the crossing by themselves, so you can use crossed cables or not crossed cables, even mix them together in a connection.)

There is also a guide at: https://medium.com/@TarunChinmai/sharing-internet-connection-from-a-linux-machine-over-ethernet-a5cbbd775a4f, which uses Google's DNS addresses instead. Check it out.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .