Ready to Start Your Career?
March 8, 2017
How to Create an Encrypted Container in Linux for Cloud Storage
March 8, 2017
How to Create an Encrypted Container in Linux to Use on Cloud Storage ServicesGreetings Cybrarians.This is my first publication, and we will learn how to secure our content when we are using a third party service for Cloud Storage (i.e. Dropbox).We can use this technique for local encryption too.Of course, we have to know that the best solution is out there and its name is Open Source, like ownCloud, nextCloud, etc.But if we don't have the hardware, the knowledge or the time to build and maintenance our cloud and we are using closed source third party software like Dropbox, it's a good practice to keep our sensitive data encrypted in the Cloud and unencrypted local only.This tutorial has created, tested, and it's working on Ubuntu 16.04 LTS, but it's expected to work on any other Linux Distribution.We will use cryptsetup, a tool that it is not preinstalled in Ubuntu.
Module Check and InstallationFirst, we should check for the right module if it's loaded.
lsmod | grep dm_cryptIf we have an output, we can proceed. If not, we should load the module with the following command
sudo modprobe -v dm_cryptLet's install cryptsetup now with the following command:
sudo apt-get install cryptsetup
Creating the File ContainerWith the following command, we will create a File Container of 1GB size. In there we can create and store files and folders of our interest, usually sensitive data we don't want others to have access.
fallocate -l 1GB PRIVATENow we will encrypt the "PRIVATE" File (container) with cryptsetup.
cryptsetup -v luksFormat PRIVATEWe answer "Yes" at Warning and we write the passphrase twice.
WARNING!========This will overwrite data on PRIVATE irrevocably.Are you sure? (Type uppercase yes): YESEnter passphrase: Verify passphrase: Command successful.We can see information about the encrypted space we just created, with the following command:
cryptsetup -v luksDump PRIVATEExample Results:
LUKS header information for PRIVATEVersion: 1Cipher name: aesCipher mode: xts-plain64Hash spec: sha1Payload offset: 4096MK bits: 256..................
Opening the container and creating the filesystemTo create and store files and folders, we have to create a filesystem. That's the magic here. We will use a simple file (PRIVATE) as a container with a filesystem in where we can store other files and folders.To do that we have to open the container with the following command:
sudo cryptsetup -v luksOpen PRIVATE DECRYPTEDcryptsetup needs administrative (sudo-root) privileges to work.We need to specify a NAME for the unencrypted space to be opened.Here we use the name DECRYPTED.Keep in mind that in Linux capital letters are different than small.Now we have our space attached under /dev/mapper/DECRYPTEDExample Output:
[sudo] password for nickth: Enter passphrase for PRIVATE: Key slot 0 unlocked.Command successful.See here that it asks for our passphrase, the one we have created earlier.Now we can create our filesystem, and I prefer the well tested and reliable ext4.
sudo mkfs -t ext4 /dev/mapper/DECRYPTED
Mounting the ContainerFrom now on, we can mount the unencrypted device (the one under /dev/mapper/)
sudo mount /dev/mapper/DECRYPTED/mntLet's see if it has anything inside.
cd /mnt && lsWhat we should see is the standard lost+found directory.
Unmounting and Closing the ContainerWhen we have done with our files and folders inside the (unencrypted) container, we should use the following commands to unmount and close the container properly.
sudo umount /mnt
sudo cryptsetup luksClose DECRYPTEDOf course /mnt is not an excellent place to mounting our (unencrypted) container. Better would be to create a directory somewhere with properly rights, meaning only WE have access.The only thing we can do now is to move the closed (encrypted) file (container) named PRIVATE to our Dropbox (we took Dropbox here as an example) directory.