Saturday, 7 May 2016

Linux Booting procedure

The stages involved in Linux Booting Process are:
1. BIOS
2. Boot Loader
    - MBR
    - GRUB
3. Kernel
4. Init
5. Runlevel scripts
6. User Interface
STEP 1. BIOS
  • This is the first thing which loads once you power on your machine.
  • When you press the power button of the machine, CPU looks out into ROM for further instruction.
  • The ROM contains JUMP function in the form of instruction which tells the CPU to bring up the BIOS
  • BIOS determine all the list of bootable devices available in the system.
  • Prompts to select bootable device which can be Hard Disk, CD/DVD-ROM, Floppy Drive, USB Flash Memory Stick Etc..
  • Operating System tries to boot from Hard Disk where the MBR contains primary boot loader.
STEP 2. Boot Loader 
To be very brief this phase includes loading of the boot loader (MBR and GRUB/LILO) into memory to bring up the kernel.

MBR (Master Boot Record)
  • It is the first sector of the Hard Disk with a size of 512 bytes.
  • The first 434 - 446 bytes are the primary boot loader, 64 bytes for partition table and 6 bytes for MBR validation timestamp.
NOTE: Now MBR directly cannot load the kernel as it is unaware of the file system concept and requires a boot loader with file system driver for each supported file systems, so that they can be understood and accessed by the boot loader itself.

To overcome this situation GRUB is used with the details of the file system in
/boot/grub.conf and file system drivers

GRUB (Grand Unified Boot loader)

this loads the kernel in 3 stages

GRUB stage 1: 
  • The primary boot loader takes up less than 512 bytes of disk space in the MBR - too small a space to contain the instructions necessary to load a complex operating system. 
  • Instead the primary boot loader performs the function of loading either the stage 1.5 or stage 2 boot loader.
GRUB Stage 1.5: 
  • Stage 1 can load the stage 2 directly, but it is normally set up to load the stage 1.5. 
  • This can happen when the /boot partition is situated beyond the 1024 cylinder head of the hard drive. 
  • GRUB Stage 1.5 is located in the first 30 KB of Hard Disk immediately after MBR and before the first partition.
  • This space is utilized to store file system drivers and modules.
  • This enabled stage 1.5 to load stage 2 to load from any known location on the file system i.e. /boot/grub

GRUB Stage 2:
  • This is responsible for loading kernel from /boot/grub/grub.conf and any other modules needed
  • Loads a GUI interface i.e. splash image located at /grub/splash.xpm.gz with list of available kernels where you can manually select the kernel or else after the default timeout value the selected kernel will boot
The original file is /etc/grub.conf of which you can observe a symlink file at /boot/grub/grub.conf
STEP 3. Kernel
This can be considered the heart of operating system responsible for handling all system processes.

Kernel is loaded in the following stages:
  1. Kernel as soon as it is loaded configures hardware and memory allocated to the system.
  2. Next it uncompresses the initrd image (compressed using zlib into  zImage or bzImage formats) and mounts it and loads all the necessary drivers.
  3. Loading and unloading of kernel modules is done with the help of programs like insmod, and rmmod present in the initrd image.
  4. Looks out for hard disk types be it a LVM or RAID.
  5. Unmounts initrd image and frees up all the memory occupied by the disk image.
  6. Then kernel mounts the root partition as specified in grub.conf as read-only.
  7. Next it runs the init process
STEP 4. Init Process
  • Executes the system to boot into the run level as specified in /etc/inittab

You can check current runlevel details of your system using below command on the terminal

# who -r

         
run-level 3  Jan 28 23:29                   last=S
  • Next as per the fstab entry file system's integrity is checked and root partition is re-mounted as read-write (earlier it was mounted as read-only).
