Weekend Project: Part 1 – Securing a Raspberry Pi

Recently I dug out the old Raspberry Pi with the aim of using it as a VPN and a DNS backhole. I have always hand a handle on my privacy online but since looking up a new office chair online last week Facebook has provided me with an onslaught of ads for various office chairs, normally I brush this off tracking cookies never really bother me to much but, for some reason this got me triggered. So this weekend I will be turning my Raspberry Pi into a VPN and DNS black hole. The Raspberry Pi will sit on my home network, any devices phone’s, laptops, PC’s etc can be configured to send traffic through the Pi and then out to the internet fully encrpted.

First things first is down download the latest version of Raspbian, you can find it here : https://www.raspberrypi.org/downloads/raspbian/

I am pretty well versed in a Linux/Unix command line and am comfertable in my Linux knowlage to know the various tools, where they are and if they are not present where to get them so, I went for the Raspian Lite image just to keep things as lean as possible.

Tip: If you plan to run your Pi as a headless server, after writing the Raspbian image to an SD Card create a file called ‘ssh’ (file can be empty) on the card to enable ssh on first boot. This used to be enabled by default but a recent change due to worries of attackers high jacking un-secure Raspberry Pi’s prompted the folks over an Pi HQ to turn SSH as default. This tip saves you having to connect a monitor, keyboard & mouse to your Pi just to turn on SSH.

First Things First

At this point I assume you have your Raspberry Pi powered on, to find you Pi’s IP address you can look up your router to see whats connected or try to ping raspberrypi.local. Once you have the IP you can ssh using Putty or Terminal with the following command;

ssh -l pi 192.168.1.141

Defualt password for pi user is raspberry

Change pi users password

At the end of this guide we will delete the pi user from the system but first things first lets change that default password. Just type the below;

pi@raspberrypi:~ $ passwd
Changing password for pi.
Current password:
New password:
Retype new password:
passwd: password updated successfully

Now that the password has been changed we will update the Pi so we are running the latest and greatest. The example below shows the command to run, although my Pi is already updated so it shows as no updates, expect this to take 10-15min download and install updates on your Pi.

pi@raspberrypi:~ $ sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade
Hit:1 http://raspbian.raspberrypi.org/raspbian buster InRelease
Hit:2 http://archive.raspberrypi.org/debian buster InRelease
Reading package lists… Done
Reading package lists… Done
Building dependency tree
Reading state information… Done
Calculating upgrade… Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Reading package lists… Done
Building dependency tree
Reading state information… Done
Calculating upgrade… Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Now we have an up-to-date system lets create a new user and remove the default pi user. A new user can be created with the useradd comand. We will set a password for the new user.

pi@raspberrypi:~ $ sudo useradd paz
pi@raspberrypi:~ $ sudo usermod -a -G adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,input,netdev,gpio,i2c,spi paz

pi@raspberrypi:~ $ sudo passwd paz
New password:
Retype new password:
passwd: password updated successfully
pi@raspberrypi:~ $
pi@raspberrypi:~ $ exit
logout
Connection to 192.168.1.141 closed.

Lets disconnect as the pi user and connect as our new user, in my case ‘paz’.
pazy@Andrews-Air Downloads % ssh -l paz 192.168.1.141
paz@192.168.1.141's password:
Linux raspberrypi 4.19.75-v7+ #1270 SMP Tue Sep 24 18:45:11 BST 2019 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
paz@raspberrypi:~ $

Finally we can delete the pi user from our Raspberry Pi;

paz@raspberrypi:~ $ sudo deluser pi
Removing user pi' ... Warning: grouppi' has no more members.
Done.

paz@raspberrypi:~ $ sudo rm -rf /home/pi

We now have an up-to-date Pi and new user different to the default to connect with. To many this is enough but if your intent to connect your Pi to face the internet then you should be performing further steps to prevent attacks. On th next section we will guide you though this.

The Next Level

To secure your Raspberry Pi further we will insall fail2ban and change the defauly SSH port from 22 to something of your choosing.

Changing SSH port is simple just edit the file /etc/ssh/ssd_config, where you see the entry #Port remove the # and change the port number from the default 22 to something of your choosing. In my case I use 2222.

Once you update /etc/ssh/ssd_config you will need to restart the ssh service using the below command;

paz@raspberrypi:~ $ sudo service ssh restart

If we exit our session on our Pi and try and re-connect you will see our connection fails as we no longer have ssh running on port 22, we must use -p option to specify port at 2222.

pazy@Andrews-Air Downloads % ssh -l paz 192.168.1.141 -p 22
ssh: connect to host 192.168.1.141 port 22: Connection refused
pazy@Andrews-Air Downloads %
pazy@Andrews-Air Downloads %
pazy@Andrews-Air Downloads % ssh -l paz 192.168.1.141 -p 2222

Next we will install fail2ban. fail2ban analyses system logs for failed attempts to login to you Pi, should X number of attempts result in a fail that IP address is then banned from accessing the host. This provided pretty good protection from anyone trying to brute force thier way onto your Pi.

To install fail2ban do the following;

paz@raspberrypi:~ $ sudo apt-get install fail2ban

With fail2ban you much copy the .config file to .local as per below;

paz@raspberrypi:~ $ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

If you wish to edit configuration for example the number of bad attempts allowed before a ban edit the jail.local file.

While the article does seem pretty long it will only take 15min or so to run through the commands. Next I will write about setting up a VPN and Pi-hole on your Pi.

Changes

Changed from wordpress.com to wordpress.org. Let’s see how it goes.

On another note I am off to Gothenburg in a couple of weeks. Pictures soon.

Cya.

Use scp to download a file from a server

Firing this command up because I always forget it.

You can easily grab a file off a server from the command line using secure copy:


scp user@domain.com:/home/user/fileyouwant.txt fileyouwant.txt

This will download the file from your server to your local computer.

1 year on

Hi,

My blog is now 1 year old, I took a look at the current site stats and have just passed 1000 views 😀

So over 12 months that is 83 views per month works out at around 3 per day. Maybe if i post more these will go up.

At the moment I am happy with the blog direction I may start hosting it myself rather than hear on WordPress only time will tell.

Glasgow Botanic Gardens

It was a nice day yesterday so I decided to take a walk around Glasgows Botanic Gardens, I’ve been there before but never inside the green houses.

 

I took some pictures (not many) here are the four I actually like out of what I took.

I took them with a Lumix-LX5 that I recently bought its the first time its really left my flat.

P1010492 P1010493 P1010486021813 P1010489021813

How to watch the SuperBowl if you are outside the US or any US show

If you don’t want to or cant watch the Super Bowl on the TV then if you use the below Google Chome/Firefox extension you can stream it live on the CBS website.

The addon Media Hint is great as it allows you to access Region Blocked US content so if anyone whats to see US Neflix then turn this App on and you can gain advantage of the US Netflix library  Disable the addon and your back to the UK.

Obviously Netflix isn’t the only service Hulu works well managed to get caught up with the US office and The Thick of It.

The link for the extension:

https://mediahint.com/