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
Related posts:
How to Disconnect User from SSH Connection
How to Remove PDF Password in Linux
How to Connect to MySQL Server via SSH Tunnel
How to Convert Webpage to PDF in Linux
How to Set Password for Single User Mode in Linux
How to Create Cron Job using Shell Script
How to Fix "mv: Argument List too Long" Error
Find files with special characters in name Linux
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.