Skip to content

Instantly share code, notes, and snippets.

@mastersign
Last active November 23, 2022 07:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mastersign/4473d8ddde6d7dd92726 to your computer and use it in GitHub Desktop.
Save mastersign/4473d8ddde6d7dd92726 to your computer and use it in GitHub Desktop.
A small cheat sheet for setting up a raspberry pi for dev and fun.

Raspberry PI

A collection of tips and hints to admin a Raspberry PI with Raspbian for development and fun.

Tips

  • root previliges
    • sudo <command> for one command
    • sudo -i for shell with root previliges
  • package management
    • apt-get update update local copy of package lists
    • apt-cache search <package> find package
    • apt-get install <package> install package
    • apt-get upgrade (<package>|all) [-y] update package
    • apt-get dist-upgrade update all packages with potentially new major versions
    • apt-get purge removes a package with all config files
  • system control
    • sudo raspi-config
    • date --set 2010-12-31 set system date
    • date --set 16:0500 set system time
    • startx start X server (graphical desktop)
    • sudo shutdown -h now shutdown and halt
    • sudo shutdown -r now or sudo reboot reboot
  • system information
    • cat /proc/cpuinfo hardware info
    • vcgencmd measure_clock arm cpu frequency
    • uname -a OS info
    • free memory info
    • df partition info
  • editor nano
  • set timezone and sync with server
    • sudo dpkg-reconfigure tzdata
    • sudo ntpd -q -g
  • get write permissions in /usr/local as pi
    • sudo usermod -aG staff pi
  • update library path incase a library can not be loaded despite the fact that it is there
    • sudo ldconfig

Configuration and Setup

Networking

  • use LAN with default settings
  • install guessnet sudo apt-get install guessnet -y
  • use WiFi Config (startx) and create WLAN profile with id_string 'wlan_home'
  • edit /etc/network/interfaces
auto lo eth0 wlan0

## Loopback interface

iface lo inet loopback

## Hot-Plug activated
allow-hotplug eth0 wlan0

## LAN interface

mapping eth0
    script /usr/sbin/guessnet-ifupdown
    map lan_home
    map no_cable
    map default: default

# static IP configuration for home LAN
iface lan_home inet static
    address 192.168.66.101
    netmask 255.255.255.0
    gateway 192.168.66.254
    dns-domain signumorg.no-ip.biz
    dns-nameservers 192.168.66.254
    test1 peer address 192.168.66.254 mac XX:XX:XX:XX:XX:XX source 192.168.66.101

# manual configuration if no cable
iface no_cable inet manual
    test missing-cable
    pre-up echo No link present.
    pre-up false

## WLAN interface

# configuration with wpa_supplicant
iface wlan0 inet manual
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

# dynamic IP configuration for wpa_supplicant profiles
iface wlan_home inet static
    address 192.168.66.102
    netmask 255.255.255.0
    gateway 192.168.66.254
    dns-domain my-domain.no-ip.com
    dns-nameservers 192.168.66.254

## Default settings for interfaces
iface default inet dhcp
iface manual inet manual
  • use sudo /etc/init.d/networking restart to restart interfaces

SSH Key Pair

Create a key pair for the pi user:

ssh-keygen -t rsa -b 4096

This created the files ~/.ssh/id_rsa (private key) and ~./id_rsa.pub (public key).

Copy public key from another system/user into ~/.ssh, eg. ~/.ssh/pi@another-rpi.pub. Append the foreign public key to ~/.ssh/authorized_keys:

cat ~/.ssh/pi@another-rpi.pub >> ~/.ssh/authorized_keys

Boot debugging

Install bootlogd

sudo apt-get install bootlogd -y

Activate bootlogd by creating config file

sudo echo "# Run bootlogd on startup?
BOOTLOGD_ENABLED=yes" > /etc/default/bootlogd

Create script for filtering escape sequences from logfile

mkdir ~/bin
echo "sed 's/\^\[\([0-9]\|\[?\?[0-9;]\+[A-Za-z]\?\[\?\)//g' /var/log/boot" > ~/bin/lastboot
chmod a+x ~/bin/lastboot

Call lastboot to view boot messages from last boot

lastboot | less

XRDP / screen

sudo apt-get install xrdp -y
sudo apt-get install screen -y

PHP / NGINX

sudo apt-get install php5-cli php5-fpm -y

sudo apt-get install nginx -y

Default document root is /usr/share/nginx/www

Setup Configuration

create new configuration:

cd /etc/nginx
sudo rm sites-enabled/default
sudo cp sites-available/default sites-available/my-default
sudo ln -s /etc/nginx/sites-available/my-default sites-enabled/default

activate PHP in the configuration: uncomment location ~ \.php$ { and matching close bracket

Prepare Filesystem

add pi to www-data and change permissions of nginx doc root:

sudo adduser pi www-data
sudo chgrp -R www-data /usr/share/nginx/www
sudo chmod -R g+rw /usr/share/nginx/www
sudo chmod g+s /usr/share/nginx/www

logout and login with pi to use new permissions

add symbolic link to home directory:

cd ~
ln -s /usr/share/nginx/www www

Start NGINX

sudo service nginx start

Python

Installing SetupTools with easy_install.

wget https://bootstrap.pypa.io/ez_setup.py -O - | python
wget https://bootstrap.pypa.io/ez_setup.py -O - | python3

Logwatch

Install logwatch

sudo apt-get update
sudo apt-get install logwatch

Configure cron command

sudo nano /etc/cron.daily/00logwatch

Change the line after #execute into

/usr/sbin/logwatch --mailto <YourEmail@Address.com> --format html

Running logwatch for a specific service

sudo logwatch --service <Service> --detail high --range "between -3 days and today" --mailto <YourEmail@Address.com> --format html

To get more info about the possible ranges

logwatch --range Help

Outgoing Email

From http://iqjar.com/jar/sending-emails-from-the-raspberry-pi/

Install sSMTP and Mail Utils

sudo service sendmail stop    
sudo apt-get update
sudo apt-get purge sendmail
sudo apt-get install ssmtp mailutils -y

Configure sSMTP

sudo nano /etc/ssmtp/ssmtp.conf

This file contains the configuration of sSMTP. Mandatory lines are:

root=postmaster
mailhub=<YourSmtpServer>:587
hostname=raspberrypi
AuthUser=<YourUserName>
AuthPass=<YourPassword>
UseSTARTTLS=YES

Replace text in anglebrackets with your info.

Optional lines are:

  • For rewriting the sending domain name in the mail header:
    rewriteDomain=<YourDomain>
  • For allowing the sender to override the From field in the header:
    FromLineOverride=NO

Setup Accounts

nano /etc/ssmtp/revaliases

This file contains data about the email accounts for existing Linux users in the format:

local_account:outgoing_address:mailhub[:port]

E.g.

root:root@yourdomain:smtp.yourmailserver:587
www-data:www@yourdomain:smtp.yourmailserver.587

The outgoing address is only used if the configuration parameter FromLineOverride is set to NO.

Set Permissions

The read permissions for /etc/ssmtp/ssmtp.conf control which local Linux account is able to send mails.

The owner user of the file is root and per default, the owner group of the file is root.

To have fine control of the permissions we will permit the read access from all.

sudo chmod 640 /etc/ssmtp/ssmtp.conf
sudo chmod 640 /etc/ssmtp/revaliases

Then create a group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment