How to Encrypt a File in Ubuntu Using GPG?

GPG (GNU Privacy Guard) is a powerful command-line tool that uses public key cryptography to secure your files and communications. It allows you to encrypt, decrypt, and sign data, ensuring that your sensitive information remains protected.

In this guide, we’ll walk you through how to install GPG, generate a key pair, and use it to encrypt and decrypt files on Ubuntu.

 

Step 1: Install GPG on Ubuntu

Start by updating your system and installing GPG:

sudo apt update
sudo apt install gnupg -y

This will install the necessary tools to manage GPG keys and perform encryption tasks.

 

Step 2: Generate a New GPG Key Pair

Create a new GPG key by running:

gpg --full-generate-key

You’ll be prompted to:

Select key type (default is RSA and RSA)

Set key size (2048 or 4096 bits recommended)

Define expiration date

Enter your name and email address for the key ID

Once complete, your key will be generated and ready to use.

 

Step 3: List Existing GPG Keys

To view the keys available on your system:

gpg --list-keys

This helps confirm that your key has been created and is accessible.

 

Step 4: Encrypt a File Using GPG

Use the following command to encrypt a file:

gpg -e -r recipient_email filename

Replace recipient_email with the email used when generating the GPG key, and filename with the file you want to secure.

This creates a new encrypted file named filename.gpg.

 

Step 5: Decrypt the Encrypted File

To decrypt the .gpg file and reveal its content:

gpg -d filename.gpg

You’ll be prompted for the private key’s passphrase (if one was set), after which the file contents will be displayed or extracted.

 

Conclusion

You’ve now learned how to install GPG, generate encryption keys, and use them to encrypt and decrypt files on Ubuntu. GPG is an essential tool for securing sensitive documents, scripts, and communication in both personal and professional environments.

Facebook
Twitter
LinkedIn
Reddit
WhatsApp
Telegram
Email
Print