tech

My Raspberry PI Cookbook

I figured that I might as well save a Raspberry PI cookbook somewhere.

Note that these are for Raspberry PI OS. Some of these will work on other Linux distributions (especially Debian since Raspbian derives from it). te that these are for Raspberry PI OS.

But you know, Linux being linux and distributions being distributions, not all of them will work for everything. Since I only use Raspberry PI right now, I’ll just stick to the commands being for the Raspberry PI.

I will update this cookbook every now and then when I play with my PI.

User

Check what groups the current user is under

groups

Disk space

Displaying disk space in human-readable format

df -h

Processes

Displaying processes

ps

Monitor processes (press ‘q’ to quit)

htop

File

Monitoring a file

# Monitors the last N lines of a file
# -n is optional
tail -n [N]  -f [FILENAME]

Printing a file

cat [FILENAME]

Printing a in reverse (line-wise)

tac [FILENAME]

Grepping a file

grep [OPTIONS (optional)] [PATTERN] [FILES]

# for example, this greps all "hello" in the files main.c and main.h
grep hello main.c main.h

Grepping a file with regex

grep -E [PATTERN] [FILES]

Network

Displays active network connections and their corresponding processes

sudo netstat -nltp

Enable VNC and SSH

Open the terminal on the RPI, then:

sudo raspi-config

Select [Interfacing Options] > VNC > Yes > Ok

Enable SSH with public/private key authentication

Let’s define device R as the RPI and device C as the computer you want to connect from. We assume that C has already generated its public/private key pair.

Open the terminal on R, then:

sudo raspi-config

Select [Interfacing Options] > SSH > Yes > Ok

Then, make the following directory:

install -d -m 700 ~/.ssh

Open your favourite text editor (e.g. nano, vim, nvim) on ~/.ssh/authorized_keys. I like vim so:

sudo vim ~/.ssh/authorized_keys

Then somehow, someway, copy paste the public key from device C into that file and save.

Then, make sure that the permissions of that file is set correctly:

sudo chmod 644 ~/.ssh/authorized_keys

Finally, we have to disable password authentication. Open the following file:

sudo vim /etc/ssh/sshd_config

Add the following line:

PasswordAuthentication no

For any errors related to this, the log is avaliable at this file:

/var/log/auth.log

NOTE: You can, of course, use ssh-copy-id command from device C to setup the RSA authentication on device R. It basically does the steps related to placing device C’s public key into device R’s authorized_keys file. That assumes that you DO have ssh-copy-id available in device C.

Docker

Installation

Run the crazy script that will setup Docker.

curl -sSL https://get.docker.com | sh

Any user that requires Docker will need to be added to the ‘docker’ group. Let’s say my username is ‘momo’, I will then do the following:

sudo usermod -aG docker momo

Log out and log in again for the changes to take effect. After you log in, enter the command below display the list of groups you are under.

groups

Check that ‘docker’ is in the list displayed.

To test that Docker is running, do the following:

docker run hello-world

You should see text displayed as follows:

Hello from Docker!
This message shows that your installation appears to be working correctly.

Useful commands

Clean up any unused resources (including images)

docker system prune -a

List all containers

docker ps -a

Remove containers

docker rm [CONTAINER ID OR NAME...]

# Example: removing all containers
docker rm $(docker ps -a -q)

List all images

docker images -a

Remove images.

docker rmi [Images...]

# Example: removing ALL images
docker rmi $(docker images -a -q)

Stop container.

docker stop [CONTAINER ID OR NAME]

# Example: stops all containers
docker stop $(docker ps -a -q)

Compose: setups up a bunch of container and…does a bunch of things. Docker Compose needs a YML file to run. By default, it runes docker-compose.yml in the current directory.

docker compose up -d 

systemd.service

systemd.service is a way to manage your application into services in Debian/Ubuntu based Linux. Simply write create a file with a .service extention containing content like this:

[Unit]
Description=My discord bot service
After=network.target

[Service]
ExecStart=/command/to/execute
WorkingDirectory=/home/place/to/execute
StandardOutput=inherit
StandardError=inherit
Restart=always


[Install]
WantedBy=multi-user.target

Toss it into /etc/systemd/your_app.service, and you could be able to use it like a service.

systemctl start your_app