GPG - Encrypt and Decrypt Files with a Key on Linux (2024)

In computing, encryption is a popular and most times the recommended technique of hiding information in a secretive format. GnuPG is one of the useful tools for encrypting information (files) on Linux systems.

GnuPG (also known as GNU Privacy Guard or simply GPG) is GNU’s tool used to encrypt data and create digital signatures that contribute to overall information security. It is a complete and free implementation of the OpenPGP Internet standard that provides an advanced key management solution.

There are two versions of GPG available:

  1. gpg – a standalone version that is more suited for servers and embedded platforms.
  2. gpg2 – a version intended for desktops and requires several other modules to be installed.

In some popular Linux distributions such as Debian, the gnupg2 package is a dummy transitional package that provides symlinks from gpg2 to gpg.

This guide shows how to generate a GPG key pair, export and share public keys, encrypt a file, and share and decrypt a file using GPG in Linux systems.

It demonstrates information sharing between two parties:

  1. Kili Aaron ([emailprotected]) whose command prompt is aaronk@tecmint.
  2. Test Admin ([emailprotected]) whose command prompt is root@server.

The file shared between the two parties is called secret.txt, which contains a highly sensitive password that the Test Admin wants to share with user Kili Aaron.

You can view the contents of the secret.txt file that contains the password and other remote access details using the following cat command as shown. It exists on the Test Admin’s server:

# cat secret.txt
GPG - Encrypt and Decrypt Files with a Key on Linux (1)

Install GnuPG (GNU Privacy Guard) on Linux

To install the GnuPG package, run the appropriate command for your Linux distribution as shown. Note that the gnupg package must be installed on the two systems sharing data.

$ sudo apt install gnupg [On Debian, Ubuntu and Mint]$ sudo yum install gnupg [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]$ sudo emerge -a app-crypt/gnupg [On Gentoo Linux]$ sudo apk add gnupg [On Alpine Linux]$ sudo pacman -S gnupg [On Arch Linux]$ sudo zypper install gnupg [On OpenSUSE] 

Generating New GPG Key Pairs in Linux

To generate new key pairs (public and private), run the gpg command with the --full-generate-key flag on both systems and follow the prompts to define the kind of key, the key size, how long the key should be valid, a user ID to identify your key, and a secure passphrase for the key as shown in the screenshot that follows.

$ gpg --full-generate-key
GPG - Encrypt and Decrypt Files with a Key on Linux (2)

List GPG Key Pairs in Linux

To list the public GPG key you have just created together with other existing keys, run the gpg command with the --list-public-keys flag. To perform a long listing, add the --keyid-format=long flag.

$ gpg --list-public-keysOR$ gpg --list-public-keys --keyid-format=long
GPG - Encrypt and Decrypt Files with a Key on Linux (3)

To list the secret GPG key you have just created together with other existing keys, run the gpg command with the --list-secret-keys flag. To perform a long listing, add the --keyid-format=long flag.

$ gpg --list-secret-keysOR$ gpg --list-secret-keys --keyid-format=long
GPG - Encrypt and Decrypt Files with a Key on Linux (4)

Export Keys with GPG in Linux

Once the GPG key pairs have been generated on both sides, the two parties can export their public keys into a file and share via email or other means.

--------- On Kili Aaron Server --------- $ gpg --list-public-keys$ gpg --export -o aaronsec.key 15B4814FB0F21208FB5076E7A937C15009BAC996--------- On Test Admin Server ---------# gpg --list-public-keys# gpg --export -o tadminsec.key BC39679E5FF48D4A6AEF6F3437211F0B4D6D8A61
GPG - Encrypt and Decrypt Files with a Key on Linux (5)

Import Keys with GPG in Linux

Next, exchange the public keys either via email or secure other means such as using the scp command as shown:

$ scp aaronsec.key [emailprotected]:/root/$ scp [emailprotected]:/root/tadminsec.key ./
GPG - Encrypt and Decrypt Files with a Key on Linux (6)

Next, import the public key from the opposite end into the local system public keyring by adding the --import flag as shown.

# gpg --import aaronsec.key# gpg --import tadminsec.key
GPG - Encrypt and Decrypt Files with a Key on Linux (7)

To check if the imported public key exists in the local system keyring, list the available public keys as shown.

# gpg --list-public-keys
GPG - Encrypt and Decrypt Files with a Key on Linux (8)

Encrypting Files Using GPG in Linux

Now let’s look at how to encrypt the secret file using gpg keys. For this section, we will run the commands on the Test Admin’s server.

To encrypt a plain text file using the just created GPG key pair, run the following command. The -e or --encrypt flag enables encryption and the -r or --recipient flag is used to specify the recipient ID and secret.txt is the plain text file to be encrypted.

The following command encrypts the file secret.txt using the recipient [emailprotected]’s public key:

#gpg -e -r [emailprotected] secret.txt OR# gpg --encrypt --recipient [emailprotected] secret.txt

If the previous command run successfully, a new file (the original filename ending with .gpg extension) will be generated in the current directory:

$ ls secret.txt.gpg
GPG - Encrypt and Decrypt Files with a Key on Linux (9)

To store the encrypted information in a different file, use the -o or --output option followed by a filename. In this example, the preferred filename is node_configs:

# gpg -e -r [emailprotected] -o node_configs secret.txtOR# gpg --encrypt --recipient [emailprotected] --output node_configs secret.txt

Now share the encrypted file with your partner via email or other secure means.

Decrypting Files Using GPG in Linux

To decrypt a file encrypted using gpg, add the -d or --decrypt flag and specify the encrypted filename. By default, the decrypted information will be displayed in standard output. You can store it in a file using the -o flag as shown.

$ gpg -d -o secrets.txt secrets.txt.gpg$ ls secrets.txt
GPG - Encrypt and Decrypt Files with a Key on Linux (10)

You might also like:

  • 10 Best Linux File and Disk Encryption Tools
  • 5 Best Software and Apps for Linux with Data Encryption
  • How to Encrypt and Decrypt Files and Directories in Linux

For more information, see the gpg/gpg2 man page as shown.

$ man gpgOR$ man gpg2

That’s it for the scope of this guide. GPG is a commonly used tool for encrypting and decrypting information or files in Linux. If you have any comments to share about this guide, use the feedback form below.

GPG - Encrypt and Decrypt Files with a Key on Linux (2024)
Top Articles
ADHD: Why can I only focus at night? - Sanctum Healthcare
57 Small Changes You Can Easily Make to Improve Your Life
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
How To Cut Eelgrass Grounded
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Umn Biology
Obituaries, 2001 | El Paso County, TXGenWeb
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Colin Donnell Lpsg
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
San Pedro Sula To Miami Google Flights
Selly Medaline
Latest Posts
Article information

Author: Duncan Muller

Last Updated:

Views: 6271

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Duncan Muller

Birthday: 1997-01-13

Address: Apt. 505 914 Phillip Crossroad, O'Konborough, NV 62411

Phone: +8555305800947

Job: Construction Agent

Hobby: Shopping, Table tennis, Snowboarding, Rafting, Motor sports, Homebrewing, Taxidermy

Introduction: My name is Duncan Muller, I am a enchanting, good, gentle, modern, tasty, nice, elegant person who loves writing and wants to share my knowledge and understanding with you.