https://superuser.com/questions/370389/how-do-i-password-protect-a-tgz-file-with-tar-in-unix
2028 tar zcvpf - demo_bak/ |ccrypt > demo_bak.tar.tgz.cpt
2029 ls
2030 mkdir test
2031 mv demo_bak.tar.tgz.cpt test/
2032 ls
2033 cd test/
2034 ls
2035 tar -zxvpf demo_bak.tar.tgz.cpt
2036 man ccrypt
2037 ccrypt -d demo_bak.tar.tgz.cpt
2038 ls
2039 tar -zxvpf demo_bak.tar.tgz
2040 ls
2041 history -30
2042 history |tail -30
How do I password protect a .tgz file with tar in Unix?
I'm using the Unix tar command as follows to tar up a directory and its files:
tar cvzf fileToTar.tgz directoryToTar
Is there a way to password protect the .tgz file? I've created password-protected ZIP files on Windows so I would assume Unix has the same capability. Any ideas?
linux unix tar archiving password-protection
10722 bronze badges
asked Dec 21 '11 at 19:00
31611 gold badge33 silver badges44 bronze badges
migrated from stackoverflow.com Dec 21 '11 at 21:00
This question came from our site for professional and enthusiast programmers.
add a comment
5 Answers
37
Simple examples:
cat filename | crypt > filename.crypt
gpg -c –o filename.gpg filename
13011 silver badge1010 bronze badges
answered Dec 21 '11 at 19:08
63177 silver badges77 bronze badges
-
2
this makes no sense, where is the password? – Alexander Mills May 7 at 19:27
-
2
@AlexanderMills Most password-accepting tools prompt the user for it from the terminal rather than a command line argument, as to prevent the password showing up in history. – Daffy May 26 at 3:52
add a comment
33
You can use command:
zip -P password file.zip file
Or better:
zip -e file.zip file
man zip
67222 gold badges1313 silver badges2828 bronze badges
answered Dec 22 '11 at 6:27
43133 silver badges22 bronze badges
add a comment
16
Neither the tar
format nor the gz
format has built-in support for password-protecting files.
The Windows zip
format combines several different piece of functionality: compression (e.g. gzip), archiving multiple files into one (e.g. tar), encryption (e.g. gnupg), and probably others. Unix tends to have individual tools, each of which does one thing well, and lets you combine them.
The Unix equivalent of a password-protected .zip
file would probably be called something like foo.tar.gz.gpg
or foo.tgz.gpg
.
And there are open-source zip
and unzip
tools for Unix, though they may not provide all the capabilities of the Windows versions (I'm fairly sure the newer .zipx
format isn't supported).
7,7471414 gold badges7070 silver badges9494 bronze badges
answered Dec 21 '11 at 21:44
4,43022 gold badges1919 silver badges3131 bronze badges
add a comment
12
You can use gpg (=GnuPG):
gpg -o fileToTar.tgz.gpg --symmetric fileToTar.tgz
This will prompt you for a passphrase.
To decrypt the file later on, just do a:
gpg fileToTar.tgz.gpg
This will prompt you, again, for the passphrase.
answered Jul 20 '15 at 5:57
1,15411 gold badge1212 silver badges1616 bronze badges
-
Note:
-c
is short for--symmetric
, i.e., use the default symmetric cipher, which means that the same passphrase is used for both encryption and decryption. (As opposed to asymmetric, which involves public keys and private keys.) – Evgeni Sergeev Nov 24 '17 at 5:30
add a comment
7
You can use ccrypt.
Things can be encrypted by a pipe:
tar cvvjf - /path/to/files | ccrypt > backup.tar.bz2.cpt
Or in place:
ccrypt backup.tar.bz2
For automating, you can save a passkey into a file and use this passkey to encrypt:
ccrypt -k ~/.passkey backup.tar.bz2