[UPDATED!] USING YOUR NEW RASPBERRY PI 3 AS A WIFI ACCESS POINT WITH HOSTAPD

来源:互联网 发布:数据库管理系统培训 编辑:程序博客网 时间:2024/06/05 03:02

转自:https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/



There's a new Raspberry Pi. This is exciting. It also has on-board WiFi. This makes it doubly exciting!

One of my first thoughts was, can I use it as a SoftAP for some ESP8266 sensor nodes? As it turns out, you can, and it's not that difficult, as the BCM43438 chip is supported by the open-source brcmfmac driver!

PACKAGES

The first step is to install the required packages: sudo apt-get install dnsmasq hostapd

I'll go into a little detail about the two:

  • hostapd - This is the package that allows you to use the built in WiFi as an access point
  • dnsmasq - This is a combined DHCP and DNS server that's very easy to configure

If you want something a little more 'heavyweight', you can use the isc-dhcp-server and bind9 packages for DHCP and DNS respectively, but for our purposes, dnsmasq works just fine.

CONFIGURE YOUR INTERFACES

The first thing you'll need to do is to configure your wlan0 interface with a static IP.

If you're connected to the Pi via WiFi, connect via ethernet/serial/keyboard first.

In newer Raspian versions, interface configuration is handled by dhcpcd by default. We need to tell it to ignore wlan0, as we will be configuring it with a static IP address elsewhere. So open up the dhcpcd configuration file with sudo nano /etc/dhcpcd.conf and add the following line to the bottom of the file:

denyinterfaces wlan0  

Note: This must be ABOVE any interface lines you may have added!

Now we need to configure our static IP. To do this open up the interface configuration file with sudo nano /etc/network/interfaces and edit the wlan0section so that it looks like this:

allow-hotplug wlan0  iface wlan0 inet static      address 172.24.1.1    netmask 255.255.255.0    network 172.24.1.0    broadcast 172.24.1.255#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Restart dhcpcd with sudo service dhcpcd restart and then reload the configuration for wlan0 with sudo ifdown wlan0; sudo ifup wlan0.

CONFIGURE HOSTAPD

Next, we need to configure hostapd. Create a new configuration file with sudo nano /etc/hostapd/hostapd.conf with the following contents:

# This is the name of the WiFi interface we configured aboveinterface=wlan0# Use the nl80211 driver with the brcmfmac driverdriver=nl80211# This is the name of the networkssid=Pi3-AP# Use the 2.4GHz bandhw_mode=g# Use channel 6channel=6# Enable 802.11nieee80211n=1# Enable WMMwmm_enabled=1# Enable 40MHz channels with 20ns guard intervalht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]# Accept all MAC addressesmacaddr_acl=0# Use WPA authenticationauth_algs=1# Require clients to know the network nameignore_broadcast_ssid=0# Use WPA2wpa=2# Use a pre-shared keywpa_key_mgmt=WPA-PSK# The network passphrasewpa_passphrase=raspberry# Use AES, instead of TKIPrsn_pairwise=CCMP

We can check if it's working at this stage by running sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf. If it's all gone well thus far, you should be able to see to the network Pi3-AP! If you try connecting to it, you will see some output from the Pi, but you won't receive and IP address until we set up dnsmasq in the next step. Use Ctrl+C to stop it.

We aren't quite done yet, because we also need to tell hostapd where to look for the config file when it starts up on boot. Open up the default configuration file with sudo nano /etc/default/hostapd and find the line #DAEMON_CONF="" and replace it with DAEMON_CONF="/etc/hostapd/hostapd.conf".

CONFIGURE DNSMASQ

The shipped dnsmasq config file contains a wealth of information on how to use it, but the majority of it is largely redundant for our purposes. I'd advise moving it (rather than deleting it), and creating a new one with

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig  sudo nano /etc/dnsmasq.conf  

Paste the following into the new file:

interface=wlan0      # Use interface wlan0  listen-address=172.24.1.1 # Explicitly specify the address to listen on  bind-interfaces      # Bind to the interface to make sure we aren't sending things elsewhere  server=8.8.8.8       # Forward DNS requests to Google DNS  domain-needed        # Don't forward short names  bogus-priv           # Never forward addresses in the non-routed address spaces.  dhcp-range=172.24.1.50,172.24.1.150,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time  

SET UP IPV4 FORWARDING

One of the last things that we need to do before we send traffic anywhere is to enable packet forwarding.

To do this, open up the sysctl.conf file with sudo nano /etc/sysctl.conf, and remove the # from the beginning of the line containing net.ipv4.ip_forward=1. This will enable it on the next reboot, but because we are impatient, activate it immediately with : 
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

We also need to share our Pi's internet connection to our devices connected over WiFi by the configuring a NAT between our wlan0 interface and our eth0interface. We can do this using the following commands:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE  sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT  sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT  

However, we need these rules to be applied every time we reboot the Pi, so run sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" to save the rules to the file /etc/iptables.ipv4.nat. Now we need to run this after each reboot, so open the rc.local file with sudo nano /etc/rc.local and just above the line exit 0, add the following line:

iptables-restore < /etc/iptables.ipv4.nat  

WE'RE ALMOST THERE!

Now we just need to start our services:

sudo service hostapd start  sudo service dnsmasq start  

And that's it! You should now be able to connect to the internet through your Pi, via the on-board WiFi!

To double check we have got everything configured correctly, reboot with sudo reboot.

EDIT: Thanks to Justin for helping iron out some of the errors in this post!

EDIT2: Thanks to Ashok for several performance related enhancements!

EDIT3: Thanks to Lasse for some amendments to the dnsmasq configuration!

EDIT4: Fixed race condition between dhcpcd and dnsmasq, wlan0 is no longer configured by dhcpcd.

PHIL MARTIN

Read more posts by this author.

SHARE THIS POST

 

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 康佳服务支持平台 康佳净水器多少钱 康佳电视无法开机 康佳电视升级包下载 康佳电视智能遥控器 康佳智能电视价格 康佳电视自动关机 康佳42寸液晶电视报价 康佳双开门冰箱价格 康佳电视led40x9600uf 康佳电视系统升级 康佳电视65寸 康佳液晶电视55寸 康佳电视led50m6180af 康佳投影手机 康佳电视led50x1200af konka手机 konka手机多少钱 komka knoka konka konka电视 康信 康健 康健助行器 康健陶瓷 康健生态木 如今康健 康健坐便椅 汤臣康健 康健瓷砖怎么样 康健外国语实验中学 安享康健 爱康健 爱康健齿科价目表 负氧离子康健仪 身体康健 康健妇婴医院 负离子康健仪 远盟康健 新华康健吉顺