How to Password Protect Files and Folders in Linux
Whether you want to share a file online, store it in the cloud, transport it on a USB drive, or add it to your archives, password protecting it is the best approach to securing its contents and preventing unauthorized access.
The same goes for folders. Therefore, it is crucial to encrypt them as well. In Linux, you have several ways to password protect files and folders. Most of these methods use encryption, which provides additional protection over others.
Here’s an overview of all the different methods you can use to password protect files and folders in Linux.
How to Password Protect Files in Linux
Linux has several utilities to help you password protect/encrypt files on your computer. 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 distributions and is quite easy to use.
GnuPG is accessible through the CLI. Here’s how you can use it to encrypt a file:
- Open the terminal.
- Use the cd command and the ls command to navigate to the directory containing the file you want to password protect.
- Once in the directory, run the following command to encrypt your file:
gpg -c filename
- Finally, when prompted for a passphrase, type 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 will need to decrypt it. To do this, run the following command, and when prompted for a password, enter the one you used to encrypt the file and press Walk in:
GnuPG will return a decrypted version of the file to your current working directory.
If you want to use another encryption algorithm, first check which ones are supported 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 allows you to password protect files in Linux. It comes pre-installed on all major Linux distributions and is ideal for archiving or compressing multiple files into an encrypted archive.
Follow these steps to use Zip to password protect a file:
- Open the terminal and use the CD and ls commands to go to 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 file_archive.zip with the filename you want to give to the resulting 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 encryption time and press Walk in to decrypt the file.
Method 3: Encrypt a file using mcrypt
mcrypt is a replacement for crypt that makes it easier to encrypt files in Linux. It supports various encryption standards and you have the option to specify the encryption algorithm according to your preference.
Here are the steps to encrypt a file using mcrypt:
- Open the terminal and, using cd and ls, navigate to the directory containing the file you want to encrypt.
- Enter the command below to list all supported encryption algorithms:
- Finally, encrypt your file using:
mcrypt -a algorithm_name filename
When prompted for a passphrase, enter it twice and press Walk in.
mcrypt will now encrypt your file and save it with the “.nc” extension. If you want to open this file, you will need to decrypt it. To do this, run:
mcrypt -d filename.nc
And then, enter the decryption passphrase.
How to Password Protect Folders in Linux
Similar to how you password protect files in Linux, password protecting folders is also simple. Below is a list of all the methods you can use and the steps to use them.
Method 1: Encrypt Folder Using GNOME EncFS Manager
GNOME EncFS Manager is a mounting utility that allows you to easily encrypt folders in Linux. It is supported by most Linux distributions and has a neat GUI, which makes it easy to use.
Here are the steps to use GNOME EncFS Manager to encrypt a folder on Linux:
- Open the application menu, find EncF GNOME and run it.
- Press the plus (+) 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. Be sure to create a new (empty) folder here. (You’ll need to move 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 to mount the encrypted folder.
- Below Passwordenter 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 in the selected destination directory. Double-click this mounted drive to access the contents of the folder inside. To unmount it, check the box next to it. Or right-click on the reserve and select To disassemble.
Now when you want to mount it again, just check the box. GNOME EncFS Manager will ask you for your password. Enter the password and you should now be able to access all of its contents.
Method 2: Encrypt a folder using VeraCrypt
VeraCrypt is a free and open-source disk encryption tool. It is 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.
To download: VeraCrypt
Then launch it from the applications menu and follow these instructions:
- Select a drive location and click Create Volume.
- Select Create an encrypted file container and knock Next.
- Choose volume type as VeraCrypt standard.
- Click on Choose the folder and create a file to store the encrypted disk.
- Select an encryption algorithm and a hash algorithm according to your preference and click Next.
- Give a volume size for this drive.
- Enter a password to lock this drive twice and press Next.
- Choose the volume format type i.e. file system from the drop-down list.
- Finally, hit Format to create a VeraCrypt volume.
Now you need to mount this encrypted volume. To do this, return to the main VeraCrypt window and click Select File under Volume. Choose the file and click To go up. When prompted for a password, enter the password you set when creating the volume.
VeraCrypt will now mount your encrypted drive, and afterwards you can add all your personal data (files or folders) to this drive. When you are done working with these files and want to lock and hide them, unmount the drive.
Effectively secure personal data on Linux
Depending on your preferences, you can choose any of the methods and tools listed above to password protect your files and folders in Linux and prevent unauthorized access. If you share your computer with someone, this can be a very effective way to ensure that no one but you can access your personal data.