Raspberry Pi

14 July 2021 Link



Purpose

I wanted to setup a small low power system as a hub for adding automation scripts on my home network and also attach home automation devices.

Devices

As of April 2016 I considered the BeagleBone Black and Raspberry Pi 3.
The cost of the raspberry pi was lower $35 compared to $48 and its feature set is much better:
  • More RAM 1GB vs 512MB
  • 1.2GHz processor vs 1GHz
  • Built in Wifi Controller
  • Built in Bluetooth and Bluetooth LE

Only thing is Raspberry Pi does not have on board storage compared to 4GB on Beaglebone. I ordered the board and am waiting for the board to arrive by tomorrow. I have also ordered a 64GB class 10 SD card to use with the Raspberry Pi board.

Setup

Ubuntu Mate

  1. Downloaded Ubuntu Mate 16.04 from here
  2. Extracted it with Winrar
  3. Burned the image on my 64GB MicroSD card using Win32 Disk Imager
  4. Connected a HDMI monitor and USB mouse and keyboard to the Pi and plugged in the MicroSD card.
  5. Plugged in the power supply and the Ubuntu setup began.
  6. Got a few low disk space warnings but ignored them
  7. After setup done then clicked on Raspberry Pi Info button on the Welcome Dialog and in there was a button to Resize Image which resized the root partition to cover the entire MicroSD Card
  8. The internet connection wifi or ethernet should be set to connect for all users so that when the device boots up in a server mode it connects to the network without anyone logging in so ssh can connect to it.
  9. One of my old monitors was not showing its full resolution going to Display settings showed that Raspberry thought it was an unknown monitor. To fix that I had to edit the file /boot/config.txt to uncomment the line disable_overscan=1
  10. To route the sound to the headphone jack use the command sudo amixer cset numid=3 1. To direct the sound to HDMI use sudo amixer cset numid=3 2
  11. After this disabled the graphical user interface:
    sudo graphical disable

New user

  1. Added new user:
    sudo adduser newusername
  2. Add to sudoers list
    sudo adduser newusername sudo

IP and host settings

  • Make sure the name in /etc/hostname is also there in /etc/hosts. Below are example contents of the 2 files
/etc/hosts:
127.0.0.1	localhost
::1		localhost ip6-localhost ip6-loopback
ff02::1		ip6-allnodes
ff02::2		ip6-allrouters

127.0.1.1	raspberrypi
/etc/hostname:
raspberrypi

The 2 linksbelow give good information about setting static IP in the raspberry pi:
https://www.ionos.com/digitalguide/server/configuration/provide-raspberry-pi-with-a-static-ip-address/#:~:text=To%20assign%20an%20IP%20address,with%20the%20IPv4%20address%20192.168.
https://www.raspberrypi.org/documentation/configuration/tcpip/

Since I have multiple raspberry pi's on my LAN. I make their IPs static by configuring on the router so I can manage all from 1 place.

Wifi dropping problem

I kept getting my wifi drop connection. I saw the solution here by searching for some lines I saw in /var/log/syslog when the wifi dropped. Basically you need to turn off the power save mode. To do that run the following commands:
> sudo iw wlan0 get power_save
> sudo iw wlan0 set power_save off
To make the change permanent add this line to /etc/rc.local file:
/sbin/iw dev wlan0 set power_save off

SSH Server

  1. SSH was already running so setting up the authentication protection according to the link: http://serverfault.com/questions/275669/ssh-sshd-how-do-i-set-max-login-attempts
  2. Do the following steps to set the max login attempts per session to 1 and to lock out for 120 seconds
  3. Add the following line to /etc/ssh/sshd_config
    MaxAuthTries 1
  4. Add the following firewall rules
    1. Create a new chain
      sudo iptables -N SSHATTACK
      sudo iptables -A SSHATTACK -j LOG --log-prefix "Possible SSH attack! " --log-level 7
      sudo iptables -A SSHATTACK -j DROP
    2. Block each IP address for 120 seconds which establishe more than three connections within 120 seconds. In case of the forth connection attempt, the request gets delegated to the SSHATTACK chain, which is responsible for logging the possible ssh attack and finally drops the request.
      iptables -A INPUT -i eth0 -p tcp -m state --dport 22 --state NEW -m recent --set
      iptables -A INPUT -i eth0 -p tcp -m state --dport 22 --state NEW -m recent --update --seconds 120 --hitcount 4 -j SSHATTACK

