encrypt folders in linux

How to Encrypt Folder in Linux

Sometimes you may need to encrypt folder in Linux. Linux provides many simple tools for this purpose. gpg is the most popular tool used for file encryption in Linux. It is already installed in most Linux distributions and does not require additional setup. In this article, we will learn how to encrypt folder in Linux. You can use these steps on almost every Linux system.

How to Encrypt Folder in Linux

Here are the steps to encrypt folder in Linux.

1. Convert Directory to File

gpg utility can encrypt only files. So first we simply archive the folder into a .tar file. Let us say you have a folder called /home/data on your system. Here is the command to convert directory into a .tar archive.

$ tar czf data.tar.gz /home/data

2. Prepare Key

Run the following command to generate key for encryption.

$ gpg --gen-key

You will see a list of options, asking you to select the kind of key to be generated and the kind of encryption method for the key. Next, you will be asked for the key length. To select the default option in each case, just hit enter key.

Finally, you will be asked if you want to set an expiration for this key. If you want to use it forever, enter 0.

Next, you will be asked to enter a passphrase for the key. This passphrase is for your key and the file you will be encrypting using this key. It is to protect the key file in case it is stolen.

3. Encrypt File

Next, use gpg command to encrypt .tar.gz file.

$ gpg -e -r USERNAME ~USERNAME/filename

Here is the example command to encrypt our .tar.gz file. Replace

$ gpg -e -r ubuntu ~ubuntu/data.tar.gz

This will create encrypted file data.tar.gz.gpg that you can use, in the same folder as input file.

4. Decrypt File

If you want to decrypt the file, run the following command.

$ gpg -d -o ~USERNAME/decrypted ~USERNAME/filename

Here is the command to decrypt our data.tar.gz.gpg file created above.

$ gpg -d -o ~USERNAME/decrypted ~USERNAME/data.tar.gz.gpg

In this article, we have learnt how to encrypt and decrypt folders in Linux. The key is to convert folder into a file and then use gpg tool to encrypt it.

Also read:

How to Use SSH Instead of HTTPS in Git
How to Encrypt File in Linux
How to Repair Ubuntu 18.04 using USB
How to Monitor Disk IO Performance in Linux
How to Monitor Outgoing HTTP Requests in Linux

Leave a Reply

Your email address will not be published.