11 KiB
Configuring Wi-Fi
The Raspberry Pi Wi-Fi adapter of has two main operating modes:
-
Client mode – RPi connects to an existing Wi-Fi network.
-
Access point mode – RPi creates a Wi-Fi network that you can connect to.
When using the RPi image, the Wi-Fi adapter works in the [access point mode] by default (Wi-Fi.md).
Changing the password or SSID (of the network name)
-
Edit file
/etc/wpa_supplicant/wpa_supplicant.conf(using SSH connection):sudo nano /etc/wpa_supplicant/wpa_supplicant.confTo change the name of the Wi-Fi network, change the value of parameter
ssid, to change the password, change parameterpsk. For example:network={ ssid="my-super-ssid" psk="cleverwifi123" mode=2 proto=RSN key_mgmt=WPA-PSK pairwise=CCMP group=CCMP auth_alg=OPEN } -
Restart Raspberry Pi.
Caution
The length of the password for the Wi-Fi network should be at least 8 characters.
In case of incorrect settings
wpa_supplicant.conf, Raspberry Pi will stop distributing Wi-Fi!
Switching adapter to the client mode
-
Disable the
dnsmasqservice.sudo systemctl stop dnsmasq sudo systemctl disable dnsmasq -
Enable obtaining IP address on the wireless interface by the DHCP client.
For this purpose, remove the following lines
interface wlan0 static ip_address=192.168.11.1/24from file
/etc/dhcpcd.confmanually, or run the following commands.sudo sed -i 's/interface wlan0//' /etc/dhcpcd.conf sudo sed -i 's/static ip_address=192.168.11.1\/24//' /etc/dhcpcd.conf -
Configure
wpa_supplicantto connect to an existing access point.cat << EOF | sudo tee /etc/wpa_supplicant/wpa_supplicant.conf ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=GB network={ ssid="CLEVER" psk="cleverwifi" } EOFwhere
CLEVERis the name of the network, andcleverwifiis the password. -
Restart the
dhcpcdservice.sudo systemctl restart dhcpcd
Switching the adapter to the access point mode
-
Enable the static IP address in the wireless interface.
For this purpose, add the following lines
interface wlan0 static ip_address=192.168.11.1/24to file
/etc/dhcpcd.confmanually, or run the following command.cat << EOF | sudo tee -a /etc/dhcpcd.conf interface wlan0 static ip_address=192.168.11.1/24 EOF -
Configure wpa_supplicant to work in the access point mode.
cat << EOF | sudo tee /etc/wpa_supplicant/wpa_supplicant.conf ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=GB network={ ssid="CLEVER-$(head -c 100 /dev/urandom | xxd -ps -c 100 | sed -e 's/[^0-9]//g' | cut -c 1-4)" psk="cleverwifi" mode=2 proto=RSN key_mgmt=WPA-PSK pairwise=CCMP group=CCMP auth_alg=OPEN } EOF -
Restart the
dhcpcdservice.sudo systemctl restart dhcpcd -
Enable the
dnsmasqservice.sudo systemctl enable dnsmasq sudo systemctl start dnsmasq
Below you can read more about how RPi networking is organized.
RPi network organization
Network operation in the image is supported by two pre-installed services:
- networking — the service enables all network interfaces at the moment of start [5].
- dhcpcd — the service ensures configuration of addressing and routing on the interfaces obtained dynamically, or specified statically in the config file.
To work in the router (access point) mode, RPi requires a DHCP server. It is used to automatically send the settings of the current network to connected clients. isc-dhcp-server or dnsmasq may be used as such server.
dhcpcd
Starting with Raspbian Jessy, network settings are no longer defined in the /etc/network/interfaces file. Now dhcpcd is used for sending addressing and routing settings[4].
By default, a dhcp client is enabled in all interfaces. Settings of the interfaces are changed in the /etc/dhcpcd.conf file. To start an access point, specify a static IP address. To do this, add the following lines to the end of the file:
interface wlan0
static ip_address=192.168.11.1/24
Note
If the interface is wireless (wlan), the
dhcpcdservice triggerswpa_supplicant[13], which in turn works directly with the Wi-Fi adapter, and sets it to the specified state.
wpa_supplicant
wpa_supplicant — the service configures the Wi-Fi adapter. The wpa_supplicant service runs not as standalone (although it exists as such), and runs as a dhcpcd child process.
By default, the config file should contain path /etc/wpa_supplicant/wpa_supplicant.conf.
An example of the configuration file:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB
network={
ssid=\"CLEVER-SMIRNOV\"
psk=\"cleverwifi\"
mode=2
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
group=CCMP
auth_alg=OPEN
}
Inside the config file, general wpa_supplicant settings, and the settings for the adapter configuration are specified. The configuration file also contains network section with the basic settings of the Wi-Fi network, such as network SSID, password, adapter operating mode. There may be several such sections, but only the first one is the working one. For example, if the first section contains connection to an unavailable network, the adapter will be configured according to a next valid section, if there is one. Read more about the syntax of wpa_supplicant.conf [TODO WIKI].
wpa_passphrase
wpa_passphrase — a utility for creating the network section.
wpa_passphrase SSID PASSWORD
After running the command, copy the resulting section to your config file. You may remove the commented field psk, and leave only the field with the password hash, or vice versa.
network={
ssid="SSID"
#psk="PASSWORD"
psk=c2161655c6ba444d8df94cbbf4e9c5c4c61fc37702b9c66ed37aee1545a5a333
}
Multiple Wi-Fi adapters
The system may use multiple Wi-Fi adapters. If drivers are properly connected to them, they may be viewed by calling ifconfig (e.g. wlan0 and wlan1).
If you have multiple adapters, the same working network section will be used for all of them. This is due to the fact that for each interface, dhcpcd separately creates a child wpa_supplicant process, which runs the same code ( since the config is the same).
To make multiple adapters work with individual settings, the mechanism for running different configuration scripts is implemented in the called standard dhcpcd script. To use it, rename the standard config file as follows: wpa_supplicant-<interface name>.conf, for example wpa_supplicant-wlan0.conf.
To apply the settings, restart the parent process — the dhcpcd service. This can be done by running the following command:
sudo systemctl restart dhcpcd
DHCP server
dnsmasq-base
dnsmasq-base — a command-line utility, which is not a service. To use dnsmasq as a service, install the dnsmasq package.
sudo apt install dnsmasq-base
# Calling dnsmasq-base
sudo dnsmasq --interface=wlan0 --address=/clever/coex/192.168.11.1 --no-daemon --dhcp-range=192.168.11.100,192.168.11.200,12h --no-hosts --filterwin2k --bogus-priv --domain-needed --quiet-dhcp6 --log-queries
# More about dnsmasq-base
dnsmasq --help
# or
man dnsmasq
dnsmasq
sudo apt install dnsmasq
cat << EOF | sudo tee -a /etc/dnsmasq.conf
interface=wlan0
address=/clever/coex/192.168.11.1
dhcp-range=192.168.11.100,192.168.11.200,12h
no-hosts
filterwin2k
bogus-priv
domain-needed
quiet-dhcp6
EOF
isc-dhcp-server
sudo apt install isc-dhcp-server
# https://www.shellhacks.com/ru/sed-find-replace-string-in-file/
sed -i 's/INTERFACESv4=\"\"/INTERFACESv4=\"wlan0\"/' /etc/default/isc-dhcp-server
cat << EOF | sudo tee /etc/dhcp/dhcpd.conf
subnet 192.168.11.0 netmask 255.255.255.0 {
range 192.168.11.11 192.168.11.254;
#option domain-name-servers 8.8.8.8;
#option domain-name "rpi.local";
option routers 192.168.11.1;
option broadcast-address 192.168.11.255;
default-lease-time 600;
max-lease-time 7200;
}
EOF
cat << EOF | sudo tee /etc/network/if-up.d/isc-dhcp-server && sudo chmod +x /etc/network/if-up.d/isc-dhcp-server
#!/bin/sh
if [ "\$IFACE" = "--all" ];
then sleep 10 && systemctl start isc-dhcp-server.service &
fi
EOF
Links
- habr.com Linux WiFi from the command line with wpa_supplicant
- wiki.archlinux.org WPA supplicant (Russian)
- blog.hoxnox.com: WiFi access point with wpa_supplicant
- dmitrysnotes.ru: Raspberry Pi 3. Assigning a static IP addresses
- thegeekdiary.com: Linux OS Service ‘network’
- frillip.com: Using your new Raspberry Pi 3 as a Wi-Fi access point with hostapt (it also contains instructions for setting up forwarding for using RPi as an Internet gateway)
- habr.com: Configuring a ddns server on a GNU/Linux Debian 6 (Good article on configuring a ddns server based on
bindandisc-dhcp-server) - pro-gram.ru to: Setting up and configuring a DHCP server in Ubuntu 16.04. (setup isc-dhcp-server)
- expert-orda.ru: Configuring a DHCP server in Ubuntu (setup isc-dhcp-server)
- academicfox.com: A Raspberry Pi wireless access point (WiFi access point) (setting the routes, hostapd, isc-dhcp-server)
- weworkweplay.com: Automatically connect a Raspberry Pi to a Wifi network (Contains settings for creating an open access point)
- wiki.archlinux.org: WPA supplicant
- wiki.archlinux.org: dhcpcd (dhcpcd hook wpa_supplicant)