Skip to content

Raspberry Pi 2 Node Setup

Christopher Guess edited this page Sep 4, 2017 · 37 revisions

0. Intro

This document provides step-by-step instructions on configuring a Raspberry Pi 2 into a Wi-Fi access point running a LDLN node. All setup is done remotely, no monitor, keyboard or mouse is required.

This list assumes you're using a Mac, something like OS X 10.8+. Using Windows or Linux shouldn't be a big difference.

A lot of these instructions are weird with the newest version of Rasbian based on Debian Stretch. Notes below where it should be slightly different.

1. Requirements

  • OS X Computer with Ethernet port
  • Micro SD card reader
  • Raspberry Pi 2
  • Wi-FI USB dongle (tested with RTL8192 and RT5370, which require different hostapd builds!)
  • Ethernet cable
  • Micro-USB power cable

1.0 Recommended Micro SD Cards

TODO

2. Initial Flash

  1. Download a fresh Rasbian image: https://downloads.raspberrypi.org/raspbian_lite_latest (note this wiki was previously written using http://downloads.raspberrypi.org/raspbian_latest but since then the lite version has been released)
  2. Download ApplePi-Baker: http://www.tweaking4all.com/downloads/raspberrypi/ApplePi-Baker.zip -- This will allow you to flash the image easily, there's a few different ways, this is the easiest
  3. Flash the image to the card using the IMG Recipe section of the ApplePi-Baker UI, do NOT use the NOOBS Recipe section

3. Connecting to the Pi to Configure

There a few ways to connect your computer to the Pi.

3.0 Connect to Pi through Ethernet

  1. Once card is flashed and mounted on the machine open the cmdline.txt file on the card

  2. Add ip=192.168.2.1 to the end of the list of commands (pay attention to newlines, as these commands are sensitive to where line breaks occur!)

  3. Save the file clicking yes to any weird warnings that pop up

  4. Since ssh is disabled by default, you must also create a file called ssh in the same directory on the card. The contents of this file does not matter, but it must exist in order for ssh to be toggled on. The system will remove this file on first boot. (see here for details)

  5. Assemble the Pi with the newly flashed card and Wi-Fi dongle

  6. Connect the Ethernet cable to your computer and the Pi

  7. Set your computer's Ethernet device to a static IP of 192.168.2.2

  8. Plug in and boot up the Pi

  9. On your computer open up the terminal

  10. SSH into the Pi with username pi and password raspberry

    ssh pi@192.168.2.1

Note: If you are having issues here, double-check your physical setup. It is surprisingly common to have a loose USB cable or ethernet adapter, and to become increasingly frustrated while myopically not understanding why software config isn't solving your hardware problem. ;)

3.0.1 Connect the Pi to a local Wi-Fi network

You only need to do this if you Pi is not already connected to the internet via ethernet

  1. sudo nano /etc/network/interfaces

  2. Replace the content of the file with the following (replacing the appropriate SSID and password):

     auto lo
     
     iface lo inet loopback
     allow-hotplug eth0
     iface eth0 inet dhcp
     
     allow-hotplug wlan0
     iface wlan0 inet dhcp
     wpa-ssid "YOUR_SSID"
     wpa-psk "YOUR_PASSWORD"
    
  3. sudo ifdown wlan0

  4. sudo ifup wlan0 --- If you cannot get an IP address assigned check the SSID and password again, this seems to be the main problem

  5. confirm you can see the wider Internet --- ping www.google.com

3.1 Shared connection through laptop tethering

Reason This is useful if you're tethering your laptop through a bluetooth or wired internet connection.

Method

  1. Connect mac to internet via some way that's not direct ethernet or Wi-Fi
  2. Turn on Wi-Fi internet sharing in System Preferences -> Sharing, make the network settings something pretty simple
  3. SSH to the Pi through ethernet using the steps above
  4. Set up the /etc/interface/networking file to connect to the Wi-Fi network your computer is creating
  5. Follow the steps laied out.

3.2 Direct Ethernet Connection To Router

If you can plug your Pi directly into a router with internet access, use this method

Reason Simpler setup and easier testing because you don't need to direct connect through your computer

Method

  1. Instead of connecting ethernet to your computer and the Pi, connect the Pi directly to an available ethernet port on your router
  2. Examine your router's DHCP table to determine which address the Pi has been assigned
  3. SSH in through the Pi's IP

Potential Issues

  • Public or workplace networks may firewall each individual device and you won't be able to SSH between the devices
  • If you don't have access to the DHCP logs you can't tell which IP the Pi has been assigned (unless you can plug in a monitor and log in directly)

4. Set Pi configuration Options

4.0 Change User Password

With all of the attention being paid to IoT (and how the "S" in that acronym stands for "security" hehe), it is in our best interest to change the default password. After ssh'ing into the pi:

  • sudo raspi-config
  • navigate to Change User Password
  • enter and confirm a new password
  • Choose Finish from the main menu, and reboot if you are done with all config

4.1 Hostname

If you have many devices connected to a network, it can be useful (but still optional) to change the hostname to something meaningful. After ssh'ing into the pi:

  • sudo raspi-config
  • navigate to Hostname
  • enter a hostname (e.g. LDLN_Base_Station_1)
  • Choose Finish from the main menu, and reboot if you are done with all config

4.2 Boot Options

The default boot options are not exactly optimized for LDLN. To update these, ssh into the pi, then:

  • sudo raspi-config
  • navigate to Boot Options
    • navigate to Desktop/CLI
      • choose Console Autologin
  • navigate to Boot Options
    • navigate to Wait for Network at Boot
      • choose No
  • Choose Finish from the main menu, and reboot if you are done with all config

4.3 Expand Filesystem

Fulfill all unused portion of the disk:

  • sudo raspi-config
  • Navigate to Advanced Options
    • Navigate to Expand Filesystem
  • Choose Finish from the main menu, and reboot if you are done with all config

OR Set a custom partition size: https://raspberrypi.stackexchange.com/questions/499/how-can-i-resize-my-root-partition

You might want to consider setting a custom size if you are creating a base image.

4.4 Revert To Old Network Style

Debian Stretched decided to make network device names dependent on MAC addresses. This is dumb unless you're running large servers with a lot of ethernet interfaces. Since everything breaks otherwise we change it back here.

  1. sudo nano /boot/cmdline.txt
  2. add net.ifnames=0 to the end of the top line
  3. sudo reboot now

5. Install Packages

5.0 Update apt and install dependencies

  1. sudo apt-get update (note if you're having issues with the mirror servers, see this post on how to update your apt-get servers to something more reliable; namely, change the url in /etc/apt/sources.list to one of these and add Google's DNS servers to /etc/resolv.conf)
  2. sudo apt-get install isc-dhcp-server libnss-mdns libssl-dev openssl

5.1 install hostapd from source

5.1.0 Download the right source of hostapd depending on your WiFi dongle

Follow only one of the three following blocks of instructions (a, b, or c) depending on your dongle.

5.1.0.a For Raspberry Pi 3 Built-In Wifi

Adopted from this: https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/

  1. sudo apt-get install hostapd
  2. sudo nano /etc/dhcpcd.conf
  3. Add to the bottom of the file (but ABOVE any 'INTERFACE' line): denyinterfaces wlan0
5.1.0.b For the RTL8192 Wifi Dongle
  1. Download hostapd: wget https://github.com/jenssegers/RTL8188-hostapd/archive/v1.1.tar.gz
    1. See all releases: https://github.com/jenssegers/RTL8188-hostapd/releases
  2. tar -zxvf v1.1.tar.gz
  3. cd RTL8188-hostapd-1.1/hostapd
5.1.0.c For the RT5370 Wifi Dongle (The 'Wi-Pi' from Element 14 is this)
  1. sudo apt-get install libnl-3-dev libnl-genl-3-dev
  2. Look for the latest build of hostapd at http://w1.fi/hostapd/ (which is 2.6 at the time of writing)
  3. wget http://w1.fi/releases/hostapd-x.y.z.tar.gz
  4. tar -xzvf hostapd-x.y.z.tar.gz
  5. cd hostapd-x.y.z/hostapd
  6. cp defconfig .config
  7. vi .config
  8. uncomment #CONFIG_DRIVER_NL80211=y
  9. uncomment #CONFIG_LIBNL32=y

5.2 Build the source, install, check configs

  1. sudo make
  2. sudo make install
  3. sudo nano /etc/init.d/hostapd to make sure DAEMON_SBIN is pointing to the correct binary (e.g. your make installed installation may have gone to /usr/local/bin/hostapd rather than the default of /usr/sbin, which will affect the daemon starting on boot!)

Useful tip for debugging hostapd config: http://unix.stackexchange.com/a/123515 Useful walkthroughs for working with additional dongles that don't fit the instructions above: https://wireless.wiki.kernel.org/en/users/Documentation/hostapd and http://askubuntu.com/questions/617973/fatal-error-netlink-genl-genl-h-no-such-file-or-directory/617991

5.3 Create /etc/default/hostapd to enable hostapd on boot

sudo nano /etc/default/hostapd

Put in:

RUN_DAEMON="yes"
DAEMON_CONF="/etc/hostapd/hostapd.conf"
DAEMON_OPTS="-dd"

5.4 Ensure it runs on boot:

sudo update-rc.d hostapd defaults
sudo update-rc.d hostapd enable

6. Configure the Access Point

  1. Edit the DHCPD config file
  2. sudo nano /etc/dhcp/dhcpd.conf
  3. comment out the following two lines --- option domain-name "example.org"; --- option domain-name-servers ns1.example.org, ns2.example.org;
  4. uncomment the following line --- #authoritative
  5. at the end of the file add the following
