Raspberry Pi

From miki
Revision as of 11:28, 29 April 2022 by Mip (talk | contribs) (→‎Setup Pi as WiFi HotSpot (bridge))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

References

Install Raspbian
  • Use etcher to write Raspbian image on disk.
  • Boot image as is. Do not expand image! This will be done at first boot.
Find the pi
ping raspberrypi.local
Enable SSH
  • Mount SD card
  • Create a file ssh on boot partition [1]:
touch /media/*/boot/ssh
1st login
  • User pi.
  • Password raspberry.
  • Sudo access without password.
Configure
sudo raspi-config
Repositories

Network

Detect all Raspberry Pi on local network

From [2].

sudo nmap -sP 192.168.1.0/24 | awk '/^Nmap/{ip=$NF}/B8:27:EB/{print ip}'

where 192.168.1.* will be your local network mask.

We can also simply look for string Raspberry since nmap correctly identifies pi mac address:

sudo nmap -sP 192.168.1.0/24 | grep -B2 Raspberry

where 192.168.1.* will be your local network mask.

Raspberry in BRIDGE mode
Methods above fail if Raspberry is setup to bridge wlan0-eth0 connections. In that case, the visible MAC address is the one of the wlan0 interface. It can be detected because the address will be reported twice:
sudo nmap -sP 172.19.2.0/23|sort|uniq -d
# Host is up (0.18s latency).
# MAC Address: 00:0F:13:05:46:C2 (Nisca)              <-- MAC address of the Pi in bridge mode
# MAC Address: 80:86:F2:99:56:B5 (Intel Corporate)
Now we can repeat the request above to get the IP address:
sudo nmap -sP 172.19.2.0/23 | awk '/^Nmap/{ip=$NF}/00:0F:13/{print ip}'

Setup WiFi

Reference:

The quickest way is to use raspi-config tool

sudo raspi-config

Otherwise, setting up WiFi is quite easy.

  • First get the WiFi network details:
sudo iwlist wlan0 scan
# wlan0     Scan completed :
#           Cell 01 - Address: A1:B2:C3:D4:E5:F6
#                     Channel:6
#                     Frequency:2.437 GHz (Channel 6)
#                     Quality=69/70  Signal level=-41 dBm  
#                     Encryption key:off
#                     ESSID:"testing"
#                     Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
#                               24 Mb/s; 36 Mb/s; 54 Mb/s
#                     Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s
  • Create a new network entry in /etc/wpa_supplicant/wpa_supplicant.conf
network={
    ssid="testing"
    psk="testingPassword"
}
Better yet, use wpa_passphrase to generate an encrypted PSK:
wpa_passphrase testing >> sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null
sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
# network={
#     ssid="testing"
#     #psk="testingPassword"            <-- REMOVE THIS LINE!!!
#     psk=131e1e221f6e06e3911a2d11ff2fac9182665c004de85300f9cac208a6a80531
# }
Remove the password from the output produced by wpa_passphrase for extra security.
  • On Raspberry Pi 3 Model B+, set the country code in /etc/wpa_supplicant/wpa_supplicant.conf
country=BE
  • Reconfigure the interface
sudo wpa_cli -i wlan0 reconfigure
  • Check interface configuration
ip addr show dev wlan0

It is also possible to

  • Configure multiple WiFi networks.
  • Set a priority, eg. priority=1.

Setup Pi as WiFi HotSpot (router)

This setup the Pi has a WiFi access point (AP) in router mode (NAT).

References:

Setup Pi as WiFi HotSpot (bridge)

(Updated for Debian Jessie - Raspberry Pi 3+ - See history for old method)

References
Install necessary packages
sudo apt install hostapd hostap-utils bridge-utils
Configure eth0 / wlan0 / br0 interfaces
  • Content of /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

allow-auto lo
iface lo inet loopback
  • Content of /etc/network/interfaces.d/eth0
# interfaces(5) file used by ifup(8) and ifdown(8)

allow-hotplug eth0
iface eth0 inet dhcp
  • Content of /etc/network/interfaces.d/wlan0
# interfaces(5) file used by ifup(8) and ifdown(8)

allow-hotplug wlan0
iface wlan0 inet manual
  • Content of /etc/network/interfaces.d/br0
# interfaces(5) file used by ifup(8) and ifdown(8)
 
allow-auto br0
iface br0 inet dhcp
        bridge_fd 1
        bridge_hello 3
        bridge_maxage 10
        bridge_stp off
        bridge_ports eth0 wlan0
  • Restart network
sudo service networking restart   # or
sudo systemctl restart networking

# Check new interfaces
ifconfig    # or
ip addr
Configure hostapd
  • Enable conf file, /etc/default/hostapd:
-#DAEMON_CONF=""
+DAEMON_CONF="/etc/hostapd/hostapd.conf"
  • Copy default configuration file
sudo cp /usr/share/doc/hostapd/examples/hostapd.conf.gz /etc/hostapd/
cd /etc/hostapd/
sudo gunzip hostapd.conf.gz
  • Set the following variables
interface=wlan0
bridge=br0
ssid=<SSIDHERE>
country_code=BE
channel=4
ignore_broadcast_ssid=1           # SSID will not be broadcasted
wpa=2
wpa_passphrase=<PASSPHRASE_HERE>
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
  • Enable ipv4 forwarding
sudo sysctl -w net.ipv4.ip_forward=1
sudo vi /etc/sysctl.conf
# net.ipv4.ip_forward=1
  • Restart hostapd
sudo systemctl restart hostapd

Disable HDMI CEC / Anynet+

Add to /boot/config.txt [3]:

hdmi_ignore_cec=1

Troubleshoot

403 Forbidden when apt-get upgrade

From [4]:

Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main libc-dev-bin armhf 2.13-38+rpi2+deb7u8           
  403  Forbidden
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main libc6-dev armhf 2.13-38+rpi2+deb7u8              
  403  Forbidden

Edit /etc/apt/apt.conf:

-deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi
+#deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi
+deb http://archive.raspbian.org/raspbian wheezy main contrib non-free
+deb-src http://archive.raspbian.org/raspbian wheezy main contrib non-free