Encrypt a File in Linux: When and How
In this article
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
gpginstalled (check withgpg --version; if missing, install it via your package manager, e.g.,sudo apt install gnupgon 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
- Open a terminal and navigate to the directory containing the file you want to encrypt. Use
cd /path/to/directory. - Encrypt with a passphrase (symmetric encryption): Run
gpg -c filename. Replacefilenamewith your actual file name. You'll be prompted to enter a passphrase twice. The output will be a new file namedfilename.gpg. This is the encrypted version; the original file remains unchanged. - 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 createsfilename.gpgthat only the holder of the matching private key can decrypt. - Verify the encrypted file: Use
ls -l filename.gpgto confirm the file exists and is not empty. You can also runfile filename.gpgto see that it's recognized as GPG encrypted data. - 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, usegpg -d filename.gpg > filenameorgpg -o filename -d filename.gpg. - 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 filenameto overwrite and delete it securely, instead ofrmwhich 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
-rwith 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-keyso 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.ascto add a key. Verify the fingerprint withgpg --fingerprintto 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.ascand store it securely offline.
Troubleshooting
- gpg: command not found: Install gnupg using your package manager (e.g.,
sudo apt install gnupgon Debian/Ubuntu,sudo dnf install gnupg2on 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 --versionand 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 -cagain. - If decryption prints garbage or fails: The file might be corrupted or not actually encrypted with gpg. Use
file filename.gpgto 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.