Ubuntu

How to Password Protect a File Using Vim on Linux

Password-Protect-File-Using-Vim-Linux

Encryption is the process of encoding files in a manner that they can only be accessed by those who are authorized. It has become increasingly important to encrypt your files, but it does not mean that you have to use an encrypted filesystem. When we use an encrypted filesystem, whole data is encrypted. But sometimes, this may cause trouble as forgetting a password can result in losing all the data residing on the filesystem. To avoid such circumstances, it is better to encrypt individual files because if you forget the password, you will lose only one file.

The vim text editor, included on Linux offers built-in functionality for file encryption. In this post, we will be seeing how to install Vim editor and use it to encrypt the files with a password.

Note: The procedure has been performed on Ubuntu 20.04 LTS. The installation of Vim varies for various distributions but the procedure to use Vim is the same for all distributions.

Installing the Vim Editor

Vim is included in Linux repositories; you only need to install it. Following are the commands to install Vim in various Linux distributions. You should have sudo privileges to install Vim in your system.

For Debian based distributions (Debian, Ubuntu, Linux Mint):

$ sudo apt-get install vim

For RPM (Red Hat, Fedora, CentOS, SUSE):

$ sudo dnf install vim

For Arch Linux and Manjaro:

$ sudo pacman -S vim

Once installed, you can check whether your Vim version supports encryption by running the following command in Terminal:

$ vim --version

If +cryptv is listed under the Features included (+) or not(-) list; it means your Vim version supports encryption.

Password Protect a File Using Vim in Linux

Once Vim is installed and you have verified that it supports encryption, the next step to encrypt the files with a password is very simple. Normally, when you create a file in vim, you simply type vim followed by the filename. In the same manner, to create a new file protected with a password, execute the same command but with -x option as follows:

$ vim -x example.txt

Replace example.txt with your filename. The -x in this command tells Vim that you want to password protect this file.

After executing the above command, you will be asked to enter a password for this file by prompting you with:

Enter encryption key:

Type a password and press Enter. Make sure you remember it or else; you will not be able to access the file.

Once you typed the password, it will ask you to enter it again to make sure you did not make any typing error.

Enter the same key again:

Type the same password again and press Enter.

Once you set the password and confirmed it, a new file with the specified name (example.txt in our case) will be opened in the Vim editor. Now you can use Vim exactly the way you use it before for text editing. Once you are done with text editing, save and exit the file. You can do this by pressing the Esc key, then typing :wq, and pressing Enter.

Now you will have an encrypted file protected with a password.

To verify if your file is encrypted, try viewing this file with the cat command or opening it in any other text editor like Nano, Gedit, etc. This is how our password protected example.txt file looks like when we opened it in Gedit.

Open the password-protected file

If you want to open the password-protected file, again open it in Vim like this:

$ vim example.txt

Now you will be asked to provide the password to decrypt the file with the following prompt:

Enter encryption key:

Enter the password to decrypt the file. Once you entered the key, your file will open in Vim editor and you will be able to view and edit it as a normal file. Once you are done with file editing, save and close it and your file will be encrypted again.

Change the password for the encrypted file

To change the password for the encrypted file, open the file in Vim.

$ vim example.txt

Now provide the encryption password. Then type :X (in the Normal mode) and press Enter.

After that, you will be asked to enter a new encryption key. Enter the key and confirm it by entering it again.

Then save and exit the file.

Remove the password for the encrypted file

To remove the password for the encrypted file, open the file in Vim.

$ vim example.txt

Now provide an encryption password. Then type :X (in the Normal mode) and press Enter.

Now when you are asked to enter a new encryption key, just hit Enter twice. Then save and exit the file.

By doing so, your file will be decrypted and you will not be asked to enter the encryption key when you open the file again in the future.

In this post, we have explained how to password protect a file using Vim on Linux. We have also explained how to access a password-protected file, change, and remove its password if you need to do so. When password protecting a file, make sure you remember it, or else you will not be able to access your file. You can also use a password management software if you find it difficult to remember passwords. Or visit our guide on how to install Teampass password manager on Ubuntu.

Similar Posts

Leave a Reply

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