Sunday, November 13, 2011

Mutexes VS Semaphore

Mutex: Mutex is a program object that is typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section."

Semaphore: A semaphore is like an integer, with three differences:
  • When you create the semaphore, you can initialize its value to any integer, but after that the only  operations you are allowed to perform are increment (increase by one) and decrement (decrease by one). You cannot read the current value of the semaphore.
  • When a thread decrements the semaphore, if the result is negative, the thread blocks itself and cannot continue until another thread increments the semaphore.
  • When a thread increments the semaphore, if there are other threads waiting, one of the waiting threads gets unblocked.
To say that a thread blocks itself is to say that it notifies the scheduler that it cannot proceed. The scheduler will prevent the thread from running until an event occurs that causes the thread to become unblocked. In the tradition of mixed metaphors in computer science, unblocking is often called  as€œWaking.

From the definitions, it is very much clear that mutex contains binary value either True or False but a semaphore contains an integer value which holds those many number of keys to get that resource.

To further understand these two, lets consider an example
  • Mutex is a key to a toilet. One person can have the key - occupy the toilet-at the time. When finished, the person gives (frees) the key to the next person in the queue.
  • Semaphore on the other hand is analogous to the he number of free identical toilet keys. Say we have four toilets with identical locks and keys. The semaphore count - the count of keys - is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.
One of the often asked question is "what is the difference between a mutex and a binary semaphore?"
  By binary semaphore I mean that the value store in the semaphore is 1. So its similar to the mutex. But there are quiet a few good characteristics that distinguish the two.
  • Mutexes can be recursive (so reentrant lock works), and Semaphores are not.
  • One significant difference - a semaphore may be procured and vacated by any thread in any sequence (so long as the count is never negative), but a mutex may only be unlocked by the thread that locked it. Attempting to unlock a mutex which was locked by another thread is undefined behavior
  • The Mutex class enforces thread identity by virtue of owners, so a mutex can be released only by the thread that acquired it. In contrast, the Semaphore class doesnot enforce thread identity.

Now lets understand what are recursive mutexes:
POSIX allows mutexes to be recursive. That means the same thread can lock the same mutex twice and won't deadlock. Of course it also needs to unlock it twice, otherwise no other thread can obtain the mutex. Not all systems supporting pthreads also support recursive mutexes, but if they want to be POSIX conform, they have to.

Now, you may ask why on the earth would I call lock on the same mutex. To find the answer please refer links 1 and 2. It has pretty good explanation and I dont want to duplicate the effort.

There is something called as Futexes aswell, these are Fast user level mutexes, pthread library on linux implements Futexes, so all your user level programs that spawn thread are already using Futexes. Futexes have an edge over mutexes and hence are very useful.

[] http://stackoverflow.com/questions/2415082/when-to-use-recursive-mutex
[] http://stackoverflow.com/questions/187761/recursive-lock-mutex-vs-non-recursive-lock-mutex
[] http://blog.feabhas.com/2009/09/mutex-vs-semaphores-%E2%80%93-part-2-the-mutex/
[] http://web.itu.edu.tr/kesgin/mul06/intel/instr/cmpxchg.html

Monday, September 12, 2011

Formatting a filesystem using loopback Devices



When it comes to selecting a filesystem, Linux provides its users with a vast set of filesystem to choose from. There are many filesystems which are part of the linux kernel and there are others that are developed over fuse layer and the list is constantly growing everyday. When ever a new filesystem is released you might not want to format it on a partition (because of limited disk space, or to avoid disk fragmentation etc). One of the best method to try out a new filesystem is to format it on a loop back device and mount it on a directory. In this post I will demonstrate how to create a loopback device and then format it with ext4 (you can use any Filesystem of your choice) and the mount it on a directory.

The very first step is to create a file which will act as a psuedo partition so choose the size appropriately. There are two ways of doing it
  * using dd command
  * using truncate command
I prefer truncate over dd  because truncate creates a sparse file so the file does not consume any
disk space untill you add data into it but file created using dd will consume the disk space. To confirm that, 
truncate --size=1GB file-1GB
  Now check the output of
ls -l file-1GB
du -h file-1GB
ls will report the file size as 1GB but du will report the file size as 0. So, it can be confirmed that the file doesn't use any disk space.

  Now try
dd if=/dev/zero of=file-1GB bs=1KB count=1000000
  Now check the output of ls and du.

So, now you have created partition but the system doesn't recongnize it. So lets attach it to a device file.
losetup -f
Let's assume that it retured /dev/loop0, now run
losetup /dev/loop0 file-1GB
Okay. Now all set, choose a filesystem of your choice and format the device. I choose ext4 to demonstrate it.
mkfs.ext4 /dev/loop0
Now mount the device on a directory.
mkdir /my-fs
mount -t ext4 -oloop /dev/loop0 /my-fs.
Thats it. You can confirm it by running
df
your Filesystem will be listed there. Happy hacking :)

Note: When you reboot the system your filesystem will not be displayed by df command. You can either add the entry into /etc/fstab or write a script that will attach the file-1GB to a device and run the mount command. A sample script can be found here

Sunday, August 7, 2011

Explore VIM


"Give me six hours to chop down a tree and I will spend the first four sharpening the axe.”
– Abraham Lincoln
I start the post with an interesting note. We must have our tools ready before we start our work, it makes our work more fun otherwise it becomes a pain. A similar case is with the programmers. They spend most of there time in front of the terminal either writing new code or refactoring the old code. Having an environment customized as per one's need can make a huge difference.

Vim is one of the pieces of software that I use almost everyday. I have been a VIM user since the college days. I started with vi, When I started using it I was a bit annoyed because I had to remember all those commands to browse through the file, changing the modes to move from one line to another was a pain. But when I first used VIM, I was like I have been waiting for something like this because it had all the features that were provided by vi and it had many more. It was definitely more powerful than vi. In this post I will share some of the features which I personally have found to be very useful.
  • In VIM searching a word in the given file is achieved  by typing "/" in the command mode. But adding the following lines to your .vimrc file can make a huge difference.

    To move the cursor to the matched string while typing the search pattern, set
    set incsearch
     With 'ignorecase', searching is not case sensitive:
    set ignorecase
     If 'ignorecase' is on, you may also want:
    set smartcase
    When 'ignorecase' and 'smartcase' are both on, if a pattern contains an uppercase letter, it is case sensitive, otherwise, it is not. For example, "/The" would find only "The", while "/the" would find "the" or "The" etc.

    We can also search for a word in the file by placing the cursor on the word to be searched and pressing "*", this will take you to the next instance of the word in the file.
    Press "n" to move to the next instance of the searched word and "N" to goto the previous instance of the searched string.
  • Searching and replacing is one of the very common task. One can achieve this in VIM by typing
    :%s/search-string/replace-string/
    This will search for "search-string" and replace only the first matched instance of that string with "replace-string. To replace all the instances of the "search-string" add "g" at the end of the command.
    :%s/search-string/replace-string/g
    To search and replace only in few lines, provide the range of lines in the command. To do a find and replace only in lines 50-100, run
    :50,100s/search-string/replace-string/g
    To delete "search-string", donot pass any parameter in the replace-string, it will delete the "search-string". To delete all the "search-string" in lines 50-100, run
    :50,100s/search-string//g
  • Type "gf" in command mode to open the file containing the word the cursor currently points to.
  • Most of the time while browsing the code we feel the need to have more than one file opened in the terminal, either we open each file in a seperate terminal or close the current file and open the other. But VIM has tabs. Yes, it provides tabs just the way the browser's provide tabs. To open a file in the new VIM tab, type
    :tabedit file_name


    Use "ctrl+alt+pgup/pgdn" to traverse between the VIM tabs.

    Type :help tabedit in the command mode to explore more.
  • You use some words frequently in your document and wish there was a way that it could be quickly filled in the next time you use the  same word, VIM provides autofill feature. Press
    ctrl+n
    to achieve this.


  • To add line numbers to the file type
    :set number
















  • Good programming is all about making the code more readable, one of the ways  of achieving it is through indenting the code. Most of the time coping a piece of code from one block to another breaks this, VIM provides a way to perform some actions on the selected block. Press shift+v to select a block, as shown in the figure below:
     
Selecting a block with shift+v



       
          Once a block is selected we can
              *  Move the block 8 spaces to the left by pressing shift + < or 8 spaces to the right by pressing
                  shift + > so as to indent the code
              *  Copy the block by pressing "y" and then pasting it somewhere else by pressing "p".
              *  Search and replace a word only in the selected block, press
                  ":'<,'>s/search_string/replace_string/g"


        Or we can select column's by pressing "ctrl + v" as shown in the figure below.

         
Selecting a Column by pressing cntl + v














  • This is my favorite, VIM allows to split the page horizontally and vertically. This is of great use when we want to view two different files at a time. To split the file vertically press :vsp in the command mode.

    Vertical split















              Press ":sp" to split the screen horizontally. Press ":help vsp" to explore more about how to
              switch between the window etc.
    • Upper case and Lower case: VIM provides an easy way to change the case of the line to UPPER case or LOWER case. Here is the command,

                gUU or guu 

      Place the cursor on the line which you want to convert the case and in the command mode type these letters. gUU will convert the text to upper case and guu will convert the text to lower case.

      You can also flip the case of a set of character with the help of "~" command. To explore more on this type

              :help gUU
    • To open files located in other directories, you can use the full or relative paths such as 
    :e file name 
    • redo and undo: In Vim to undo the last command press

                undo- u

      and to redo the last command press

                ctrl+r

      in command mode
    • When you copy some code from the browser or other editors like gedit and paste it into Vim, you may loose the alignment. To keep the alignment intact, type

                :set paste

       and then paste the code. To change back to normal mode, type

                :set nopaste

      in command mode.
    • To find more help on Vim type ":help" command mode.
    • Find below a few other options that can be set in your .vimrc

                * set scrolloff=100
                * set cursorline
                * set autoindent " always set autoindenting on
                * set backup " keep a backup file
                * set backupdir=~/bkp/vim
                * set history=50 " keep 50 lines of command line history
                * set background=dark

    Some useful links:
    [1]  http://vim.wikia.com/wiki/Vim_Tips_Wiki
    [2]  https://github.com/mja054/Random-tweeks/blob/master/vimrc - to find a sample vimrc
    [3] http://dan.hersam.com/docs/vim.html
    [4] http://vimdoc.sourceforge.net/htmldoc/options.html#'shiftwidth'

    Thursday, July 7, 2011

    Segmentation Fault

    A segmentation fault or bus error occurs when the hardware notifies a operating system about a memory access violation. On receiving the notification the OS sends a signal to the process which caused the exception. Now its upto the process receiving the exception to decide how it would like to handle this exception. By default the process receiving the signal dumps its state in to a core file and terminates but the default signal handler can be overridden to customize how the signal is handled.

    There are many ways to get a segmentation fault, at least in the lower-level languages such as C(++). A common way to get a segmentation fault is to dereference a null pointer:
    int *p = NULL;
    *p = 1;
    Segmentation fault also happens when you try to write to a portion of memory that was marked as read-only:
    char *str = "Foo"; // Compiler marks the constant string as read-only
    *str = 'b';              // Which means this is illegal and results in a segfault
    Accessing Dangling pointer also causes segmentation faults like here:
    char *p = NULL;
    {
        char c;
        p = &c;
    }
    // Now p is dangling
    The pointer p dangles because it points to character variable c that ceased to exist after the block ended. And when you try to dereference dangling pointer (like *p='A'), you would probably get a segmentation fault.


    Another reason for segmentation fault is to recurse without a base case, which causes a stack overflow:
    int main() { main(); }
    Segmentation fault also happens when accessing a region of memory already freed

    Whenever segmentation faults happens, a core is generated by the process in the current directory. By default, most of the Linux distros disable the creation of core. So if you dont find the core file run the following command to enable the creation of core files.
    $ ulimit -c unlimited
     Generally core files are created as core.<pid>, pid is the pid of the process that created the core.

    Running the above command will set ulimit to unlimited on that terminal. So when you start a new terminal it would not be set. So to make it default in all the new terminals add it into ~/.bashrc.

    But to make sense out of the core dump you must compile the program with debugging symbols. Lets take an example program to understand how to use the core dump to the fullest.
    $ cat hello.c
    #include <stdio.h>
    int
    main ()
    {
            int *p = NULL;
            *p = 2;
            return 0;
    }
     Now enable debugging symbols while compiling
    $ gcc -g -O0 hello.c -o hello
    Now when you run this program, you will receive the following error
    $ ./hello
    Segmentation fault (core dumped)
    You will find that a core file is created in the current directory.

    Now run the core with the debugger (on most of the distros gdb is provided if not install it)
    $ gdb hello core.5123
    It will display the line in the program that caused the error. There are many other options that you can use with gdb to debug your issue, we will visit that in future posts.

    Some of the best practices to follow while programming in C/C++ is to 
    • Always check for NULL before dereferencing the pointers. Example

               if (ptr == NULL)
                         //return with error
    • Also check for NULL after the call to dynamic memory allocation functions like malloc, calloc, new etc. Example

              ptr = (int *) malloc (10 * sizeof (int));
              if (ptr == NULL)
                         //return with error
    References
    * http://collectd.org/wiki/index.php/Core_file
    * http://stackoverflow.com/questions/2346806/what-is-segmentation-fault
    * http://en.wikipedia.org/wiki/Segmentation_fault

    Monday, June 27, 2011

    SSH: "Remote host Identification has changed" error


    SSH is the most common and widely used method for remote login. It is better the Telnet because it provides authentication mechanism to secure your machine (If you dont want to enter your password each time when you login, checkout the previous post on passwordless-ssh-login-howto). But, sometimes one may find that the machine that we used to login sometime back is refusing to authenticate. If the error that you found is
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
    Someone could be eavesdropping on you right now (man-in-the-middle attack)!
    It is also possible that the RSA host key has just been changed.
    The fingerprint for the RSA key sent by the remote host is
    9b:8e:77:08:7c:8f:2d:f7:0b:4e:dc:d3:98:29:dd:ec.
    Please contact your system administrator.
    Add correct host key in /root/.ssh/known_hosts to get rid of this message.
    Offending key in /root/.ssh/known_hosts:8
    RSA host key for 192.168.1.8 has changed and you have requested strict checking.
    Host key verification failed.
    Just delete the file ~/.ssh/known-hosts.
    > rm ~/.ssh/known-hosts
     Thats it, now try running ssh at the terminal it will succeed.

    Thursday, June 16, 2011

    Ubuntu: Creating a bootable Pendrive

     In this post, I will demonstrate how to create a bootable pendrive from an .iso image on ubuntu. Creating a bootable pendrive is very easy in ubuntu. If you are using ubuntu 10.10, then goto
    System->Administrator->Startup Disk Creator
    Select Startup Disk Creator it will prompt for password if you are not root, enter the password and it starts. It looks like this

    Startup Disk Creator


    Once you have started the Startup Disk Creator follow these steps to create a bootable Pendrive:

    1. click on  "other" button and select the location where you have stored the .iso file.


    2. Then if you want to erase all the data on the pendrive then press on "Erase Disk" button and wait for it complete. Skip this step if already empty.


    3. Select "Make Startup Disk" button.







    Thats it. Once it completes you have a bootable pendrive which you may use it for
    • liveCD
    • installing a fresh distro installation

    I have tested this for creating a bootable pendrive from ubuntu-11.04.iso on ubuntu-10.10. If you happen to use it for other distros then do post your results

    Friday, June 3, 2011

    mencoder: Converting .ogv to .avi

    recordMyDesktop produces videos in .ogv format. Youtube and many other websites fail to play the .ogv video. Use mencoder to convert the .ogv video. mencoder is a simple movie encoder, designed to encode MPlayer-playable movies to other  MPlayer-playable formats.

    In ubuntu, install the mencoder using
    >sudo apt-get install mencoder
    once installed, use the following command to covert the .ogv file to .avi file.
    >mencoder video.ogv -ovc xvid -oac mp3lame -xvidencopts pass=1 -o video.avi
     There are many more options to mencoder, read the man page for more info.

    gtk-recordMyDesktop

    The Chinese proverb "A picture is worth thousand words" fits perfectly here, so I will be demonstrating by showing a video.


    The front end makes use of the commandline options to perform the task, which means that the things we achieve through the gui can also be done through the commandline.

    Prev 

    Simple video capture

    Simple video capture:
    We start by exploring the recordMyDesktop through command line. If you just cant wait to capture your first video, then follow the command to start recordMyDesktop from the command line
    >recordmydesktop --no-sound -o my-first-video.ogv
     When you enter this command you will see



                                               
    From the screenshot, you can see that the recordMyDesktop records the desktop session untill a ctrl+c is pressed to stop it. On receiving the signal, it stops capturing the video and starts encoding the video. Once it completes, we have successfully captured the video. By default the video is saved in the home directory.

    By default recordMyDesktop captures the whole screen, but we can pass options to configure it according to our needs. For more information check the man page.

    Prev Next 

    RecordMyDesktop

    Intro:

    recordMyDesktop is an open source free software used to record desktop session on GNU/Linux. It is a very handy application to have on Linux and one can find lot of videos on net which make use of recordMyDesktop.  The project was started by John Varouhakis in 2006 and he continued to be the maintainer of the project for a long time. The project has not seen an active traffic for last year or two. The last release was  0.3.8.1. The project is hosted on sourceforge. Here is the link to download the source code.
     
    Like many other linux softwares out there, recordMyDesktop also comes in two flavors: the typical of linux commandline and two graphical frontends, gtk-recordMyDesktop and qt-recordMyDesktop. Most of the Linux destro's have recordMyDesktop pre-installed in them. To install it on ubuntu, use
    > sudo apt-get install gtk-recordmydesktop 
    gtk version of recordMyDesktop
    One last important nugget is recordMyDesktop supports only .ogv theora for video encoding, and .ogv vorbis for audio. If you want to convert your screencasts to other formats there are many good Linux tools for this and also you can find very helpful information here.

    Next 

    Saturday, May 21, 2011

    Code intendation

    In linux, indenting the C code is very easy only if you happen to know tools for doing it. One such tool is "indent". It is a linux command that can be used to indent the C code. By default it indents the code in "gnu" style.
    >indent file.c
     By using this command, we can even convert from one C style to another. For instance if you are acquainted to linux style of coding but you have a file that is in different style of coding, then use this command to convert the coding style to linux style of coding
    >indent -linux file.c
    Here, linux is an option to indent. There are many more options, so start exploring. 

    Thursday, May 5, 2011

    Temporary files - C, Shell and Perl way

    All most all of the programing languages allow us to create temporary files and directories. In this post I am going to explore the creation of temporary files in C, Perl and Shell script.

    C Implentation:
    To create temporary files in C, we make use of the library function mkstemp () which is defined under stdlib header file. The syntax of mkstemp ()
    int mkstemp (char *template);
    The mkstemp file takes a character array (make sure that its not a character constant but an array, as it will be modified) and returns a file descriptor to the file. It will create a file with the default permissions.

    Here, the template is a character array which has its last 6 characters marked "XXXXXX". These characters will be used to generate a unique name.

    An example of template can be "fileXXXXXX";

    Note: mkstemp() will create the temporary file in the current directory.  To create a file in a particular directory prefix the absolute path of the directory like to create a file under /tmp use
    "/tmp/fileXXXXXX"
    A sample C program
    #include <stdio.h>
    #include <stdlib.h>

    int
    main ()
    {
            char filename[] = "/tmp/fileXXXXXX";
            int  fd;

            fd = mkstemp (filename);

            printf ("file=%s\n", filename);

            /* perform the operation on the file desc
             * ....
             */

            close (fd);
            unlink (filename);

            return 0;
    }
    The temporary file is not deleted hence it must be deleted manually.

    You may also look into "mkdtemp (3)", "mkostemp (3)"

    Shell implementation:
    The create a temporary file in shell scripts, make use of $tempfile. It creates a temporary file in /tmp directory by default.

    Sample program:
    #!/bin/sh

    x=$(tempfile)

    ## Perform operations on file

    rm $x
     Perl implementation:
    Perl makes use of the File module to create the temporary files. I think perl gives more liberty to us. We can tune it according to our need like one of the main things that other languages missed is the deletion of the temporary file when the process terminates and many more. Like Shell perl also creates the temporary file under /tmp directory.

    Sample program:
    #!/usr/bin/perl

    use File::Temp 'tempfile';

    ($handle, $filename) = tempfile (UNLINK => 1, SUFFIX => '.dsk');

    ## perform the operation on the file
    The tempfile function returns a handle and the filename.

    Monday, April 25, 2011

    Bochs(2.4.6): A cross platform IA-32 Emulator HOWTO

    Hello Everyone,

    Few days back I installed Bochs from source code, I needed it start my work on Pintos (I will write about this in future posts), it was fun doing it. Bochs is free open source project hosted on sourceforge, so you can download it for free and use it and also make changes to the source and send in your patches and post questions on there mailing list, I must say its one of the active mailing list.

    Bochs is a portable x86 and x86-64 IBM PC compatible emulator and a debugger mostly written in C++ and distributed as free software under GNU. So what it means is that it provides an environment similar to the pc but with in the OS (hope I am not confusing), thus providing virtualization similar to another popular open source project Qemu. These emulator's by there name emulate the physical hardware for the OS to run on, they go to an extent of providing a pseudo environment where the OS thinks that it is running on the bare metal hardware but it will be one more process in the user's process list which the user can kill, restart and do what ever he wants with it. So these emulator's are commonly used by
    • OS kernel developers because they have to just start a new process when the kernel crashes rather than restarting the system.
    • Most of them use it to try out new OS distributions, mainly to run apps which are not available on there base OS. So a user using a LINUX machine can run window's within his/her machine.
     Bochs is capable of emulating
    • Harddrives
    • CD Drives
    • Floppy drives
    Unlike  Qemu (which uses KVM), Bochs doesnot provide any cpu virtualization thus one can find that it is much slower to run.

    In this post I will be demonstrating the installation of Bocs(2.4.6) from the sourcecode on Ubuntu (if you haven't tried it yet, go grab an Installation CD, it's worth a try). Before we do that, Bochs requires few packages to be pre-installed. So, follow to steps to install Bochs(2.4.6):
    1. Since it is written in C++, it requires a c++ compiler, so run
      sudo apt-get install build-essential
    2. The following packages are required to enable X-window's gui used by Bochs to provide a terminal when we run our OS.
      sudo apt-get install libx11-dev
      sudo apt-get install xserver-xorg-dev
      sudo apt-get install xorg-dev
    3. Ncurses  library, it provides API's, that allows the programmers to write text in a terminal (follow the wiki link http://en.wikipedia.org/wiki/Ncurses for more info).
      sudo apt-get install libncurses5-dev
    4. Flex is a free software alternative to lex
      sudo apt-get intall flex
    5. Lastly, GTK library to enable the internal debugger (use --enable-debugger option while running configure).
      apt-get install libgtk2.0-dev
      apt-get install libgtk2.0-0-dbg

    Once these packages are installed, we are just few steps.
    • Download the gipped package and unzip it using
      tar xf bochs-2.4.6.tar.gz
    • cd into the un-tarred directory and run configure (follow the link to see more compiling options). I used
      ./configure --enable-gdb-stub --with-x --with-x11 --with-term --with-nogui --prefix installation-path
      to install the simple bochs and
      ./configure --enable-debugger --enable-disasm --with-x --with-x11 --with-term --with-nogui --prefix installation-path
      to install the Bochs with internal debugger enabled. Use prefix to give different names to two different binaries.

    • Now run
      make && make install
    That's it. Now start using it :)

    Wednesday, April 20, 2011

    Scrolling up and down the Terminal

    I will be posting some of the interesting and useful tips to ease the use of command line in Linux. Today's tip is HOWTO scroll through the terminal page up and down. To scroll through the terminal use

               cntrl + shift + [up arrow | down arrow] [page up | page down]

    Tuesday, April 19, 2011

    Passwordless SSH login HOWTO

    Hello Folks,

    Its been a long time since my last post, but now I would try to be more consistent. In this post I am going to explain the step's to setup "Passwordless SSH" to a remote machine.

    Quite often we feel the need to login to a remote machine (maybe to backup our daily work or to run few test's..) what ever maybe the reason, we endup entering the password of that machine. Today we will learn howto setup a "Passwordless SSH" to a remote machine.

    Before we begin, there are few prerequisites that are needed to successfully perform the task at hand. The remote machine should have sshd (ssh server) installed on it. If not install it using the command

         > sudo apt-get install openssh-server

    The above command works only on Ubuntu, on other OS's like fedora use "yum install" etc.

    Now a days most of the OS's have openssh-client installed on them by default, if not present install it. This is to be present on the machine from which you are going to ssh to the remote machine.

    Once these are done, follow the step's below to successfully perform passwordless ssh login.

    1. Enter the command

         >ssh-keygen -t rsa
           Generating public/private rsa key pair.
           Enter file in which to save the key (/user/.ssh/id_rsa):
           Enter passphrase (empty for no passphrase):
           Enter same passphrase again:
           Your identification has been saved in /user/.ssh/id_rsa.
           Your public key has been saved in /user/.ssh/id_rsa.pub.
           The key fingerprint is:
           e2:8e:8a:91:f2:db:06:77:22:29:27:e0:4f:7a:3b:71 user@user-laptop
           The key's randomart image is:
           +--[ RSA 2048]----+
            |                           |
            |                           |
            |                           |
            |.                          |
            |o  .  . S                |
            |oo=ooE..            |
            |++++oo.            |
            |.= =.o                 |
            |. === .               |
           +--------------------------+

    For more info on ssh-keygen go through its man page.

    When it asks for the file just press enter and when it asks for the passphrase enter the passphrase else just press enter.

    Now it would have generated two files id_rsa and id_rsa.pub in /user/.ssh directory. The id_rsa file is the private key which you must never disclose it to anyone.

    2. Now copy the public key to the remoter machine. One of my favorite ssh command variant for this is

        > ssh-copy-id -i /user/.ssh/id_rsa.pub remote-user@remote-machine

    That's it, now you can login into the remote machine without entering the password.


    One of the common problem that we face while using ssh to login to the remote machine may be

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
    Someone could be eavesdropping on you right now (man-in-the-middle attack)!
    It is also possible that the RSA host key has just been changed.
    The fingerprint for the RSA key sent by the remote host is
    af:b6:ab:a9:c5:5e:ab:bb:b8:1c:1c:d6:82:0c:ce:9b.
    Please contact your system administrator.
    Add correct host key in /user/.ssh/known_hosts to get rid of this message.
    Offending key in /user/.ssh/known_hosts:34
    RSA host key for remote-machine-ip has changed and you have requested strict checking.
    Host key verification failed.


    If you face this problem, then just delete the file /user/.ssh/known_hosts and you are good to go.

    Sunday, March 20, 2011

    Toggling through the directory hierarchy

    I recently came across pushd and popd commands and found them to be of great help. I use command line to do a most of my work, while I am working there are lot of time's when I move deep into the directory hierarchy and then return to the directory I was previously working in. This is sometime's  tiring and time consuming. If you are one of them and don't know about the pushd and popd command's then you are at the right place. Let me explain it with an example -

           $ pwd
           /home/linux_code

    Now you wish to look at the code which might be under fs/minix and then later want to come back to the current directory then first push the current directory to the stack (that is what I think pushd stands for -push directory). It takes an argument (the directory to be saved/pushed).

          $pushd .
          $cd fs/minix

    Once you are done with the work just use the popd command to return to the previously saved direcotory. 

          $popd
          $pwd
          /home/linux_code

    Also note that, if you start pushing more than one directory on to the stack, then it will be popped in the reverse order (since stack follows Last In First Out (LIFO) order). So start using.

    Friday, February 18, 2011

    Hello World

    Hello Folks,

    This Junaid. I am working as a software engineer. I am new to this blogging world and  have been thinking of writing a blog for quite some time now. The questions that I had in my mind was when there are hundreds of blogs on the net that speak on almost every topic then what is the need of one more blog. A simple answer to it is that it will help me document my work and if it helps any one else then its an added point.

    So here I am with my first post , being a software engineer's, many of you must have observed that "HELLO WORLD" is the very first program that every programming language book teaches us to write. Thus, I thought why not start my blog with the title "hello world".

    I am an admirer of open-source philosophy and a linux user by choice. In this blog I 'll be speaking about linux, emerging technologies, open source tools, file system and many more things. So, folks do follow up regularly and do write your valuable comments.