Monday, May 23, 2011

How to SSH without password / passphrase


Goal: to login from local machine to Remote machine / Server via ssh without typing password
This simple tutorial explains how to SSH to a remote machine without typing your password. You can use this technique if you find yourself logging in to the same machine frequently and find typing your password tedious. It is also useful in scenarios when you have a script which needs to pull some files from a remote machine or perform a task on a remote machine via SSH, and you want to run this script automatically without having a human to type a password. May be deployed on Cloud , setting up a Hadoop Cluster
These instructions work on Linux and Mac. You can achieve the same result on Windows using Putty, but I haven’t documented the putty specific instructions here.
Step 1 of 2 : On local machine: Generate Authentication Keys
  1. ssh-keygen -t rsa

Accept the default choice. Hit enter.
Enter passphrase (empty for no passphrase):Enter same passphrase again:
Hit enter twice. A passphrase encrypts your private key so that no one can see it. However, you should NOT encrypt your private key if you want a password-less login.
  1. [hadoop@uidlt02 ~]$ ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /home/hadoop/.ssh/id_rsa.
  7. Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. 53:04:95:b3:d7:ba:aa:2e:90:7a:c0:18:8c:b6:b6:fd hadoop@uidlt02.xxx.com
  10. The key's randomart image is:
  11. +--[ RSA 2048]----+
  12. [hadoop@uidlt02 ~]$

What just happened?
On your local server you just created 2 files in your ~/.ssh directory.
  1. [hadoop@uidlt02 ~]$ ls -l ~/.ssh/
  2. total 16
  3. -rw-------. 1 hadoop hadoop 1675 May 23 15:30 id_rsa
  4. -rw-r--r--. 1 hadoop hadoop 406 May 23 15:30 id_rsa.pub
  5. -rw-r--r--. 1 hadoop hadoop 5488 May 22 04:58 known_hosts
  6. [hadoop@uidlt02 ~]$

id_rsa contains your private key. id_rsa.pub contains your public key.
Step 2 of 2 : On remote machine: authorize password less login
Login to remote machine
  1. ssh hostname -l username

Enter yes and press enter.
Enter your password, and hit enter.
Create a .ssh directory on the remote machine and create a authorized_keys file in that directory. You need to copy the entire contents of your local machine’s ‘id_rsa.pub’ and paste it in the .authorized_keys file on the remote server.
  1. mkdir -p .ssh
  2. chmod 700 .ssh
  3. cd .ssh
  4. touch authorized_keys
  5. chmod 644 authorized_keys
  6. vi authorized_keys
  7. # copy-paste the entire contents of your local machine's ~/.ssh/id_rsa.pub file in authorized_keys
  8. # logout
  9. exit


Important: Make sure you have the right permissions for .ssh directory and authorized_keysfile, as shown in chmod command above otherwise SSH will not honor your authorized_keys.
You should now be able to login to the remote server without typing your password.
  1. # type this command from your local machine
  2. ssh hostname -l username
SSH should log you in without password! Now, you can also scp or rsync (over ssh) without having to enter your password.

Wednesday, May 18, 2011

How to : Free Word to PDF Converter

All of us require at some point of time to convert a *.doc file to *.pdf . Yeah ! you guessed it right as alll of you already know , just thought of sharing :


# It compresses the size.
# It makes more portable.
# It makes sure your document is untampered , no accidental modification happens.


Here are the sites which allows you to do it for free, but only with some restrictions :


# freepdfconvert
# Adobecreatepdf
# Pdfonline


Enjoy happy converting !!

Sunday, May 8, 2011

How to : get command prompt by registry file addition

Very frequently we need to create command prompt at a certain folder. Most users directly go to command prompt and then type out the entire pathname to get to the required folder. 
Would it not be much easier to get to the command prompt by directly right-clicking on the folder? 
Below is how you can do it. This is valid only for Win XP. Other operating systems have not been checked.

1. Create a text file wherever it is easy for you to access it. 
2. Copy and past the following data into the file command.txt :

REGEDIT4
[HKEY_CLASSES_ROOT\Directory\shell\DosHere]
@="Command &Prompt Here"

[HKEY_CLASSES_ROOT\Directory\shell\DosHere\command]
@="C:\\Windows\\System32\\cmd.exe /k cd \"%1\""

[HKEY_CLASSES_ROOT\Drive\shell\DosHere]
@="Command &Prompt Here"

[HKEY_CLASSES_ROOT\Drive\shell\DosHere\command]
@="C:\\Windows\\System32\\cmd.exe /k cd \"%1\""

3. Save the file
4. Now rename the extension from a command.txt to a command.reg (registry type file). Go to DOS prompt and type C:\ren command.txt command.reg
5. Double clicking the file will pop up a dialog box saying "Are you sure you want to add the data to the registry".
6. Say yes and the data gets added into your windows registry.
7. Now any folder where you want a command prompt, it is as easy as right clicking on the folder and choosing "Command Prompt Here". 

Simple as that....

Matrix Checkerboard


We wish to find, for an arbitrary N, an N x N matrix with the following property: each entry in the matrix is the smallest non-negative number which does not appear either above the entry or to its left.
For example, for N=6, we might have the matrix:
012345
103254
230167
321076
456701
547610

  1. Write a function that computes the entry in a particular row and column. That is, complete the following:

    int entry_at(int row, int column) {

        // ...

    }

  2. Can you write the entry_at function to run in constant space and constant time?
Soln: Assume we count rows and columns starting with 0: 0, 1, 2, and so on. Let amn denote the number that appears in the (m, n)-cell. Then

amn = m ^ n.
It follows that this particular Latin square serves as the "addition" table for the XOR operation over the set of nonnegative integers. In particular, using the binary representation,
a100, 1000= 100 ^ 1000
= (1100100)2 ^ (1111101000)2
= (1110001100)2
= 908.
[Of course, if initially rows and columns were counted starting with 1, then we should consider instead 99 ^ 999 which is 900!]

Saturday, May 7, 2011

Memory Layout of a C Program - Stack Wise


    high  --------------
        |               |
        | Arguments and |
        |  environment  |
        |   variables   |
        |               |
        |---------------|
        |     Stack     |<--|--
        |(grow downward)|   |
        |               |   |User
        |               |   |Stack
        |               |   |Frame
        |               |   |
        | (grow upward) |   |( Mind the Gap )
        |      Heap     |<--|--
        |---------------|
        |      BSS      |<-- uninitialized static data(block started by symbol)
         |               |      long  sum[1000];
        |---------------|
        |      Data     |<-- initilized static data(int   maxcount = 99)
        |---------------|
        |      Code     |<-- text segment machine instructions

    low
Stack : where automatic variables are stored, along with information that is 
saved each time a function is called. Each time a function is called, the 
address of where to return to and certain information about the caller's 
environment, such as some of the machine registers, are saved on the stack. The 
newly called function then allocates room on the stack for its automatic and 
temporary variables. This is how recursive functions in C can work. Each time a 
recursive function calls itself, a new stack frame is used, so one set of 
variables doesn't interfere with the variables from another instance of the 
function. 
Text Segment: The text segment contains the actual code to be executed. It's 
usually sharable, so multiple instances of a program can share the text segment 
to lower memory requirements. This segment is usually marked read-only so a 
program can't modify its own instructions. 
Initialized Data Segment: This segment contains global variables which are 
initialized by the programmer
Uninitialized Data Segment: Also named "bss" (block started by symbol) which 
was an operator used by an old assembler. This segment contains uninitialized 
global variables. All variables in this segment are initialized to 0 or NULL 
pointers before the program begins to execute. 
The stack: The stack is a collection of stack frames which will be described in 
the next section. When a new frame needs to be added (as a result of a newly 
called function), the stack grows downward. 
Every time a function is called, an area of memory is set aside, called a stack frame, 
for the new function call. This area of memory holds some crucial information, like: 
1. Storage space for all the automatic variables for the newly called function. 
2. The line number of the calling function to return to when the called function 
returns. 
3. The arguments, or parameters, of the called function. 
The heap: Most dynamic memory, whether requested via C's malloc() and friends 
or C++'s new is doled out to the program from the heap. The C library also gets 
dynamic memory for its own personal workspace from the heap as well. As more 
memory is requested "on the fly", the heap grows upward. 

Virtual Reality

Virtual functions are a key feature of C++. Via dynamic binding( Late Binding ), they provide a powerful mechanism to change the semantics of a function at run time. To support the virtual function mechanism, different schema's have been adopted. I shall discuss the method used by Microsoft Visual C++ compiler (Model proposed by Martin O' Riordan).
Whenever a class declares a virtual function or is derived directly or indirectly from a class which declares a virtual function, the complier adds an extra hidden member variable which points to the virtual table. A virtual table is nothing but an array of pointers to the virtual functions. The entries in the virtual table are changed at run time to point to the correct function.Consider the following class :






    class Base { 
    public : Base() { } ~Base() { } virtual void VirtualFunc() { cout << "Base::VirtualFunc()" << endl ; }







    }; Base defines a trivial virtual function VirtualFunc(). Let us derive a new class 'Derived' from 'Base'.
      class Derived : public Base { public : Derived() { } ~CDerived() { } void VirtualFunc() { cout << "Derived::VirtualFunc()" << endl ; }
      };
As you can see, Derived has overriden VirtualFunc(). When we create an instance of Derived, typical object layout is shown below :






0064FDE0 84 30 41 00 E8 FD 64 „0A.èýd
0064FDE7 00 54 30 41 00 28 FE .T0A.(þ
0064FDEE 64 00 CF 10 40 00 01 d.Ï.@..
0064FDF5 00 00 00 38 FE 64 00 ...8þd.
0064FDFC D9 27 40 00 01 00 00 Ù'@....
0064FE03 00 48 02 76 00 98 02 .H.v.˜.
0064FE0A 76 00 68 F1 59 81 48 v.hñY.H
The first four bytes are pointer to the virtual table. The virtual table itself contains pointers to the virtual functions of the object. In our case, it is a pointer to VirtualFunc().
Memory layout of the virtual table is :





00413084 32 10 40 00 FF FF FF 2.@.ÿÿÿ 
0041308B FF DE 2D 40 00 EB 2D ÿÞ-@.ë- 
00413092 40 00 00 00 00 00 FF @.....ÿ 
00413099 FF FF FF 00 00 00 00 ÿÿÿ....


The first four bytes are pointer to VirtualFunc().
With this information, we can tweak an object and make it do weird things !! (just for fun)
Let us change the virtual table pointer and point it to our own table !!!.







////////////////////////////////////////// Modify.cpp ///////////////////////////////////////////////////
#include "iostream.h" 
#include "memory.h" 

//Pointer to a function returning void
typedef void (*PFN)();
typedef struct
{
PFN Fn;
} VTable;

//The function which will replace VirtualFunc
void ModifyFunc() { cout << " Modified the vitual table !!!" << endl ; }
int main()
{
Derived DerivedObj;

Base *pBase = &DerivedObj;
//Create our own virtual table
VTable MyOwnTable;
//Point Fn to ModifyFunc
MyOwnTable.Fn = ModifyFunc;
//Holder for pointer to virtual table
VTable *pVTable = &MyOwnTable;
//Modify the virtual table pointer by changing the first 4 bytes (assuming a long holds a pointer)
memcpy(&DerivedObj, &pVTable , sizeof(long));
//Call the virtual function
pBase->VirtualFunc();
//Strange !! ModifyFunc() is called ... enjoy playing :)
return 0;
}

A similar though slightly complex technique is used for multiple inheritance and virtual inheritance. Might as well write about them sometime...

Compiling Kernel Modules

Kernel modules need to be compiled a bit differently from regular userspace apps. Former kernel versions required us to care much about these settings, which are usually stored in Makefiles. Although hierarchically organized, many redundant settings accumulated in sublevel Makefiles and made them large and rather difficult to maintain. Fortunately, there is a new way of doing these things, called kbuild, and the build process for external loadable modules is now fully integrated into the standard kernel build mechanism. To learn more on how to compile modules which are not part of the official kernel (such as all the examples you'll find in this guide), see file linux/Documentation/kbuild/modules.txt

So, let's look at a simple Makefile for compiling a module named hello-1.c :

Example 2-2. Makefile for a basic kernel module

obj-m += hello-1.o  all:  make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules  clean:  make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
From a technical point of view just the first line is really necessary, the "all" and "clean" targets were added for pure convenience.

Now you can compile the module by issuing the command make . You should obtain an output which resembles the following:


$ makemake -C /lib/modules/2.6.11/build M=/root/lkmpg-examples/02-HelloWorld modules
make[1]: Entering directory `/usr/src/linux-2.6.11'   CC [M]  /root/lkmpg-examples/02-HelloWorld/hello-1.o  Building modules, stage 2.   MODPOST   CC      /root/lkmpg-examples/02-HelloWorld/hello-1.mod.o   LD [M]  /root/lkmpg-examples/02-HelloWorld/hello-1.ko make[1]: Leaving directory `/usr/src/linux-2.6.11'
$

Note that kernel 2.6 introduces a new file naming convention: kernel modules now have a .ko extension (in place of the old .o extension) which easily distinguishes them from conventional object files. The reason for this is that they contain an additional .modinfo section that where additional information about the module is kept. We'll soon see what this information is good for.

Use modinfo hello-*.ko to see what kind of information it is.


$ modinfo hello-1.ko filename:       hello-1.ko vermagic:       2.6.11 preempt PENTIUMII 4KSTACKS gcc-3.3 depends:


HOW TO : Compile Linux Kernel 2.6


This is the start point, if you want to play around with your customized kernel. Compiling custom kernel has its own advantages and disadvantages. Compiling kernel needs to understand few things and then just type couple of commands. This step by step howto covers compiling Linux kernel version 2.6.xx under Debian GNU Linux. However, instructions remains the same for any other distribution except for apt-get command.

Step # 1 Get Latest Linux kernel code


Visit http://kernel.org/ and download the latest source code. File name would be linux-x.y.z.tar.bz2, where x.y.z is actual version number. For example file linux-2.6.38.5.tar.bz2 represents 2.6.38.5 kernel version. Use wget command to download kernel source code:
$ cd /tmp
$ wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-x.y.z.tar.bz2

Note: Replace x.y.z with actual version number. USE sudo before the commands, if you are not the root / admin

Step # 2 Extract tar (.tar.bz3) file



Type the following command:
# sudo tar -xjvf linux-2.6.38.5.tar.bz2 -C /usr/src
# cd /usr/src

Step # 3 Configure kernel



Before you configure kernel make sure you have development tools (gcc compilers and related tools) are installed on your system. If gcc compiler and tools are not installed then use apt-get command under Debian Linux to install development tools.
# sudo apt-get install gcc
Now you can start kernel configuration by typing any one of the command:

  • $ make menuconfig - Text based color menus, radiolists & dialogs. This option also useful on remote server if you wanna compile kernel remotely.
  • $ make xconfig - X windows (Qt) based configuration tool, works best under KDE desktop
  • $ make gconfig - X windows (Gtk) based configuration tool, works best under Gnome Dekstop.


For example make menuconfig command launches following screen:
$ sudo make menuconfig

You have to select different options as per your need. Each configuration option has HELP button associated with it so select help button to get help.

Step # 4 Compile kernel



Start compiling to create a compressed kernel image, enter:
$ sudo makeStart compiling to kernel modules:
$ sudo make modules

Install kernel modules (become a root user, use su command):
$ su -
# sudo make modules_install

Step # 5 Install kernel


So far we have compiled kernel and installed kernel modules. It is time to install kernel itself.
# sudo make install
It will install three files into /boot directory as well as modification to your kernel grub configuration file:
  • System.map-2.6.38.5
  • config-2.6.38.5
  • vmlinuz-2.6.38.5

Step # 6: Create an initrd image



Type the following command at a shell prompt:
# cd /boot
# sudo mkinitramfs -o initrd.img-2.6.38.5 2.6.38.5

initrd images contains device driver which needed to load rest of the operating system later on. Not all computer requires initrd, but it is safe to create one.

Step # 7 Modify Grub configuration file - /boot/grub/menu.lst


Open file using vi:
# vi /boot/grub/menu.lst

title           Debian GNU/Linux, kernel 2.6.38.5 Default root            (hd0,0) kernel          /boot/vmlinuz root=/dev/hdb1 ro initrd          /boot/initrd.img-2.6.38.5 savedefault boot

Remember to setup correct root=/dev/hdXX device. Save and close the file. If you think editing and writing all lines by hand is too much for you, try out update-grub command to update the lines for each kernel in /boot/grub/menu.lst file. Just type the command:
# update-grub
Simple. Isn't?

Step # 8 : Reboot computer and boot into your new kernel


Just issue reboot command:
# reboot

Step # 9 : Check your new kernel version


Just issue the command:
# uname -a

Linux sudipto 2.6.38.5 #33-Ubuntu SMP Sun Sep 19 20:34:50 UTC 2010 i686_64 GNU/Linux