Laptop WIFI router on Debian

Discussion in 'Installation/Configuration' started by SamTzu, Aug 15, 2024.

  1. SamTzu

    SamTzu Active Member

    Code:
    #!/usr/bin/env bash
    # This is a service to forward traffic from WiFi to Ethernet
    # This script works on Debian 12
    # First You need to manually setup your WIFI password
    # Make sure your LAN device is named correctly
    # This script uses enp0s25 as the ethernet device name
    
    set -e
    
    [ $EUID -ne 0 ] && echo "run as root" >&2 && exit 1
    
    # parprouted  - Proxy ARP IP bridging daemon
    # dhcp-helper - DHCP/BOOTP relay agent
    
    apt update && apt install -y dhcpcd dhcpcd5 dhcp-helper parprouted
    
    systemctl stop dhcp-helper
    systemctl enable dhcp-helper
    
    # Enable ipv4 forwarding.
    sed -i'' s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/ /etc/sysctl.conf
    
    # Service configuration for standard WiFi connection. Connectivity will
    # be lost if the username and password are incorrect.
    systemctl restart wpa_supplicant.service
    
    # Enable IP forwarding for wlan0 if it's not already enabled.
    grep '^option ip-forwarding 1$' /etc/dhcpcd.conf || printf "option ip-forwarding 1\n" >> /etc/dhcpcd.conf
    
    # Disable dhcpcd control of ethernet.
    grep '^denyinterfaces eth0$' /etc/dhcpcd.conf || printf "denyinterfaces eth0\n" >> /etc/dhcpcd.conf
    
    # Configure dhcp-helper.
    cat > /etc/default/dhcp-helper <<EOF
    DHCPHELPER_OPTS="-b wlan0"
    EOF
    
    # Enable avahi reflector if it's not already enabled.
    sed -i'' 's/#enable-reflector=no/enable-reflector=yes/' /etc/avahi/avahi-daemon.conf
    grep '^enable-reflector=yes$' /etc/avahi/avahi-daemon.conf || {
      printf "something went wrong...\n\n"
      printf "Manually set 'enable-reflector=yes in /etc/avahi/avahi-daemon.conf'\n"
    }
    
    # This is a service to forward traffic from WiFi to Ethernet.
    cat <<'EOF' >/usr/lib/systemd/system/parprouted.service
    
    [Unit]
    Description=proxy arp routing service
    Documentation=https://raspberrypi.stackexchange.com/q/88954/79866
    Requires=sys-subsystem-net-devices-wlan0.device dhcpcd.service
    After=sys-subsystem-net-devices-wlan0.device dhcpcd.service
    
    [Service]
    Type=forking
    
    # Restart until wlan0 gained carrier
    Restart=on-failure
    RestartSec=5
    TimeoutStartSec=30
    
    # clone the dhcp-allocated IP to ethernet device so dhcp-helper will relay for the correct subnet
    ExecStartPre=/bin/bash -c '/sbin/ip addr add $(/sbin/ip -4 -br addr show wlan0 | /bin/grep -Po "\\d+\\.\\d+\\.\\d+\\.\\d+")/32 dev enp0s25'
    ExecStartPre=/sbin/ip link set dev enp0s25 up
    ExecStartPre=/sbin/ip link set wlan0 promisc on
    ExecStart=-/usr/sbin/parprouted enp0s25 wlan0
    ExecStopPost=/sbin/ip link set wlan0 promisc off
    ExecStopPost=/sbin/ip link set dev enp0s25 down
    ExecStopPost=/bin/bash -c '/sbin/ip addr del $(/sbin/ip -4 -br addr show wlan0 | /bin/grep -Po "\\d+\\.\\d+\\.\\d+\\.\\d+")/32 dev enp0s25'
    
    [Install]
    WantedBy=wpa_supplicant.service
    EOF
    
    systemctl daemon-reload
    systemctl enable parprouted
    systemctl start parprouted dhcp-helper
    _______________________________________

    PS. My Ethernet IP is manually set. Note Default gateway address!
    192.168.1.10
    255.255.255.0
    0.0.0.0

    Sami Mattila
    Internet-Content

    Telephone: 00358442421231
    Email: [email protected]
    LinkedIN: samiedwardmattila
    Site: https://www.ic4.eu
    Perfect gift (1€/month). Family-name email address!
    https://rossi.id
    @rossi.id
     
    Last edited: Oct 5, 2024
  2. SamTzu

    SamTzu Active Member

    Just tried this on my old Dell laptop
    Works like a charm.
    (Though Debian 12 needed the proprietary WIFI firmware repo added in APT sources.)
    Just remember to chmod +x the script.
     
  3. SamTzu

    SamTzu Active Member

    Odd...
    Had to change order of apt install. DHCP first. Routing second.
    Sometimes DNS just dies. No idea why.
    My homelab Proxmox (that uses bridged network) can't get DHCP on local ethernet and since WIFI does not work with bridged networking... this does not work with Proxmox.
    Who ever decided that you can't bridge WIFI? I'd like to have a few words with him.
     
    Last edited: Aug 18, 2024
  4. SamTzu

    SamTzu Active Member

    Next. How to add WIFI config with password from terminal and make this script work on headless Debian?
     
  5. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    It's best if you put in the code tag, otherwise, is not easily readable.
     
  6. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Debian Wiki has info: https://wiki.debian.org/WiFi/HowToUse
    Perhaps conmann or Using ifupdown and wpasupplicant.
    On Raspberry Pi I had instructions on how to write WIFI configuration to boot disk, I assume similar thing would work on regular Debian.
     
  7. ahrasis

    ahrasis Well-Known Member HowtoForge Supporter

    I don't think that read as a question but a statement of his next tip.
     
    Taleman likes this.
  8. SamTzu

    SamTzu Active Member

    My bad. I fixed that.
     
    ahrasis likes this.
  9. SamTzu

    SamTzu Active Member

    Seems like there is basically two practical ways to do WIFI with terminal only.
    Easy way is to add something like this in /etc/networking/interfaces
    Code:
    # Wireless interface
    allow-hotplug wlp1s0
    iface wlp1s0 inet static
        address 192.168.1.101/24
        gateway 192.168.1.1
        wpa-ssid ***
        wpa-psk ***
    ...or use wpa_supplicant.conf file.

    Configure wpasupplicant:
    Code:
    wpa_passphrase SSIDNAME PASSWORD >> /etc/wpa_supplicant/wpa_supplicant.conf
    Determine wireless adapter device name:
    Code:
    root@px1:~# dmesg | grep wlp
    [    4.374531] iwlwifi 0000:04:00.0 wlp1s0: renamed from wlan0
    Create /etc/systemd/system/wpa_supplicant.service and add configuration (specify YOUR wireless interface on the ExecStart line):
    Code:
    touch /etc/systemd/system/wpa_supplicant.service
    Code:
    [Unit]
    Description=WPA supplicant
    Before=network.target
    After=dbus.service
    Wants=network.target
    IgnoreOnIsolate=true
    
    [Service]
    Type=dbus
    BusName=fi.w1.wpa_supplicant1
    ExecStart=/sbin/wpa_supplicant -u -s -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlp1s0
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    Alias=dbus-fi.w1.wpa_supplicant1.service
    Enable wpasupplicant service:
    Code:
    systemctl enable wpa_supplicant
    Configure /etc/network/interfaces:
    Code:
    auto lo
    iface lo inet loopback
    iface enp0s25 inet manual
    
    auto wlp1s0
    iface wlp1s0 inet manual
       address 192.168.1.100/24
       gateway 192.168.1.1
    
    auto vmbr0     # or bridge to other NIC
    iface vmbr0 inet static
       address 192.168.2.1/24
       bridge-ports none
       bridge-stp off
       bridge-fd 0
    
    source /etc/network/interfaces.d/*
    Restart wpa_supplicant and networking services to connect wireless adapter to wifi network:
    Code:
    systemctl restart wpa_supplicant && systemctl restart networking
     
    Last edited: Oct 3, 2024

Share This Page