Do you want to password protect a folder in Linux command line? If Yes, here is the method to create password protected file in Linux command line.
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.
In simple words we can that the GNU Privacy Guard (GPG or gpg) tool is a native OS security tool for encrypting files which provides digital encryption and signing services using the OpenPGP standard.
‘gpg’ is the OpenPGP only version of the GNU Privacy Guard (GnuPG). It is a tool to provide digital encryption and signing services using the OpenPGP standard.
How to Password Protect File in Linux
Here is the syntax to password protect file in Linux.
$ gpg -c /path/to/file
gpg’ may be run with no commands, in which case it will perform a reasonable action depending on the type of file it is given as input (an encrypted message is decrypted, a signature is verified, a file containing keys is listed).
The command option -c or –symmetric is used to encrypt with a symmetric cipher using a passphrase. Note that this may be combined with –sign (for a signed and symmetrically encrypted message), –encrypt (for a message that may be decrypted via a secret key or a passphrase), or –sign and –encrypt together (for a signed message that may be decrypted via a secret key or a passphrase).
Similarly, you can use the –decrypt or -d command option to decrypt the file given on the command line. If the decrypted file is signed, the signature is also verified.
When you run the command, you will be asked to enter passphrase and confirm it. Note that when you don’t mention full file path in above command, gpg will look for the file in your present working directory.
Also, on some Linux/Unix 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
Open Password Protected Files in Linux
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/FILE-NAME.gpg
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.
Password protect a file in Vim
Users can also password protect a file in Vim. Enter the following command in order to do so:
$ vim -x [FILE-NAME].txt
While creating a text file on VIM, the -x command option indicates to encrypt the file. After executing the command, a blank file by the specified name will open in the Vim editor. Now write/insert the data or some text (first press the ‘i’ key). Once done, then quit and save the file by pressing Esc+wq keys.
This is how to create a password-protected text file through the Vim editor.
Open a Password Protected File
To open a password-protected text file enter the following command:
$ vim [filename].txt
Then the system will ask you to enter the encryption key/password to open the file.