Raspberry Pi Knowledgebase

A collection of useful tips and tricks, implemented step by step but without explanation

Samba Setup

  1. Run sudo apt-get install samba samba-common-bin
  2. Run mkdir -m 1700 share
  3. Run sudo nano /etc/samba/smb.conf
    Add the following at the end of the file:
    [share]
    Comment = Shared folder on RPi
    Path = home/pi/share
    Browseable = yes
    Writeable = Yes
    create mask = 0700
    directory mask = 0700
    ; This line and the following may be omitted but are here
    ; commented out to keep them available for future use if needed
    ; Public = yes
    ; Guest ok = yes
    ; only guest = no
  4. Run sudo smbpasswd -a pi
    and enter the password you’ll use to access the share remotely
  5. Run sudo /etc/init.d/smbd restart

Basic SSH/SCP

To access the Pi on the network: ssh pi@.local

To transfer a file to the Pi: scp pi@.local:/

Advanced SSH

  1. On client:
    1. cd $HOME
    2. mkdir .ssh
    3. chmod 700 .ssh
    4. cd .ssh
    5. ssh-keygen -t rsa -f
    6. scp .pub @:/home//.ssh/.pub
    7. nano config
    8. Add the following on separate lines, no numbers, one space inset after first line:
      1. Host
      2. Hostname
      3. User
      4. IdentityFile ~/.ssh/
      5. StrictHostChecking yes
  2. On server:
    1. cd $HOME
    2. mkdir .ssh
    3. chmod 700 .ssh
    4. cd .ssh
    5. cat .pub >> authorized_keys
    6. sudo nano /etc/ssh/sshd_config
    7. Change #Password Authentication yes to PasswordAuthentication no
  3. On client:
    1. ssh

Note For iPad access (using Termius) generate the keys on the server, copy private key to iPad, set up Termius for key access, delete private key on server.

Change Default Shell

chsh -s $(which zsh)

Mount a USB Disk at the Command Line

  1. Connect disk then run: sudo fdisk -l
    gives, for example, /dev/sda1 as the disk file reference
  2. Run sudo mkdir /media/usbdrive
  3. Run sudo chgrp -R users /media/usbdrive
  4. Run sudo chmod -R g+w /media/usbdrive
  5. Run sudo mount /dev/sda1 /media/usbdrive -o rw -t vfat

Note Steps 2-4 can be omitted if /media/usbdrive already exists

Mount a NAS Drive at the Command Line

  1. Run mkdir /home/pi/nas
  2. Run sudo mount -t cifs -o user=<your_nas_username>,password=<your_nas_password> //192.168.0.2/ /home/pi/nas
  3. Optional: mount drive at startup
    1. Run sudo nano /etc/fstab
    2. Add as a line at the end:

      //192.168.0.2/ /home/pi/nas cifs user=<your_nas_username>,password=<your_nas_password>,noauto,x-systemd.automount 0 0

Version Info

  • For Linux version, run: cat /proc/version
  • For the Pi’s board version, run: cat /proc/cpuinfo

Disable internal Bluetooth

  1. Run sudo nano /boot/config.txt
  2. Add to the end of the file: dtoverlay=pi3-disable-bt
  3. Run sudo systemctl disable hciuart.service
  4. Run sudo systemctl disable bluealsa.service
  5. Run sudo systemctl disable bluetooth.service
  6. Run sudo shutdown -r now

Set up WiFi at the Command Line

  1. Run sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
  2. At the end of the file add:
    network={
      ssid=""
      psk=""

      key_mgmt=WPA-PSK
    }
    Note Do not put spaces either side of the equals sign.
  3. To perform a scan to check WiFi operation, run sudo iwlist wlan0 scan

You can add multiple networks.

Use Dropbox

  1. Run git clone https://github.com/andreafabrizi/Dropbox-Uploader.git
  2. Run cd Dropbox-Uploader
  3. Run chmod +x dropbox_uploader.sh
  4. Go to https://developer.dropbox.com and sign in. Create an app then generate an app token. Paste this into dropbox_uploader.sh when requested (on first run).
  5. To upload an item, run ./dropbox_uploader.sh upload
    To delete an item, run ./dropbox_uploader.sh delete

Script source: https://github.com/andreafabrizi/Dropbox-Uploader

Node.js Setup

For Pi 3 B+

  1. Run curl -sL https://deb.nodesource.com/setup_{version_number}.x | sudo -E bash -
  2. Run sudo apt install -y nodejs

Further information: https://github.com/nodesource/distributions

For Pi Zero

Visit https://nodejs.org/dist/latest to find out which is the latest version (X.Y.Z), eg. https://nodejs.org/dist/v10.13.0/node-v10.13.0-linux-armv6l.tar.gz

  1. mdkir tmp
  2. cd tmp
  3. wget https://nodejs.org/dist/vX.Y.Z/node-vX.Y.Z-linux-armv6l.tar.gz
  4. tar -xzf node-vX.Y.Z-linux-armv6l.tar.gz
  5. cd node-vX.Y.Z-linux-armv6l
  6. sudo cp -r * /usr/local/
  7. export PATH=$PATH:/usr/local/bin
  8. cd ~
  9. rm -r tmp

Back-up SD Card to Mac

Obtain the disk number of the SD card:

diskutil list
diskutil umountdisk /dev/disk1

Copy the SD contents to a file, piping via gzip to reduce the size:

sudo dd if=/dev/rdisk1 bs=1m | gzip > ~/Desktop/pi.gz

To restore, get the disk number using diskutil as above, then unmount the card:

diskutil umountdisk /dev/disk1

Restore from a local file, pi.gz:

RPi.GPIO Basics

To import the RPi.GPIO module:

import RPi.GPIO as GPIO

Specify which pin numbering system you are using — Raspberry Pi standard (GPIO.BOARD) or Broadcom specification (GPIO.BCM) — you are using using:

GPIO.setmode(GPIO.BOARD)

To silence multiple usage warnings:

GPIO.setwarnings(False)

Configure a pin (‘channel’ in RPi.GPIO parlance):

GPIO.setup(pin_number, GPIO.IN)
GPIO.setup(pin_number, GPIO.OUT)

To specify an initial value for an output pin:

GPIO.setup(pin_number, GPIO.OUT, initial=GPIO.HIGH)

Note Instead of one pin number, pass in an array of pin numbers.

To read the value of a GPIO pin:

pin_value = GPIO.input(pin_number)

To set the output state of a pin:

GPIO.output(pin_number, state)

where state can be 0 / GPIO.LOW / False, 1 / GPIO.HIGH / True, or a tuple of values when passing in an array of pin numbers.

To clean up any resources used:

GPIO.cleanup()

Specify a pin number (of array of pin numbers) for finer cleanup control.

VNC Virtual Desktop

To run a virtual desktop ad hoc from the command line:

vncserver

Options

To set the virtual desktop size:

-randr=<X>x<Y>

where X and Y are the screen width and height.

To stop the virtual desktop:

-kill :<n>

where n is the  desktop number (default: 1)

Start the Virtual Desktop at Boot

  1. sudo nano /etc/systemd/system/vncvirtualdesktop.service
  2. Add the script below.
  3. sudo systemctl enable vncvirtualdesktop.service
[Unit]
Description=Start VNC Server Virtual Desktop

[Service]
Type=oneshot
ExecStart=/bin/su pi -c /usr/bin/vncserver
ExecStop=/usr/bin/vncserver -kill :1
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target