STEP 5. Runlevel scripts
A no. of runlevel scripts are defined inside /etc/rc.d/rcx.d
  • Based on the selected runlevel, the init process then executes startup scripts located in subdirectories of the /etc/rc.d directory.
  • Scripts used for runlevels 0 to 6 are located in subdirectories /etc/rc.d/rc0.d through /etc/rc.d/rc6.d, respectively.
  • For more details on scripts inside /etc/rc.d follow the below link
    What are the s and k scripts in the etc rcx.d directories
  • Lastly, init runs whatever it finds in /etc/rc.d/rc.local (regardless of run level). rc.local is rather special in that it is executed every time that you change run levels.
NOTE: rc.local is not used in all the distros as for example Debian.
Next if everything goes fine you should be able to see the Login Screen on your system.

Linux Directory Structure

Overview
Everything in Linux can be reduced to a file. Partitions are associated with files such as /dev/hda1. Hardware components are associated with files such as /dev/modem. The Filesystem Hierarchy Standard (FHS) is the official way to organize files in Unix and Linux directories.
Linux file system and directory structure
Several major directories are associated with all modern Unix/Linux operating systems. These directories organize user files, drivers, kernels, logs, programs, utilities, and more into different categories. The standardization of the FHS makes it easier for users of other Unix-based operating systems to understand the basics of Linux. All of the other directories shown in Table are subdirectories of the root directory, unless they are mounted separately.
Directory
Description
/
The root directory, the top-level directory in the FHS. All other directories are subdirectories of root, which is always mounted on some partition. All directories that are not mounted on a separate partition are included in the root directory’s partition.
/bin
Essential command line utilities. Should not be mounted separately; otherwise, it could be difficult to get to these utilities when using a rescue disk.
/boot
Includes Linux startup files, including the Linux kernel. Can be small; 16MB is usually adequate for a typical modular kernel. If you use multiple kernels, such as for testing a kernel upgrade, increase the size of this partition accordingly.
/etc
Most basic configuration files.
/dev
Hardware and software device drivers for everything from floppy drives to terminals. Do not mount this directory on a separate partition.
/home
Home directories for almost every user.
/lib
Program libraries for the kernel and various command line utilities. Do not mount this directory on a separate partition.
/mnt
The mount point for removable media, including floppy drives, CD-ROMs, and Zip disks.
/opt
Applications such as WordPerfect or Star Office.
/proc
Currently running kernel-related processes, including device assignments such as IRQ ports, I/O addresses, and DMA channels.
/root
The home directory of the root user.
/sbin
System administration commands. Don't mount this directory separately.
/tmp
Temporary files. By default, Red Hat Linux deletes all files in this directory periodically.
/usr
Small programs accessible to all users. Includes many system administration commands and utilities.
/var
Variable data, including log files and printer spools.

Bash Shell features

BASH Features


BASH: Bourne again shell

 when you login to the Server using any CLI based tool OR login to the CLI mode you can see as like below screen. 

$ sign = local user/non-privilege user
# sign = Administrator/Privileged user
 

 [root@sankar ~]#


Shells can be changed based the user compatibility and requirements, most of default shell is 'BASH' 


