password protect file in linux

How to Password Protect File in Linux

Many times you may need to add password protection to your files if you want to store them on cloud or transfer them over internet, or carry them on a USB. Linux makes it easy to password protect your files using gpg (GNU-pg) command. It comes pre-installed in most Linux distributions. In this article, we will look at how to password protect file in Linux.


How to Password Protect File in Linux

Here is the syntax to password protect file in Linux.

gpg -c /path/to/file

Here is an example to encrypt file /home/user/data.txt with password. When you run the command, you will be asked to enter passphrase and confirm it.

$ sudo gpg -c /home/user/data.txt
Enter passphrase:
Repeat passphrase:

Now gpg will create an encrypted .gpg file /home/user/data.txt.gpg. Please note, if you don’t mention full file path in above command, gpg will look for the file in your present working directory.

On some systems, you may need to additionally use the option –no-symkey-cache to create encrypted file. This enforces use of the pass phrase to decrypt the file.

$ sudo gpg -c -no-symkey-cache /home/user/data.txt
Enter passphrase:
Repeat passphrase:

To decrypt the file, call gpg command on encrypted file as shown below. Change the file path depending on the location of .gpg file.

$ gpg /home/user/data.txt.gpg
Enter passphrase:

You will be asked to enter passphrase. When you enter the right password, it will automatically create the original file data.txt at the same location as your .gpg file.

If you want to password protect folders using gpg command, you can archive it into a file and then use gpg command to encrypt the archived file.

In this article, we have looked at how to easily password protect file using gpg command.

Also read:

How to Block or Disable User Login in Linux
How to Check Kernel Version in Linux
How to Create CSR for Wildcard SSL Certificate
How to Remove Unused Kernels in Ubuntu
How to Remove Unused Kernels in Ubuntu

3 thoughts on “How to Password Protect File in Linux

  1. Thank you very much for these various tutorials.
    Your selection of tutorials is really good.
    It’s been a few decades since I first learned how to do some of these things.
    Your tutorials are well-written and to the point; a good way to relearn without fuss.

  2. When I first tried this, the file was decrypted without having to enter the pass phrase. On looking at the man page for gpg under the -c option, it suggested additionally using the option –no-symkey-cache . This enforces use of the pass phrase to decrypt the file.

Leave a Reply

Your email address will not be published. Required fields are marked *