Sharing (wifi) internet through ethernet interface

7,905

There are two ways to accomplish this. I'm not sure how to go about this using a GUI, but here's the standard way on Ubuntu. I will use the UFW and netplan tools which come with later versions of Ubuntu, as an example. One of these options should be applied on the laptop.

Setup a NAT

The reason your current setup isn't working is that you haven't setup a NAT, or Network Address Translation. A NAT is what is responsible for changing the device's IP as packets enter and leave your NAT gateway. A wireless router does this, and this is the reason devices have private IP addresses, but can still contact the outside internet.

In Ubuntu, setting up a NAT is fairly straightforward because of the built-in UFW utility. In the file /etc/ufw/sysctl.conf, comment out the line net.ipv4.ip_forward=1.

Also, set this line in /etc/default/ufw
DEFAULT_FORWARD_POLICY="ACCEPT"

Now, in the file /etc/ufw/before.rules, add these lines just before the filter rules:

*nat
:POSTROUTING ACCEPT [0:0]

-A POSTROUTING -o <WIRELESS INTERFACE> -j MASQUERADE

COMMIT

Change <WIRELESS INTERFACE> to the name of your wireless interface, found using ip link. Once you have made these changes, restart ufw to apply changes sudo systemctl restart ufw, and also make sure the firewall is enabled by running sudo ufw enable.

Your current configuration with static IPs on the ethernet interface will work fine, and your secondary device should now be able to contact the outside.

Setup a Bridge

Another option is to bridge your ethernet interface and your wireless interface, essentially turning your laptop into a network switch. This is the best option if you want your device to act as a normal device on your network (as in as far as the other devices in your network is concerned, this is just another wireless client). You can configure your interfaces in the file /etc/netplan/01-netcfg.yaml or /etc/netplan/50-cloud-init.yaml. Whichever file you have. This example assumed DHCP is provided from your wireless interface. If it is not, you can find other examples for netplan here.

network:
  version: 2
  renderer: networkd
  ethernets:
    <ETHERNET INTERFACE>:
      dhcp4: no
    <WIRELESS INTERFACE>:
      dhcp4: no
  bridges:
    br0:
      dhcp4: yes
      interfaces:
        - <ETHERNET INTERFACE>
        - <WIRELESS INTERFACE>

Change to the interface your secondary device is connected to.

Then, run sudo netplan apply to apply those changes. If you run ip a you can confirm that the bridge interface br0 exists. Now, once you connect the secondary device and enable DHCP on that device, it will obtain an IP address from the wireless router and act as any other wireless device does on the network.

The bridge is now your primary interface from your wireless router, meaning if you wanted to assign a static ip on your laptop, for example, you would need to assign it on the bridge interface, not the wireless interface.

Share:
7,905
Sguit
Author by

Sguit

Updated on September 18, 2022

Comments

  • Sguit
    Sguit almost 2 years

    I want to connect two devices, the first is a laptop with ubuntu (connected to internet via wifi), and the second one is a device with a distro linux compiled with yocto. These two devices are connected each other with an ethernet cable. I want the internet acces on the second device. On the laptop i've setted a static IP on the eth interface (192.168.3.254), and i've allowed the wifi sharing option to other computers (so to the eth iface too). In the second device i've the following etc/network/interfaces:

    auto eth0
    iface eth0 inet static
        address 192.168.3.22
        netmask 255.255.255.0
        network 192.168.3.0
        gateway 192.168.3.254
    

    Now i'm able to start ssh session, but the only ping that works on the secon device is on 192.168.3.254. I've tried to ping 8.8.8.8 or 1.1.1.1 but nothing! What i'm doing wrong?

    • A.B
      A.B over 4 years
      You will have to give way more informations about "wifi sharing" on the laptop. The issue is probably on the laptop, not the yocto device.
    • Sguit
      Sguit over 4 years
      The laptop is connected to a WiFi Access Point with default settings. My only change to this settings (as mentioned in a lot of raspberry forum) is been enabling the ICS over all other computer connected.