Angefangen hat alles mit Ferien im Ausland (Deutschland). Wir hatten zwar kostenloses Internet, aber der Zugang war doch sehr mühsam (Zugänge für jedes Gerät eines, andauernd wieder anmelden). Da kam mir die Idee, wieso nicht mit einem Raspberry Pi einen Access Point erstellen mit der selben SSID / Passphrase wie zuhause und NAT (Network Address Translation)?
Vorteil:
- Nur einmal anmelden
- Alle Geräte, welche schon zuhause mit WLAN funktionieren, tun das auch unterwegs
- Nur der erste muss sich anmelden und alle Geräte nutzen denselben Zugang
Natürlich lässt sich die Idee noch erweitern:
- Weitere SSID für Gäste
- OpenVPN nach Hause
- Anschluss Mobile-USB
Wichtig: Aus Sicherheitsgründen muss ich in diesem Artikel alle Pfade verfremden. /_ entspricht in jedem Fall dem Wurzelverzeichnis /!
Installation von Raspian
Holt euch das Image von der Raspberry Pi Seite (am besten das Lite) und installiert es auf einer SD-Karte/microSD-Karte. Es gibt nun 2 Vorgehensweisen:
Variante 1: HDMI / Keyboard
- Steckt die (micro)SD-Karte in den Raspberry Pi, schliesst eine USB-Tastatur, die Stromversorgung und via HDMI ein Monitor/Fernseher an
- Wartet bis das "login:" erscheint
- Anmelden als Benutzer pi (Standard-Passwort = raspberry)
Variante 2: SSH
- Erstellt auf der (micro)SD-Karte die Datei ssh (ohne Erweiterung)
- Steckt die (micro)SD-Karte in den Raspberry Pi, schliesst die Stromversorgung und das Netzwerkkabel an
- Ca. 30s warten
- Such mit einem Netzwerk-Scanner (ich nutze Fing auf Android) nach dem raspberry und der IP-Adresse
- Anmelden als Benutzer pi (Standard-Passwort = raspberry) mit "ssh pi@127.0.0.1" (127.0.0.1 solltet ihr mit der gefunden IP-Adresse ersetzen)
Passwort ändern
Das Standard-Passwort ist ALLEN bekannt und dies wird bereits aktiv ausgenutzt! Darum Passwort ändern, bevor das Gerät direkt vom Internet erreichbar ist.
- Im Terminal den Befehl "sudo -s" eintippen und ihr wechselt in den Administrator-Modus
- Mit dem Befehl "passwd pi" das neue Passwort setzen
Raspbian aktualisieren
Ihr solltet euer Raspbian immer aktuell halten. Verwundbare IoT-Geräte sind eine Plage, schaut das euer Raspberry Pi nicht dazugehört.
- Im Terminal den Befehl "sudo -s" eintippen und ihr wechselt in den Administrator-Modus
- Der Befehl "apt-get update ; apt-get upgrade" aktualisiert euer Raspbian
Aufsetzen des Access-Points
Dies ist eine vereinfachte Übersetzung von diesem Artikel.
Wireless LAN statisch konfigurieren
Die Datei /_etc/dhcpcd.conf als Administrator (root) mit dem bevorzugten Editor (bei mir ist das vi) öffnen und am Ende folgende Zeile hinzufügen: "denyinterfaces wlan0". Danach speichern und schliessen.
Die Datei /_etc/network/interfaces öffnen und die Zeilen zu wlan0 ersetzen mit:
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
Mit den folgenden Befehlen die neue Konfigurationen laden: "service dhcpcd restart" und "ifdown wlan0; ifup wlan0"
Access-Point installieren & konfigurieren
Mit dem Befehl "apt-get install dnsmasq hostapd" die benötigte Software installieren.
Hostapd konfigurieren
Die Datei /_etc/hostapd/hostapd.conf als Administrator (root) mit dem bevorzugten Editor öffnen und folgenden Text hinzufügen:
# 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 # Require clients to know the network name ignore_broadcast_ssid=0 # Use WPA2 wpa=2 # Use a pre-shared key wpa_key_mgmt=WPA-PSK # The network passphrase wpa_passphrase=raspberry # Use AES, instead of TKIP rsn_pairwise=CCMP
Bitte die Zeilen ssid & wpa_passphrase anpassen!
Die Datei /_etc/default/hostapd editieren und die soeben neu erstellte Datei referenzieren: DAEMON_CONF="/_etc/hostapd/hostapd.conf"
Dnsmasq konfigurieren
Die originale Konfigurationsdatei von dnsmasq sichern mit: mv /_etc/dnsmasq.conf /_etc/dnsmasq.conf.orig
Danach eine neue Konfigurationsdatei /_etc/dnsmasq.conf mit folgendem Text erstellen:
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
IP-Weiterleitung konfigurieren
In der Datei /_etc/sysctl.conf die Zeile aktivieren (# entfernen) damit nach einem Neustart IP-Pakete weitergeleitet werden können: net.ipv4.ip_forward=1
Damit jetzt kein Neustart nötig ist aktiviert der folgende Befehl diese Verhalten umgehend: echo 1 > /_proc/sys/net/ipv4/ip_forward
NAT konfigurieren
Die folgenden Befehle konfigurieren das NAT und speichern das Ergebnis in eine Textdatei:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
iptables-save > /_etc/iptables.ipv4.nat
Laden der neuen Textdatei währen des Boot-Vorgangs. Editiere die Date /_etc/rc.local und füge folgende Zeile vor dem exit hinzu: iptables-restore < /_etc/iptables.ipv4.nat
Services starten
Mit den folgenden Befehlen werden die 2 neuen Services gestartet:
service hostapd start
service dnsmasq start
GESCHAFFT, Der AccessPoint sollte nun funktionieren. Gleich ausprobieren!
Netzwerk-Timeout ausser Kraft setzen
Mit dem Befehl "crontab -e" folgende Zeile einfügen "* * * * * ping -c 1 8.8.8.8 > /dev/null". Dieser Eintrag veranlasst Deinen Raspberry PI jede Minute einen einzigen Ping an den Google-DNS-Server (8.8.8.8) zu senden.
Automatisch USB-Disks/-Sticks als Share freigeben
Mit dem Befehl "apt-get install samba usbmount" die benötigte Software installieren.
Samba konfigurieren
Die Datei /_etc/samba/smb.conf als Administrator (root) im bevorzugten Editor öffnen und folgende Zeilen am Ende von [global] hinzufügen:
include = /_etc/samba/usb.conf.d/usb0.conf
include = /_etc/samba/usb.conf.d/usb1.conf
include = /_etc/samba/usb.conf.d/usb2.conf
include = /_etc/samba/usb.conf.d/usb3.conf
include = /_etc/samba/usb.conf.d/usb4.conf
include = /_etc/samba/usb.conf.d/usb5.conf
include = /_etc/samba/usb.conf.d/usb6.conf
include = /_etc/samba/usb.conf.d/usb7.conf
Nun diese Dateien erstellen (leer) damit es beim neu starten keine Fehler gibt:
mkdir /_etc/samba/usb.conf.d
touch /_etc/samba/usb.conf.d/usb0.conf
touch /_etc/samba/usb.conf.d/usb1.conf
touch /_etc/samba/usb.conf.d/usb2.conf
touch /_etc/samba/usb.conf.d/usb3.conf
touch /_etc/samba/usb.conf.d/usb4.conf
touch /_etc/samba/usb.conf.d/usb5.conf
touch /_etc/samba/usb.conf.d/usb6.conf
touch /_etc/samba/usb.conf.d/usb7.conf
Nun den Service neu starten:
service smbd stop
service smbd start
Usbmount konfigurieren
Die Datei /_etc/usbmount/mount.d/samba_rw_all editieren und folgenden Text hinzufügen:
#!/bin/sh # This script shares a connected device using samba. # Copyright (C) 2016 Norbert Kuemin # # This file is free software; the copyright holder gives unlimited # permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # # This file is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY, to the extent permitted by law; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. # set -e #UM_DEVICE - filename of the device node #UM_MOUNTPOINT - mointpoint #UM_FILESYSTEM - filesystem type #UM_MOUNTOPTIONS - mount options that have been passed to the mount command #UM_VENDOR - vendor of the device (empty if unknown) #UM_MODEL - model name of the device (empty if unknown) #UM_UUID - UUID of the device # Replace spaces with underscores, remove special characters in vendor # and model name.
SMB_DIR=/_etc/samba/usb.conf.d BASEMOUNT=`basename $UM_MOUNTPOINT` SHAREINFOFILE=${UM_MOUNTPOINT}/sharename.info if [ -r $SHAREINFOFILE ] then SHARENAME=$(cat $SHAREINFOFILE | sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]//g') else SHARENAME="$(blkid -s UUID -o value ${UM_DEVICE})" fi cat > $SMB_DIR/$BASEMOUNT.conf <<EOF [$SHARENAME] comment = $UM_VENDOR $UM_MODEL path = $UM_MOUNTPOINT browsable = yes writable = yes read only = no guest only = yes create mask = 0644 directory mask = 0755 force user = root force group = root EOF /_usr/sbin/service smbd reload exit 0
Die Datei /_etc/usbmount/umount.d/samba_rw_all editieren und folgenden Text hinzufügen:
#!/bin/sh # This script shares a connected device using samba. # Copyright (C) 2016 Norbert Kuemin # # This file is free software; the copyright holder gives unlimited # permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # # This file is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY, to the extent permitted by law; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. # set -e #UM_DEVICE - filename of the device node #UM_MOUNTPOINT - mointpoint #UM_FILESYSTEM - filesystem type #UM_MOUNTOPTIONS - mount options that have been passed to the mount command #UM_VENDOR - vendor of the device (empty if unknown) #UM_MODEL - model name of the device (empty if unknown) # Replace spaces with underscores, remove special characters in vendor # and model name. #UM_VENDOR=`echo "$UM_VENDOR" | sed 's/ /_/g; s/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-]//g'` #UM_MODEL=`echo "$UM_MODEL" | sed 's/ /_/g; s/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-]//g'` SMB_DIR=/_etc/samba/usb.conf.d BASEMOUNT=`basename $UM_MOUNTPOINT` if [ -f $SMB_DIR/$BASEMOUNT.conf ] then rm -f $SMB_DIR/$BASEMOUNT.conf touch $SMB_DIR/$BASEMOUNT.conf fi /_usr/sbin/service smbd reload exit 0
Nun die beiden Dateien noch die Berechtigung zur Ausführng erteilen: chmod 755 /_etc/usbmount/*mount.d/samba_rw_all