Setting up a Raspberry Pi
ndom91
April 5, 2018
This is a quick run down of the steps I like to take to initialize a fresh Raspberry Pi. Mostly so I can find it again later, but maybe you'll find it useful too. The first step is downloading and writing an OS onto an SD Card so your device has something to boot from. Operating System First, download an OS - for beginners I suggest Raspbian. Its a Debian based OS maintained by the Raspberry Pi Foundation. - Download Overview Page Raspi OS - arm64 server - arm64 desktop - arm32 desktop Ubuntu Server 20.04 LTS - arm64 - arm32 Writing SD Card If you're on a Linux distribution, you can search your package manager for rpi-imager. For example, on Ubuntu you can install it via: Other platforms can be downloaded here. dd Alternatively, you can write the image to your SD card via dd First, find out which device your sdcard is: Here you can see I have a device named sdb which is 16gb. This is my SD card. Once we have a disk image and figured out which device our sdcard is found under, we can run dd. - if = input file - of = output file - status=progress = show the write progress dd is colloquially known as 'disk destroyer' because if you're not careful you can write over your entire active disk destroying your system.. So be careful what you enter on the command line with dd! Setup Finally, once you have your SD card written, it will most likely mount the two new partitions automatically. You should see a ROOT and BOOT partition. If not, mount the BOOT partition briefly and navigate to it. You can now add two small files to enable the SSH Server on boot, and connect to the wifi network of your choosing automatically. This way you never have to plug in a monitor / keyboard / mouse / etc. The default credentials on a RaspiOS installation are: pi/raspberry ssh Create an empty file named ssh without an extension in the root of the
BOOT partition WiFi Also in the root of the BOOT partition: Create a file named wpasupplicant.conf. With the following contents: First Boot After booting for the first time, you'll probably want to setup a few things such as deploying your SSH key of choice, setting up a static IP, etc. Packages The very first thing you'll want to do, is update the apt cache and do an upgrade to get all the packages up-to-date from when the image was created until now. Then, some of the first packages I like to install on a headless system include: Then set vim as your default editor New User To add a new user, you can use adduser. This will open a multi-step wizard which begins by asking for the new password. Then it asks for Fullname, City, State, etc. these fields I always leave empty. You may also want to give your new user sudo permissions to be able to run 'sudo ..' commands. Change to root with su and then execute: Now your new user will be able to run sudo .. commands with their new password. Hostname You can set a new hostname via sudo hostnamectl set-hostname [name]. You may also have to adjust the local hosts file to set the new hostname as 127.0.0.1. And adjust the lines for 127.0.0.1 and 127.0.1.1 to append your new hostname. SSH Key To copy our SSH key to your new raspberry pi, you can use ssh-copy-id. You can specify which key to install on the new device with the -i flag. From your main host (i.e. not the raspberry pi) run: That's all there is to it! Static IP The latest version of Raspbian is based upon Debian 10 Buster. To set a static IP there, we'll want to use dhcpcd. This systemd service and config is started by default in Buster, we'll just have to adjust some config files. There is a section around line 45 which is commented out describing how to setup a static ip. Simply uncomment this and provide the correct values for your network. Once that is done, simply restart dhcpcd via systemctl It may hang for a minute, but should come right back up because it will have retained your old IP address as well until the next reboot. So for now the device will be reachable under the IP you initially SSHed into it with, and the new IP you just set as static. Upon your next reboot only the new IP should be available. ### Pro Tip Setup a /.ssh/config file to make SSHing into your commonly used machines much easier. Enter the following contents, adjusted for your keys, device IPs, etc. obviously Now you can SSH onto your pi via ssh ndo-docker, for example! If you have any other steps you take every time you setup a new raspberry pi, feel free to send them my way!
Discussion in the ATmosphere