How to password protect a file in Linux

Written by
Date: 2020-05-09 10:30:00 00:00

Any file can be password protected if you are using Linux, that is a good idea, in order to have them stored in the cloud, or carry them in a usb dongle.

To encrypt a file in Linux we will use, gpg which is parte of the GnuPG. Syntax of gpg

gpg -c filename.ext

Let's imagine we have a file called my-personal-info.txt

I want to encrypt with a password to protect the file, run this command.

gpg -c my-personal-info.txt

The output of the command is:

$ gpg -c my-personal-info.txt 
Enter passphrase: 

Enter a good passphrase, be sure to remember it, and do not use an easy one.

Then you will get

Repeat passphrase: 

Now gpg has created an encrypted file called my-personal-info.txt.gpg, the original file is still there, so you may want to erase it, or only transport the encrypted one, or send by email the encrypted one.

To decrypt a file just enter this command.

gpg my-personal-info.txt.gpg

You will be asked for the password or passphrase, and the file will be recreated. This is a good idea to store some important information like passwords to important sites.