How to encrypt a file on Linux (and when you should) (2024)

How to encrypt a file on Linux (and when you should) (1)

Your security and privacy have both become critically important. Businesses are not the only entities that have to keep sensitive information from prying eyes. You might have bank account details, contracts, wills, and other files on your desktop that should be locked behind a password, so only you can access them.

Also:The best Linux laptops you can buy

But how do you do this, if Linux is your operating system of choice? Believe it or not, it's actually pretty simple. I want to show you two different methods -- one using the command line and one that makes use of the built-in file manager -- so you too can protect those important documents.

You can do this with just about any type of file (text, .docx, .odt, PDF, .jpg, or you name it). One word of warning: Both methods do require using the command line. However, the GUI method only requires that you use the command line to install the required integration for the file manager.

Also: How to create hidden files in Linux (and what not to use them for)

With that said, let's get to the processes.

The command line method of encrypting files

What you need: The only thing you need for this is a running instance of Linux and a file to encrypt. That's it.

1. Open a terminal window and generate a GPG key

The first thing to do is open the terminal window from your desktop menu. Once it's open, you'll want to generate a GPG key with the command:

gpg --gen-key

You'll be asked to enter your real name and an email address, then type "O "to Okay the information. After that, you type/verify a passphrase for the key.

2. Change into the directory housing the file

With your key created, navigate to the folder housing the file to be encrypted. Let's say the file is in ~/Documents. Change to that directory with the command:

cd ~/Documents

3. Encrypt the file

We're going to use the gpg command to encrypt the file. For example, we'll encrypt the file zdnet_test with the command:

gpg -c zdnet_test

The -c option tells gpg the zdnet_test file is to be encrypted. You will then be asked to type and verify a password for the encrypted file.

Once you've encrypted the file, you'll notice there are two files: zdnet_test and zdnet_test.gpg. The file with the .gpg extension is the encrypted file. At this point, you can remove the initial test file with the command:

rm zdnet_test

4. Configure the password cache agent

Oddly enough, the GPG tool caches passwords. Because of this, you (or anyone who has access to your system) could decrypt the file without having to type the password with the command gpg zdnet_test. That's not safe. To get around this, we have to disable password caching for the GPG agent. To do this, create a new file with the command:

nano ~/.gnupg/gpg-agent.conf

In that file, paste the following lines:

default-cache-ttl 1max-cache-ttl 1

Next, restart the agent with the command:

echo RELOADAGENT | gpg-connect-agent

Now, when you (or anyone) types the decrypt command, gpg zdnet_test, the password prompt will appear. Until that password is successfully entered, the contents of the file will remain encrypted.

The GUI (Graphical User Interface) method of encrypting files

This method is significantly more efficient.

1. Install the required software

Before you use the GUI method, make sure to take care of Steps 1 and 4 above. You only have to do this once. After that, you'll need to install a piece of software with the command:

sudo apt-get install seahorse-nautilus -y

If you're using a distribution based on RHEL or Fedora Linux, that command would be:

sudo dnf install seahorse-nautilus -y

Once installed, restart Nautilus with the command:

nautilus -q

2. Open the Nautilus file manager

Now, open the file manager and navigate to the folder containing our zdnet_test file. Right-click the file and select the "Encrypt" option.

How to encrypt a file on Linux (and when you should) (2)

3. Select your encryption method

You can now either select the GPG key you created earlier or just use a passphrase for the encryption. If you opt to go the key route, make sure to select the key you created and then click "OK." You won't be prompted for a passphrase if you go this route. But if you opt to just use a passphrase, you will be prompted to type and verify a new passphrase for the encrypted file.

How to encrypt a file on Linux (and when you should) (3)

4. Decrypt the file

With the file encrypted, you can then decrypt it by right-clicking the encrypted file and selecting "Open With Decrypt File." After this step, you'll be asked to name the decrypted file and click "Save." Then, for the encryption, you'll be prompted to either type the passphrase for your GPG key or the passphrase you added.

How to encrypt a file on Linux (and when you should) (4)

Whichever method you choose, I would highly recommend you test (using a test file) to make sure it works as expected before you actually encrypt an actual file you want to protect.

Once you have the process down (and it works as expected), you should then be safe to remove the unencrypted file. If you leave the unencrypted file on your drive, it can be accessed by anyone who can log into your desktop.

Also: Pop!_OS has a complicated name but it makes using Linux so easy

And that's how you encrypt and decrypt a file on the Linux operating system without having to install more complicated volume encryption tools.

See also

  • How to install Ubuntu Linux (It's easy!)
  • How to run a Windows app on Linux with Wine
  • How to get started with Git on Linux
  • How to kill a process in Linux
How to encrypt a file on Linux (and when you should) (2024)

FAQs

Which files do you need to encrypt answer? ›

As a good rule of thumb, you need to encrypt any file that contains sensitive or confidential information, whether it's legal or financial, business, or personal. This can include personal identification information, financial details, medical records, trade secrets, and more.

What is the best way to encrypt Linux? ›

In Linux environment Linux Unified Key Setup (LUKS) is used for encrypting entire block devices, hard drive, SSDs and even removable storage drives. Full hard drive encryption is possible only during the installation of the Linux operating system. In this case it will encrypt both the swap space and system partitions.

How do I secure a file in Linux? ›

Locking Files

The easiest way to lock others from even viewing a file is to change its permissions. Simply right-click on the file, select the “Properties” option, then click on the “Permissions” tab, and finally change the type of access of other users to “None”.

How to encrypt a file with command? ›

In Command Prompt, navigate to the directory where the files or folders you want to encrypt are located using the 'cd' command. Once in the desired directory, enter the following command: 'cipher /e [filename or folder name]'. Press Enter to initiate the encryption process.

How to encrypt a file in Unix? ›

Encrypt/Decrypt Files in Linux using Ccrypt
  1. -e, –encrypt :Encrypt. This is the default mode. ...
  2. -d, –decrypt: Decrypt. ...
  3. -c, –cat: Decrypt one or more files to standard output. ...
  4. -x, –keychange: Change the key of encrypted data. ...
  5. -u, –unixcrypt: Simulate the old unix crypt command.
May 15, 2019

What is the best way to encrypt a file? ›

How to encrypt a file
  1. Right-click (or press and hold) a file or folder and select Properties.
  2. Select the Advanced button and select the Encrypt contents to secure data check box.
  3. Select OK to close the Advanced Attributes window, select Apply, and then select OK.

How do I know what files need to be encrypted? ›

Files that need to be restricted and encrypted include, but aren't limited to the following:
  1. Legal documents.
  2. Financial records and information.
  3. Archived data.
  4. Personally Identifiable Information (PII)
  5. Patient health information (PHI)
  6. Trade secrets, copyrights, and intellectual property.

When would you encrypt a file? ›

Your financial records, including bank statements, tax returns, investment portfolios, and credit card statements, are a goldmine for cybercriminals. Encrypting these files can prevent unapproved access, reducing the risk of financial fraud and identity theft.

How do I zip and encrypt a file in Linux? ›

Create a Password-Protected ZIP File in Linux Using GUI
  1. Step 1: Go to the file location and right-click on the file.
  2. Step 2: Then click on the compress option.
  3. Step 3: Then click on the other option and set your password and click on Create option.
Feb 23, 2021

Can Linux be encrypted? ›

Linux devices can be encrypted in one of two ways: Full-disk encryption: Encrypting the block device before it is mounted on the system. File-based encryption: Encrypting only a folder or file using native filesystem features.

What is the command mostly commonly used to encrypt files in Linux? ›

gpg. One of the standard and most well know tools for encrypting files on Linux is gpg.

How to encrypt files in Linux script? ›

Encrypting and Decrypting Files
  1. ./encrypt.sh -e -i <input-file> -o <output-file> -k <key>
  2. ./encrypt.sh -e -i <input-file> -o <output-file> -p <password>
  3. ./encrypt.sh -e -i <input-file> -o <output-file>

How do I lock a file in Linux? ›

To enable mandatory file locking in Linux, two requirements must be satisfied:
  1. We must mount the file system with the mand option (mount -o mand FILESYSTEM MOUNT_POINT).
  2. We must turn on the set-group-ID bit and turn off the group-execute bit for the files we are about to lock (chmod g+s,g-x FILE).
Mar 18, 2024

How to secure data in Linux? ›

Businesses typically rely on SSH keys and passwords to secure their Linux servers. If you have created a new user profile with sudo privileges, using SSH keys and strong passwords is a must. This prevents just anyone from using that profile and making unauthorized changes to your server configuration.

Does Linux have encryption? ›

Linux devices can be encrypted in one of two ways: Full-disk encryption: Encrypting the block device before it is mounted on the system. File-based encryption: Encrypting only a folder or file using native filesystem features.

How do I encrypt a file in Ubuntu? ›

How to encrypt a file in Linux
  1. Step 1: Install GnuPG in Ubuntu. ...
  2. Step 2: Set the Cipher Algorithm. ...
  3. Step 3: Linux encrypt a file. ...
  4. Step 4: Linux encrypt a file with password. ...
  5. Step 1: Install Gnome Encfs Manager. ...
  6. Step 2: Create a new encrypted directory. ...
  7. Step 3: Mount directory. ...
  8. Step 4: Unmounting an encrypted folder.

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 5939

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.