Raspberry pi headless ssh: How to setup your Raspberry Pi, Headless!

Raspberry Pi Headless Setup (and other things) · GitHub

※ 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
  • Execute commands:

    $ sudo nano /etc/rc.local

    Add the commands or scripts before the exit 0 so it runs when RPI restarts.
    Some info : https://www.raspberrypi.org/documentation/linux/usage/rc-local.md

  • Change configuration parameters:

    $ sudo nano /boot/config.txt

    Add the changes as needed. Example: dtoverlay=disable-wifi
    Some info: https://www.raspberrypi.org/documentation/configuration/config-txt/

    See /boot/overlays/README for more info:
    https://github.com/raspberrypi/firmware/tree/master/boot/overlays

  • Run scripts:

    $ crontab -e

    Use @reboot at the begining of the line and & at the end.
    Example: @reboot python /home/pi/example.sh &

    [Ctrl + O]
    [Enter]
    [Ctrl + X]

    Crontab man page: https://manpages.debian.org/buster/cron/crontab.5.en.html
    Crontab tool: https://crontab.guru


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

How to enable SSH in Raspberry Pi? (Headless)

What is SSH?

The SSH protocol (also referred to as Secure Shell) is a method for secure remote login from one computer to another. It provides several alternative options for strong authentication, and it protects the communications security and integrity with strong encryption.

In our case we use our Desktop/Android device to logic to our Raspberry Pi remotely.

What will be covered?

  • STEP 1: Download and setup Raspbian OS
  • STEP 2: Enable SSH
  • STEP 3: Connect to a network(wifi or LAN)
  • STEP 4: Find the IP address of your Pi
  • Final STEP: SSH login

STEP 1: Download and setup Raspbian OS

Note:Skip this step if you have already setup the Raspbian in pi beforehand. 
  • To download the latest Raspbian OS first download Any One from here .
  • Next, download and install BalenaEtcher from here.
  • Insert SD card to your computer and open BalenaEtcher .
  • Select the raspbian image you downloaded earlier.
  • Select your SD card
  • Finally click on Flash!
It will take some time(Have patience  or a cup  of tea :P) to complete the Flashing process.

Finally This part is done!!

STEP 2 : Enable SSH

  • First, remove and reinsert SD card to the computer once it is flashed.

In Linux :

Simply open a terminal and create a file named “ssh” in the boot directory of the SD cardby using the below command

nano ssh

In Windows:

  • First, open the boot” Directory from “This PC“.
  • Next,Create a new “text document” like this in the “boot” directory named ssh
  • Now we need to remove the extension of this file i.e from “ssh.txt” to “ssh”
  • For this go to the view tab and check the checkbox “File name extensions” under Show/hide.
  • Now,your file will look like this(Extension is displayed along with file name)
  • Next just rename the file and remove the extension.
  • Once done the file type will change to “File” from “Text Document

That is it. With the next boot, SSH will be enabled.

STEP 3: Connect to a network(wifi or LAN)

First Method -Using LAN or Ethernet: In this method just connect your Pi to your router using an ethernet cable and power on the Pi. This will automatically connect your Pi to your local network.

Second Method Using Wifi: For this, we need to add another file inside the “boot” directory with the file name “wpa_supplicant.conf

  • Now open this file “wpa_supplicant.conf” with any text editor(i used notepad) and type the following and save it:
country=IN
update_config=1
ctrl_interface=/var/run/wpa_supplicant
network={
scan_ssid=1
ssid="Replace with your wifi name/SSID"
psk="Replace with the wifi password "
}

Country Code should be set using the ISO/IEC alpha2 code for the country.

This part is done !!

STEP 4: Find IP address of your Pi

To connect to your Raspberry Pi remotely, you need two things (we’ll help you find them):

  • Its IP address on the local network
  • An SSH client (SSH is a communication protocol between machines)

First power on the pi and wait for few seconds for the pi to boot up.

Next, we need to find the IP address of pi on your network. For this we can use any method

I use an app called Network Scanner to find the ip address.(playstore link)

Final Step :SSH login

Now open Cmd in windows or Terminal in linux and type:

ssh [email protected]

Here my IP address was “192.168.31.6”.Replace with your IP address.

  • Type yes and press Enter.
  • Now enter the password as “raspberry”.[This is the default password.]
Note: when you type the password it is not visible on the screen .This is normal no need to worry.just type it and press enter.
  • Now you have access to the command line of your Raspberry Pi remotely from another computer or device on the same network using SSH.

That’s it !! You have successfully connected via SSH into Pi.

How to Set Up a Raspberry Pi Headless

The Raspberry Pi is a computer that can do so much in such a small and discrete form-factor.

I’m going to show you how you can set up your Raspberry Pi to automatically enable SSH and connect to Wi-Fi without the need to configure this directly from the device, allowing you to use the Raspberry Pi without a keyboard or monitor connected.

Video Tutorial

Click here to display content from YouTube.

Learn more in YouTube’s privacy policy