gpg Linux files text encryption

Encrypting Linux Files and Text with GPG

This article introduces how to encrypt Linux files and text with GPG, an encryption/decryption software that can be used from the Linux PC/server command line. Passphrase only. Public key authentication…

Shou Arisaka
2 min read
Oct 14, 2025

This article introduces how to encrypt Linux files and text with GPG, an encryption/decryption software that can be used from the Linux PC/server command line.

Passphrase Only

$ cat > doc.txt
contents here.
$ gpg --symmetric doc.txt
gpg: gpg-agent is not available in this session
$ ls
total 0
 13792273860433232 -rwxrwxrwx 1 yuis yuis   92 Apr 30 13:35 doc.txt.gpg
 12947848930301330 drwxrwxrwx 1 yuis yuis 4096 Apr 30 13:35 .
  8162774326223499 -rwxrwxrwx 1 yuis yuis   15 Apr 30 13:34 doc.txt
166351711236005864 drwxrwxrwx 1 yuis yuis 4096 Apr 30 13:34 ..
$ gpg --decrypt doc.txt.gpg^C
$ cat doc.txt.gpg
♦♥☻R�D u�D∟`�K☺U�e�↕←�q�<(�NCsh§`�>� ՞☺x�♦∟ޑ,a�Y�>2�☻0�t��↑�4��*m�Y¶��q
&\�$ gpg --decrypt doc.txt.gpg
gpg: AES encrypted data
gpg: gpg-agent is not available in this session
gpg: encrypted with 1 passphrase
contents here.

The above commands are executed in the following steps:

  1. Encrypt with gpg --symmetric doc.txt
  2. Enter passphrase
  3. doc.txt.gpg is generated
  4. Decrypt with gpg --decrypt doc.txt.gpg
  5. Enter passphrase
  6. doc.txt is generated

Public Key Authentication

gpg2 --list-keys
    # Set email address and passphrase e.g. [email protected]

cat << EOT > file.txt
content.
EOT

gpg --encrypt --recipient '[email protected]' file.txt
# or
# gpg --encrypt --recipient '[email protected]' <<< 'content.' > file.txt.gpg

gpg --decrypt file.txt.gpg
# or
# gpg --output out.txt --decrypt hoge.txt.gpg

The above commands are executed in the following steps:

  1. Display list of public keys with gpg2 --list-keys
  2. Create file with cat << EOT > file.txt
  3. Encrypt with gpg --encrypt
  4. Enter email address
  5. file.txt.gpg is generated
  6. Decrypt with gpg --decrypt file.txt.gpg
  7. Enter passphrase
  8. file.txt is generated

Share this article

Shou Arisaka Oct 14, 2025

🔗 Copy Links