Smart Systems for Home

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

Linux Password Protect Files With Encryption

· 5 min read · Home Tech

In this article
  1. Why Password Protection Means Encryption
  2. Encrypting a Single File with GnuPG
  3. Encrypting a Whole Directory with gpg or tar
  4. Encrypting an Entire Partition or Drive with LUKS
  5. Encrypting a Folder with eCryptfs (for home directories)
  6. How to Choose the Right Method
  7. Common Problems and Mistakes
  8. Frequently Asked Questions

Yes, you can password-protect files and directories in Linux using encryption, and the most common and secure way is with GnuPG (gpg) for individual files or LUKS for entire volumes. This article explains the practical methods, how they work, and how to choose the right one for your needs.

Why Password Protection Means Encryption

On Linux, a simple file permission change (like chmod 600) only restricts who can access the file while the system is running. It does not protect the data if the hard drive is removed, the system is booted from a live USB, or the file is sent over the internet. True password protection requires encryption: the file's contents are transformed into unreadable ciphertext, and a password (or key) is required to decrypt it back to plaintext. Without encryption, any user with root privileges or physical access to the disk can read your data.

Encrypting a Single File with GnuPG

GnuPG (also called GPG) is the standard command-line tool for file encryption on Linux. It uses symmetric encryption when you provide a passphrase directly. Here's how to use it:

  1. Open a terminal.
  2. To encrypt a file, run: gpg -c filename.txt
  3. You will be prompted to enter a passphrase twice. Choose a strong one.
  4. GPG creates a new file named filename.txt.gpg (the original remains unchanged).
  5. Delete the original file securely with shred -u filename.txt to prevent recovery.
  6. To decrypt later, run: gpg filename.txt.gpg — it will ask for the passphrase and recreate the original file.

You can also use the --cipher-algo option to choose a cipher, e.g., gpg -c --cipher-algo AES256 filename.txt. AES-256 is a solid default.

Encrypting a Whole Directory with gpg or tar

GPG works on individual files, but you can encrypt a whole directory by first creating a tarball. Use the tar command to archive the folder, then pipe it to GPG:

tar -czf - /path/to/folder | gpg -c > folder.tar.gz.gpg

To decrypt and extract:

gpg -d folder.tar.gz.gpg | tar -xzf -

This creates a single encrypted archive. Note that the -c option uses symmetric encryption with a passphrase. For asymmetric encryption (which uses a public/private key pair), you would use gpg -e -r recipient, but for personal password protection, symmetric is simpler.

Encrypting an Entire Partition or Drive with LUKS

If you need to protect a whole disk, partition, or a USB drive, LUKS (Linux Unified Key Setup) is the standard. It is the encryption backend used by most Linux distributions' full-disk encryption. LUKS creates an encrypted container that you unlock with a passphrase. Here's a basic example using cryptsetup:

  1. Install cryptsetup if not present (sudo apt install cryptsetup on Debian/Ubuntu).
  2. Format the partition (e.g., /dev/sdb1) with LUKS: sudo cryptsetup luksFormat /dev/sdb1 — this destroys existing data, so be careful.
  3. Open the encrypted container: sudo cryptsetup open /dev/sdb1 myencrypted
  4. Now you can format it with a filesystem (e.g., sudo mkfs.ext4 /dev/mapper/myencrypted) and mount it.
  5. To close it, unmount and run sudo cryptsetup close myencrypted.

LUKS is more robust for volumes because it stores multiple key slots, allowing you to change the passphrase without re-encrypting the data. It also works with key files, which can be useful for automation.

Encrypting a Folder with eCryptfs (for home directories)

Another approach is eCryptfs, which is used to encrypt home directories in some Ubuntu installations. It works at the filesystem level, encrypting individual files as they are written. However, it is less commonly used now and can be tricky to set up manually. For most users, GPG or LUKS are simpler and more portable.

How to Choose the Right Method

Your choice depends on what you need to protect:

  • Single file or a few files: Use GPG with -c. It's fast, simple, and works on any Linux system.
  • Many files in a folder: Create a tarball and encrypt it with GPG, or use a tool like 7z (which supports AES-256) if you prefer a GUI.
  • Whole disk or partition, or external drive: Use LUKS. It provides transparent encryption for everything on the volume.
  • Cloud sync or email: Encrypt each file individually with GPG before uploading or sending.

For a GUI alternative, kleopatra (for GPG) and cryptsetup front-ends like cryptkeeper (though older) can help. But the command line is often the most reliable.

Common Problems and Mistakes

  • Forgetting the passphrase: There is no recovery. If you lose it, the data is gone forever. No backdoor exists.
  • Leaving the original file: Always securely delete the original after encryption. Use shred -u or at least rm -P if available.
  • Using weak ciphers: Stick to AES-256 or similar. Avoid old ciphers like CAST5 or Blowfish if possible.
  • Encrypting with a public key by mistake: If you use gpg -e without -c, it will try to encrypt to a recipient, not a password. For password-only, use -c.
  • Not checking file permissions: After encryption, the .gpg file is readable by anyone with access to the system. Set permissions with chmod 600 to limit access.

Frequently Asked Questions

Can I use a password to protect a PDF or Office document on Linux?

Yes, LibreOffice supports password-protecting documents with AES encryption. When saving, check the “Save with password” option. For PDFs, you can use tools like qpdf or pdftk to add a password, but note that PDF passwords often only restrict opening, not copying content.

Is it safe to use the same password for encryption as my login password?

Generally no. If an attacker obtains your login password, they could also decrypt files. Use a separate, strong passphrase for encryption.

Does gpg -c use my public key?

No, -c stands for symmetric encryption, which uses only a passphrase. No key pair is involved. This is the simplest method for personal use.

Can I encrypt a file so that it can be decrypted on Windows or macOS?

Yes. GPG is cross-platform. You can use gpg on Windows (via Gpg4win) and macOS (via GPGTools) to decrypt files encrypted on Linux. LUKS is Linux-specific, but you can use VeraCrypt for cross-platform volume encryption.

What if I need to encrypt a file for someone else to read?

Use asymmetric encryption: gpg -e -r recipient@example.com file. This encrypts to the recipient's public key. They decrypt with their private key. You don't need to share a password.

How do I change the password on an encrypted file?

For GPG, you must decrypt and re-encrypt with a new passphrase. For LUKS, you can use cryptsetup luksChangeKey /dev/sdX to change the passphrase without re-encrypting the data.