Raspberry Pi

From 7West
Revision as of 14:03, 10 December 2024 by Animationb (talk | contribs) (Created page with "This is a page full of first-time set up instructions and how to's for the Raspberry Pi. == First-Time Setup == ==== Headless Operations ==== Place a file titled '''ssh''' in the "boot" directory, when the SD Card is plugged in to a desktop computer. Place a file titled '''wpa_supplicant.conf''' in the same "boot" directory with the following code: <nowiki> country=US ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="<SSID>" sc...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This is a page full of first-time set up instructions and how to's for the Raspberry Pi.

First-Time Setup

Headless Operations

Place a file titled ssh in the "boot" directory, when the SD Card is plugged in to a desktop computer.

Place a file titled wpa_supplicant.conf in the same "boot" directory with the following code:

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

network={
 ssid="<SSID>"
 scan_ssid=1
 psk="<PASSWORD>"
 key_mgmt=WPA-PSK
}

These steps will allow the Raspberry Pi to be reachable via ssh on the first boot. Use Window's PuTTY or Linux's ssh to remote in. Use your router's webpage to find the IP Address of the newly minted Raspberry Pi. If you do not have access, try ssh'ing to the hostname "raspberrypi". On the first boot, the password will be the same as the hostname.

First Time Essential Commands

Enter passwd to change the password. Enter the following to always require password when using sudo:

sudo visudo /etc/sudoers.d/010_pi-nopasswd

Then NOPASSWD to PASSWD.

Enter sudo raspi-config to edit several key Raspberry Pi configurations:

  • Change the hostname
  • Reduce GPU memory (optional)
  • Expand the file system
  • (Optional) Boot / Auto Login

Lastly run sudo apt-get update then sudo apt-get upgrade to update all the software. This will take a while.

SD Card Longevity

SD Cards are not meant to be written to so many times (i.e. log files). To ensure the SD Card lasts as long as possible, we will do two things.

This will disable the swap file setup on the SD Card by default[1]:

sudo swapoff --all
sudo apt-get remove dphys-swapfile

This will install log2ram[2], which tries to delegate all log file writing to RAM:

sudo apt-get install git
git clone https://github.com/azlux/log2ram && cd log2ram
chmod +x install.sh && sudo ./install.sh
cd .. && rm -r log2ram

How to's

Aliases

Editing .bashrc (located in the pi home directory) will let you add aliases. At the bottom of the file add the following:

alias <shortcut>="<what you want the shortcut to do>"

Make sure there is a carriage return after the last line in the file.

Uncomplicated Firewall

This lets you set up a firewall and add exceptions easily. Enter the following into the terminal[3]:

sudo apt-get install ufw
sudo ufw allow ssh
sudo ufw enable

Now you can only reach the Raspberry Pi via SSH. You can enter sudo ufw app list to see other services to add exceptions for. (www = http and https)

NTFS External HDD

If you are headless and mounting an external drive with the NTFS file system, you will need to do the following. External drives will probably not mount if you are headless. First use fdisk -l to find the sda of the external drive. Then in /etc/rc.local add the following line:

sudo mount /dev/sda# -t ntfs-3g -o permissions /mount/point/folder

Now you can see the drive on reboots and can edit permissions with chown and chmod. By default, the drive is owned by root. You can change this with:

sudo chown -R USER /mount/point/folder

Modify permissions as you please with chmod. 700 is recommended.

Samba Server: Network File Server

This will set up a shared folder on the Raspberry Pi that will be editable from Windows computers on the same network. Enter the following code on the Raspberry Pi[4][5]:

sudo apt-get install samba samba-common-bin smbclient cifs-utils
sudo smbpasswd -a pi
mkdir ~/shared
sudo nano /etc/samba/smb.conf

Inside the config file, ensure there is a line workgroup = WORKGROUP and add the following at the end:

[share]
     path = /home/pi/shared
     browseable=yes
     writeable=yes
     only guest=no
     create mask=0777
     directory mask=0777
     public=no

Finally, in Windows File Explorer, right click on "This PC" and select "Map Network Drive...". Chose a drive letter and set Folder to \\<hostname>\share. Ensure "Connect using different credentials" is checked. Click finish and enter the credentials for user pi twice.

If you'd like to setup an external HDD, change the [share] to another name and the path to the drive: /media/pi/<Drive Name>. This is better for lots of storage as an HDD has much better endurance than an SD Card.

Note: sometimes you get lucky and this folder appears in "Network" in File Explorer without having to map the drive, but it's been inconsistent.

Disabling WiFi

Used if you have the Raspberry Pi only used for its Ethernet connection and WiFi is useless. In the file /boot/config.txt add the following line[6]:

dtoverlay=disable-wifi

Flask Python Webserver: Run Python from a website

Haven't done this yet, so figure it out: Flask

Raspberry Pi Watchdog

If you need the Raspberry Pi to run nonstop, you can set up the Watchdog Timer to trigger a reboot if the system hangs. Enter the following in the terminal [7]:

sudo su
echo 'dtparam=watchdog=on' >> /boot/config.txt
reboot
sudo apt-get update
sudo apt-get install watchdog
sudo su
echo 'watchdog-device = /dev/watchdog' >> /etc/watchdog.conf
echo 'watchdog-timeout = 15' >> /etc/watchdog.conf
echo 'max-load-1 = 24' >> /etc/watchdog.conf
sudo systemctl enable watchdog
sudo systemctl start watchdog
sudo systemctl status watchdog

It will start itself at boot as well.

Remote Desktop

This will allow you to use the GUI Desktop of the Raspberry Pi remotely. Install xrdp:

sudo apt-get install xrdp

From Windows, launch Remote Desktop Connection and connect to the IP Address or hostname of the Raspberry Pi. Maybe?

rclone and Google Drive

This will let you backup folders to Google Drive. rclone Install offers a simple walkthrough and can help with any questions. Enter the following in the terminal:

curl https://rclone.org/install.sh | sudo bash

Use rclone's Google Drive specific setup page to install a new remote. Be sure to enter a Client ID and Client Secret; setting it up is shown here. If your name is Adriano, you've already set most of that up.

Two commands are the most important: copy and sync. Copy moves all files from the source to the destination, sync forces the destination to look like the source (including deleting files). I recommend sync, just because I don't like clutter. Create a shell script rclone_cron.sh somewhere:

#!/bin/bash
if pidof -o %PPID -x “rclone_cron.sh”; then
exit 1
fi
rclone sync -v -u /media/pi/Elements/gdrive rgdrive:sync
rclone sync -v -u rgdrive:sync /media/pi/Elements/gdrive
exit

The first part makes sure the syncing isn't already running. The script syncs "twice", once to make the Google Drive the same as the Network Drive (ignoring more recent files). The second sync can bring more recent files from Google Drive to the Network Drive. Be sure to modify the script to be executable (chmod a+x) Place this script in crontab (crontab -e):

0 */4 * * * <path>/rclone_cron.sh > /dev/null 2>&1

Now it will sync every 4 hours and log nothing. If you need an immediate sync, just run rclone_cron.sh.

Setting up MediaWiki

Kinda moot because it's set up here, and if this crashes and I have to set it up again, I can't read this. However, I will back this page up somewhere. Enter the following into the terminal[8]:

sudo apt-get install -y mediawiki

This will take a while. Once it is done, we must setup the "SQL Database" (except the package is called mariadb on Raspberry Pi):

sudo mariadb -u root -p
CREATE USER 'wikiUser'@'localhost' IDENTIFIED BY 'THISpasswordSHOULDbeCHANGED';
quit;
sudo mariadb -u root -p
CREATE DATABASE my_wiki;
use my_wiki;
GRANT ALL ON my_wiki.* TO 'wikiUser'@'localhost';
quit;

That password set for wikiUser will be stored in plaintext, so don't pick a good one

No in a browser go to <hostname>/mediawiki. Set up, mostly keeping defaults. Be sure to use the wikiUser and not root for the database user.

Move the LocalSettings.php file to the instructed directory and edit it to change what you want to change. IMPORTANT: change $wgMainCacheType = CACHE_ACCEL to $wgMainCacheType = CACHE_ANYTHING

The /mediawiki used as the main directory for the Wiki, is at /var/lib/mediawiki. It's all protected, so I recommend running sudo su before editing any of it.

To setup TLS/HTTPS, visit Certbot and be sure to forward port 443.

Setting up a Dynamic DNS

I use a Google Domain for this website, so I have to set up a Dynamic DNS in order for the domain to redirect to the Raspberry Pi. First we have to install ddclient[9]:

sudo apt-get install ddclient
sudo nano /etc/ddclient.conf

Now modify the .conf file you just opened. Note: during the install process ddclient probably prompted you to enter all this info already, but modify this .conf file to show this[10]:

protocol=dyndns2
use=web
server=domains.google.com
ssl=yes
login=<username>
password='<password>'
7west.org

Use the ' around the password. You get the username and password from the DNS settings of your Google Domain.

ddclient will now update your IP Address every 5 minutes.

Git Remote Server

On the server, cd to the parent directory:

mkdir RepoName
cd RepoName
git init --bare

Now on the machine with the code and local git repository:

git remote add RemoteName ssh://USER@DOMAIN.com:PORT/file/path/to/RepoName
git push -u RemoteName main

If it fails, it might be your known_hosts file. Look there and delete the applicable row. You also will have to log into USER@DOMAIN.com every time you push.

To clone this remote repo, cd to the desired parent directory and enter the following on the destination machine:

git clone ssh://USER@DOMAIN.com:PORT/file/path/to/RepoName
git checkout main

Dwarf Fortress Server

This is pretty silly to have here, but here it is. Download and install Dwarf Fortress as the wiki says. Get the dependancies as needed.

Modify init.txt in df_linux/data/init. Turn off sound and change PRINT_MODE to TEXT. Ensure your terminal is set to Use bright colors for bold text (this is in Preferences for the terminal). Then run the following:

sudo apt install xvfb
Xvfb :1 -screen 0 1024x768x16 & 
export DISPLAY=:1
/path/to/df

You might also want to change some key bindings, like the Plus key.

References