Internet Connection Sharing: Difference between revisions

From NixOS Wiki
imported>Onny
mNo edit summary
imported>Prateekrajgautam
No edit summary
Line 11: Line 11:
sudo create_ap wlan0 eth0 MyAccessPoint
sudo create_ap wlan0 eth0 MyAccessPoint
</syntaxhighlight>
</syntaxhighlight>
Here <code>eth0</code> is the ethernet interface connected to the internet, and <code>wlan0</code> is the wifi interface to share the internet connection with a wifi hotspot. However, these names may not be the same on your system. To check the name of your hardware interface use the command <code>ifconfig</code> or <code>ifconfig | grep ^[^:]*:</code> It will list network adapters on your system. Then modify command accordingly <code>sudo create_ap <wifiInterfaceName> <ethernetInterfaceName> MyAccessPoint <Password></code>


=== Configuration ===
=== Configuration ===

Revision as of 05:18, 14 December 2023

The following example will describe how to share an active internet connection over a WiFi hotspot or alternatively via ethernet.

Share via WiFi

Usage

Share an existing internet connection of a wired interface eth0 using a wifi hotspot on wlan0 with the access point name MyAccessPoint.

nix shell nixpkgs#linux-wifi-hotspot
sudo create_ap wlan0 eth0 MyAccessPoint

Here eth0 is the ethernet interface connected to the internet, and wlan0 is the wifi interface to share the internet connection with a wifi hotspot. However, these names may not be the same on your system. To check the name of your hardware interface use the command ifconfig or ifconfig | grep ^[^:]*: It will list network adapters on your system. Then modify command accordingly sudo create_ap <wifiInterfaceName> <ethernetInterfaceName> MyAccessPoint <Password>



Configuration

Persistent share an existing internet connection of a wired interface eth0 using a wifi hotspot on wlan0 with the access point name My Wifi Hotspot. The network is protected with a simple WPA2 pre-shared key 12345678.

services.create_ap = {
  enable = true;
  settings = {
    INTERNET_IFACE = "eth0";
    WIFI_IFACE = "wlan0";
    SSID = "My Wifi Hotspot";
    PASSPHRASE = "12345678";
  };
};

Share via ethernet

Usage

Share an existing internet connection of a wireless interface wlan0 to clients connected on a ethernet device eth0. Make sure you've got TCP and UDP port 53 for the dnsmasq DHCP server opened in your Firewall.

# Setup ethernet device
ip link set up eth0
ip addr add 10.0.0.1/24 dev eth0

# Enable packet forwarding
sysctl net.ipv4.ip_forward=1

# Enable NAT for leaving packets
nft add rule nat POSTROUTING oifname wlan0 masquerade

# Start dnsmasq for DHCP
dnsmasq -d -i eth0 --dhcp-range=10.0.0.2,10.0.0.255,255.255.255.0,24h

To cleanup the configured interface run following commands

ip addr del 10.0.0.1/24 dev eth0
ip link set down eth0
# Get handle_number with: nft -a list table nat
nft delete rule nat POSTROUTING handle <handle_number>