[root@sankar ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/dash
You have new mail in /var/spool/mail/root
[root@sankar ~]#



Command Line Completion:
Single Tab – it will provide the best option
Double Tab – it will provide all the possible options (Autocompletes)
Command Line Editing:
Ctrl+a            -           It moves cursor to the Home line position
Ctrl+b            -           Moves the cursor back one character
Ctrl+c            -           Sends the signal SIGINT to the current task, which aborts and close it.
Ctrl+d            -           Close current shell prompt
Ctrl+e            -           It will move a cursor to end of the bash
Ctrl+f             -           Moves cursor forward one character
Ctrl+g            -           Abort the research and restore the original file
Ctrl+h            -           Deletes the previous character (Same as backspace)
Ctrl+k            -           It is used to delete the command from the courser to line home position
Ctrl+l             -           Clear the screen
Ctrl+u            -           Clears the line content before the cursor and copies it into the clipboard
Ctrl+y            -           Yank the content from the cursor position
Ctrl+z            -           Sends the signal SIGTSTP to the current task, which suspend it
Ctrl+Shift+c -           Copy selected text
Ctrl+Shift+v            -           Paste the copied content
Ctrl+Shift+t             -           Tab
Ctrl+Shift+n            -           New Terminal
Ctrl+Shift+w            -           Close Tab
Ctrl+Shfift+q           -           Close a Terminal
Alt+b              -           Moves the cursor backward one word
Alt+c              -           Capitalizes the character under the cursor and moves to the end of word
Alt+d              -           Cut the word after the cursor
Alt+f               -           Moves the cursor forward one word
Alt+l               -           Lowers the case of every character from the cursor’s position to the end
Alt+.               -           Insert the last argument to the previous command
Command Line History: Command line history will be saved, when you execute any command in terminal.
 
$ history        -           this command will display all the previous executed commands
$ history –c   -           Clear the command history
$ !<number> -          it executes mentioned number command
Note: Default history size is 1000 commands

[root@sankar ~]# history





$ !<charectar>         -           it will display/execute matching character command
$ !!                              -           it will execute last executed command
Sophisticated prompt control:
?          -           It will replace a single character
Example:       rm –rf a?        -           it will delete the files with two characters
                        Rm –rf a??     -           it will delete the three characters files after “a”
*          -           replace multiple number of characters

Example:       rm –f  a*        - it will remove all the files which are starting with “a”

Piping and Redirecting:
Redirecting input and output from standard stream to user defined place
1.    Input RD       -0                                 <         
2.    Output RD -1              Values        >          Symbols
3.    Error RD       -2                                 >

Example:       $ sort < <file name>
                        $ wc < <file name>
                        $ history > f2
                        $mkdir d1 2> e1




Piping sending output or one command as a input to the another command

| pipe
pipe Symbol





Page related command:
            Less: is used to see the command output page by page in up and down way
            More: we can’t go upward downward, just see the output fit to the screen
 

Windows Vs Linux

Windows Vs Linux

                                Windows
                                      Linux
    • It is a proprietary software everything need to buy
    • It is an open source software everything is free
    • Less Secure
    • More Secure
    • More costly
    • Less Cost compare to windows
    • Chance to get a carpal tunnel syndrome
    • There is no chance to get carpal tunnel syndrome
    • Developed by Microsoft corporation
    • Developed by Linus Torvalds
    • Options need to select
    • You can develop anything as per your requirement
    • Kernel is not editable
    • Kernel is editable

Linux Architecture

Linux Architecture




Kernel: It is a program. Kernel is a Core component of operating system, interacts directly with hardware and provides low level services to upper layer components.
Shell: An interface to kernel, hiding complexity of kernel’s functions from users. Takes commands from user and executes kernel’s functions.
User Interface: In information technology, the user interface (UI) is everything designed into an information device with which a human being may interact. Two types CLI (Command line interface) and GUI (Graphical User Interface).
System Library - System libraries are special functions or programs using which application programs or system utilities accesses Kernel's features. These libraries implements most of the functionality of the operating system and do not requires kernel module's code access rights.
System Utility - System Utility programs are responsible to do specialized, individual level tasks.

Kernel Mode vs User Mode


        Kernel component code executes in a special privileged mode called kernel mode with full access to all resources of the computer. This code represents a single process, executes in single address space and do not require any context switch and hence is very efficient and fast. Kernel runs each processes and provides system services to processes, provides protected access to hardwares to processes.

        Support code which is not required to run in kernel mode is in System Library. User programs and other system programs works in User Mode which has no access to system hardwares and kernel code. User programs/ utilities use System libraries to access Kernel functions to get system's low level tasks.
Basic Features

Following are some of the important features of Linux Operating System.

    Portable - Portability means softwares can works on different types of hardwares in same way.Linux kernel and application programs supports their installation on any kind of hardware platform.

    Open Source - Linux source code is freely available and it is community based development project. Multiple teams works in collaboration to enhance the capability of Linux operating system and it is continuously evolving.

    Multi-User - Linux is a multiuser system means multiple users can access system resources like memory/ ram/ application programs at same time.

    Multiprogramming - Linux is a multiprogramming system means multiple applications can run at same time.

    Hierarchical File System - Linux provides a standard file structure in which system files/ user files are arranged.

    Shell - Linux provides a special interpreter program which can be used to execute commands of the operating system. It can be used to do various types of operations, call application programs etc.

    Security - Linux provides user security using authentication features like password protection/ controlled access to specific files/ encryption of data.


 

Linux System Architecture is consists of following layers

    Hardware layer - Hardware consists of all peripheral devices (RAM/ HDD/ CPU etc).

    Kernel - Core component of Operating System, interacts directly with hardware, provides low level services to upper layer components.

    Shell - An interface to kernel, hiding complexity of kernel's functions from users. Takes commands from user and executes kernel's functions.

    Utilities - Utility programs giving user most of the functionalities of an operating systems.

Linux Introduction

Linux Introduction


     UNIX is the first Operating system in the world, developed by Kem Thompson and Dennis Ritchie in 1969 at Bell Lab by AT&T Company
            IBM    :           AIX
            SGI     :           IRIX
            Sun     :           Solaris
            Free software foundation organization, they start a project by name GNU. The main aim of this project is to develop such an operating system that can run on any platform.
    In 1991, a student Linuz Torvalds developed a kernel named Linux’s kernel plus GNU application called Linux operating system.
Linux is an open source technology.
Different companies that provide Linux in Market are Redhat, SuSe, Scientific, Centos, and Knoppix etc.

Features:


Ø  Linux is the fastest Operating system in the world. It runs 2 to 3 times fast than windows OS.
Ø  Linux is the much secured OS because there is no any problem of virus.
Ø  Linux file format is text format and windows file format is binary format.
Ø  Linux is very reliable OS because kernel of Linux is very stable as compare to windows kernel not crashed easily.
Ø  Kernel of Linux is very small in size it can be stored in floppy.
Ø  Linux uses the x-Window system which is advanced network windowing system. Using this system we can display output of any workstation monitor attached in the network.

Advantages:


Ø  Virus Proof
Ø  Crash Proof
Ø  Economical
Ø  Multiuser, Multi-Tasking and Multi processing capacity

Login Modes:

Two modes:  

1.Text mode (CLI)   - All the commands can be executed   

2.Graphical Mode (GUI)
Login to Text mode we have use (Ctrl+Alt+F1…..F6, F8….F12) (Ctrl+Alt+F7) for Graphical Mode


Open Source

       The idea behind Open Source software is rather simple: when programmers can read, distribute and change code, the code will mature. People can adapt it, fix it, debug it, and they can do it at a speed that dwarfs the performance of software developers at conventional companies. This software will be more flexible and of a better quality than software that has been developed using the conventional channels, because more people have tested it in more different conditions than the closed software developer ever can.

        The Open Source initiative started to make this clear to the commercial world, and very slowly, commercial vendors are starting to see the point. While lots of academics and technical people have already been convinced for 20 years now that this is the way to go, commercial vendors needed applications like the Internet to make them realize they can profit from Open Source. Now Linux has grown past the stage where it was almost exclusively an academic system, useful only to a handful of people with a technical background. Now Linux provides more than the operating system: there is an entire infrastructure supporting the chain of effort of creating an operating system, of making and testing programs for it, of bringing everything to the users, of supplying maintenance, updates and support and customizations, etcetera. Today, Linux is ready to accept the challenge of a fast-changing world.

Monday, 25 April 2016

Script to gather server information



Create a file called systeminfo.sh
# touch systeminfo.sh
 


#!/bin/bash
## Purpose: Collecting system Information Gather server information within a second
## Author Name: Sankar
## Date: 26th Apr 2016
MAILIST=sankarrhcss@gmail.com
DOMAIN=domain
TEMP=/tmp/temp
FILE=/tmp/asset/info.txt
USERID=`id -g`
if [ $USERID == 0 ]; then
echo "Script is Running ..."
else
echo "Please run the script using root user"
exit
fi
mkdir /tmp/asset
touch /tmp/temp
touch /tmp/asset/info.txt
echo -e "\n" > $FILE
echo "Collecting System Information" >> $FILE
echo -e "\n" >> $FILE
echo "collecting information...."
echo "Date: `date`" >> $FILE
echo "HostName: `hostname`" >> $FILE
echo "Installed OS Version: `cat /etc/issue |head -1`" >> $FILE
echo "`/sbin/ifconfig -a |grep "inet addr" | awk 'BEGIN { FS = ":" } ; { print $2 }'`" > $TEMP
echo "IP Address : `egrep '^10' $TEMP |awk '{ print $1}'`" >> $FILE
## Identifying Hardware Platform ##
HF=`uname -i`
if [ $HF == i386 ]; then
echo "Hardware Platform: 32Bit" >> $FILE
else if [ $HF == x86_64 ]; then
echo "Hardware Platform: 64Bit" >> $FILE
else
echo "Hardware Not Matched" >> $FILE
fi
fi
## Colleting Hardware Details ##
echo " " >> $FILE
echo "## Hardware Information" >> $FILE
echo " " >> $FILE
echo "Serial Number     : `lshal |grep system.hardware.serial`" >> $FILE
echo "Serial Number     : `/usr/sbin/dmidecode -s system-serial-number`" >> $FILE
echo "Model Number      : `lshal |grep system.hardware.product`" >> $FILE
echo "Model Number      : `/usr/sbin/dmidecode |grep "SKU Number"`" >> $FILE
echo "Hardware Vendor   : `lshal |grep system.hardware.vendor`" >> $FILE
echo "Hardware Info     : `dmesg |grep DMI`" >> $FILE
## Redhat Version ##
echo " " >> $FILE
echo "## OS Version" >> $FILE
head -n1 /etc/issue >> $FILE
echo -en '\n' >> $FILE
uname -a >> $FILE
## CPU Info ##
echo " " >> $FILE
echo " " >> $FILE
echo "## CPU Information" >> $FILE
grep "model name" /proc/cpuinfo >> $FILE
## RAM/MEMORY Info ##
echo " " >> $FILE
echo " " >> $FILE
echo "## Memory Information" >> $FILE
grep MemTotal /proc/meminfo >> $FILE
y=`grep MemTotal /proc/meminfo |awk '{ print $2 }'`
mb="$(( $y / 1024 ))"
gb="$(( $mb / 1024 ))"
echo "RAM : $gb GB" >> $FILE
## Verify the machine is using NIS ##
echo -e '\n' >> $FILE
cat /etc/yp.conf |grep $DOMAIN > /tmp/yptemp.txt
count=`cat /tmp/yptemp.txt | wc -l`
if [ $count -gt 0 ];
then
echo "`hostname` is part of NIS Domain" >> $FILE
cat /etc/yp.conf |grep finnis >> $FILE
else
echo "`hostname` is not part of NIS" >> $FILE
fi
echo -e '\n' >> $FILE
cat /etc/yp.conf |grep domain >> $FILE
echo -e '\n' >> $FILE
echo "Hard Disk Info" >> $FILE
fdisk -l 2>&1 | grep Disk | grep -v "identifier" | grep -v "valid partition" | awk '{print $2,$3,$4}' >> $FILE
echo -e '\n' >> $FILE
echo "Running Services" >> $FILE
service --status-all |grep running. |awk '{ print $1,$5}' >> $FILE
/bin/mail -s "Linux Asset Inventory `hostname`" $MAILIST < $FILE