Setup a new Raspberry Pi

Salvador Guerrero
4 min readAug 27, 2023

--

There are a ton of tutorials out there, but these are the steps I take when setting up a new Raspberry Pi with my touch and features.

Hardware

The below list is similar to my setup:

Raspberry Pi Instructions

Install Raspberry Pi OS Lite 64-BIT using Raspberry Pi Imager.

I like to install the Lite version because I don’t hook up a monitor to it; I use it via SSH.

Before you hit the write button, configure as many settings as you can by hitting the settings button. I like to configure hostname, SSH, Wireless LAN, and locale.

While turned off, plug in the SDCard and connect it.

From a computer (I use macOS), launch the terminal and login:

$ ssh satoshi@raspberrypi.local

Optionally, disable wifi, as it is always connected via ethernet (to reduce the load).

$ sudo ifconfig wlan0 down

and also persist the config after reboot by modifying /boot/config.txt

sudo vi /boot/config.txt

add the following line

dtoverlay=disable-wifi

Now, time to update it!

$ sudo apt-get update
$ sudo apt-get upgrade --yes
$ sudo reboot

If you installed the full desktop version (not Lite), you can disable X-Server by following the below command if you decide to not use the monitor anymore:

$ sudo raspi-config

System Options > Boot / Auto Login > Console

Finish > Reboot

Install Vim

$ sudo apt install vim --yes

Configure mail

I like to set up email to use it for alerts. I posted the instructions and config files on configuring Gmail and iCloud for Postfix here. If you have any problems, don’t hesitate to message me.

If you want to forward emails sent to your local user create a .forward file in your home directory containing the email address and change the permissions as follows:

$ echo "EMAIL@ADDRESS.COM" > ~/.forward
$ chmod 644 ~/.forward

Create a non-root user for running public services

# add a new user with a home folder
$ sudo useradd sandbox -m

# set a password
$ sudo passwd sandbox

# create the mail file, otherwise it will fail when running the mail command
$ sudo touch /var/mail/sandbox

# fix mail file permissions
$ sudo chown sandbox:mail /var/mail/sandbox
$ sudo chmod o-r,g+w /var/mail/sandbox

# see that mail inbox works
$ sudo su - sandbox
$ mail
$ echo "email body" | mail -a "From: Sender <sender@mail.com>" -s "Email Subject" RECIPIENT@EMAIL.COM
$ exit

Reduce activity on the microSD card

Reducing the activity on the microSD will prolong its life. I will be sharing the instructions I followed to mitigate this:

  1. Move logs to an external drive using symlink

Before we move on, install lsof

$ sudo apt install lsof

and run it to get a list of all the services that have opened files in /var/log

$ sudo lsof | grep /var/log

In my case, I only have systemd-j and rsyslogd

now stop these services

$ sudo systemctl stop syslog.socket rsyslog

Move the log folder to the external drive, here’s my example

$ sudo mv /var/log /mnt/sda/

Now create a symlink

$ sudo ln -s /mnt/sda/log /var/log

Configure system journald, open the file

$ sudo vi /etc/systemd/journald.conf

and paste the following line

LogsDirectory=/mnt/sda/log/journal

Now reboot

$ sudo reboot

2. Reduce the swappiness
The default setting for swappiness is 60; a value of 10 is recommended by this post to reduce swap usage.
- A value of 100 tells the kernel to aggressively use swap cache
- A value of 0 disables swap cache.

Print the current swappiness value:

$ cat /proc/sys/vm/swappiness

To change the swappiness value permanently, open sysctl.conf

$ sudo vi /etc/sysctl.conf 

and add the following line towards the end of the file

vm.swappiness=10

If you want to go further, move the swap file to the external SSD by editing dphys-swapfile.
(I did this because my Raspberry was indeed using the swap file when synchronizing Bitcoin)

$ sudo vi /etc/dphys-swapfile 

and modifying it as follows:

# external drive
CONF_SWAPFILE=/mnt/sda/swap

# also, increase the size swap size to match the RAM
CONF_SWAPSIZE=7812

# Set the limit to the actual RAM size
CONF_MAXSWAP=7812

Stop bitcoind and reboot with and that’s it!

Next, install and configure the firewall, and tor! 🔒

References

--

--

Salvador Guerrero

Computer Science Engineer, Cross-Platform App Developer, Open Source contributor. 🇲🇽🇺🇸