Raspberry Pi has become one of the most popular platforms for embedded projects both for hobbyists and professionals. There are lots of information on the web about various aspects of using these single-board computers (SBCs) and this post is merely a step-by-step description of setting up a couple of RPis for my personal projects.

The SBCs I chose were Raspberry Pi 3 Model B+ and Rapsberry Pi Zero W. The former was intended to be used in headless mode and the latter should be connected to a TV. The most convenient type of connection to both RPis was ssh, so SSH server should be enabled on both of them from the beginning. Initial set up of an SBC consists of the following steps:

  • prepare SD card with operating system
  • change default user
  • connect to network

Lets go into a bit more details.

Prepare microSD card

Installing official operating system on microSD card is an easy step. Just download image from the Raspbian page, extract it from archive and write it on the card using dd or official utility. Raspbian is the official operating system for all models of the Raspberry Pi.

dd if=2019-09-26-raspbian-buster.img of=/dev/mmcblk0 bs=4M conv=fsync

Two partitions will be created on the card: boot and rootfs.

By default, SSH server is disabled on Raspbian. It can be enabled by placing a file named ssh onto the boot partition on the microSD card. Next time the Raspberry Pi boots, it will enable SSH server and remove the file from partition.

Multicast DNS is supported out-of-the-box by the Avahi service, so the newly installed and powered up Raspberry Pi connected to network over WiFi or simple cable connection to a router should be accessible by ssh under the name raspberrypi.local.

PING raspberrypi.local (192.168.1.142) 56(84) bytes of data.
64 bytes from raspberrypi.lan (192.168.1.142): icmp_seq=1 ttl=64 time=0.264 ms

The system host name of the Raspberry Pi can be changed in /etc/hostname. If it turned out that several RPis are connected to the same network at the same time, a number will be attached to the hostname:

raspberrypi-N.local

Change default user on Raspberry Pi

The default user on Raspbian is pi with password rapsberry. Using default login credentials for a stand-alone board is convenient, but it may be not a good idea in case the computer is connected to a network as such connection may be a security threat. That is why I prefer to delete default user account and create a new one with different name and password. It is also worth adding the new user to several groups (i.e., sudoers group) for convenience.

$ sudo adduser hifi
Adding user `hifi' ...
Adding new group `hifi' (1001) ...
Adding new user `hifi' (1001) with group `hifi' ...
Creating home directory `/home/hifi' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for hifi
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y
$ sudo adduser hifi sudo
Adding user `hifi' to group `sudo' ...
Adding user hifi to group sudo
Done.

... repeat this step as needed

$ groups hifi
hifi : hifi dialout sudo audio video

After that it is time to login into Raspberry Pi using your account and delete the default user and its home directory as it will not be needed any more.

$ sudo deluser -remove-home pi

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for hifi:
Removing user `pi' ...
Warning: group `pi' has no more members.
Done.

The default accout can also be modified using usermod command, but this command does not change the name of current user. Using this command over ssh connection requires almost the same amount of steps as described above.

Don’t forget to change the default root password as well.

$ sudo passwd root
New password:
Retype new password:
passwd: password updated successfully

If the SBC’s desktop is planned to be used, then it makes sense to change the default user for desktop autologin from deleted pi to new user.

sudo vim /etc/lightdm/lightdm.conf

Change the following line to new user name in this file:

autologin-user=pi

Connect to network

Wired connection should work out of the box, just connect Raspberry Pi to a router, power it up and ssh to the board. Wireless connection requires appropriate configuration file for wpa_supplicant. This file named wpa_supplicant.conf should be placed to boot partition, and RPi will copy it into correct location in the root file system during next boot. An example of the configuration file is as following:

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

network={
    ssid="guest_net"
    scan_ssid=1
    psk="top_secret"
    key_mgmt=WPA-PSK
}

Disable radio interfaces

Raspberry Pi’s current kernels support the concept of Device Tree overlays, which allows user to modify the hardware configuration and enable or disable certain hardware blocks. My headless Raspberry Pi uses cable connection to router and WiFi/Bluetooth modules are not required in this particular setup. I decided to disable these modules to save a little bit of power and added the following lines into /boot/config.txt:

dtoverlay=pi3-disable-wifi
dtoverlay=pi3-disable-bt

Full description of the available overlays can be found in /boot/overlays/README.