×


Password Protect a File Using Vim on Ubuntu - Step by Step process ?

Vim is a powerful, feature-rich, highly extensible text editor that is used in all Linux OS. It is available in repositories of nearly all major Linux distributions. Because of its performance and low memory consumption, it is the top choice of most Linux users.

One another useful feature of the Vim editor is its built-in support for encryption. It is very helpful particularly in a multi-user environment where you can encrypt your files so that no one can access your confidential information.

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.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Ubuntu related queries for Linux Users.

In this context, we shall look into how to install Vim editor and use it to encrypt the files with a password.


How to install Vim Editor on Ubuntu ?

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.


How to 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. 

i. 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.

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

Enter encryption key:

iii. 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.

iv. Enter the same key again:

v. 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. 

vi. 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.


How to Open the password-protected file ?

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

$ vim example.txt

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

iii. Enter encryption key:

iv. 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.


How to Change the password for the encrypted file ?

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

$ vim example.txt

ii. Now provide the encryption password.

Then type :X (in the Normal mode) and press Enter.

iii. After that, you will be asked to enter a new encryption key.

Enter the key and confirm it by entering it again.

iv. Then save and exit the file.


How to Remove the password for the encrypted file ?

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

$ vim example.txt

ii. Now provide an encryption password.

Then type :X (in the Normal mode) and press Enter.

iii. Now when you are asked to enter a new encryption key, just hit Enter twice.

Then save and exit the file.

iv. 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.


[Need urgent assistance in fixing missing packages on Ubuntu Linux ? We are available to help you today. ]


Conclusion

This article covers how to password protect a file using Vim on Linux.  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 .

Always remember to appropriately secure text files that could contain secret info such as usernames and passwords, financial account info and so on, using strong encryption and a password.


To install VIM in Linux:

Vim is available in the repositories of most major distributions. 

So, use your distribution package manager to get installed.

For Debian based systems, use apt-get or apt package manager to install vim.

$ sudo apt-get install vim

For RHEL/CentOS based systems, use yum package manager to install vim.

$ sudo yum install vim-enhanced

For Fedora system, use dnf package manager to install vim.

$ sudo dnf install vim-enhanced

For openSUSE system, use zypper package manager to install pass.

$ sudo zypper in vim

For Arch Linux based systems, use pacman package manager to install vim.

$ sudo pacman -S vim