Skip to content

Instantly share code, notes, and snippets.

@weiland
Last active June 16, 2019 09:56
Show Gist options
  • Save weiland/39f30b7e4e03dc59e4bffaf6bdc1f27d to your computer and use it in GitHub Desktop.
Save weiland/39f30b7e4e03dc59e4bffaf6bdc1f27d to your computer and use it in GitHub Desktop.
Setup new raspberry pi! \o/

Setup a new Raspberry PI

Setup up a new Raspberry PI headless (without using a monitor, keyboard, mouse, LAN-cable) using WIFI and SSH.

Some assumptions:

  • on a mac (local machine)
  • the SD card is disk2 at /dev/disk2
  • the PI image is named image.img
  • the PI's IP is 10.0.0.1
  • there is some ssh-key at ~/.ssh/id_rsa (on the mac)
  • there is a wifi

Download image

Download a current Lite image of Raspbian from https://www.raspberrypi.org/downloads/raspbian/.

wget -O image.img https://downloads.raspberrypi.org/raspbian_lite_latest
# or
curl https://downloads.raspberrypi.org/raspbian_lite_latest --output image.img

Prepare SD card

https://www.raspberrypi.org/documentation/installation/installing-images/mac.md

# find the SD card (e.g. /dev/disk2 or mayby /dev/disk4)
diskutil list

# unmount the entire card (not just the partition)
diskutil unmountDisk /dev/disk2

# will most likely fail (dd: invalid number '1m') due to installed GNU coreutils
sudo dd bs=1m if=image.img of=/dev/rdisk2 conv=sync

# so run:
sudo dd bs=1M if=image.img of=/dev/rdisk2 conv=sync

# after the dd one could theoretically eject the card running
sudo diskutil eject /dev/rdisk2

Enable SSH: (By creating an empty filename named ssh in the boot root directory.)

touch /Volumes/boot/ssh

Enable WIFI (and add the SSID and password)

touch /Volumes/boot/wpa_supplicant.conf

It's contents:

country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="🌊"
    psk="the-password"
}

Now eject the SD card: sudo diskutil eject /dev/rdisk2. And put the card into the PI and power the PI.

Now you should find the PI in your network (check router, scan your network or use the Fing App in your phone). Obtain the IP address.

Maybe assign a static IP in your router.

Add SSH public key:

ssh-copy-id -i ~/.ssh/id_rsa pi@10.0.0.1
# or copy the key and create in on the pi
cat ~/.ssh/id_rsa.pub | pbcopy
# and on the pi:
mkdir ~/.ssh
vi ~/.ssh/authorized_keys

Optionally: Add ssh shortcut to ~/.ssh/config:

Host pi
  HostName 10.0.0.1
  User pi

Setup pi

Raspi Config

sudo raspi-config

Updates!

sudo apt-get update -y
sudo apt-get upgrade -y

Static IP

sudo vi /etc/dhcpcd.conf
  1. change pi password (passwd) or add own sudo user (sudo adduser USER sudo) and remove pi user (sudo deluser -remove-home pi).
  2. if public keys are set up, disable password auth.
  3. iptables
  4. further security options (e.g. change SSH port)
  5. install fish shell
  6. add dotfiles for shell, tmux and vim

Change/Update the WIFI settings:

sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

Change sudo password

sudo passwd root

Cleanup packages

sudo apt autoremove --purge && sudo apt clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment