Image by meineresterampe from Pixabay

Encrypting an existing user home folder in Ubuntu should be a single-command task, but it got tricky for me with some complications. I’ll try to explain what issues I had and how I fixed them. I used the latest LTS 20.04.1 version of Ubuntu.

First off, you’ll probably want to encrypt the whole drive instead, since some sensitive data may reside not only in your home folder but also outside of it. For instance, you should definitely encrypt the swap file in addition to home folder encryption, as the swap file may contain all kinds of sensitive information offloaded from the memory.

This article will cover both encrypting the home directory, and encrypting the swap file.

Once again, home folder encryption is tricky, and it may ruin your home folder, so first, do a backup. Please use this guide at your own risk.

Encrypting the home folder

Start with installing eCryptFS:

sudo apt install ecryptfs-utils cryptsetup

Now you’ll need an additional local administrator user to encrypt your home folder, since when you’re logged in, some of the files in this folder may be open or inaccessible. To make sure you don’t loose anything during the encryption, this process has to be done from another user. The easiest way to create one is as follows:

sudo adduser tempadmin
sudo usermod -aG sudo tempadmin

Now log out and log in using this tempadmin user. Before running the utility, make sure you have enough free space on the drive — it should be at least 2.5 times the size of your home folder you want to encrypt.

Run the utility that’s supposed to encrypt your home folder, providing the name of the user whose home folder you want to encrypt (it’s just user in my example):

sudo ecryptfs-migrate-home -u user

If you try to run the utility and it fails due to insufficient free space, it may leave an .ecryptfs folder in your /home folder. This may prevent the utility to run the second time, but at this stage it should be safe to just remove this .ecryptfs folder, clean up the free space, and restart the utility.

Once the utility asks for the login passphrase, input the password for the user. eCryptFS will use the password that you input during logging in to decrypt your home folder, so basically the same password will be used both for logging in and for decryption.

Now, this may run smoothly for you, and after lots of log messages, you’ll see a report about successful home folder encryption. But for me, the user that I wanted to encrypt was not a local one, but a domain user from Active Directory. eCryptFS asks for its password and tries to validate it, but validating the password against Active Directory is probably too much for it, so it just says your passphrase is incorrect:

ERROR: Your login passphrase is incorrect

You could dance around with PAM modules to teach the tool how to authenticate, but I decided for a more simple solution.

The ecryptfs-migrate-home is just a script wrapper around the ecryptfs-setup-private utility. This second utility has the --nopwcheck flag instructing it not to validate the password. There’s already a patch allowing the user to specify this flag for ecryptfs-migrate-home also, however, it’s not available in the version installed from the Ubuntu 20.04 default apt repositories.

So to quickly fix this issue, I’ve edited the /usr/bin/ecryptfs-migrate-home file and added this key manually to the place where the ecryptfs-setup-private utility is called. The following line:

if ! ecryptfs-setup-private -u "$USER_NAME" -b $ECRYPTFS_SETUP_PRIVATE_ARGS; then

should be changed as follows:

if ! ecryptfs-setup-private --nopwcheck -u "$USER_NAME" -b $ECRYPTFS_SETUP_PRIVATE_ARGS; then

After restarting ecryptfs-migrate-home, the utility asked for my passphrase but didn’t validate it, which allowed me to proceed. However, if you follow this path, make sure you input the correct domain password when asked for the passphrase, as its correctness won’t be verified by the tool.

The tool starts copying your home folder to a separate location using rsync and produces a lot of output — a line for each file in your home folder. Once again, you may be lucky, and it works flawlessly. However, for me it ended with:

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1183) [sender=3.1.1

It was really hard to understand the cause of the error due to the sheer log size which had overflown my terminal. However, it appeared that the utility actually encrypted the home folder successfully.

At this step, you should log out and log in with the user whose home folder you’ve encrypted. Do not reboot the system — this will leave the encryption unfinished, and you’ll most likely lose your data.

After logging in, check if you can read from and write to your home folder. Then wait for a minute, and a window should appear prompting you to record your encryption passphrase, which means that the encryption has finished successfully:

Click “Run this action now”. A terminal window with the passphrase will open. Save this passphrase in a safe place, as it will be needed to manually recover the home folder in case you won’t be able to log in.

After verifying that everything works, check out the backup of your home folder left by the utility at /home/<username>.<random string>. It should be removed preferably with the wipe utility, at least with a single pass (see -q -Q 1 key), or even more, depending on your paranoid level:

sudo apt install wipe
wipe -r -q -Q 1 /home/user.A0oZcl8r

This is not a 100% sure way to remove all sensitive data from your drive, since you most likely use a journaled file system which keeps a lot of metadata that can’t be securely wiped even with this utility.

Encrypting the swap file

This should be a one-liner, and may be done under your regular user:

sudo ecryptfs-setup-swap

However, you may get an error similar to the following:

swapon: cannot open /dev/mapper/cryptswap1: No such file or directory

This is the output of the swapon -a command that tries to activate the new swap file but the encrypted partition is not there. Try starting the cryptdisks service:

sudo cryptdisks_start cryptswap1

And then activate the swap manually:

sudo swapon -a

You can now check if the swap works:

$ swapon -s
Filename    Type      Size      Used  Priority
/dev/dm-0   partition 33553916  0     -2

Don’t forget to wipe the previous swap file in a secure way, e.g. using the shred utility:

sudo shred -vz /swapfile

How to check if your home folder is encrypted

The easiest way is to check for the /home/.ecryptfs directory:

ls -la /home/

If it’s there, there’s a good chance your home folder is encrypted. However, to be absolutely sure, create a boot drive, boot from it, mount your local drive e.g. with the following command:

sudo mkdir /media/temp
sudo mount /dev/nvme0n1p2 /media/temp

And check the contents of your /media/temp/home/<username>/ folder which you’ve encrypted. You should see something like the following:

$ sudo ls -la /media/temp/home/user/
Access-Your-Private-Data.desktop  README.txt

If you see only those two files, this means your home folder is encrypted successfully.

Here are some of the links that have helped me in the process of encrypting my home folder: