Create a Password Protected .7z File in Linux

Last updated on Dec 28, 2019 in Linux

With 7zip utility in Linux, you can protect a 7z file with a password. There are two options to do that. The other one is more secure than the other.

Insecure Way

$ 7za a myfile.7z myfile.txt -pPASSWORD

The p stands for password which is entered directly after the -p. By this way, the command is stored in command history so the password can be easily seen later in plain text format. This method is not recommended

Secure Way

$ 7za a myfile.7z myfile.txt -p -mhe=on 
7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Xeon(R) CPU E5-2680 v2 @ 2.80GHz (306E4),ASM,AES-NI)

Scanning the drive:
1 file, 9 bytes (1 KiB)

Creating archive: myfile.7z
Items to compress: 1

Enter password (will not be echoed):
Verify password (will not be echoed) :

Files read from disk: 1
Archive size: 215 bytes (1 KiB)
Everything is Ok

The -p stands for password, and it is prompted interactively and not echoed. The password prompt is repeated to ensure there is no typing errors. The -mhe=on hides the directory listing which anybody could see even without the password.

To decompress password protected .7z file, just extract the file normally and enter the password when prompted: Extract .7z File in Linux