Installing Lua

  1. Opened the terminal from Applications->System Tools->MATE Terminal
  2. First did
    sudo apt-get update
  3. Now installed the libreadline library needed for Lua compilation
    sudo apt-get install libreadline-dev
  4. Download the Lua package and extract it.
  5. Go to the lua directory and do
    sudo make linux test

Installing OpenResty

  1. Download OpenResty from this page
  2. Install the prerequisites:
    apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential
  3. Install using the following commands:
    tar xvf ngx_openresty-VERSION.tar.gz
    cd ngx_openresty-VERSION/
    ./configure --with-pcre-jit --with-ipv6 
    make
    make install
    
  4. Finally add the command-line utilities provided by OpenResty to your PATH environment, like adding the following line to ~/.bashrc
    export PATH=/usr/local/openresty/bin:/usr/local/openresty/nginx/sbin:$PATH

Starting and stopping NginX

  • Start NginX:
nginx -p `pwd`/ -c path/to/nginx.conf
  • Re-start NginX:
kill -HUP $( cat logs/nginx.pid )
  • Stop NginX
kill -QUIT $( cat logs/nginx.pid )

Mapping Local Network drive

  • Install cifs-utils:
sudo apt-get install samba-common smbclient samba-common-bin cifs-utils
  • Make a directory in /media for the mount location
sudo mkdir /media/Data
  • Now mount the file
sudo mount -t cifs //172.29.32.184/sharename /media/Data/
For mybooklive I had to do"
sudo mount.cifs //192.168.0.197/Public /media/Data -o guest
After Raspberry Pi Update I had to do:
sudo mount -t cifs //192.168.0.197/Public /media/Data --verbose -o guest,vers=1.0,sec=ntlmssp
  • See here for more information

Audio Output

Look at this link: https://www.raspberrypi.org/documentation/configuration/audio-config.md to redirect audio betweek HDMI and headphone jack

Samba Server

https://www.raspberrypi.org/magpi/samba-file-server/ PDF version is here

Connecting a VLC player

To connect to the samba server from fire TV or other android devices to stream movies, etc. you can use VLC player.
In the VLC player however don't go to Local network to add the server. Go to add server to favorites and then in that form select the SMB protocol, blank for port. Then choose the URL as: pi@192.168.0.188 or something similar where pi is the user name and the local IP of the raspberry pi. Give the folder where you want to start and add it. When you open this it will ask for the password, enter it and click remember password and then OK. Now it should work without asking the password again and again.

Mount USB Drive

To mount the USB Drive I followed the instructions here
  1. Firstly install the following package if you’re using an NTFS drive.
    sudo apt-get install ntfs-3g
  2. Now if you don’t know the drive UUID, then we will need to get it. You can get a list of drives and their UUID that is currently connected to the Pi by running the following command.
    ls -l /dev/disk/by-uuid
  3. Now there is likely to be quite a few drives listed. Simply look for any drive that has an address of /sda* where * is a number. In my example, it is 1, so we will use the UUID of CA1C-06BC to mount to.
  4. Now it is important that we create a clean directory to mount the USB too. To create the directory, enter the following:
    sudo mkdir /media/usb1
  5. Next, we will need to obtain both the uid and the gid. These numbers are important as we will need them to set the correct permissions for the USB drive. To get the gid enter the following command. (If you’re using a different user than pi then make sure you update the username in the following commands). Got 1000
    id -g pi
  6. Get the uid enter the following command. (Got 1000)
    id -u pi
  7. Next, we need to make an edit to the fstab file. It is the file that is called on boot up to set up the drives. To edit this file, enter the following command:
    sudo nano /etc/fstab
  8. Now add the following line to the bottom of this file. Remember to update the line with all the relevant details for your USB drive. (For example, your drive will have a different UUID)
    UUID=CA1C-06BC /media/usb1 auto nofail,uid=enter_uid_here,gid=enter_gid_here,noatime 0 0
  9. Now since the Pi automatically mounts the drive, we will need to unmount the drive. A simple way to do this is to use the following command (Replace /dev/sda1 with the address relevant to your Pi).
    sudo umount /dev/sda1
  10. Now you can use the following command, and your drives should become mounted.
    sudo mount -a
  11. If you want to make sure the drives are restored after the Pi has been shut down then simply run the following command:
    sudo reboot