Oct 19, 2023 8 min read

How to Password-Protect Files and Folders on Linux

Learn to password-protect files and folders on Linux with our step-by-step tutorial. It protects sensitive information from unauthorized access.

Password-Protect Files and Folders on Linux
Table of Contents

Introduction

Before we begin talking about how to password-protect files and folders on Linux, let's briefly understand – What does it mean to password-protect files and folders?

Password-protecting files and folders is an essential security measure that ensures your sensitive information remains safe from unauthorized access. By using passwords, you can add an extra layer of protection to your important files and folders, preventing them from being opened or modified by anyone without the correct password.

It is a simple yet effective method that helps safeguard your data, whether it's personal documents, confidential work files, or private photos. Learn how to password-protect your files and folders to keep your digital assets secure and maintain your peace of mind.

In this tutorial, you will learn various ways to password-protect files and folders on Linux are shown here. We will also address a few FAQs on how to password-protect files and folders on Linux.

Advantages of Password-Protect Files and Folders

  1. Enhanced Data Security: Password protection adds an extra layer of security to prevent unauthorized access to your sensitive files and folders.
  2. Privacy Protection: Keep your personal and confidential information private by password-protecting your files and folders.
  3. Access Control: Ensure that only authorized individuals with the correct password can view or modify your important files.
  4. Peace of Mind: With password protection, you can feel confident that your data is secure, even if your device falls into the wrong hands.
  5. Versatile Security Solution: Password protection can be applied to various file types and folders, making it a versatile security measure for different purposes.

How to Password-Protect Files on Linux

You may password-protect/encrypt files on your computer using a variety of Linux applications. Here are some approaches you can use to accomplish this.

Method 1: Encrypt a File Using GnuPG

GnuPG is a free Linux programme that can encrypt files using a variety of encryption methods. Most Linux distributions already have it installed, and using it is rather simple.

GnuPG can be accessed through the CLI. The following is how to use it to encrypt a file:

1) Launch the terminal.
2) To go to the directory containing the file you want to password-protect, use the cd command and ls command.
3) Run the following command to encrypt your file once you are in the directory:

gpg -c filename

4) Last but not least, enter a strong and memorable passphrase when requested.

In your current working directory, GnuPG will now produce an encrypted file (with a .gpg extension). You'll have to decrypt it in order to access it. Run the following command to accomplish this, and then type the password you used to encrypt the file when requested and press Enter:

gpg filename.gpg

A decrypted version of the file in your current working directory will be returned by GnuPG.

Run the following command to see which encryption algorithms are supported before using any other ones.

gpg --version

Afterward, include the preferred algorithm in the command as follows:

gpg -c --cipher-algo algorithm_name filename

Method 2: Encrypt a File Using Zip

Another CLI tool that enables password protection for files on Linux is zip. It is perfect for archiving or compressing several files into a single encrypted archive and comes pre-installed on all major Linux distros.

To use Zip to password-protect a file, follow these steps:

1) To access the directory containing the files to encrypt, open the terminal and use the cd and ls commands.

2) To create a password-protected zip file, type the following command in the appropriate format:

zip --password preferred_password archive_file.zip filename1 filename2

Replace preferred_password with the encryption password you want to use, and archive_file.zip with the file name you want to give the encrypted archive.

Now, unzip the ZIP and input your password to access these files. Run the following command instead if you want to do it through the terminal:

unzip archive_file.zip

Zip will request a password from you. The file can be decrypted by entering the password you chose when it was encrypted and pressing Enter.

Method 3: Encrypt a File Using mcrypt

A crypt replacement called mcrypt makes it simple to encrypt files under Linux. It supports a variety of encryption protocols, and you can choose the encryption algorithm according to your preferences.

Here are the steps for using mcrypt to encrypt a file:

1) Open the terminal and enter the directory containing the file you wish to encrypt using the commands cd and ls.

2) To view a list of all the supported encryption algorithms, enter the command below:

mcrypt --list

3) Then use the following to encrypt your file:

mcrypt -a algorithm_name filename

Enter the passphrase twice and press Enter when prompted.

Your file will now be encrypted and saved with the ".nc" extension thanks to mcrypt. You must decrypt this file in order to open it. Do this by running:

mcrypt -d filename.nc

Enter the decryption passphrase next.

