The instructions below make the assumption that the Laptop (Server) is connected as indicated below - pardon my ascii art. Specifically, that it has a wired connection to the LAN and also has a Wireless interface.
^ ^
| | SSID: This here be my WiFi
| | PSK: St@yTh3#ell0ut!
/ \ CHANNEL: 6
----------------- / \ -----------------
| Laptop | |wlan0 wlan0| | Router |
| (Server) ---| |--- |
| ---| |--- |
| | |eth0 -- -- eth0| | |
----------------- | | -----------------
| |
-----------------
| Switch |
-----------------
In this scenario, you can extend the Router's WiFi by using the following configuration. The steps below require that hostapd
and dnsmasq
be installed on the Laptop:
cat <<EOF | ~/extendwifi.sh
#!/bin/bash
sudo apt install -y hostapd
sudo mv /etc/hostapd.conf{,.old}
wget -qO- https://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf | egrep -v '^$|^#' | sed -e 's/test/This here be my WiFi/;s/rts_threshold=-1/rts_threshold=2347/;s/fragm_threshold=-1/fragm_threshold=2346/' | sudo tee /etc/hostapd/hostapd.conf
echo -e "wpa=2\nwpa_passphrase=St@yTh3#ell0ut!\nwpa_key_mgmt=WPA-PSK WPA-EAP WPA-PSK-SHA256 WPA-EAP-SHA256" | sudo tee /etc/hostapd/hostapd.conf
EOF
chmod +x ~/extendwifi.sh
~/extendwifi.sh
Once the script above has been executed, you should restart the hostapd
service systemctl restart hostapd
If you have WiFiAnalyzer (open-source)
installed on your Android phone, then you should be able to now see this new access point (check the MAC addresses). See it? Good! If not, double check your configuration. Of note is the channel; make sure to change it in /etc/hostapd/hostapd.conf
if they appear on the same channel.
Now, since this procedure has turned your lowly Laptop into a Router, we now need to deal with some networking bits. For that, you will definitely need dnsmasq
and here's what you'll need to do:
- Install dnsmasq -
sudo apt install -y dnsmasq
- Enable IP Forwarding (file
/etc/sysctl.conf
)
nano /etc/sysctl.conf
- Uncomment
net.ipv4.ip_forward=1
- Save the file and exit the editor
sudo sysctl -p
Now we need to create the configuration for dnsmasq. Let's assume that we'll create a new subnet with the following details:
- Subnet IP : 10.11.12.0/24
- DHCP Range : 10.11.12.13 to 10.11.12.32
- This gives you 20 devices that you can attach to this wifi subnet.
- Increase if you need more
- wlan0 IP : 10.11.12.1/24
- You will need to have somehow added this IP to the interface
- There are many tutorials on how to accomplish this on the internet
- Lease Period : 12hours
- DNS Server : 192.168.1.1
- Assuming that the Router is at IP 192.168.1.1 and is also the DNS Server
Copy and paste the lines below on the terminal and hit enter:
cat <<EOF | sudo tee /etc/hostapd_dnsmasq.conf
bind_interfaces
interface=wlan0
dhcp-range=10.11.12.13,10.11.12.32,255.255.255.0,12h # increase if you need more
dhcp-option=option:router,10.11.12.1
dhcp-option=option:dns-server,192.168.1.1
EOF
Now we need to create a service file to start the dnsmasq service
Copy and paste the lines below on the terminal and hit enter:
cat <<EOF | sudo tee /etc/systemd/system/hostapd_dnsmasq.service
[Unit]
Description=DHCP and DNS caching server for wlan1.
After=network.target
[Service]
ExecStartPre=/sbin/iptables -t nat -A POSTROUTING ! -o wlan0 -j MASQUERADE
ExecStart=/usr/sbin/dnsmasq -k --conf-file=/etc/hostapd_dnsmasq.conf
ExecStopPost=/sbin/iptables -t nat -D POSTROUTING ! -o wlan0 -j MASQUERADE
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=300
[Install]
WantedBy=multi-user.target
EOF
Now that all bits and pieces are in place, let's start the service:
sudo systemctl restart hostapd_dnsmasq
I always use restart
even though the service is stopped. This is my best practice.
If all is working as intended, then you can enable the service so it starts automatically on Laptop reboot:
sudo systemctl enable hostapd_dnsmasq
Enjoy!
NOTE
: If your Laptop does not have an RJ45 port, then your only option is to use multiple WiFi dongles and definitely a different SSID for the new WiFi AP. The configuration will be similar; the main difference being that you will not be extending your current WiFi but rather creating a new one.
CAVEAT EMPTOR
I have a similar setting that I occasionally utilise when I'm testing. If you break anything on your system, you alone are responsible for the breakage and you get to keep all the pieces.