Whether you want to share a file online, store it in the cloud, carry it on a thumb drive, or add it to your archive, password-protecting it is the best approach to secure its content and prevent unauthorized access.
The same holds true for folders too. Therefore, it’s crucial to encrypt them as well. On Linux, you have several ways to password-protect files and folders. Most of these methods use encryption, which offers additional protection than others.
Here’s a look at all the different methods you can use to password-protect files and folders on Linux.
How to Password-Protect Files on Linux
Linux has multiple utilities to help you password-protect/encrypt files on your computer. Listed below are the methods you can use to do this.
Method 1: Encrypt a File Using GnuPG
GnuPG is a free Linux program that supports multiple encryption standards for file encryption. It comes pre-installed on most Linux distros and is pretty easy to use.
GnuPG is accessible via the CLI. Here’s how you can use it to encrypt a file :
- Open the terminal.
- Use the cd command and ls command to navigate to the directory containing the file you want to password-protect.
- Once inside the directory, run the following command to encrypt your file:
gpg -c filename
- Finally, when prompted for a passphrase, type in one that’s strong and easy to remember.
GnuPG will now create an encrypted file (with .gpg extension) in your current working directory. To access it, you’ll need to decrypt it. For this, run the following command, and when prompted for a password, enter the one you used to encrypt the file and hit Enter:
GnuPG will return a decrypted version of the file in your current working directory.
If you’d like to use any other encryption algorithm, first, check the supported ones by running:
And then, specify the desired algorithm in the command like this:
Method 2: Encrypt a File Using Zip
Zip is another CLI utility that lets you password-protect files on Linux. It comes pre-installed on all major Linux distros and is ideal for archiving or zipping multiple files into one encrypted archive.
Follow these steps to use Zip for password-protecting a file:
- Open the terminal and use the cd and ls commands to go into the directory with the files to encrypt.
- Enter the command in the following format to create a password-protected zip file:
zip --password preferred_password archive_file.zip filename1 filename2
Here, replace preferred_password with the password you want to use to encrypt the archive and archive_file.zip with the file name you want to give to the resultant archive.
Now, when you want to access these files, unzip the archive and enter your password. Or, to do it via the terminal, run:
Zip will now ask you for a password. Enter the password you set at the time of encryption and hit Enter to decrypt the file.
Method 3: Encrypt a File Using mcrypt
mcrypt is a crypt replacement that makes encrypting files on Linux easy. It supports various encryption standards and you have the option to specify the encryption algorithm based on your preference.
Here are the steps to encrypt a file using mcrypt:
- Open the terminal, and using cd and ls, go into the directory that contains the file you want to encrypt.
- Enter the command below to list out all the supported encryption algorithms:
- Finally, encrypt your file using:
mcrypt -a algorithm_name filename
When asked for a passphrase, enter it twice and hit Enter.
mcrypt will now encrypt your file and save it with the “.nc” extension. If you wish to open this file, you’ll need to decrypt it. To do this, run:
mcrypt -d filename.nc
And then, enter the decryption passphrase.
How to Password-Protect Folders on Linux
Similar to how you password-protected files on Linux, password-protecting folders is also straightforward. Below is a list of all the methods you can use and the steps to use them.
Method 1: Encrypt a Folder Using GNOME EncFS Manager
GNOME EncFS Manager is a mounting utility that lets you encrypt folders on Linux with ease. It’s supported by most Linux distros and has a neat GUI, which makes it easy to use.
Here are the steps to use GNOME EncFS Manager for encrypting a folder on Linux:
- Open the applications menu, search for GNOME EncFs and launch it.
- Tap on the plus (+) icon in the toolbar. In the dialog box that follows, select the second radio button under Directory or drive to encrypt or import and select a folder. Make sure to create a new (empty) folder here. (You’ll need to move over the contents of your original folder to this folder later.)
- Select an appropriate option for the mount directory, i.e., the location where you want the encrypted folder to be mounted.
- Under Password, enter the password you want to use to encrypt the folder twice.
- Hit Create to create an encrypted folder.
GNOME EncFS Manager will now create an encrypted folder (also called stash) and mount it to your selected destination directory. Double-click on this mounted drive to access the contents of the folder inside. To unmount it, check off the checkbox beside it. Or right-click on the stash and select Unmount.
Now, when you want to mount it again, just tick the checkbox. GNOME EncFS Manager will ask you for your password. Enter the password, and you should now be able to access all its contents.
Method 2: Encrypt a Folder Using VeraCrypt
VeraCrypt is a free and open-source disk encryption tool. It’s available on all major operating systems, and you can use it to create an encrypted directory on Linux where you can store files and folders securely.
First, download VeraCrypt on your computer from the link below and install it.
Next, launch it from the applications menu and follow these instructions:
- Select a drive slot and click on Create Volume.
- Select Create an encrypted file container and hit Next.
- Choose volume type as Standard VeraCrypt.
- Click on Select File and create a file to store the encrypted disk.
- Select an encryption algorithm and hash algorithm as per your preference and hit Next.
- Give a volume size for this drive.
- Enter a password to lock this drive twice and hit Next.
- Choose volume format type, i.e., file system from the dropdown.
- Finally, hit Format to create a VeraCrypt volume.
Now, you need to mount this encrypted volume. To do this, go back to the VeraCrypt main window and click on Select File under Volume. Choose the file and click on Mount. When prompted for a password, enter the password you set at the time of volume creation.
VeraCrypt will now mount your encrypted drive, and subsequently, you can add all your personal data (files or folders) to this drive. When you’re finished working with these files and wish to lock and hide them away, unmount the drive.
Secure Personal Data on Linux Effectively
Depending on your preference, you can pick any method and tool listed above to password-protect your files and folders on Linux and prevent unauthorized access. If you share your computer with someone, this can be a really effective way to ensure no one besides you can access your personal data.