Skip to content

Instantly share code, notes, and snippets.

@rpsu
Last active August 21, 2021 17:31
Show Gist options
  • Save rpsu/15888ae56bd92248a8e3ba4d170b714b to your computer and use it in GitHub Desktop.
Save rpsu/15888ae56bd92248a8e3ba4d170b714b to your computer and use it in GitHub Desktop.

Raspberry Pi Zero setup without keyboard and display adapters

Raspberry Pi comes with HDMI mini socket, and the Adapter HDMI mini to HDMI costs 4-5 €. However, I did not have one laying around so I flashed the Micro SD card I had and added the wifi configuration to it.

Then I can SSH directly to the box using the dynamic IP address my router provides.

  1. Stick the Micro SD to laptop with an adapter

  2. Download Raspberry Pi OS Lite from https://www.raspberrypi.org/software/operating-systems/

  3. Use a flasher tool, for example Etcher. Flashing wipes all data from the card!

  4. Create file wpa_supplicant.conf with this content:

     ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
     update_config=1
    
     network={
             ssid="REPLACE_THIS_WITH_WIFI_NAME"
             psk="REPLACE_THIS_WITH_WIFI_PASSWORD"
             proto=RSN
             key_mgmt=WPA-PSK
             pairwise=CCMP
             auth_alg=OPEN
     }
    

    The file will be editable later on in location /etc/wpa_supplicant/wpa_supplicant.conf inside the Raspberry Pi.

  5. Create an empty file ssh.

  6. Copy both files onto the root of the flashed Micro SD.

  7. Find out the current Pi's IP address, for example with Terminal command

  8. (which lists all devices on your local network)

      arp -a
    
  9. Boot the Raspberry Pi and SSH to it:

     ssh [email protected] # 
    
  10. Disable IPv6

    Edit system configuration file.

    $ sudo nano /etc/sysctl.conf
    # Add this to the end of the file
    net.ipv6.conf.all.disable_ipv6=1
    net.ipv6.conf.default.disable_ipv6=1
    net.ipv6.conf.lo.disable_ipv6=1
    net.ipv6.conf.eth0.disable_ipv6 = 1
    

    Disable IPv6 also by reloading /proc stuff.

    $ sudo nano /etc/rc.local 
    # Add this right above the `exit 0`
    /etc/init.d/procps force-reload
    

    You can test this works like this, and the latter command should not return anything.

    sudo sh /etc/rc.local
    ifconfig | grep inet6.
    
  11. Update the Raspberry Pi to use fixed IP address by editing DHCP configuration:

    sudo nano /etc/dhcpcd.conf
    

    Look for section "# Example static IP configuration" and read through. You probably need to add something like this at the end of the file:

     interface wlan0
     static ip_address=192.168.1.5/24
     static routers=192.168.1.1
     static domain_name_servers=192.168.1.1
    

    interface is wlan0 for wifi, and eth0 for wired connection.
    ip_address is your fixed IP address (and keep the /24 - it means all IPs 192.168.1.* are local).
    routers is your DCHP server, which is usually also the wifi router.
    domain_name_servers is most likely the same as your routers IP address. Add more by separating additional values with a space (192.168.1.1 4.2.2.4 etc.) I also enabled option ntp_servers by removing the hash (#) from the beginning of the line.

  12. Login again using the fixed IP address

    $ ssh [email protected]
    
  13. Turn off wifi power management (sleep mode)

    Add this to file /etc/network/interfaces to turn off wifi (wlan0) power management:

    allow-hotplug wlan0
    iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    wireless-power off
    

    @see: https://community.openhab.org/t/disable-wifi-sleep-mode-on-pi3-and-pi-zero-w/29917, https://www.kuerbis.org/2016/03/raspberry-pi-3-kurztipps-wlan-sleep-mode-verhindern/

    You can check if the power management is on or off with either of these two:

    $ iwconfig 
    # displays among other things "Power Management:off"
    
    $ iw wlan0 get power_save 
    Power save: off
    
    # Turn off until the next reboot with
    $ iw wlan0 set power_save off
    
  14. Update Raspberry Pi hostname

    $ sudo nano /etc/hostname
    homeraspi
    
    # update also hosts file to point the new hostname to localhost IP
    $ sudo nano /etc/hosts
    # replace old hostname on this line
    127.0.1.1               raspberrypi
    # with your new one
    127.0.1.1               homeraspi 
    

    You may want to update also your own laptop's /etc/hosts file with this record (add it to the end of the file).

    # on your laptop
    $ sudo nano /etc/hosts 
    192.168.1.5            yourhomeraspi
    
  15. Don't forget to change the password for pi user account!

    passwd 
    
  16. Put your SSH key to Raspberry Pi's pi user's account

    Now you can login using without your password, which is not just faster to connect to Raspberry Pi but also safer.

    First create the .ssh folder in Raspberry Pi:

    mkdir /home/pi/.ssh
    

    Assuming your SSH key is in file id_rsa.pub copy it from your laptop to Raspberry Pi:

    # on your laptop
    scp ~/.ssh/id_rsa.pub pi@yourhomeraspi:/home/pi/.ssh/ 
    
    # On Raspberry Pi
    cd /home/pi/.ssh
    cat id_rsa.pub > authorized_keys
    rm id_rsa.pub
    
  17. Update all packages

    sudo apt update
    sudo apt upgrade
    sudo apt autoremove
    
  18. Reboot Raspberry Pi

    sudo reboot
    

Sources:

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