--- layout: post title: "Set up encryption using Let's Encrypt" description: "Tutorial how to encrypt your connection with Home Assistant." date: 2015-12-13 10:05:00 -0800 date_formatted: "December 13, 2015" author: Paulus Schoutsen & Martin Hjelmare author_twitter: balloob comments: true categories: How-To og_image: /images/blog/2015-12-lets-encrypt/letsencrypt-secured-fb.png --- Exposing your Home Assistant instance outside of your network always has been tricky. You have to set up port forwarding on your router and most likely add a dynamic DNS service to work around your ISP changing your IP. After this you would be able to use Home Assistant from anywhere but there is one big red flag: no encryption. This tutorial will take you through the steps to setup a dynamic DNS for your IP and allow trusted encrypted connection to it - for free using [DuckDNS] and [Let's Encrypt].
### {% linkable_title DuckDNS %}
The first step is to set up [DuckDNS]. This is a free dynamic DNS service that you can use to get a DuckDNS.org subdomain to point at your house. A dynamic DNS service works by having your home computer tell DuckDNS.org every 5 minutes what its IP is so that DuckDNS can make sure your domain name is set up correctly.
For this example we will assume our domain is hass-example.duckdns.org.
First step is to acquire and set up our domain name. For this, go to [DuckDNS], log in with any of the supported login providers and add a domain. After this check out their [installation instructions][duckdns-install] to finish your installation of DuckDNS. If you're on a Raspberry Pi, see 'Pi' in the category 'Operating Systems'.
### {% linkable_title Let's Encrypt %}
[Let's Encrypt] is a free, automated, and open certificate authority (CA). We will use this to acquire a certificate that can be used to encrypted our connection with Home Assistant.
Let's Encrypt will give you a free 90-day certificate if you pass their domain validation challenge. Domains are validated by having certain data be accessible on your domain for Let's Encrypt ([they describe it better themselves][letsencrypt-technology]).
Assuming that your home is behind a router, the first thing to do is to set up port forwarding from your router to your computer that will run Let's Encrypt. For the Let's Encrypt set up we need to forward external port `80` to internal port `80` (http connections). This can be set up by accessing your router admin interface ([Site with port forwarding instructions per router][port-forward]). This port forward must be active whenever you want to request a new certificate from Let's Encrypt, typically every three months. If you normally don't use or have an app that listens to port `80`, it should be safe to leave the port open. This will make renewing certificates easier.
Now you're ready to install and run the client that requests certificates from Let's Encrypt. The following example will use the platform independent script to install and run the [certbot][certbot] client from Let's Encrypt. If there is a certbot package for your OS, it's recommended to install the package instead of the platform independent script. Read the [docs][certbot] for more information. There are also other clients that might offer more customization and options. See the [client options page][letsencrypt-clients] at Let's Encrypt.
```bash
$ mkdir certbot
$ cd certbot/
$ wget https://dl.eff.org/certbot-auto
$ chmod a+x certbot-auto
$ ./certbot-auto certonly --standalone \
--standalone-supported-challenges http-01 \
--email your@email.address \
-d hass-example.duckdns.org
```
If you're using Docker, run the following command to generate the required keys:
```bash
sudo mkdir /etc/letsencrypt /var/lib/letsencrypt
sudo docker run -it --rm -p 80:80 --name certbot \
-v "/etc/letsencrypt:/etc/letsencrypt" \
-v "/var/lib/letsencrypt:/var/lib/letsencrypt" \
quay.io/letsencrypt/letsencrypt:latest certonly \
--standalone --standalone-supported-challenges http-01 \
--email your@email.address -d hass-example.duckdns.org
```
With either method your certificate will be generated and put in the directory `/etc/letsencrypt/live/hass-example.duckdns.org`. As the lifetime is only 90 days, you will have to repeat this every 90 days. There's a special command to simplify renewing certificates:
```bash
./certbot-auto renew --quiet --no-self-upgrade --standalone \
--standalone-supported-challenges http-01
```