One place for hosting & domains

      How To Use Certbot Standalone Mode to Retrieve Let’s Encrypt SSL Certificates on Ubuntu 20.04


      Introduction

      Let’s Encrypt is a service offering free SSL certificates through an automated API. The most popular Let’s Encrypt client is EFF’s Certbot.

      Certbot offers a variety of ways to validate your domain, fetch certificates, and automatically configure Apache and Nginx. In this tutorial, we’ll discuss Certbot’s standalone mode and how to use it to secure other types of services, such as a mail server or a message broker like RabbitMQ.

      We won’t discuss the details of SSL configuration, but when you are done you will have a valid certificate that is automatically renewed. Additionally, you will be able to automate reloading your service to pick up the renewed certificate.

      Prerequisites

      Before starting this tutorial, you will need:

      • An Ubuntu 20.04 server with a non-root, sudo-enabled user and basic firewall set up, as detailed in this Ubuntu 20.04 server setup tutorial.
      • A domain name pointed at your server. If you are using a DigitalOcean Droplet, you can accomplish this by following our “Domains and DNS” documentation. This tutorial will use your_domain throughout.
      • Port 80 or 443 must be unused on your server. If the service you’re trying to secure is on a machine with a web server that occupies both of those ports, you’ll need to use a different mode such as Certbot’s webroot mode.

      Step 1 — Installing Certbot

      Certbot recommends using their snap package for installation. Snap packages work on nearly all Linux flavours, but they required that you’ve installed snapd first in order to manage snap packages. Ubuntu 20.04 comes with support for snaps out of the box, so you can start by making sure your snapd core is up to date:

      • sudo snap install core; sudo snap refresh core

      If you’re working on a server that previously had an older version of certbot installed, you should remove it before going any further:

      After that, you can install the certbot package:

      • sudo snap install --classic certbot

      Finally, you can link the certbot command from the snap install directory to your path, so you’ll be able to run it by just typing certbot. This isn’t necessary with all packages, but snaps tend to be less intrusive by default, so they don’t conflict with any other system packages by accident:

      • sudo ln -s /snap/bin/certbot /usr/bin/certbot

      Now that we have Certbot installed, let’s run it to get our certificate.

      Step 2 — Running Certbot

      Certbot needs to answer a cryptographic challenge issued by the Let’s Encrypt API in order to prove we control our domain. It uses ports 80 (HTTP) or 443 (HTTPS) to accomplish this. Open up the appropriate port(s) in your firewall:

      Output

      Rule added Rule added (v6)

      We can now run Certbot to get our certificate. We’ll use the --standalone option to tell Certbot to handle the challenge using its own built-in web server. Finally, the -d flag is used to specify the domain you’re requesting a certificate for. You can add multiple -d options to cover multiple domains in one certificate.

      • sudo certbot certonly --standalone -d your_domain

      When running the command, you will be prompted to enter an email address and agree to the terms of service. After doing so, you should see a message telling you the process was successful and where your certificates are stored:

      Output

      IMPORTANT NOTES: Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem Key is saved at: /etc/letsencrypt/live/your_domain/privkey.pem This certificate expires on 2022-02-10. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - If you like Certbot, please consider supporting our work by: * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate * Donating to EFF: https://eff.org/donate-le

      You should now have your certificates. In the next step, we will inspect some of the files that we downloaded and learn about their functionality.

      Step 3 — Configuring Your Application

      Configuring your application for SSL is beyond the scope of this article, as each application has different requirements and configuration options, but let’s take a look at what Certbot has downloaded for us. Use ls to list out the directory that holds our keys and certificates:

      • sudo ls /etc/letsencrypt/live/your_domain

      Output

      cert.pem chain.pem fullchain.pem privkey.pem README

      The README file in this directory has more information about each of these files. Most often you’ll only need two of these files:

      • privkey.pem: This is the private key for the certificate. This needs to be kept safe and secret, which is why most of the /etc/letsencrypt directory has very restrictive permissions and is accessible by only the root user. Most software configuration will refer to this as something similar to ssl-certificate-key or ssl-certificate-key-file.
      • fullchain.pem: This is our certificate, bundled with all intermediate certificates. Most software will use this file for the actual certificate, and will refer to it in their configuration with a name like ‘ssl-certificate’.

      For more information on the other files present, refer to the “[Where are my certificateshttps://eff-certbot.readthedocs.io/en/stable/using.html#where-are-my-certificates)” section of the Certbot docs.

      Some software will need its certificates in other formats, in other locations, or with other user permissions. It is best to leave everything in the letsencrypt directory, and not change any permissions in there (permissions will just be overwritten upon renewal anyway), but sometimes that’s just not an option. In that case, you’ll need to write a script to move files and change permissions as needed. This script will need to be run whenever Certbot renews the certificates, which we’ll talk about next.

      Step 4 — Handling Certbot Automatic Renewals

      Let’s Encrypt’s certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process. The certbot package we installed takes care of this for us by adding a renew script to /etc/cron.d. This script runs twice a day and will renew any certificate that’s within thirty days of expiration.

      With our certificates renewing automatically, we still need a way to run other tasks after a renewal. We need to at least restart or reload our server to pick up the new certificates, and as mentioned in Step 3 we may need to manipulate the certificate files in some way to make them work with the software we’re using. This is the purpose of Certbot’s renew_hook option.

      To add a renew_hook, we update Certbot’s renewal config file. Certbot remembers all the details of how you first fetched the certificate, and will run with the same options upon renewal. We just need to add in our hook. Open the config file with you favorite editor:

      • sudo nano /etc/letsencrypt/renewal/your_domain.conf

      A text file will open with some configuration options. You can add a hook on the last line that will reload any web-facing services, making them use the renewed certificate:

      your_domain.conf’>/etc/letsencrypt/renewal/your_domain.conf

      renew_hook = systemctl reload your_service
      

      Update the command above to whatever you need to run to reload your server or run your custom file munging script. Usually, on Ubuntu, you’ll mostly be using systemctl to reload a service. Save and close the file, then run a Certbot dry run to make sure the syntax is ok:

      • sudo certbot renew --dry-run

      If you see no errors, you’re all set. Certbot is set to renew when necessary and run any commands needed to get your service using the new files.

      Conclusion

      In this tutorial, we’ve installed the Certbot Let’s Encrypt client, downloaded an SSL certificate using standalone mode, and enabled automatic renewals with renew hooks. This should give you a good start on using Let’s Encrypt certificates with services other than your typical web server.

      For more information, please refer to Certbot’s documentation.



      Source link

      How To Create Let’s Encrypt Wildcard Certificates with Certbot


      Introduction

      A wildcard certificate is an SSL certificate that can secure any number of subdomains with a single certificate. You may want a wildcard certificate in cases where you need to support multiple subdomains but don’t want to configure them all individually.

      Let’s Encrypt is an SSL certificate authority that grants free certificates using an automated API. In this tutorial you will create a Let’s Encrypt wildcard certificate by following these steps:

      1. Making sure you have your DNS set up correctly
      2. Installing the Certbot plugins needed to complete DNS-based challenges
      3. Authorizing Certbot to access to your DNS provider
      4. Fetching your certificates

      This information is intended to be useful for any Linux distribution and any server software, but you may have to fill in some gaps with further documentation, which we will link to as we go.

      Prerequisites

      This tutorial assumes you already have the following:

      • The Certbot utility installed, version 0.22.0 or later. If you need help installing Certbot, please visit our Let's Encrypt tag page, where you can find installation guides for a variety of Linux distributions and servers. Some common setups are listed below:
      • A domain name, and a DNS provider that is supported by Certbot. See Certbot’s DNS plugin list for a list of supported providers

      Let’s begin by setting up and testing our DNS records.

      Step 1 — Setting up Wildcard DNS

      Before we fetch our wildcard SSL certificate, we should make sure our server is responding to requests on multiple subdomains. This will typically be accomplished by setting up a wildcard DNS record, which looks similar to this:

      *.example.com.   3600  IN  A  203.0.113.1
      

      The * wildcard character is treated as a stand-in for any hostname. This example DNS record would match one.example.com, and two.example.com. It would not match the bare example.com nor would it match one.two.example.com because the * wildcard will only expand to one hostname, not to multiple levels of names.

      Additionally a wildcard DNS record can only have one wildcard character, so *.*.example.com is not allowed.

      Please refer to your DNS provider’s documentation to set up the correct DNS entries. You will want to add either an A or CNAME wildcard record before proceeding.

      Note: If you are using DigitalOcean to manage your DNS, please see How to Create, Edit, and Delete DNS Records in our product documentation for more information.

      To test that your wildcard DNS is working as intended, use the host command to query a few hostnames:

      Be sure to substitute your own domain and hostname above. Also, remember that it sometimes takes a few minutes for DNS records to propagate through the system. If you just added your DNS record and are getting errors, wait a few minutes and try again.

      When the hostname you entered resolves properly, you’ll output similar to the following:

      Output

      one.example.com has address 203.0.113.1

      Otherwise, you’ll see an NXDOMAIN error:

      Output

      Host one.example.com not found: 3(NXDOMAIN)

      Once you’ve verified that multiple subdomains are resolving to your server, you can continue on to the next step, where you’ll configure Certbot to connect to your DNS provider.

      Step 2 — Installing the Correct Certbot DNS Plugin

      Before issuing certificates, Let’s Encrypt performs a challenge to verify that you control the hosts you’re requesting certificates for. In the case of a wildcard certificate, we need to prove that we control the entire domain. We do this by responding to a DNS-based challenge, where Certbot answers the challenge by creating a special DNS record in the target domain. Let’s Encrypt’s servers then verify this record before issuing the certificate.

      In order to connect to your DNS provider, Certbot needs a plugin. Please see Certbot’s DNS plugin list to get the name of the appropriate plugin for your DNS provider.

      For instance, the DigitalOcean provider is called certbot-dns-digitalocean. We can install the certbot-dns-digitalocean plugin on Ubuntu and Debian by installing the following package:

      • sudo apt install python3-certbot-dns-digitalocean

      Other plugins should follow the same naming format. Swap your provider’s name into the command above if you’re using a different service.

      On CentOS and other RPM-based distributions the installation command may be dnf:

      • dnf install python3-certbot-dns-digitalocean

      Or yum:

      • yum install python3-certbot-dns-digitalocean

      You also may need to install additional package repositories on these distributions to get access to the Certbot plugin packages.

      To verify that the plugin was installed correctly, you can ask Certbot to list its current plugins:

      Output

      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * dns-digitalocean Description: Obtain certs using a DNS TXT record (if you are using DigitalOcean for DNS). Interfaces: IAuthenticator, IPlugin Entry point: dns-digitalocean = certbot_dns_digitalocean.dns_digitalocean:Authenticator * standalone Description: Spin up a temporary webserver Interfaces: IAuthenticator, IPlugin Entry point: standalone = certbot.plugins.standalone:Authenticator * webroot Description: Place files in webroot directory Interfaces: IAuthenticator, IPlugin Entry point: webroot = certbot.plugins.webroot:Authenticator - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

      In the above output, the dns-digitalocean plugin is listed first, along with the default standalone and webroot plugins.

      When you have verified that the correct plugin is installed, continue on to the next step to configure it.

      Step 3 — Configuring the Certbot Plugin

      Because Certbot needs to connect to your DNS provider and create DNS records on your behalf, you’ll need to give it permission to do so. This involves getting an API token or other authentication information from your DNS provider, and putting it in a secure credentials file that Certbot will later read from.

      Because each provider has a different authentication process, please refer to the documentation for your particular Certbot DNS plugin for more information on what tokens or keys you’ll need to obtain.

      For this example, we will continue using the dns-digitalocean plugin, and will store our credentials in the file ~/certbot-creds.ini.

      We will create this file using the nano text editor:

      This will open up a new blank text file. You’ll want to add your information based on the instructions for your particular DNS provider. DigitalOcean requires a single API token, so it will look like this:

      ~/certbot-creds.ini

      dns_digitalocean_token = 235dea9d8856f5b0df87af5edc7b4491a92745ef617073f3ed8820b5a10c80d2
      

      Be sure to replace the example token above with your own information.

      Save and close the file. If you’re using nano, type CTRL+O (for “write out”), hit ENTER, then CTRL+X to exit.

      After creating the file, you will need to restrict its permissions so that your secret is not leaked to other users. The following chmod command will give read and write access to only your user:

      • chmod 600 ~/certbot-creds.ini

      Once you’ve set up your credentials file, you’re ready to actually request the certificate.

      Step 4 — Retrieving the Certificate

      At this point, retrieving your Let’s Encrypt wildcard certificate is similar to “normal” non-wildcard certificates. The main changes to the process are to specify the DNS-based challenge, and point to our DNS credentials file. Additionally we’ll use a wildcard domain with the -d flag:

      • sudo certbot certonly
      • --dns-digitalocean
      • --dns-digitalocean-credentials ~/certbot-creds.ini
      • -d '*.example.com'

      Note that you cannot use the --nginx or --apache plugins to automatically configure those servers with a wildcard certificate. We use the certonly command instead, to only download the certificate.

      When running the above command, you may be presented with a few questions to answer if this is your first time running Certbot. After answering them, Cerbot will perform the challenge, the Let’s Encrypt servers will verify it, and your new certificate will be downloaded and saved to /etc/letsencrypt/. You should see output similar to the following:

      Output

      IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2021-09-27. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

      You have successfully generated a wildcard SSL certificate! Your next step is to configure your server application to use it. We’ll link to some resources that can help with this in the next section.

      Conclusion

      In this tutorial you configured Certbot and downloaded a wildcard SSL certificate from the Let’s Encrypt certificate authority. You are now ready to configure your server software to use this certificate to secure its connections.

      For more information on what certificate files were downloaded, and how to handle gracefully restarting your applications when Certbot automatically updates your certificates, take a look at Steps 3 and 4 of our tutorial How To Use Certbot Standalone Mode to Retrieve Let’s Encrypt SSL Certificates on Ubuntu 18.04.



      Source link

      How To Acquire a Let’s Encrypt Certificate Using DNS Validation with certbot-dns-digitalocean on Ubuntu 20.04


      The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      The majority of Let’s Encrypt certificates are issued using HTTP validation, which allows for the installation of certificates on a single server. However, HTTP validation is not always suitable for issuing certificates for use on load-balanced websites, nor can you use this validation to issue wildcard certificates.

      DNS validation allows for certificate issuance requests to be verified using DNS records, rather than by serving content over HTTP. This means that certificates can be issued simultaneously for a cluster of web servers running behind a load balancer, or for a system that isn’t directly accessible over the internet.

      In this tutorial, you will use the certbot-dns-digitalocean hook for Certbot to issue a Let’s Encrypt certificate using DNS validation via the DigitalOcean API.

      You can use the certbot-dns-digitalocean tool to integrate Certbot with DigitalOcean’s DNS management API, allowing the certificate validation records to be automatically configured on-the-fly when you request a certificate.

      Another key benefit of certbot-dns-digitalocean is that you can use it to issue certificates for individual servers that may be running behind a load balancer, or are otherwise not directly accessible over HTTP. In these cases, you can’t use traditional HTTP certificate validation, unless you set the validation files on each and every server, which can be inconvenient. The certbot-dns-digitalocean tool is also useful if you want to issue a certificate for a server that isn’t accessible over the internet, for example an internal system or staging environment.

      certbot-dns-digitalocean also fully supports wildcard certificates, which can only be issued using DNS validation.

      Prerequisites

      To complete this tutorial, you will need:

      • An Ubuntu 20.04 server set up by following the Initial Server Setup with Ubuntu 20.04, including a sudo non-root user.

      • A domain name managed via your DigitalOcean account—that is, for managing DNS records. In this particular example, we will use your_domain and subdomain.your_domain, as well as *.your_domain for a wildcard certificate, however you can adjust this for other domains or subdomains if required.

      • A DigitalOcean API key (Personal Access Token) with read and write permissions. To create one, visit How to Create a Personal Access Token.

      Once you have these ready, log in to your server as your non-root user to begin.

      Step 1 — Installing Certbot

      In this step, you will install Certbot, which is a program to issue and manage Let’s Encrypt certificates.

      Certbot is available within the official Ubuntu Apt repositories, so you can install it using the default system package manager:

      • sudo apt update
      • sudo apt install certbot

      Once the installation has completed, you can check with the following command:

      This will output something similar to the following:

      Output

      certbot 0.40.0

      In this step you installed Certbot. Next, you will download and install the acme-dns-certbot hook.

      Step 2 — Installing and Configuring certbot-dns-digitalocean

      Now that you’ve installed the base Certbot program, you can download and install certbot-dns-digitalocean, which will allow Certbot to operate in DNS validation mode using the DigitalOcean DNS management API.

      Like Certbot itself, which you installed in Step 1, the certbot-dns-digitalocean utility is available within Ubuntu’s default repositories. However, the Certbot repository contains a more reliably updated version, so it is always recommended to use this where possible.

      Continue by installing the package for certbot-dns-digitalocean:

      • sudo apt install python3-certbot-dns-digitalocean

      Once the installation has completed, you need to set up a configuration file containing the DigitalOcean API key/Personal Access Token that you generated as part of the prerequisites.

      Begin by creating the creds.ini file in a private location:

      • touch ~/certbot-creds.ini

      Next, restrict the permissions on the file in order to make sure no other user on your server can read it:

      • chmod go-rwx ~/certbot-creds.ini

      Finally, open the file using your text editor and add your DigitalOcean access token:

      The content of the file will be as follows:

      ~/certbot-creds.ini

      dns_digitalocean_token = your_digitalocean_access_token
      

      Once done, save and close the file.

      Warning: Your DigitalOcean access token grants access to your DigitalOcean account, so you must protect it as you would a password. Do not share it with anyone or check it into a public code repository.

      In this step, you downloaded and installed the certbot-dns-digitalocean utility and created a configuration file containing your API credentials.

      Step 3 — Issuing a Certificate

      In this step, you’ll issue a certificate using Certbot and the DigitalOcean API.

      To issue your first certificate, run Certbot using the following arguments, making sure to specify the correct path to your credentials file as well as your domains:

      • sudo certbot certonly --dns-digitalocean --dns-digitalocean-credentials ~/certbot-creds.ini -d your_domain -d subdomain.your_domain

      Note: If you see an unsafe permissions on credentials configuration file warning, this indicates that the file permissions have not been correctly restricted, thus allowing other users on your server to access your token. Please double-check with the chmod command in Step 2.

      Certbot will take a few seconds to request the certificate; you will then receive a message confirming that it has issued your certificate:

      Output

      ... Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/your_domain/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/your_domain/privkey.pem ...

      In the event that the certificate issuance fails, this may be because there wasn’t sufficient time for the DNS changes to propagate. You can optionally increase the DNS propagation delay to give more time for the verification DNS records to propagate and be picked up by Let’s Encrypt. The delay is 10 seconds by default, but you can increase this using the --dns-digitalocean-propagation-seconds argument:

      • sudo certbot certonly --dns-digitalocean --dns-digitalocean-credentials ~/certbot-creds.ini --dns-digitalocean-propagation-seconds 30 -d your_domain -d subdomain.your_domain

      Finally, you can also use certbot-dns-digitalocean to issue wildcard certificates for your domain:

      • sudo certbot certonly --dns-digitalocean --dns-digitalocean-credentials ~/certbot-creds.ini -d *.your_domain

      Note: In some cases, requesting multiple certificates for the same hostnames in a short time period can cause issuance to begin failing. This is due to rate limits and the DNS time-to-live (TTL) value, which can sometimes cause delays in new DNS changes being propagated.

      To mitigate this, you may wish to wait out the duration of the TTL, or consider adjusting the --dns-digitalocean-propagation-seconds option that was detailed earlier in this step.

      In this step, you used Certbot with certbot-dns-digitalocean for the first time and issued your initial certificates.

      Step 4 — Renewing Certificates

      In this final step, you will renew certificates using Certbot with certbot-dns-digitalocean.

      Once your certificates are nearing expiry, Certbot is able to automatically renew them for you:

      The renewal process can run start-to-finish without user interaction. It will also remember the configuration options that you specified during the initial setup.

      By default, Certbot will run this as an automatic scheduled system task, meaning that no further maintenance is needed for your certificates. You can check that the scheduled task has been correctly installed by printing out the status of the associated system service, which is certbot.timer:

      • sudo systemctl status certbot.timer

      This will output something similar to the following, which shows the loaded task scheduled to run twice per day:

      Output

      ● certbot.timer - Run certbot twice daily Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled) Active: active (waiting) since Sun 2020-11-22 18:18:40 UTC; 2 weeks 6 days ago Trigger: Sun 2020-12-13 7:17:57 UTC; 11h left Nov 22 18:18:40 droplet1 systemd[1]: Started Run certbot twice daily.

      However, to test that this is working without having to wait until nearer the expiry date of your certificate(s), you can trigger a ‘dry run’. This will simulate the renewal process without making any actual changes to your configuration.

      You can trigger a dry run using the standard renew command, but with the --dry-run argument:

      • sudo certbot renew --dry-run

      This will output something similar to the following, which will provide assurance that the renewal process is functioning correctly:

      Output

      ... Cert not due for renewal, but simulating renewal for dry run Plugins selected: Authenticator dns-digitalocean, Installer None Renewing an existing certificate Performing the following challenges: dns-01 challenge for your_domain dns-01 challenge for subdomain.your_domain Waiting 10 seconds for DNS changes to propagate Waiting for verification... Cleaning up challenges ...

      In this final step, you tested the automatic renewal process within Certbot.

      Conclusion

      In this tutorial, you set up Certbot with certbot-dns-digitalocean to issue certificates using DNS validation with the DigitalOcean DNS management API.

      If you’re interested in learning more about certbot-dns-digitalocean, you may wish to review the official documentation for the utility:

      Alternatively, if you aren’t using DigitalOcean to manage your DNS records, you may wish to check out How to Acquire a Let’s Encrypt Certificate using DNS Validation with acme-dns-certbot on Ubuntu 18.04, which is a provider-agnostic alternative to certbot-dns-digitalocean.

      Finally, if you would like some further technical reading, you could dig into the details of ACME DNS validation by reviewing the relevant section of the official RFC document, which outlines how the process works:



      Source link