Encrypt and Decrypt files with GPG in linux

Today, I would like to share about encrypting and decrypting files with GPG in linux. Let’s take a look.

GPG, known as GNU Privacy Guard (GnuPG), is an open-source command-line tool to encrypt things like files, emails, messages and so on.


GPG usually pre-installed with most of the Linux Distributions. But just in case, you can install by the following command in terminal.

sudo apt install gnupg


Generate keys

To encrypt files, you need to generate a GPG key on the system firstly.

gpg –full-generate-key

If the options to select are asked, you can read and use default values by pressing Enter.

But you will need to fill name and email.

Checking GPG keys

You can also check your key lists as follows.

Encrypting a file

Firstly, let’s create a sample text file with some text.

And let’s encrypt a file by the following command.

The recipient argument is the email you wrote when creating key.

The above command output the encrypted file with .gpg extension. You can delete original file after encryption.

And we can see the contents of the encrypted file as follow.

Decrypting a file

You can decrypt an encrypted gpg file by the following command.

This command outputs a decrypted file named decrypted_test.txt. Now you can see correct text contents of the file.

So this is all for now and for more details, I recommend to read here.

Hope you enjoy that.

By Asahi




tel. 06-6454-8833(平日 10:00~17:00)