Conveniently switch wifi to access point mode

Based on articles ( one , two ).
I know that on the hub and in google this topic has been discussed more than once, however, when I decided to make my access point “for friends” on manuals, I ran into certain difficulties, and the ready-made solutions turned out to be damp. Therefore, I suggest that inexperienced GNU / Linux users do this the way I did.

Distributing wi-fi from a 3g modem (and from a wired Internet) when there is nothing else at hand is a noble cause, so we will go along the quick way to get profit. The instruction is intended for debian-based distributions. We need hostapd - actually for distributing wi-fi, dnsmasq - for distributing ip-addresses and notify-send (not necessary) - for notifications. iptables is currently out of the box. We put hostapd and stop it:

aptitude install hostapd
service hostapd stop


In the / etc / default / hostapd file, uncomment and fix the line:

DAEMON_CONF="/etc/hostapd/hostapd.conf"


Create and edit the file /etc/hostapd/hostapd.conf

interface=wlan0
driver=nl80211
ssid=wifi_4_friends
hw_mode=g
channel=6
wpa=2
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0


Everything is simple here - the name of the access point, password, the channel on which the driver will work.
We put dnsmasq and stop it:

aptitude install dnsmasq
service dnsmasq stop


dnsmasq is good because it has everything and is easy to configure. Open the configuration file /etc/dnsmasq.conf:

interface=wlan0
dhcp-range=192.168.2.2,192.168.2.100,12h


Everything is extremely simple here, but if necessary, you can add an alternative dns server, and you can also force hosts to be sent to 127.0.0.1, thereby blocking them. See the man dnsmasq help for details. One more thing, dhcp-range addresses must be on the same network as wlan0. if for some reason you are not forced to change the ip address for wlan0 in the startup script, then specify the pool here as in wlan0. For example, at home there is a router with the address 192.168.1.1 and a network 192.168.1.0/24, then dhcp-range must be specified within this space, and also so that it does not overlap with the pool of addresses issued by the router's dhcp server. We will go in a simpler way and specify a different subnet ourselves.

Now turn off daemon startup:

update-rc.d hostapd disable
update-rc.d dnsmasq disable


All that remains is to enable / disable routing and add / remove a rule from iptables

sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE


We will do this automatically using a script. Everything is ready, and here is the wifi-ap script itself:

#!/bin/bash
#script to start/stop hostapd, dnsmasq, add/remove iptables rule
set -e
exec 3>&1
exec 2>&1 >> /tmp/wifi-ap
function print_help(){
	echo "Start/Stop Software Access Point"
	echo
	echo "Usage `basename $0` options..."
	echo "wifi-ap on to start Software AP"
	echo "wifi-ap off to stop Software AP"
	echo
	echo "log-file - /tmp/wifi-ap"
	echo
}
if [ $# = 0 ]; then
	print_help >&3
		exit 0
fi
if [ $1 = on ]; then
		ifconfig wlan0 192.168.2.1
		service dnsmasq start
		sysctl net.ipv4.ip_forward=1
		iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
		service hostapd start
		notify-send --expire-time=4000 "Software Access Point" "start"
	exit 0
fi
if [ $1 = off ]; then
		service dnsmasq stop
		service hostapd stop
		ifconfig wlan0 192.168.1.4
		sysctl net.ipv4.ip_forward=0
		iptables -D POSTROUTING -t nat -o ppp0 -j MASQUERADE
		notify-send --expire-time=4000 "Software Access Point" "stop"
	exit 0
fi


It takes 2 parameters, on and off. You can easily adjust it for yourself and, if necessary, replace the ppp0 interface with eth0 (or another, at your discretion).

I am a debian user and do not use sudo, but you may need it.

Have a nice use.

Also popular now: