Skip to content

Tar with Password: Encrypt and Decrypt tar Files with OpenSSL

Updated 5 min read

Updated: 18 October 2026 · Applies to: GNU tar 1.35 with OpenSSL 3.5.7, GnuPG 2.4.7 and age 1.2.1 on Debian 13, tested

tar has no password option of its own. To make a tar archive with a password, pipe it through openssl enc with AES-256 and PBKDF2 key derivation, and use the same options to decrypt it:

tar -czf - project | openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -out project.tar.gz.enc
openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 -in project.tar.gz.enc | tar -xzf -

OpenSSL asks for the password. Older guides leave out -pbkdf2 and -iter, which current OpenSSL warns about (see step 4). GnuPG and age are good alternatives and are shown at the end.

1. Encrypt a folder into a password-protected archive

$ tar -czf - project | openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -out project.tar.gz.enc
enter AES-256-CBC encryption password:
Verifying - enter AES-256-CBC encryption password:
  • tar -czf - project: pack and gzip the folder project and write it to standard output (-) instead of a file.
  • enc -aes-256-cbc: encrypt with AES-256 in CBC mode.
  • -pbkdf2 -iter 600000: turn the password into a key with PBKDF2 and 600,000 rounds. More rounds make guessing the password slower. Without -iter, OpenSSL uses 10,000. You need the same number to decrypt, so keep it with your notes.
  • -out: the encrypted file. A random salt is added by default; the file starts with the text Salted__.

If the two passwords do not match, OpenSSL stops with Verify failure and bad password read.

2. Decrypt, list or extract the archive

List the files without extracting them:

$ openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 -in project.tar.gz.enc | tar -tzf -
enter AES-256-CBC decryption password:
project/
project/config.ini
project/docs/
project/docs/notes.txt

Extract into a folder of your choice with -C (the folder must exist):

mkdir -p restore
openssl enc -d -aes-256-cbc -pbkdf2 -iter 600000 -in project.tar.gz.enc | tar -xzf - -C restore

A wrong password, a different -iter value or a missing -pbkdf2 all end the same way (without -pbkdf2, the warning from step 4 comes first). Lab output:

bad decrypt
...:error:1C800064:Provider routines:ossl_cipher_unpadblock:bad decrypt:...

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

(OpenSSL error line shortened.) For more tar options, see How to Extract a tar File to a Specific Directory or Extract Only One Folder in Linux.

3. Use a password file in scripts and backups

A backup job cannot type a password. Keep it in a file that only root can read and point OpenSSL at it with -pass file::

umask 077
nano /root/.archive-pass          # one line: the password
tar -czf - /var/www | openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -pass file:/root/.archive-pass -out www.tar.gz.enc

OpenSSL reads only the first line of the file. Avoid -pass pass:YourPassword and the old -k option: according to OpenSSL, a password given on the command line is visible to tools such as ps, and it also ends up in your shell history. -pass env:VARNAME works too, but OpenSSL advises caution because some systems show the environment of other processes. Store a copy of the password somewhere safe: without it the archive cannot be recovered.

4. The "deprecated key derivation" warning

The command from older guides still works, but OpenSSL 3.5 prints a warning:

$ tar -czf - * | openssl enc -e -aes256 -out secured.tar.gz
enter AES-256-CBC encryption password:
Verifying - enter AES-256-CBC encryption password:
*** WARNING : deprecated key derivation used.
Using -iter or -pbkdf2 would be better.

Without -pbkdf2, OpenSSL falls back to its old key derivation, which it now marks as deprecated. PBKDF2 with many rounds makes each password guess far slower, which matters most when the password is weak. To open an archive made this way, decrypt it with the same old command (it prints the same warning and works), then encrypt it again with the options from step 1:

openssl enc -d -aes256 -in secured.tar.gz | tar -xzf - -C restore

According to OpenSSL, the default digest changed from MD5 to SHA-256 in OpenSSL 1.1.0. For a file made with OpenSSL 1.0.2 or older, add -md md5 to the decrypt command. Also note the name: the old example called the encrypted file .tar.gz, but it is not a gzip file any more. An ending such as .tar.gz.enc avoids confusion.

5. What openssl enc does not do

openssl enc cannot use authenticated modes such as GCM: -aes-256-gcm fails with enc: AEAD ciphers not supported, and according to OpenSSL that will not change. So it does not check whether the file was changed after encryption. For archives you send to other people or keep for years, GnuPG or age are the better choice.

6. Alternative: GnuPG (gpg)

tar -czf - project | gpg --symmetric --cipher-algo AES256 -o project.tar.gz.gpg
gpg -d project.tar.gz.gpg | tar -xzf -

gpg asks for the passphrase in a prompt. When decrypting, the lab showed gpg: AES256.CFB encrypted data and gpg: encrypted with 1 passphrase; a wrong passphrase gave gpg: decryption failed: Bad session key. In scripts, add --batch --pinentry-mode loopback --passphrase-file /root/.archive-pass. More on gpg: How to Use GnuPG (gpg) on Linux: Create Keys, Encrypt, Sign and Use Keyservers.

7. Alternative: age

age is a small, modern encryption tool (package age on Debian 13). With a passphrase:

$ tar -czf - project | age -p -o project.tar.gz.age
Enter passphrase (leave empty to autogenerate a secure one):
Confirm passphrase:
$ age -d project.tar.gz.age | tar -xzf -
Enter passphrase:

A wrong passphrase gives age: error: incorrect passphrase. For unattended backups, use a key pair instead of a passphrase: age-keygen -o key.txt prints a public key starting with age1; encrypt with age -r age1... and decrypt with age -d -i key.txt. Keep key.txt off the server you back up.

If the other person needs to open the archive on Windows, a password-protected ZIP or 7z file is easier: see How to Unzip a Password Protected ZIP File in Linux (and Create One with zip or 7-Zip AES).

Official documentation: openssl-enc manual and age.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket