Encrypt a file using Vim – a programmer’s editor

This post explains how to encrypt a file in Linux using Vim ( text editor ). Here I will show how one can secure a file at the time of creation as well as during editing it. Depending upon your distribution you can install Vim :

  • $ sudo yum install vim [ RHEL / Centos machines]
  • $ sudo apt-get install vim [ Debian / Ubuntu machines ]
  • $ sudo dnf install vim [ Fedora 22 + machines ]

If you check the manual page of Vim, you will see -x option , I’m going to enable this option and see how it works.

When I open a file using -x option
vim -x foo.txt

vim -x foo.txt

 

 

 

 

If the encryption key matches you would be able to open the file.

file content

 

 

 

Next time when you open the same file ‘foo.txt’ you will be asked for encryption key, if you enter a wrong key here is what you see :

Wrong key

 

 

 

 

If you open the file using vim foo.txt

Key mismatch

 

junk characters

you will see some junk characters inside it.

If you look at the warning message above it says – “Using a weak encryption method; see :help ‘cm’ ” 

Open the file again and type ESC :help 'cm' from within the editor. It shows you a manual page for type of encryption being used.

Depending upon the version of Vim editor you have you can set type of encryption method from the above list.

Command –  : setlocal cm=blowfish

Now you won’t see above warning message.

Using the command :x you can set a crypto password after you’ve opened this file using Vim.

This tip will help you to secure sensitive files using a strong encryption method.

Useful tips on sed and awk

I was recently developing a code where I came across various search techniques and pattern matching that is possible using ‘sed’ and ‘awk’. Am sharing some of those tips here :

  • You want to search and print the second category in a given file. Here is how a file looks like :

Apple

o

n

e

Apple

t

w

o

Apple

t

h

r

e

e

Command: sed -nr ‘/^Apple/H;//,/^done/G;s/\n(\n[^\n]*){2}$//p’  file_name

Output:

t

w

o

  • Printing the sum of a given column in a file :

1  1

2  4

3  9

Command: awk ‘{ sum += $2 } END { print sum }’  file_name

Output: 14

  • sed  has the N command which will read the next line into the pattern space.

Command:  sed -n ‘/pattern/{N;p}’ file_name

In order to print 2 lines simply put {N;N;p}

  • Print section of file based on line numbers (lines 2-10, inclusive)

Command: sed -n ‘2,10p’ file_name

  • Print the last line of a file (emulates “tail -1”) 

Command: sed -n ‘$p’ file_name

I came across this page to understand few one-liner commands.

Laptop lid close behavior on Fedora Core 18

I just installed Fedora Core 18 on my laptop and learnt that GNOME no longer handles power policies on closing the laptop lid. It is done by systemd now. You can read the reference here.

I installed gnome-tweak-tool to see if any setting there can affect the behavior. Gnome-Tweak-Tool has the following settings done on my laptop – Click on “Shell” ; ‘Laptop lid,when closed, will suspend even if there is an external monitor ‘ Set this to “OFF”. Apparently it doesn’t effect.

Non-working stuffs :
Use dconf-editor to establish a setting of /org/gnome/settings-daemon/plugins/power of lid-close-ac-action to one of blank or nothing.
gnome-tweak-tool (Advanced Settings) settings on the “Shell” tab is no longer available in GNOME 3.6

Solution :
The solution for this issue is : Edit (sudo) vi /etc/systemd/logind.conf and change the line #HandleLidSwitch=suspend with HandleLidSwitch=ignore
Then restart the service using the following command : (sudo) systemctl restart systemd-logind.service
Now close the laptop lid, it will not suspend.

This is helpful when you put your laptop on a dock, and connect with external keyboard, mouse and external monitor with laptop lid closed.

Solved wireless issue on Compaq 420 laptop

My laptop ( Compaq 420 ) is armed with CentOS 6. Freshly installed.
There was some issue with the wifi driver, because of which I remained connected to a wired network.
I sat and spent some time in troubleshooting it and landed up with solutions posted on CentOS forum.
Few details regarding the Operating System :
[root@ashutosh ~]# uname -arm
Linux ashutosh 2.6.32-279.22.1.el6.x86_64 #1 SMP Wed Feb 6 03:10:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

[root@ashutosh ~]# lspci | grep controller
00:02.0 VGA compatible controller: Intel Corporation Mobile 4 Series Chipset Integrated Graphics Controller (rev 07)
00:02.1 Display controller: Intel Corporation Mobile 4 Series Chipset Integrated Graphics Controller (rev 07)
00:1a.0 USB controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #4 (rev 03)
00:1a.1 USB controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #5 (rev 03)
00:1a.2 USB controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #6 (rev 03)
00:1a.7 USB controller: Intel Corporation 82801I (ICH9 Family) USB2 EHCI Controller #2 (rev 03)
00:1d.0 USB controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #1 (rev 03)
00:1d.1 USB controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #2 (rev 03)
00:1d.2 USB controller: Intel Corporation 82801I (ICH9 Family) USB UHCI Controller #3 (rev 03)
00:1d.7 USB controller: Intel Corporation 82801I (ICH9 Family) USB2 EHCI Controller #1 (rev 03)
02:00.0 Network controller: Realtek Semiconductor Co., Ltd. RTL8191SEvA Wireless LAN Controller (rev 10)
85:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8101E/RTL8102E PCI Express Fast Ethernet controller (rev 02)

From the above output it’s clear that the laptop has a Realtek RTL8191SEvA Wireless device.
Luckily it’s supported by the kmod-8192se package from ELRepo. Please follow the instructions to setup ELRepo repository and install the package by :
yum install kmod-r8192se
and reboot.
Wireless works out of the box.

Firefox: “Save and Quit” option disappeared ?

I was finding it difficult to save my currently opened tabs whenever I quit Firefox browser ; later I learnt that up from Firefox 4, one change that has been brought is to by default stop Firefox displaying warning messages. It can be changed from the preferences.
Here is how I learnt a way to get back “Save and Quit” option in Firefox 6.0 which has disappeared somehow :
1. Type about:config into the location bar and press Enter.
2. Accept any warning messages that pops up. You will be taken to list of preferences.
3. Locate the preference browser.tabs.warnOnClose, if its value is set to false, double-click on it to change it’s state to true
4.Repeat this for 3 other preferences browser.warnOnQuit, browser.warnOnRestart and browser.showQuitWarning

Happy browsing !

How far will you get with “rm -rf /” on Linux ?

Have you ever left your Terminal logged in,  only to find a so called friend/ colleague came up and typed ” rm -rf / ” ?

I actually did so on a virtual machine (VM). It’s a CentOS VM which was used for some applications test. I created a backup of this VM and thought of shooting a video as I proceed to run ” rm -rfv / ” on it.

You can watch my video recordings here.

I also did so to learn what all directories do not get deleted with this command. And also, after the command has executed  will “rm” have removed itself  ? It seems that OS protects certain directories like /sys, /proc, /dev. At the end of the video you can see that “ls” command is not found and so is “/bin/rm”. Needless to say /bin, together with /bin/echo has been deleted. So, issuing ” echo * ”  just displayed * at the promt. I rebooted the VM and ended up with a black screen. OS is gone !! After the command finishes, disk is wiped off, including the OS. The Kernel and current processes will continue to run from memory, but many processes will die because they fail to access some file. OS will obviously fail to boot next time.

Still unanswered – Files that is currently open ( i.e rm ) even if deleted, won’t disappear from disk until closed.  I am not sure, if it is safe to assume,  the Kernel could very well load the removed file into memory and remove it from disk immediately ; and keep this memory copy running until the command is open  (  i.e rm is running )

Mistakenly, do not run this command on host system’s terminal. I did so on a VM, else you will land up putting your head on hand, hand between knees ! as one who’s world has just come to an end.

%d bloggers like this: