Skip to content

Instantly share code, notes, and snippets.

@leandrofilipe
Last active June 20, 2023 12:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leandrofilipe/7420c7bd0d24e0fdb144e8fe755492ee to your computer and use it in GitHub Desktop.
Save leandrofilipe/7420c7bd0d24e0fdb144e8fe755492ee to your computer and use it in GitHub Desktop.
Raspberry Pi Headless Setup (and other things)

※ Raspberry Pi Headless Setup ※

Installation of Raspbian OS on SD card

  1. Download Raspbian image: https://www.raspberrypi.org/downloads/raspbian/.

  2. Unzip it (optional).

  3. Format SD card with SD Memory Card Formatter application (https://www.sdcard.org/downloads/formatter/).

  4. Open Etcher application (https://www.balena.io/etcher/)

  5. Select from hard drive the Raspbian .img or .zip file.

  6. Select the SD card.

  7. Click [Flash].


Enable SSH on a headless Raspberry Pi

  1. Place a file named ssh, without any extension, onto the boot partition of the SD card:

    $ cd /Volumes/boot
    $ touch ssh
  2. Eject card.


Set up wireless networking (for devices with WiFi)

  1. Create a file named wpa_supplicant.conf in the boot partition:

    $ cd /Volumes/boot
    $ nano
  2. Write the configuration for the wireless network(s):

    (file)

     country=GB
     ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
     update_config=1
    
     network={
     	    ssid=“ssid-name1”
     	    scan_ssid=0
     	    psk=encrypted-password
     	    priority = 2
     }
    
     network={
     	    ssid=“ssid-name2”
     	    scan_ssid=0
     	    psk=encrypted-password
     	    priority = 1
     }
    

    (end of file)

  3. Save and exit:

    [Ctrl + O]
    [Ctrl + X]

  4. Eject card.


Insert card on Raspberry Pi and power it on


Change hostname

  1. Change “raspberrypi” to the new hostname, save and exit:

    $ sudo nano /etc/hostname
  2. Find line starting with 127.0.0.1 and change “raspberrypi” to the new hostname, save and exit:

    $ sudo nano /etc/hosts
  3. Reboot

    $ sudo reboot

Change login password

  1. Change password:

    $ passwd

Set up SSH key pairs

Some info: https://medium.com/risan/upgrade-your-ssh-key-to-ed25519-c6e8d60d3c54
SSH supports several public key algorithms for authentication keys. The algorithm is selected using the -t option and key size using the -b option.

  • dsa - bad!

  • rsa - ok
    Key sizes: 768 (min), 4096 (recommended), 16384 (max)

  • ecdsa - good
    Key sizes: 256, 384, 521

  • ed25519 - best
    Key size: 256 (fixed)

To check all available SSH keys on the computer run:

for key in ~/.ssh/id_*; do ssh-keygen -l -f "${key}"; done | uniq

METHOD 1

  1. On local machine
    Use existing key in ~/.ssh or generate a new one with ssh-keygen:

    $ cd ~/.ssh
    $ ssh-keygen -t ed25519
  2. On local machine
    Install the SSH key on the Raspberry Pi as an authorized key with ssh-copy-id:

    $ ssh-copy-id -i ~/.ssh/<key> <user>@<host>

METHOD 2

  1. On local machine
    Use existing key in ~/.ssh or generate a new one with ssh-keygen:

    $ cd ~/.ssh
    $ ssh-keygen -t ed25519
  2. On remote machine
    Create the .ssh directory and the authorized_keys file with the correct permissions:

    $ mkdir .ssh
    $ touch ~/.ssh/authorized_keys
    $ chmod 700 ~/.ssh
    $ chmod 600 ~/.ssh/authorized_keys
  3. On local machine
    Copy SSH public key to Raspberry Pi:

    $ scp ~/.ssh/id_ed25519.pub pi@rpi:~/.ssh/
  4. On remote machine
    Add SSH public key to authorized_keys file:

    $ cd ~/.ssh
    $ cat id_ed25519.pub >> authorized_keys
    $ rm id_ed25519.pub
– Setup complete –

※ Other Things ※

Customised command prompt

  1. Replace default prompt in ~/.bashrc file with:

    # Custom prompt
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01m\]\u@\h\[\033[01m\]:\[\033[01m\]\w \[\033[00;35m\]❯\[\033[00m\] '

Incremental history and completion

  1. Add the following lines to file ~/.inputrc:

    # Incremental history searching
    "\e[A": history-search-backward
    "\e[B": history-search-forward
    "\e[C": forward-char
    "\e[D": backward-char
    
    # TAB completion
    set completion-ignore-case on
    set show-all-if-ambiguous on
    set show-all-if-unmodified on

Create custom SSH/login message

  1. Edit static text:

    $ sudo nano /etc/motd
  2. Edit dynamic content:

    $ cd /etc/update-motd.d
  3. Create/edit file:

    $ sudo nano /etc/update-motd.d/10-uname

    (file)

     #!/bin/sh
     uname -snrvm
     echo "\033[00;32m●\033[00m Connected to `iwgetid`\033[00m"

    (end of file)


Install tmux

  1. Download and install:

    $ sudo apt-get install tmux

User management

  1. To create user:

    $ sudo adduser user
  2. To add new user to sudoers:
    Some info: https://www.garron.me/en/linux/visudo-command-sudoers-file-sudo-default-editor.html

    $ sudo visudo
    

    Edit the file as below:

    # User privilege specification
    root  ALL=(ALL:ALL) ALL
    user  ALL = NOPASSWD: ALL
    

    Save and exit.

  3. To delete user:

    $ sudo userdel -r user

Setup WiFi

  1. Encrypt WiFi password:

    $ wpa_passphrase {ssid} {password}
  2. Edit configuration:

    $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

    (file)

     country=GB
     ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
     update_config=1
    
     network={
     	ssid=“ssid-name1”
     	scan_ssid=0
     	psk=encrypted-password
     	priority = 2
     }
    
     network={
     	ssid=“ssid-name2”
     	scan_ssid=0
     	psk=encrypted-password
     	priority = 1
     }
    

    (end of file)

Managing WiFi

  1. Check network being used:

    $ iwgetid
  2. List of available networks:

    $ wpa_cli
    > scan
    > scan_results
  3. Info about wireless network interface (wlan0/1):

    $ iwconfig wlan0
    $ sudo iwlist wlan0 scan | egrep "Cell|ESSID|Signal|Rates"
  4. Disable power management for WiFi:

    $ sudo iwconfig wlan0 power off

    Add this to the /etc/rc.local file so it runs when RPI restarts.

  5. Disable WiFi

    $ sudo ifconfig wlan0 down

    To keep WiFi off across boots, add this line to /boot/config.txt:

    dtoverlay=disable-wifi
    

Attach USB storage

  1. Plug USB disk in.

  2. List available block storage devices with UUIDs and free disk space:

    $ lsblk
    $ lsblk -f
    $ df -h

    External drives will have the name sdx, where x=a,b,c,...
    Some info: https://www.linuxnix.com/lsblk-command-explained-with-examples/

  3. Create partitions:

    $ sudo fdisk /dev/sda
    Command (m for help): o                                          (wipe existing partitions)
    Command (m for help): n                                          (add new partition)
    Select (default p): [ENTER]
    Partition number (1-4, default 1): [ENTER]
    First sector (2048-613355519, default 2048): [ENTER]
    Last sector, +sectors or +size{K,M,G,T,P} (2048-613355519, default 613355519): [ENTER]
    Command (m for help): w                                          (write changes)
  4. Check the new partition was created:

    $ sudo fdisk -l
    $ sudo fdisk -l /dev/sda
  5. Format partition and attach a label:

    $ sudo mkfs.ext4 -L U1 /dev/sda1
    /dev/sda1 contains a ext4 file system - Proceed anyway? (y,n): y
  6. Create mount point:

    $ sudo mkdir /mnt/u1
  7. Mount:

    $ sudo mount /dev/sda1 /mnt/u1

    -or-

    $ sudo mount -L U1 /mnt/u1
  8. Test mount point:

    $ ls /mnt/u1/
  9. Configuration for mounting drive at system boot:

    $ sudo nano /etc/fstab

    Add line after last partition of the list:

    LABEL=U1              /mnt/u1         ext4    defaults,auto,noatime,rw,users  0       0
    

    (UUID can be used instead of LABEL, run blkid to list the partitions and their UUIDs)

    Save and exit:
    [Ctrl+O]
    [Ctrl+X]

  10. Reboot:

    $ sudo reboot
  11. Change and verify permissions:

    $ sudo chown pi:pi /mnt/u1
    $ sudo chmod 755 /mnt/u1
    $ ls -l /mnt/
    drwxr-xr-x 3 pi pi 4096 Nov  4 21:27 u1
  12. Check filesystem for errors:

    $ sudo umount /mnt/u1
    $ sudo fsck -Mn /dev/sda1

Install & configure remote.it

Some info: https://docs.remote.it/platforms/quick-start-on-raspberry-pi/install-remote.it

  1. Install the remote.it connectd package:

    $ sudo apt install connectd
  2. Run the interactive installer:

    $ sudo connectd_installer

Install & configure RealVNC

Some info: https://help.realvnc.com/hc/en-us/articles/360002249917-VNC-Connect-and-Raspberry-Pi

  1. Install RealVNC:
    (pre-installed on Raspbian)

    $ sudo apt-get update 
    $ sudo apt-get install realvnc-vnc-server
  2. Start virtual desktop:

    $ vncserver :<display-number> -geometry 1600x1200 -randr 1600x1200,1440x900,1024x768
  3. Resize with:

    $ xrandr -s 1600x1200
    $ xrandr -s 1440x900
    $ xrandr -s 1024x768
  4. Convenient to put all of these options in ~/.vnc/config file:

    -randr 800x600,1024x768,1280x800,1280x960,1280x1024,1680x1050,1920x1080,3360x1050,1024x700,1200x740,1600x1000,3200x1000
    
  5. Stop virtual desktop:

    $ vncserver -kill :<display-number>

Useful commands

  • To start VNC Server now:

    $ sudo systemctl start vncserver-x11-serviced.service
  • To start VNC Server at next boot, and every subsequent boot:

    $ sudo systemctl enable vncserver-x11-serviced.service
  • To stop VNC Server:

    $ sudo systemctl stop vncserver-x11-serviced.service
  • To prevent VNC Server starting at boot:

    $ sudo systemctl disable vncserver-x11-serviced.service

Boot actions


Raspberry Pi 3/0W SSH over WiFi Fix

  1. Edit the two configuration files:

    /etc/ssh/ssh_config
    /etc/ssh/sshd_config

  2. Add the following line to the bottom of each configuration file:

    # RaspberryPi Zero W Onboard WiFi Fix
    IPQoS 0x00
    #IPQoS cs0 cs0
  3. Restart ssh and sshd:

    $ sudo systemctl restart ssh
    $ sudo systemctl restart sshd

https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=138631&start=125

guysoft/OctoPi#294

IPQoS 0x00
https://forum.core-electronics.com.au/t/pi-zero-w-problem/944/4

#RaspberryPi 3 Onboard WiFi Fix
IPQoS cs0 cs0
https://community.openenergymonitor.org/t/ssh-issues-specific-to-pi3-onboard-wifi/2730

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment