LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Internet connection sharing between two Ubuntu computers (https://www.linuxquestions.org/questions/linux-networking-3/internet-connection-sharing-between-two-ubuntu-computers-457959/)

tsunamikitsune 06-24-2006 05:14 PM

Internet connection sharing between two Ubuntu computers
 
I'm still fairly new to Linux, so I don't quite know how to share an internet connection. Here's what my set up looks like:

Cable Modem -> ethernet cable -> Computer 1 (Network Card 1)
Computer 1 (Network Card 2) -> crossover ethernet cable -> Computer 2

In Windows XP, I could connect the crossover cable to another computer running XP and it usually had working internet. Both computers are running Ubuntu, so it shouldn't be too hard to share a connection, right? Like I said, I don't know too much about Linux, so please give me some easy to understand instructions.

TigerOC 06-25-2006 06:28 AM

Primarily you need ip-masquerading and then you need to forward requests from comp 1 to 2. This is covered in this article.
The network part of this is that the gateway for comp 2 will be the ip address of network card 2 in comp 1 if you are using static addressing. The network settings are in /etc/network/interfaces.

tsunamikitsune 06-25-2006 04:49 PM

I have a dynamic ip address, and on Windows I used no-ip to fix that problem. I installed the no-ip package, but I don't know how to set it up or run it. Any experience with this?

TigerOC 06-26-2006 12:04 PM

Quote:

Originally Posted by tsunamikitsune
I have a dynamic ip address, and on Windows I used no-ip to fix that problem. I installed the no-ip package, but I don't know how to set it up or run it. Any experience with this?

Reduce the complications by using static addressing.
The dynamic address on the wan (internet) has no bearing on the lan as they are separate networks. The cable modem will pick-up the lease on the wan and then handle the routing to the lan. The key here is not to get confused with addresses and boxes. The ethernet connection to the modem can use dynamic addressing to pick-up an address from the modem. The lan needs to be on a different subnet. eg. if the modem is issuing leases in the 192.168.1.X range then use addresses in the 192.168.0.X range for the lan.

Andrew Przelucki 05-04-2021 08:16 AM

There is a simple guide by Oracle at 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 box, 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.


1) First you need to enable packet forwarding on your Linux box (see that Oracle guide I provided).

2) Next, you can run these commands, as the Oracle 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

When you reboot your linux box, you will have to reenter these commands - I just created a script that runs them at each boot. You can make these changes permanent with the appropriate "service" call (again, see the Oracle guide). 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

Again, these have to be done at each boot, unless you want to make them permanent. But I recommend running at each boot, as you might want to experiment and change things, and undoing what you saved is another challenge.

You can find your network interfaces 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, ....


3) You will also have to create a network connection between your Linux box and your Windows box(es). For example:

- On the Linux box connected to the internet (I use Fedora), with the connections manager, I create an Ethernet connection, select the appropriate interface (network card name, enp3s0 in my case) to be used for that connection, and in IPv4 settings 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 your linux box. The net mask will be set automatically (255.255.255.0, because this is a C class (private) address). Apply your settings and connect.

- On the second PC (connected by Ethernet to the first one above), also create a network connection, select the appropriate network interface (the device/card which is connecting to the first PC), use manual IPv4 setup, set address 192.168.2.101 - this will be the address of this second PC. (On a Windows box, go to "Network and Sharing Center"->"change adapter settings", and pick your network interface that corresponds to your LAN card connected to the first box that already is connected to the internet. Select that interface, right click for Properties, unselect Internet Protocol Version 6, and double click "Internet Protocol Version 4". Now you enter the address 192.168.2.101 - this will be the address of your Windows PC on that interface (on that [LAN/Ethernet] connection)). Tab down so netmask is filled out automatically (255.255.255.0). Now, you must enter the DNS address that your first Linux box is using. My modem uses DNS address 192.168.1.1 (which I can find with the command "nmcli device show <interfacename> | 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 such info). And that's it. The DNS info is critical to getting an internet connection.


So putting it short:

My modem address: 192.168.1.100, DNS 192.168.1.1.
Linux box address on LAN: 192.168.2.100, packet forwarding enabled with iptables redirecting traffic, as explained above.
Windows box 1 address: 192.168.2.101 with DNS set to 192.168.1.1.
Windows box 2 address: 192.168.2.102 with DNS set to 192.168.1.1.
(All computers connected together by a simple Ethernet switch.)

There is also a guide at: medium.com/@TarunChinmai/sharing-internet-connection-from-a-linux-machine-over-ethernet-a5cbbd775a4f, which tells you to use Google's DNS addresses, instead of the USB modem address I gave above.

TB0ne 05-04-2021 08:44 AM

Again; check the dates...this one had been closed for *FIFTEEN YEARS*.


All times are GMT -5. The time now is 12:04 PM.