My Raspberry Pi Setup

I recently had to reconfigure my Raspberry Pi because I bought a bigger micro-sd card for it (128 gb, up from my 8 gb). I thought I’d post my process since typically I do a headless install, meaning getting it up and running without a monitor, mouse or keyboard.

Flashing and Enabling SSH

First since I’m on macOS, I use a script called Pi Filler to flash the sd card with Raspbian. I still use the Raspbian image from April 2017 since the July 2017 image has something funky that messes with the flash process.

To use Pi Filler:

After you have Raspbian onto the sd card, you’ll need to enable SSH to run on boot.

To do this, you’ll need to create a blank file with the file name “ssh” and copy it to the root directory of the partition labeled “boot” on the sd card.

What I do:

  • Open Terminal on your Mac
  • Type the command “vim ssh” followed by “:wq”. This will create the empty file “ssh” in your home directory.
  • Then you can go into Finder and simply drag the “ssh” file to the “boot” partition.
  • Eject the sd card and put it into your Raspberry Pi

Once you connect your Raspberry Pi to power and ethernet, it should now boot up Raspbian with SSH enabled.

Logging in and Post Install

You will need to figure out the ip address of your Raspberry Pi in order to SSH into it. For myself, the quickest way to do this is to login to my router and look at the DHCP client table. If you don’t have access to your router, you can use a script called Pi Finder.

Once you find the local ip address of your Raspberry Pi, you can now login to it. For macOS and Linux, you just need to open your Terminal and type:

ssh pi@xxx.xxx.xxx.xxx

Where “xxx.xxx.xxx.xxx” is the ip address of the Raspberry Pi and enter the default password “raspberry”.

If you’re on Windows, you can use Putty to SSH in.

You can either decide to leave your Raspberry Pi on DHCP which means it’s ip address will change over time or you can set either the ethernet or the wireless adapter to always use a static IP address. I somewhat cheat on this, I setup DHCP Reservation on my router so every time I rebuild my Raspberry Pi, my router automatically recognizes my Pi and gives the ethernet adapter the IP address of 192.168.1.201 and gives the wireless adapter the IP address of 192.168.1.200.

The very first command that you will want to give your Raspberry Pi once you get logged in via SSH is: sudo apt-get update

This will make sure that your Pi knows where to go look for the latest software updates.

Just because I’m me and can’t stand not having my favorite text editor, vim, I usually install that next by typing: sudo apt-get install vim

After that it comes time to run: sudo raspi-config

This is a utility developed within Raspbian to configure it for the Raspberry Pi. The first option that comes up is to change the default password for the “pi” user. I usually do that first.

Then I go into Boot Options and Desktop/CLI. I always use Console with Autologin. Since I don’t use a monitor or VNC with any of my Pi’s there’s no point in it wasting memory and processor usage on loading up the GUI.

Finally, I go down to Advanced and hit Expand Filesystem. This repartitions your sd card to use the entire free space on your card.

After all of that, when you quit raspi-config it should prompt you to restart but if it doesn’t, enter: sudo shutdown -r now to go ahead and reboot your Pi.

Finally, the last thing that I do after it comes back up is: sudo apt-get upgrade This will upgrade all of the packages on your Pi to the latest versions.

Optional: Setup Wireless

I usually always run my Raspberry Pi’s off of wi-fi just to be able to put them anywhere I want and out of the way.

To setup your Raspberry Pi to automatically connect to your wireless network, type in the following command in your SSH prompt: sudo vim /etc/wpasupplicant/wpasupplicant.conf

At the end of that file add:

network={
ssid="Your wireless network name"
psk="Your wireless network password”
}

Once you do that, your Raspberry Pi will automatically connect to your wireless network the next time you reboot.

Optional: Setup External Hard Drive

I have a one terabyte external hard drive connected to my Raspberry Pi just to give me some extra space.

To add an external hard drive, firstly create the directory where you will want to access the drive. For myself, I usually use: /mnt/e so at your SSH prompt, type sudo mkdir /mnt/e

Then you’ll need to identify the device name for your drive. Typing: sudo fdisk -l will list all of the storage devices connected to your Raspberry Pi. You should be able to tell which device is your external drive based on capacity. But more than likely, if you only have one additional drive connected, it’s going to be /dev/sda1

If this is a new drive, you’ll want to format it to the Linux file system, ext4. To do this, at the SSH prompt, type: sudo mkfs.ext4 /dev/sda1

Lastly, you’re going to want to make that drive mount on boot, so type sudo vim /etc/fstab and at the end of that file, add the line: /dev/sda1 /mnt/e ext4 defaults 0 0

And there you have it! The next time your Raspberry Pi reboots, it will mount your drive on the directory /mnt/e!

Continue Reading

My Web Server Backup Script

I thought I would share this simple script that I wrote for my web server to back it up every night.

This simple python script:

  • Creates a temporary directory to copy everything to
  • Copies everything in my home directory
  • Copies everything from my root web directory
  • Copies my php.ini file (I hate resetting up my php.ini file)
  • Dumps all of the databases that I may have in MariaDB into a nice SQL dump file
  • Tars everything up into a single file
  • Then uses one of my favorite Linux CLI tools, RClone to copy the tar file to my Google Drive
  • Cleans up the temp directory that it created
#!/usr/bin/python

import os, time

f=str(time.time())
c="cwfraziercom"
p="/var/backup/"

os.popen("mkdir "+p)
os.popen("cp -r /root "+p)
os.popen("cp -r /var/www/ "+p)
os.popen("cp /etc/php.ini "+p)
os.popen("mysqldump -u root -pPassword > "+p+"sql.sql --all-databases")
os.popen("tar -zcvf /var/backup.tgz "+p)
os.popen("/root/rclone/rclone copy /var/backup.tgz g:/Backups/WebServers/"+c+"/"+f+"/")
os.popen("rm -rf "+p)
os.popen("rm -rf /var/backup.tgz")

I then just schedule the script to run every night by scheduling it as a cron job.

Also, just in case you’re wondering, the reason that I assign the file name as the value of the current epoch time is so each backup file has a unique file name. I keep all of my backups forever so the last thing I want is overlapping file names and accidentally overwriting old backups.

Continue Reading

Turning on two factor authentication on your Google Account

Those that know me, know that I’m a huge fan of two factor authentication. For those that don’t know what two factor authentication is, it’s where when you sign into an online account, you enter your username and password to login as always, but when you login, you usually get sent a text message with a six digit code that you need to verify before you can access your account.

This way, even if your password gets compromised, another person cannot access your account without having access to your phone.

To turn on two factor authentication on your Google account:

  1. Go to http://gmail.com and sign in
  2. Select your avatar on the upper right hand corner (to the right of your email address) and select My Account
  3. Select Signing in to Google under “Sign-in & Security”
  4. Select Two-Step Verification
  5. Google will once again ask for your password, go ahead and enter it
  6. It will then ask for your phone number, type it in and click Next
  7. A verification code will be sent to your phone, type in that code and hit Next
  8. Finally click “Turn On”

And you’re done! Now every time you sign in from a new computer or device, you’ll need to verify that it’s truly you by entering the verification code. Most online services now offer two factor authentication such as Facebook, Twitter, Dropbox and most banks, I highly recommend turning it on wherever possible.

Continue Reading