subnet 192.168.0.0 netmask 255.255.255.0 {
 range 192.168.0.10 192.168.0.150;
 option broadcast-address 192.168.0.255;
 option routers 192.168.0.1;
 default-lease-time 600;
 max-lease-time 7200;
 option domain-name "local-network";
 option domain-name-servers 8.8.8.8, 8.8.4.4;
}

7. Setup IPv4 Forwarding

  1. sudo nano /etc/default/isc-dhcp-server

  2. add line to the end of the file

       INTERFACESv4="wlan0"
    
  3. sudo nano /etc/sysctl.conf

  4. uncomment line starting with #net.ipv4.ip_forward=1

8. Configure Network Interface

  1. sudo ifdown wlan0
  2. sudo nano /etc/network/interfaces
  3. replace file with the following
auto lo

iface lo inet loopback
iface eth0 inet dhcp

post-up /sbin/iptables-restore < /etc/iptables-up.rules

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.1
netmask 255.255.255.0

For Raspberry Pi 3 you may try adding the following lines

network 192.168.0.0
broadcast 192.168.0.255

9. Configure Settings For Access Point Network

  1. sudo nano /etc/hostapd/hostapd.conf
  2. edit line starting with ssid= to ssid=LDLN
  3. edit line starting with wpa_passphrase= to wpa_passphrase=landline

If the hostapd.conf doesn't exist:

  1. sudo mkdir /etc/hostapd
  2. sudo nano /etc/hostapd/hostapd.conf
  3. replace it with the following
interface=wlan0
driver=nl80211
ssid=LDLN
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=My_Passphrase
wpa_key_mgmt=WPA-PSK
#wpa_pairwise=TKIP    # You better do not use this weak encryption (only used by $
rsn_pairwise=CCMP

For Raspberry Pi 3 it may work to look like this

# This is the name of the WiFi interface we configured above
interface=wlan0

# Use the nl80211 driver with the brcmfmac driver
driver=nl80211

# This is the name of the network
ssid=Pi3-AP

# Use the 2.4GHz band
hw_mode=g

# Use channel 6
channel=6

# Enable 802.11n
ieee80211n=1

# Enable WMM
wmm_enabled=1

# Enable 40MHz channels with 20ns guard interval
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]

# Accept all MAC addresses
macaddr_acl=0

# Use WPA authentication
auth_algs=1

# Use WPA2
wpa=2

# The network passphrase
wpa_passphrase=landline

Useful tip for debugging hostapd config: http://unix.stackexchange.com/a/123515

10. iptables Forwarding and Logging

10.0 Activate forwarding now

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo ifup wlan0

10.1 Turn the Pi into a router

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

10.2 Save the routing tables to disk

sudo su
iptables-save > /etc/iptables-up.rules
exit

10.3 Check the iptables

sudo su
iptables-save
exit

10.4 Make iptables persistant

sudo apt-get install iptables-persistent It will then ask you if you want to save current settings, say 'yes'

If you need to resave tables you can do this sudo netfilter-persistent save

11. Start Services

  1. sudo service isc-dhcp-server start
  2. sudo service hostapd start

The Wi-Fi network LDLN should now be visible if you look for it

12. Install LDLN Software Stack

See: https://github.com/LDLN/core/wiki/LDLN-Stack-Installation

13. Create Start Up Script

sudo nano /etc/init.d/access-point.sh

Put in:

#!/bin/bash
## BEGIN INIT INFO
# Provides: some_name
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start access point daemons at boot time
# Description: Enable service provided by daemon.
## END INIT INFO

# start access point - is this still needed?
# service hostapd start

# bring up the wi-fi, otherwise the dhcp server errors out
ifup wlan0

# bring up ethernet interface
ifup eth0

# start dhcp
service isc-dhcp-server start
  • Make executable and add to startup:

     sudo chmod +x /etc/init.d/access-point.sh
     sudo update-rc.d access-point.sh defaults
    
  • Test to ensure it works:

     sudo /etc/init.d/access-point.sh
    

14. Change Hostname

If you didn't already update the hostname in raspi-config in the earlier steps, you can do it here.

Replace raspberrypi with landline in these files:

sudo nano /etc/hosts
sudo nano /etc/hostname

Commit the changes:

sudo /etc/init.d/hostname.sh
sudo reboot

15. Final Cleanup

  1. Reboot -- sudo shutdown -r now

Future Updates For This File

  • Create instruction to download source files, copy them to the Pi and install locally. -- This will be a way to setup from a usb stick or a direct file copy so the Pi never needs to touch the internet itself

Notes

Sources

http://raspberrypihq.com/how-to-turn-a-raspberry-pi-into-a-wifi-router/

Clone this wiki locally

⌂ 换网址