If you have one or more files that you need to send to a friend or store for a long time, compressing the files into a .zip archive is a good way to save on space and combine all files into a single object. If you need to keep the file contents private, for fear that they could be intercepted by the wrong party, or that someone could try to access them on your own system, then it is possible to add a password to your zip file. In this tutorial, you will learn how to zip files and add a password to your zip archive on a Linux system.
In this tutorial you will learn:
- How to install the
- How to zip file with password using
- How to unzip a password protected zip file
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Install zip on Linux
Using zip on the command line will involve two different utilities, zip for compressing or “zipping” files and unzip for decompressing or “unzipping” files. Your Linux distro may already come with these packages installed, but not all do. You can run the respective commands on your system to install and/or update the two programs:
$ sudo apt install zip unzip
$ sudo dnf install zip unzip
$ sudo pacman -S zip unzip
Zip files with password
Let’s use the
zip command to create a password protected and compressed archive by using the examples below.
-p(password) option allows us to specify a password in our
zipcommand. The following command will create a password protected zip archive of
file2.txt, using a password of “mypassword” and the zip archive will be called
$ zip -p mypassword my_archive.zip file1.txt file2.txt
The problem with the
-poption is that our password can easily be seen by anyone looking at the terminal over our shoulder or perusing the terminal history e.g. with the
historycommand. This is generally not a good method to use, but could be employed safely in some scenarios, such as inside of a Bash script, assuming that the user sets the password and the value is stored as a variable.
- A much better method to use is
-e(encrypt). This will prompt you for a password to use, and it will be hidden in the terminal so that onlookers and those browsing the terminal history will have no idea what you typed for a password. The syntax is as follows:
$ zip -e my_archive.zip file1.txt file2.txt Enter password: Verify password: adding: file1.txt (stored 0%) adding: file2.txt (stored 0%)
As you can see above, we are prompted in the terminal to enter and confirm our password selection, but the input keystrokes are invisible.
- To unzip the files, you just use the ordinary command syntax for the
unzipcommand, but will be prompted to enter the archive’s password before the contents will be extracted.
$ unzip my_archive.zip Archive: my_archive.zip [my_archive.zip] file1.txt password: extracting: file1.txt extracting: file2.txt
In this tutorial, we saw how to compress files into a zip archive while password protecting the file on a Linux system. Using compressed and password protected archives allows us to send or store sensitive files without needing to worry about an unauthorized user getting into them. Of course, the zip archive is only as protected as your password is strong. Beware that weak passwords can always lead to your zip files being cracked.