Sunday, 8 December 2013

Linux runlevels and rc.d scripts

A linux runlevels determines the applications and services which should be run at that time.

Level      Introduction
0             Shutdown the system
1/s/S       Single user mode – for recovery and maintenance.
2             Multiuser mode with graphical interface.
3             Multiuser mode with text login screen.
4             Can be customized.
5             Multiuser mode with graphical interface and networking.
6             Reboot the system.

Note that for ubuntu system you will not find any differences between runlevel 2,3 & 5.
You can check your current runlevel of system by typing following command in your linux terminal.

$ runlevel
N 2

Here first number shows previous runlevel and second number shows current runlevel of system.N shows there are no previous runlevels after system restored.

To change the runlevel type

$ telinit 5

will change the system runlevel to 5. To set default runlevel,open '/etc/init/rc-sysinit.conf' and change the following line according to your need.
env DEFAULT_RUNLEVEL=2

To shutdown the system you can type

$ shutdown -h 3 “message to broadcast”

will scheduled the shutdown ( here option '-h' for halt )after 3 seconds with message you have set for broadcast.

chkconfig - will list registered services and display their status at run level. You need to install that from repository if it is not available in your system.
You can check which services are executed when system enters in this runlevel at

/etc/rc0.d
/etc/rc1.d
....
....
/etc/rc6.d

If you want to disable/enable any services which are running in this runlevel,you can do so by just editing scripts name in that directory.For more information you can refer 'README' document in same directory.

Thursday, 5 December 2013

Valgrind To Check Memory Leak

Valgrind is very useful tool for checking memory leak and heap summary in program running in linux system.You will find this tool easily. Just use 'apt-get install valgrind' for that.You can do it by yourself in few minutes.Follow the procedure mentioned  below and you are done !!

Make one simple program 'memory_leak.c'.

#include<stdio.h>
#include<stdlib.h>

void main()
{
    int *p;
    p=(int *)malloc(sizeof(int));
   
    if(p == NULL)
    {
        printf("out of memory\n");
    }
    *p = 5;
    printf("%d\n",*p);
   
    free(p);            
}

We will check two scenarios. 
1.Comment out line free(p) in program.---free(p) -> //free(p)

   Type following command in your linux terminal. 

$ gcc -o memory_leak memory_leak.c
$ valgrind --tool=memcheck --leak-check=yes ./memory_leak




Here we are using memcheck tool of valgrind for finding heap summary and checking leaks if possible.You can find more about it in manual page of valgrind.

2.Remove comments from line free(p) in program.---//free(p)-> free(p)

   Type the same commands shown above in terminal.

$ gcc -o memory_leak memory_leak.c
$ valgrind --tool=memcheck --leak-check=yes ./memory_leak




You can see that in first scenario valgrind will show leak summary and will inform user that there will be lost of bytes as there is '1 allocs, 0 frees', while in second scenario there is no possibilities of any leaks because all the blocks allocated were freed.
You can also make logs of output by using 'log-file' option.


Monday, 2 December 2013

Creating A Sample Filesystem Image

    Following is the procedure to creating a sample filesystem ( ext2,ext3 etc) image for testing purpose. You can mount them also.Follow below steps in your terminals to make it possible.

$ dd if=/dev/zero of=/tmp/test bs=1k count=10000

   Above command will create a file called 'test' and fills it with zeros with block size of 1Kb. So 'dd' command will create empty filesystem.



$ mkfs -t ext3 -i 1024 -b 1024 -F /tmp/test
  
   Above command will build filesystem of type 'ext3' (specified by -t option). You can change it based upon your need.


    Now we have to create mount point to mount this filesystem. Use 'mkdir' to do that.

$ mkdir /tmp/testmnt

$ sudo mount -o loop /tmp/test /tmp/testmnt

   Above command will mount the file using the loopack interface.Loop option in mount command tells mount about loopback device and it treats file as a block device.You have to provide device type information( e.g loop) otherwise it will give message as shown in below snap. You can check your filesystem by executing 'df -h' command

 

$ df -h

   You can check your filesystem by executing 'df -h' command in available disk space usage list.
   
     


Sunday, 1 December 2013

Linux Networking Commands And Configurations

Networking Commands

Here are the basic networking command you should know to monitor your network configuration and status.

Type following command into your terminal.

$ ping <ip addr> 

   To check response of icmp echo request to host machine.

   ping -t <ip addr>       (Continuous ping to network)
   ping -s 32 <ip addr>  (for sending specific bytes of data)
   ping -c 5 <ip addr>    (for sending no. of packets to destination)



$ mtr <ip addr>

   To perform combine functionality of 'traceroute' and 'ping' tool.
By applying this command you can get additional information about state, connection and percentage of lost packets of all intermediate hosts.

   mtr <ip addr>
   mtr  --report -c 4 <ip addr> (will run 4 no. of cycles in report mode of mtr)





$ netstat

   To display connection and routing table information.Here are some commands.

   netstat -a  (Connection info)
   netstat -r  ( routing table)



$ ifconfig

   To display available interfaces on machine and add or remove network to it.

   ifconfig    (tells available interfaces on comp.)
   ifconfig eth0 add <ip addr> <mask>   (To add new network to eth0)




Configuration Files

To edit network configuration or to check connection infromation you can see these files.

/etc/network/interfaces---Network configuration file.

/etc/resolv.conf---DNS configuration.

/etc/hostname---Hostname configuration.

/etc/hosts---Local DNS.




   

Saturday, 30 November 2013

NTP server setup on windows7



Steps to setup NTP on windows7 :
  1. Run the downloaded installer.
  2. Follow the steps until all files have been installed.
  3. Uncheck box ‘create an initial configuration file in the Configuration File Settings’ and click Next.
  4. Select the radio button labeled Use SYSTEM account (not recommended), make sure that all boxes are checked, and click Next.
  5. Finish installation.
  6. Make sure your service is allowed through firewall. If not then allow ntpd.exe (C:\Program Files\NTP\bin) through firewall.
  7. Navigate to Start»All Programs»Meinberg»Network Time Protocol»Service Control and just run the Start NTP Service as an Administrator.
  8. Now enter your comuter’s IP address in any client machine provided that client machine have best possible matches of timezone

Tuesday, 28 May 2013

Linux Kernel Basics

Linux Kernel Architecture



User space, that is, everything outside the kernel, both libraries and application programs, uses these. Programs in user space contain system calls that ask the kernel to do something, and the kernel does so, or returns an error code.

Many things are handled by the C library(System Libraries) itself - those are the things the user could have programmed himself, but need not since the author of the library did this job already. Maybe the presence of the library also saves some memory: many utilities can share common library code. But for the basic things, starting programs, allocating memory, file I/O etc., the C library invokes the kernel.
 
System call interface, which implements the basic functions such as read and write. Below the system call interface is the kernel code, which can be more accurately defined as the architecture-independent kernel code. This code is common to all of the processor architectures supported by Linux. Below this is the architecture-dependent code, which forms what is more commonly called a BSP (Board Support Package). This code serves as the processor and platform-specific code for the given architecture.