Running from home

Ok here goes. I am writing this up as I build a test environment so bear with me :slight_smile:.

Since people have different routers at home many of which won’t have the abililty to build IPSEC or OpenVPN tunnels I am going to base this around a raspberry pi using the following topology as a starting point:

local network (192.168.58.0/24) -> router (dynamic or static ip it doesn’t matter) -> internet

N.b. you should change 192.168.58.0/24 to match your local network subnet. Many off the shelf routers use 192.168.0.0/24 or 192.168.1.0/24.

I am going to put a raspberry pi on the local network with an ip address of 192.168.58.64 (static), create a cloud based VM on Digital Ocean and then build an OpenVPN tunnel between the two. It will look a bit like this:

raspberry pi -> encrypted tunnel -> DO VM -> internet

Once that is working you follow the MIAB instructions as if you were installing it on the DO VM (DNS glue records, PTR record etc) except you don’t install it there. Instead you install it on another local server (not the raspberry pi) which has one small modification to its network configuration prior to installing MIAB which is to set its gateway IP address to that of the raspberry pi instead of the router - i.e. it will send all non-local traffic to the raspberry pi which will forward those over the encrypted tunnel to the DO VM which becomes the point of presence.

Step 1. Create a DO droplet with Ubuntu 18.0.4. Login and install OpenVPN.

curl -L https://install.pivpn.io | bash

Don’t worry that your droplet isn’t a Raspberry PI. I chose the defaults except to select OpenVPN instead of Wireguard because I have no prior experience with it.

Reboot when prompted and login again and create a single vpn user:

pivpn add

Follow the prompts chosing sensible values. I chose ‘miab’ as the name. Save the miab.ovpn file (it will tell you where it has saved it) somewhere local as we’ll want to install it on the Raspberry PI in a bit.

Before rebooting, edit the following file

/etc/openvpn/ccd/miab and add this line to inform OpenVPN of our local subnet

iroute 192.168.58.0 255.255.255.0

Step 2. Install raspian buster lite on the raspberry pi and do a full package update.

sudo apt update && sudo apt upgrade.

Make sure it has a static IP (in this case 192.168.58.64)

sudo nano /etc/dhcpcd.conf and make the necessary changes e.g:

interface wlan0
static ip_address=192.168.58.64/24
static routers=192.168.58.1
static domain_name_servers=192.168.58.1

Enable IP packet forwarding (we’re going to be acting as router later) and disable redirects.

sudo nano /etc/sysctl.conf and add the following:

net.ipv4.ip_forward=1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0

Install openvpn & speedtest-cli

sudo apt install openvpn speedtest-cli

Copy the .ovpn file from your droplet over to /etc/openvpn/ on the Raspberry PI. If you created private key password you should also create a /etc/openvpn/maib.pass file containing that password and edit the .ovpn file adding a line under ‘client’ like so:

client
askpass /etc/openvpn/miab.pass
....

Also add keepalives to the same file to keep the tunnel up

keepalive 10 60

You can test the tunnel works by typing

sudo openvpn /etc/openvpn/miab.ovpn

and in another SSH session you should be able to ping the remote tunnel IP of the droplet

ping 10.8.0.1

Similarly from the droplet it should be possible to ping 10.8.0.3

Finally check that when you run speedtest-cli it reports

‘Testing from Digital Ocean (X.X.X.X)’

when the tunnel is up.

Finally make sure the tunnel comes up at boot. I added the command to /etc/rc.local but there are better ways to do this.

sudo nano /etc/rc.local and add

openvpn /etc/openvpn/miab.ovpn &

Also to make the Pi act as a NAT router add the following line as well.

iptables -t nat -A POSTROUTING -s 192.168.58.0/24 -o tun0 -j MASQUERADE

Note on Buster this won’t work unless we switch to iptables-legacy using

sudo update-alternatives --config iptables and select option 1.

Now reboot and re-run speedtest-cli to make sure everthing still works as expected.

Step 3. Install Ubuntu on your local server that you which to be your on premise MIAB server.

Install the version that the instructions advise you to (18.04 at the time of writing).

Change the network settings so that it has a static IP (in this case 192.168.58.85) and its default gateway is 192.168.58.64 - i.e. the Raspberry PI not your router.

Reboot and install speedtest-cli

sudo apt install speedtest-cli and run it to verify that packets are being routed via the DO droplet correctly:

Retrieving speedtest.net configuration...
Testing from Digital Ocean (167.x.x.x.)...

If that works then horray! Your on premises MIAB server routes packets oubound to your DO droplet not to your ISP. Of course you ISP still carries those packets but they are encrypted and wrapped in OpenVPN tunnel packets.

Step 4. NAT inbound HTTP(S), IMAP, SMTP, DNS traffic arriving at DO public IP (167.x.x.x) to MIAB

First test that you can ping 192.168.58.64 from the DO droplet. If not make sure that you did enter the iroute directive into /etc/openvpn/ccd/miab. I forgot the first time.

We now need to NAT inbound traffic arriving at the droplet. I.e. an HTTP connection to 167.x.x.x.:80 will be redirected to 192.168.58.85:80 causing the packets to traverse the tunnel.

Add the following into /etc/rc.local and reboot:

iptables -t nat -A PREROUTING -d 167.x.x.x.x -p tcp --dport 53 -j DNAT --to-dest 192.168.58.85:53
iptables -t nat -A PREROUTING -d 167.x.x.x.x -p tcp --dport 80 -j DNAT --to-dest 192.168.58.85:80
iptables -t nat -A PREROUTING -d 167.x.x.x.x -p tcp --dport 443 -j DNAT --to-dest 192.168.58.85:443

etc for all the ports MIAB requires - UDP 53, TCP 80, 443, 25, 587, 993, 4190.

After reboot you should be ready to install MIAB on your local server.

Note that there are some security considerations here. If someone broke into your droplet they could poke around your internal network at will. You’ll need suitable firewall rules set up on the Raspberry Pi to prevent this.

And finally a disclaimer that I’m not suggesting this is best practice but rather to show that it is possible. MIAB does a great job taking back some control of our e-mail communications and I think being able to self host is an extension of those rights over the data itself. Plus I like having my own server closet with flashing lights :slight_smile:

3 Likes