encrypt file in linux

How to Encrypt File in Linux

Sometimes you need to encrypt files & folders in Linux. File encryption makes them secure and protects them from attacks during transmission from one location to another. There are multiple encryption utilities in Linux that are free & easy to use. In this article, we will learn how to encrypt file in Linux.


How to Encrypt File in Linux

Here are the steps to encrypt file in Linux.


1. Encrypt file using gpg

gpg is a simple command line utility that allows you to easily encrypt files in Linux. It comes pre-installed in most Linux systems. You can encrypt files using password, or using files. We will look at how to encrypt files using passwords.

$ gpg -c file_path

In the above command, you need to specify filename or full file path to the file that you need to encrypt. If you specify only the filename, gpg command will look for it in present working directory.

Here is an example to encrypt file data.txt.

$ gpg -c data.txt

On running the above command, you will be prompted for a passphrase. It will generate another file with .gpg extension. If you view contents of this encrypted file, it will display weird characters proving that it has been encrypted indeed.

$ cat data.gpg


2. Decrypt File in Linux

You can decrypt encrypted files using the -d option on .gpg files.

$ gpg -d data.gpg

Again you will be prompted for a passphrase. On entering the correct password, gpg command will recreate your original file.


3. Encrypt Directory

Sometimes you may need to encrypt directory in Linux. gpg can only encrypt & decrypt files and not folders. So first we will archive the folder into a single .tar file and then run gpg command on the archive. Let us say you want to encrypt folder /projects

$ tar -cvf projects.tar ./projects
$ gpg -c projects.tar

In this case also, you will be asked for a password.


4. Encrypt Folder with zip

You can also encrypt folders with zip command. Here is the syntax to encrypt folder with zip command.

$ zip -r --encrypt secure.zip <directory>

You can also encrypt multiple files into a single zip file with encryption.

$ zip --encrypt secure.zip <file>...<file10>

In both the above cases, you will be prompted for a password.


5. Encrypt File Using Key

You can also encrypt file using private key in gpg but it is a little complicated process. First, run the following command to generate key.

$ gpg --full-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.

Once you have generated the key, you can encrypt the files using the following command.

$ gpg -e --recipient <email or name> <file>

Here is the example to encrypt file data.txt and email it to admin@example.com.

$ gpg -e --recipient admin@example.com data.txt

You will be asked for a passphrase to encrypt the file. You may also be asked for ‘real name’ to protect intellectual property.


6. Decrypt Encrypted File

If you want to decrypt the encrypted file, use the -d option.

$ gpg -d <file>.gpg

Here too, you will be asked for passphrase to decrypt the file.

In this article, we have learnt how to encrypt file in Linux.

Also read:

How to Repair Ubuntu 18.04 using USB
How to Monitor Disk IO Performance in Linux
How to Monitor Outgoing HTTP Requests in Linux
How to Pair Airpods Pro With Ubuntu
How to Repeat String N Times in Python

Leave a Reply

Your email address will not be published. Required fields are marked *