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.

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

Leave a Reply

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