How to create password protected file in Linux. Learn how to encrypt a file in Linux using gpg. Users can password protect a text file in Linux Ubuntu.
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.
‘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).
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.