How to Password-Protect Folders on Linux

On Linux, password-protecting folders is just as simple as password-protecting files. There is a list of all the methods you can use, along with guidelines, below.

Method 1: Encrypt a Folder Using GNOME EncFS Manager

A mounting tool called GNOME EncFS Manager makes it simple to encrypt folders on Linux. Most Linux distros support it, and it has a nice GUI that makes it simple to use.

Following are the instructions for using Linux's GNOME EncFS Manager to encrypt a folder:

1) Launch GNOME EncFs by going to the application menu, then searching for it.

2) Select the toolbar's plus (+) sign by tapping it. Choose a folder in the dialogue box that appears by clicking the second radio button next to Directory or drive to encrypt or import. Here, make sure to make a new, empty folder. (You must transfer the information from your original folder to this one later.)

3) Choose the best option for the mount directory, or the place where the encrypted folder should be mounted.

4) Enter the desired password twice under Password to encrypt the folder.

5) To create an encrypted folder, click Create.

Now that you've chosen your destination directory, GNOME EncFS Manager will create and mount an encrypted folder (also known as a stash). To open the folder on this mounted drive's contents, double-click on it. Selecting the checkbox next to it will unmount it. Unmount can also be chosen by right-clicking on the stash.

Simply check the checkbox again to mount it once more. Your password will be requested by GNOME EncFS Manager. You should now be able to access all of its contents after entering the password.

Method 2: Encrypt a Folder Using VeraCrypt

A free and open-source disc encryption tool is VeraCrypt. You can use it to establish an encrypted directory on Linux where you can safely store files and folders. It is accessible on every major operating system.

Install VeraCrypt on your machine after downloading it from the link below.

Download:VeraCrypt

After that, run it by selecting it from the list of applications, then follow these steps:

1) Click on Create Volume after selecting a drive slot.

2) Select Next after creating an encrypted file container.

3) Select volume type as Standard VeraCrypt.

4) To store the encrypted disc, click Select File and make a file.

5) Choose your preferred hash and encryption algorithms, then click Next.

6) Please provide the drive's volume size.

7) To lock this drive, type a password twice and select Next.

8) Select the file system option under Volume Format type in the menu.

9) To create a VeraCrypt volume, press Format.

This encrypted volume must now be mounted. Return to the VeraCrypt main window and choose Select File under Volume to accomplish this. Select the file, and then select Mount. Enter the password you chose when creating the volume if prompted for one.

Your encrypted disc will now be mounted by VeraCrypt, and you can then add all of your personal data (files or folders) to it. Unmount the drive when you're done working with these files and want to lock and hide them.

FAQs to Password-Protect Files and Folders on Linux

Can I password-protect files without compressing them?

Yes, you can use the "7zip" command-line tool to create password-protected archives without compressing the files. This method allows you to encrypt files and folders while keeping their original structure intact.

How secure is password protection on Linux?

The security level depends on the encryption algorithm and the strength of your password. Choosing a strong password and using robust encryption methods like AES can provide high security.

Can I password-protect files and folders using a graphical user interface (GUI)?

Yes, Linux offers various GUI-based tools like "Archive Manager" or "GNOME EncFS Manager" that allow you to password-protect files and folders with a few clicks.

Can I share password-protected files with others?

Yes, you can share password-protected files. Just ensure that the recipients know the correct password to access and decrypt the files on their Linux systems.

How can I remove password protection from a file or folder?

To remove password protection, you can extract the files from the password-protected archive or use decryption tools like "GnuPG" to decrypt individual files.

Can I automate password protection for multiple files or folders?

Yes, you can create scripts using command-line tools like "zip" or "7zip" to automate the password protection process for multiple files or folders.

Can I encrypt files and folders on external storage devices?

Absolutely. You can password-protect files and folders on external storage devices like USB drives or external hard drives using the same methods as on your Linux system.

Are there any alternatives to password protection for file and folder security on Linux?

Yes, you can explore other security measures like file system permissions, disk encryption, or using encrypted file systems like EncFS as alternatives or complementary methods to password protection.

Conclusion

To password-protect your files and folders on Linux and prevent unauthorized access, you can select any method and tool from the ones mentioned above based on your preferences. This can be a pretty good approach to make sure that only you have access to your personal data if you share a computer with someone.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.