Internet connection sharing between two Ubuntu computers
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
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):
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.