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


This article is all about Files and Folder 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 system. 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 which is available for Linux and MacOS also. It can use for quickly encrypt text files with a password. It is a 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 his own command line which is given below.

Use Vim editor for encrypting a text file

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

root@localhost:~# sudo apt install vim

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

[root@localhost ~]# sudo yum install vim

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

[rahul@technohelper24 ~]# sudo dnf install vim

Once full version of Vim editor is installed on your system then you can easily secure your text file. In other words, we can say that now you can encrypt your text file with passwords using Vim editor. Now 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 when creating files. To use this command first of all install full version of Vim editor on your system. To do so use given below command on your system. Like here I create an encrypted file named as rahul.txt using given below command.

[root@localhost ~]# vim -x rahul.txt

After executing this command on your system. Now you can saw that it will be prompt you for a crypt key. Here provide your key for secure your file and press Enter. After that, it will be again prompt you for entering the same key again. If the key matches after entering it for the second time then you can able to write your important content in this encrypted file. The example is given below.

 [root@localhost ~]# vim -x rahul.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 reach in to command from 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 crypt 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.

[root@localhost ~]# cat rahul.txt
VimCrypt~01!(???1?:|(ٮ?
[root@localhost ~]#

 

♦ You can also use this command for providing security to a file which is created by us 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 this tricks on your file to encrypt that file with a password. Like in given below example we encrypt a file named as satish.txt which is already present on your system. Now to encrypt satish.txt file we use given below command.

 [root@localhost ~]# vim -x satish.txt

Now after executing this command on your system. You will be prompted to enter a password, which the text file will be encrypted with. Now 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.

[root@localhost ~]# vim +X satish.txt

After executing this command on your system it asks you for the old encryption key. Now type 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 your encryption key 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 Vim to encrypt your file. To do so first of all open Vim editor on your system. For example, we launch Vim editor for creating a new file named as dd.txt on your system by using given below command.

[root@localhost ~]# vim dd.txt

Once you enter in 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 entering into command 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. Figure is shown below

how to use :wq in Vim

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

how to set password using Vim editor

Now after that save your file using :wq command and you can check this by opening your file using Vim editor. Now you can saw that your file asks a key each and every time when you want to open this file. As the figure is shown below

how to use command mode in Vim editor

                                                               THAT’S IT

These are the possible ways by which you can encrypt a text file with the password by using Vim editor. If you have any queries regarding this then solved out through the comment section. If you have found this article helpful for you then provide feedback to us. Because your feedback is valuable for us and also subscribe our blog for further updates.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s