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.

PyCon India 2013

PyCon India 2013 – a premier conference in India on using and developing the Python programming language is conducted annually by the Python developer community and it attracts the best Python programmers from India and abroad. Conferences are the best place to meet old friends, make new one, discuss technical as well as non-technical things at same place. I made it to PyCon this year and met many old faces and made new friends too. It was a three day event starting from 30th August to 1st September 2013 with the first day focusing on workshops and last two days on conference with several talks lined up in two parallel tracks. There were BoF sessions conducted too.

Day Two – After Kiran’s keynote, I attended a session on building a super fast, scalable, distributed Python project using ZeroMQ by Srinivas. He explained about the message queues we can use to scale our code. Then I met Sanket, Co-Founder of CampusHash who conducts workshops-cum-hackathon; he discussed about packaging and distributing Python code. This talk changed the way I use to build modules and package them. I happen to meet a college pal of mine Rakesh at Plivo booth. He was demoing the company’s product, this called me to attend the talk on web telephony and how they use Python to build it. I also went to BlueJeans booth to see their demo on video collaboration in cloud. Discussed about the cloud vendors they use and learnt that they have a private cloud set up on OpenStack. I had some discussion on the networking piece, it went on for sometime, and learnt where I lagged in setting it up at our datacenter. We went for a lunch break and yes food was really tasty. After the break I attended a session on configuration management using Ansible. The talk was really interesting as I have been using Chef in my day to day work. Ansible is an IT orchestration engine written in Python and it makes deployment of applications, task-execution and systems easy. Post this session I met the speaker and discussed about functionality of Salt and Fabric, what differences does it make if I choose to use Ansible in the deployments.

Day Three – Keynote speaker for third day was Kenneth Reitz, sitting in the balcony I was silently listening to his talks. Until lunch I spent my time visiting various booths, seeing demo of their products. It was just awesome. I met Pavan at McAfee booth where I shared my exposure with anti-virus testing on Open Enterprise Server during my period of stay with Novell. Old memories cherished. I met few RedHat follks with whom I discussed my experiences with OpenStack and apparently one of them was a QA guy for OpenStack. May be next time when I give a shot to OpenStack I can keep in mind about his findings for the RDO project.
Python for testing, yes, a session by RedHat lady Anisha was the one which I couldn’t miss. Slides for the talks are here. Another session on penetration testing was cancelled due to unavailability of the speaker. The last talk that I attended was about using IPython Notebook by Konark Modi.
pyconI am seen busy arranging my laptop bag sitting next to Dhruv, a RedHat guy.Later in the evening we had a AGM of PSSI and various points related to spreading Python in India were discussed.

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: