Mount An Encrypted Drive Using cryptsetup

What Is This About?

This is how to mount a LUKS encrypted drive with cryptsetup in Ubuntu 20.10 (Groovy Gorilla). My use-case is I have a headless server that has an external USB drive that's encrypted with LUKS that I need to mount from the command line. I like the flexibility it gives a little more than the what the Gnome GUI does so I'll probably use it on my desktop as well. I previously documented how to do it with udiskctl in this post but I like cryptsetup a little better. This assumes that you know the partition/device (in my case it's /dev/sdb1). That previous post shows how I found it so I'll just do the decrypting and mounting here.

So, How Do You Do It?

The syntax to decrypt the device is:

sudo cryptsetup open <device> <name>

As I mentioned above, in this case my device is /dev/sdb1. The name can be an arbitrary one (although there are limitiations on special characters so let's say it can be any alpha-numeric name) and I'll use wddata as the name. Then my command to decrypt the drive is:

sudo cryptsetup open /dev/sdb1 wddata

This will prompt you for your password and then the encryption passphrase. Once the command succeeds, it adds a link in /dev/mapper using the name you passed in so you can mount the device (assuming the directory you want to mount it in exists) like this:

sudo mount /dev/mapper/wddata /media/data

Where wddata is the name I told cryptsetup to use and /media/data is a directory that I had previously created.

Source

Besides the man-page for cryptsetup on my server, I was pointed to use cryptsetup from this StackOverflow post:

The accepted answer uses udiskctl but further down is an answer by George Schölly using cryptsetup. The syntax in the answer is the older cryptsetup syntax (which should still work) so it doesn't look exactly like what I used.