Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Encrypt a File in Linux: When and How

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Encrypting a file in Linux protects its contents from anyone who doesn't have the decryption key or passphrase. The simplest way is to use gpg (GNU Privacy Guard), which is preinstalled on most distributions. With one command, you can encrypt a file symmetrically (using a passphrase) or for a specific recipient (using their public key). You should encrypt files that contain sensitive data—passwords, financial records, personal identification, or anything you wouldn't want read if your laptop or backup drive is lost or stolen.

What You'll Need

  • A Linux system with gpg installed (check with gpg --version; if missing, install it via your package manager, e.g., sudo apt install gnupg on Debian/Ubuntu).
  • A terminal window.
  • The file you want to encrypt.
  • For asymmetric encryption: the recipient's public key (or your own if encrypting for yourself).

Step-by-Step

  1. Open a terminal and navigate to the directory containing the file you want to encrypt. Use cd /path/to/directory.
  2. Encrypt with a passphrase (symmetric encryption): Run gpg -c filename. Replace filename with your actual file name. You'll be prompted to enter a passphrase twice. The output will be a new file named filename.gpg. This is the encrypted version; the original file remains unchanged.
  3. Encrypt for a specific recipient (asymmetric encryption): If you have the recipient's public key imported (see Tips), run gpg -e -r recipient@example.com filename. This creates filename.gpg that only the holder of the matching private key can decrypt.
  4. Verify the encrypted file: Use ls -l filename.gpg to confirm the file exists and is not empty. You can also run file filename.gpg to see that it's recognized as GPG encrypted data.
  5. Decrypt the file later: To decrypt, run gpg -d filename.gpg. You'll be asked for the passphrase (if symmetric) or it will use your private key (if asymmetric). The decrypted content is printed to the terminal. To save it to a file, use gpg -d filename.gpg > filename or gpg -o filename -d filename.gpg.
  6. Delete the original file securely (optional but recommended): After confirming the encrypted file works, you may want to remove the original plaintext. Use shred -u filename to overwrite and delete it securely, instead of rm which only removes the directory entry.

Common Mistakes

  • Forgetting the passphrase: There is no recovery. If you lose the passphrase, the encrypted file is permanently unreadable.
  • Encrypting the wrong file: Double-check the file name; you might accidentally encrypt a symlink or a directory (gpg will fail on directories).
  • Leaving the original file: If you don't delete the original, the encryption is pointless—anyone can read the original.
  • Using a weak passphrase: Short or common passphrases are easy to brute-force. Use a long passphrase (at least 12 characters) with a mix of letters, numbers, and symbols.
  • Encrypting with a recipient key that doesn't exist: If you use -r with a key that isn't in your keyring, gpg will fail or prompt for a different key.

Tips

  • Use a keypair for convenience: Generate your own keypair with gpg --full-generate-key so you can encrypt files for yourself without typing a passphrase every time (you'll use your private key instead).
  • Import a recipient's public key: Use gpg --import publickey.asc to add a key. Verify the fingerprint with gpg --fingerprint to ensure it's the right person.
  • Encrypt archives: To encrypt multiple files, first create a tar archive (tar -czf archive.tar.gz files) then encrypt the archive with gpg.
  • Use a different cipher: By default, gpg uses AES-128. For stronger encryption, use gpg -c --cipher-algo AES256 filename.
  • Encrypt before syncing to cloud: If you use cloud storage or backup services, encrypt files before uploading so the service provider cannot read them.
  • Keep a backup of your private key: If you lose your private key, you can't decrypt files encrypted to it. Export it with gpg --export-secret-keys > backup.asc and store it securely offline.

Troubleshooting

  • gpg: command not found: Install gnupg using your package manager (e.g., sudo apt install gnupg on Debian/Ubuntu, sudo dnf install gnupg2 on Fedora).
  • gpg: decryption failed: No secret key: This happens when you try to decrypt a file encrypted to a recipient key, but you don't have the corresponding private key. Import the private key or use symmetric encryption instead.
  • gpg: invalid option -- 'c': You might be using an older version or a different command. Check gpg --version and ensure you're using the correct syntax. For symmetric encryption, the flag is -c (lowercase).
  • Encrypted file is larger than expected: That's normal—encryption adds overhead. It doesn't indicate a problem.
  • Passphrase prompt appears twice but you typed it differently: You must type the exact same passphrase both times. If you get an error, start over with gpg -c again.
  • If decryption prints garbage or fails: The file might be corrupted or not actually encrypted with gpg. Use file filename.gpg to check its type. If it's not recognized, you may have encrypted a file that was already compressed or binary; gpg can handle those, but the file might have been truncated during transfer.

Frequently Asked Questions

Can I encrypt a file without a passphrase?

Yes, if you use asymmetric encryption with a keypair. You encrypt with the recipient's public key, and they decrypt with their private key—no passphrase is needed at encryption time. However, the private key itself is usually protected by a passphrase.

What's the difference between symmetric and asymmetric encryption?

Symmetric encryption uses the same passphrase to encrypt and decrypt. Asymmetric encryption uses a public key to encrypt and a private key to decrypt. Symmetric is simpler for personal files; asymmetric is better for sharing with others.

How do I encrypt a file so that only I can read it?

Use symmetric encryption with a passphrase (gpg -c file) or generate your own keypair and encrypt to yourself (gpg -e -r your-email file).

Is gpg encryption secure enough for sensitive data?

Yes, GPG uses strong, well-audited encryption algorithms (like AES). It's the standard tool for file encryption on Linux and is considered secure for sensitive data.

Can I encrypt a directory with gpg?

No, gpg works on single files. To encrypt a directory, first create a tar archive (tar -czf dir.tar.gz dir/) then encrypt the archive.

What happens if I forget the passphrase?

There is no way to recover it. The encrypted file becomes permanently unreadable. Always use a passphrase you can remember or store it in a password manager.