Files and Folders management in Linux part 3 {File Security}

Files and Folders management in Linux part 3 {File Security}. In the previous article Files and folder management in Linux part 2 we learn about file types of Linux operating systems. In this article, we learn about how to provide security of a text file using Vim editor. The Vim text editor is a standard tool that is available for Linux and macOS also. It can be used for quickly encrypt text files with a password. It is faster and easier than encrypting a text with a separate utility. So we use Vim editor to encrypt text files in Linux operating system. 

Some Linux distributions such as Ubuntu include a minimal version of vim editor by default which only used for basic text editing and does not support encryption. So, you may need to install the full version of the vim editor to get this encryption support on your system. To do so each distribution has its own command line which is given below.

how to encrypt a text file in linux using vim editor

To install full version of Vim editor on Debian/Ubuntu systems use given below command

[email protected]:~# sudo apt install vim

To install full version of Vim editor on RHEL/CentOS systems use given below command

[[email protected] ~]# sudo yum install vim

To install full version of Vim editor on Fedora 30+ systems use given below command

[[email protected] ~]# sudo dnf install vim

Once the full version of the Vim editor is installed on your system then you can easily secure your text files. In other words, we can say that now you can encrypt your text files with passwords using Vim editor.

You can secure your file in two ways those two ways are given below with the example.

• Make an encrypted file using Vim editor.

• Encrypt your file using Vim editor at the time of file creation

1. How to make an encrypted file using Vim editor in Linux

Vim has a “-x” option which enables you to use encryption at the time of file creation. To use this command first of all install full version of Vim editor on your system. After that use the below-given command on your system. Like here I create an encrypted file named peanut.txt using vim editor.

[[email protected] ~]# vim -x peanut.txt

After executing this command on your system. Now it will be prompt you for an encryption key or password. Here provide your key to secure your file and press Enter. After that, it will be again prompt you for entering the same key again to verify the password. If the key matches after entering it for the second time then you can write your important content in this encrypted file.

[[email protected] ~]# vim -x peanut.txt
 Enter encryption key: *****
 Enter same key again: *****

After typing your important content in this file now you need to save your file. To do so first of all press ESC key from your keyboard to exit insert mode then type “:wq” and press Enter to save your content in that encrypted file. like figure is given below.

how to save a file in vim

Now nobody can open your file without having the encryption key. If anyone can try to open this file using the wrong key then it will show gibberish content in that file. That means your data is encrypted and nobody can access it without having a specific key. Always remember that you can only access the content of that file using  Vim editor. Because if you use another command like cat command to saw the content of file then it only shows you a gibberish content like given below. That type of result also shown on your screen when you provide the wrong key to open that encrypted file.

[[email protected] ~]# cat peanut.txt
VimCrypt~01!(???1?:|(ٮ?
[[email protected] ~]#

♦ You can also use this command to provide security to a file that is created by you a long time ago. To do so, first of all, make sure that a file is already created on your system then you have to apply given command “vim -x <file name>” on your system to encrypt that file with a password. Like in the given below example we encrypt a file named satish.txt which is already present on my system. Now to encrypt satish.txt file we use given below command.

[[email protected] ~]# vim -x satish.txt

Now after executing this command on your system. it will be prompted you to enter a password, by which you want to encrypt your text file. In it type the password which you want to use and press Enter to proceed. After that, it will again ask you for the password now type it again to confirm and press Enter. Now your file will be encrypted if you want to open that file you will need to enter this password each and every time.

How to change password of an encrypted file

Now if you want to change the password of your file then use given below command on your system.

[[email protected] ~]# vim +X satish.txt

After executing this command on your system it asks you for the old encryption key. In that type old encryption key and press Enter to proceed. Once your key is matched with the previous key then it will ask twice you for type new encryption key. If both of your new encryption keys are matched then your password for that file is changed successfully within seconds. Now your file opens with the new encryption key.

2.  How to encrypt a file at the time of creation using Vim editor

Vim has an alternate method for encrypting a text file. In this method, we use the “:X” option from the Vim editor to encrypt your file. To do so first of all open Vim editor on your system. For example, we create a file named manu.txt using Vim editor on our system we can do it by using the given below command.

[[email protected] ~]# vim manu.txt

Once you enter into the dd.txt file using Vim editor then edit that file normally. Like for entering in insert mode, we press “i” from the keyboard and to enter into command mode press “ESC” key from the keyboard. First of all, type your important content in this file by going into insert mode after that enter into command mode and type “:X” and press enter to proceed. The figure is shown below

how to encrypt a file using vim graphical

After that, you will be prompted to enter a password, by which the text file will be encrypted. Now again type this password to confirm your encryption key and press Enter to proceed.

how to set password using vim

Now after that save your file using “:wq” command and you can check this by opening your file using Vim editor. Now your encrypted file asks for a password each and every time when you want to open this file.

how to use command mode in vim editor to save a file

THAT’S IT

These are the possible ways by which you can encrypt a text file with the password by using Vim editor. Comment your thoughts.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: