Showing posts with label Tips and tricks. Show all posts
Showing posts with label Tips and tricks. Show all posts

Friday, November 8, 2013

GRUB (Grand Unified Bootloader) tutorial

MBR and GPT partition table

Booting Process: A step by step tutorial for understanding Linux boot sequence

One of the most remarkable achievement in the history of mankind is computers. Another amazing fact about this remarkable achievement called computers is that its a collection of different electronic components, and they work together in coordination to give you a meaningful output. We in our day to day lives use this complex system in one or the other way. But not many of us know how these things work together.

Press the power button on your system, and after few moments you see the login prompt or desktop. Have you ever wondered what happens behind the scenes from the time you press the power button until the desktop appears?
In this tutorial, we will be discussing how a computer boots. I must say how a computer with Linux operating system boots. I hope this will be helpful to other readers as well.

Understanding the boot process, will help you understand how the hardware and software is working together, and also will give you the required information to begin troubleshooting a booting problem you have.

Linux booting process can be divided to multiple stages. The below shown diagram explains the different stages. We will be discussing each of these stages in detail, in their respective sections.

Step 1: Power Supply & SMPS


One of the main component of a computer is SMPS(Switching Mode Power Supply). The primary objective of this component is to provide the perfect required voltage level to the motherboard and other computer components. Computer internals work in DC, however the power supply we have at home and other places are AC. SMPS converts AC to DC and maintain the required voltage level so that the computer can work flawlessly.

But the main task performed by SMPS, is to inform about the good power supply. As I told if the voltage is more/ or is less, in both the conditions a computer cannot work. As soon as you supply power to the computer, the SMPS checks the voltage level's its providing to the motherboard. If the power signal level is perfect, then SMPS will send a POWER GOOD signal to the motherboard timer.

On receiving this POWER GOOD signal from SMPS, the motherboard timer will stop sending reset signal to the CPU. Which means the power level is good and the computer can boot.

 

Step 2: Bootstrapping


Something has to be programmed by default, so that the CPU knows where to search for instructions.
This is an address location in the ROM. This address location is almost always constant in X86 based computers. The address location is FFFF:0000h.

This address location is the last region of the ROM. It only contains one instruction. The instruction is to jump to another memory address location. This JUMP command, will tell the location of the BIOS program in the ROM.
This is how the computer will come to know where the BIOS program is located.

Step 3: The Role of BIOS in booting process


The word booting comes from another word called bootstrapping. The computer knows how to bring itself up, when you press the start button, because of the instructions that are fed to a program called as BIOS. BIOS stands for Basic Input Output System. The most important use of BIOS during the booting process is POST. POST stands for Power on Self Test. Its a series of tests conducted by the bios, which confirms the proper functioning of different hardware components attached to the computer.

POST is very important thing to have before the Operating system is loaded. Just imagine if you have a faulty hard drive or faulty memory, sometimes these things can cause data loss. POST checks and confirms the integrity of the following hardware components.
  • Timer IC's
  • DMA controllers
  • CPU
  • Video ROM

A full POST check will confirm the integrity of the following devices as well.
  • Motherboard
  • Keyboard
  • Printer port
  • Hard Drive etc

If you are doing a warm start (which means you did a reset of a running machine, most of the times reset button is the small one near the power button on the CPU), a full POST check will not be conducted by the BIOS. However if you are doing a Cold Start, which means you have applied the power now, it will conduct a full POST.

BIOS determines whether its a cold or warm start, by looking at a flag in a predefined memory location. Once the POST completes, the BIOS will inform you about any problems it found with the help of beep codes (through system speaker). Different number of beep codes have different meaning.

There are two things that people often get confused with. Its CMOS & BIOS. CMOS & BIOS are two completely different things in the computer motherboard. CMOS is a small memory RAM chip that's present in the motherboard. This RAM is different from the computers main RAM chip (which are replaceable memory chips.

These days RAM chips are available in the range of Gigabytes.). Unlike the main RAM chip, CMOS RAM does not flush its memory when a computer is turned off. It remembers all the configuration with the help of a battery called CMOS battery.

Removing a CMOS battery will make the CMOS to forget all the configuration you have saved previously.
  • This is the reason you can unlock a computer that's protected with a CMOS password, by simply removing the CMOS battery.
  • Also removing the CMOS battery will make the operating system to show you wrong time. Because system time consistency is maintained in CMOS settings.

So its always advisable to replace your CMOS battery at regular intervals, for proper functioning of the computer.

Normally people say that we have modified BIOS settings. But its completely wrong. What they modified is in fact CMOS settings. CMOS settings is the place where you modify the boot order etc.

Bios settings cannot be altered by the user. It requires a flash program provided by the manufacturer.

So Let's get back to our booting process. Once the POST check is completed successfully, BIOS will look CMOS settings to know what is the boot order. Boot order is nothing but a user defined order which tells where to look for the operating system.
The order will be something like the below.

  1. CD ROM
  2. HARD DISK
  3. USB
  4. Floppy DISK

The above shown order means that the BIOS will look at CD ROM first to check whether an OS can be loaded from there, if it does not find a bootable disk in the CD ROM, it will look check whether a bootable OS is there in the hard disk, then USB and then Floppy disk.

Let's assume that you don’t have a bootable CD in your CD ROM drive, then the BIOS will turn to HARD disk.

    BIOS :- Quick Guide

    1. BIOS stands for Basic Input/Output System
    2. Performs some system integrity checks
    3. Searches, loads, and executes the boot loader program.
    4. It looks for boot loader in floppy, cd-rom, or hard drive. You can press a key (typically F12 of F2, but it depends on your system) during the BIOS startup to change the boot sequence.
    5. Once the boot loader program is detected and loaded into the memory, BIOS gives the control to it.
    6. So, in simple terms BIOS loads and executes the MBR boot loader.

    Linux Boot Process 

     The stages of a typical Linux boot process.

    Step 4: MBR 

     

    Now as you don't have any bootable CD in your CD ROM Drive, the bios will look at the second device from the boot order settings. The second device is your Hard Disk.
    BIOS is programmed to look at a permanent location on the hard disk to complete its task. This location is called a Boot sector. This is nothing but the first sector of your hard disk. This area is sometimes called as MBR (Master Boot Record). This is the location that contains the program that will help our computer to load the operating system. As soon as bios finds a valid MBR, it will load the entire content of MBR to RAM, and then further execution is done by the content of MBR.
    This first sector of the hard disk is only of 512 bytes. Yeah its too small an area for an entire boot loader program to fit in. Hence most of the operating system store only the first stage of their boot loader program in here. Only the first 440 bytes from the total of 512 bytes is used by the first stage boot loader, the remaining part is used to store partition table information.

    Root user can take an entire dump/backup of your MBR in linux with the help of the following command:
     

    Now you get like this output:



    MBR :- Quick Guide

    1. MBR stands for Master Boot Record.
    2. It is located in the 1st sector of the bootable disk. Typically /dev/hda, or /dev/sda
    3. MBR is less than 512 bytes in size. This has three components.
    4. It contains information about GRUB (or LILO in old systems).
    5. So, in simple terms MBR loads and executes the GRUB boot loader.


    Step 5: GRUB

     

    As the title of this article says "Linux Booting process", we will be having grub stage one in the first 440 bytes of the MBR. I would suggest to read the artcle to understand grub and its details.

    MBR :- Quick Guide

    1. GRUB stands for Grand Unified Bootloader.
    2. If you have multiple kernel images installed on your system, you can choose which one to be executed.
    3. GRUB displays a splash screen, waits for few seconds, if you don’t enter anything, it loads the default kernel image as specified in the grub configuration file.
    4. GRUB has the knowledge of the filesystem (the older Linux loader LILO didn’t understand filesystem).
    5. Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to this). The following is sample grub.conf of CentOS.
    6. As you notice from the above info, it contains kernel and initrd image.
    7. So, in simple terms GRUB just loads and executes Kernel and initrd images.

    1. Kernel

      1. Mounts the root file system as specified in the “root=” in grub.conf
      2. Kernel executes the /sbin/init program
      3. Since init was the 1st program to be executed by Linux Kernel, it has the process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.
      4. initrd stands for Initial RAM Disk.
      5. initrd is used by kernel as temporary root file system until kernel is booted and the real root file system is mounted. It also contains necessary drivers compiled inside, which helps it to access the hard drive partitions, and other hardware.
    2. Init

      1. Looks at the /etc/inittab file to decide the Linux run level.
      2. Following are the available run levels
        • 0 – halt
        • 1 – Single user mode
        • 2 – Multiuser, without NFS
        • 3 – Full multiuser mode
        • 4 – unused
        • 5 – X11
        • 6 – reboot
      3. Init identifies the default initlevel from /etc/inittab and uses that to load all appropriate program.
      4. Execute ‘grep initdefault /etc/inittab’ on your system to identify the default run level
      5. If you want to get into trouble, you can set the default run level to 0 or 6. Since you know what 0 and 6 means, probably you might not do that.
      6. Typically you would set the default run level to either 3 or 5
    3. Runlevel Programs

      1. When the Linux system is booting up, you might see various services getting started. For example, it might say “starting sendmail …. OK”. Those are the runlevel programs, executed from the run level directory as defined by your run level.
      2. Depending on your default init level setting, the system will execute the programs from one of the following directories.
        • Run level 0 – /etc/rc.d/rc0.d/
        • Run level 1 – /etc/rc.d/rc1.d/
        • Run level 2 – /etc/rc.d/rc2.d/
        • Run level 3 – /etc/rc.d/rc3.d/
        • Run level 4 – /etc/rc.d/rc4.d/
        • Run level 5 – /etc/rc.d/rc5.d/
        • Run level 6 – /etc/rc.d/rc6.d/
      3. Please note that there are also symbolic links available for these directory under /etc directly. So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.
      4. Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.
      5. Programs starts with S are used during startup. S for startup.
      6. Programs starts with K are used during shutdown. K for kill.
      7. There are numbers right next to S and K in the program names. Those are the sequence number in which the programs should be started or killed.
      8. For example, S12syslog is to start the syslog deamon, which has the sequence number of 12. S80sendmail is to start the sendmail daemon, which has the sequence number of 80. So, syslog program will be started before sendmail.

    There you have it. That is what happens during the Linux boot process.

Sunday, October 27, 2013

Installing and configuring SSH in Linux

SSH is a secure shell to allow access to remote machines. In this article, i’ll describe about ssh installation, configuration and more feature of ssh. 

Install Secure Shell:

root@123linuxschool:/# yum -y install openssh-server openssh-clients

Start and enable sshd server:
root@123linuxschool:/# chkconfig sshd on
root@123linuxschool:/# service sshd start

Configure OpenSSH Server

Edit /etc/ssh/sshd_config, enter:
root@123linuxschool:/# vi /etc/ssh/sshd_config

To disable login as root, edit or add the following:
PermitRootLogin no
Restrict login to user user1 and user2 only via ssh:
AllowUsers user1 user2
Change ssh port i.e. run it on non-standard port like 2221
port 2221
Save and close the file. Restart sshd:
root@123linuxschool:/# service sshd restart

Identify SSH Client Version:


Sometimes it may be necessary to identify the SSH client that currently use and it’s corresponding version number, which can be determined as shown below.
$ ssh -V
OpenSSH_4.9p1, OpenSSL 1.0.1 Mar 14 2012
To login to a remote machine called test.example.com, type the following command at a shell prompt:
ssh test.example.com
The first time you ssh into a remote machine, you will see a message similar to the following:
The authenticity of host ‘test.example.com’ can’t be established.
DSA key fingerprint is 94:68:3a:3a:bc:f3:9a:9b:01:5d:b3:07:38:e2:11:0c.
Are you sure you want to continue connecting (yes/no)?
Type yes and enter to continue. This will add the server to your list of known hosts (~/.ssh/known_hosts) as shown in the following message:
Warning: Permanently added ‘test.example.com’ (RSA) to the list of known hosts.
After that you will see a message asking the password for the remote machine. After entering your password, you will be at the shell prompt for the remote machine. If you do not specify a username that is connected to the local client machine is passed to the remote machine. If you want to specify a different username, use the following command:
ssh username@test.example.comYou can also use the syntax ssh -l username test.example.com.


The ssh command can be used to execute a command on the remote machine without connected to the shell prompt. The syntax is ssh hostnamecommand. For example, if you want to run the command ls /var/log/messages in test.example.com the remote machine, type the following command at the prompt:
ssh test.example.com ls /var/log/messages
After entering the correct password, the contents of the remote directory /var/log/messages will appear, and you will return to your local shell prompt.

Transfer files To/From remote host:

Another common use of the ssh client is to copy files from/to remote host using scp command.
Copy a file from remotehost to localhost:
localhost$scp albin@remotehost.example.com:/home/albin/remotehostfile.txt remotehostfile.txt
Copy a file from localhost to remotehost:
localhost$scp localhostfile.txt albin@remotehost.example.com:/home/albin/localhostfile.txt

Switching SSH session:


Toggle or switching SSH Session, When you connect using SSH remotehost from the localhost, you may want to return to the localhost to perform any activity and go back to remote host again. In this case, no need to disconnect the ssh session to the remote host. See, below steps are as follows.
Login to remotehost from localhost: localhost$ssh -l albin remotehost
Now you are connected to remotehost: remotehost$
To return to the localhost temporarily, type the escape character ~ and Control-Z. When typing ~ you will not immediately see the screen until you press <Control-Z> and press enter. So, on the remotehost in a new line enter the following key strokes for the following work: ~<Control-Z>
remotehost$ ~^Z
[1]+  Stopped                 ssh -l albin remotehost
localhost$
Now you’re back to the localhost and the ssh remotehost client session runs as a typical unix background job, as following mention you can check:
localhost$ jobs
[1]+  Stopped                 ssh -l albin remotehost
You can go back to the remote host ssh without required password again by bringing the background ssh remotehost session job to foreground on the localhost. See the output as shown below:
localhost$ fg %1
ssh -l albin remotehost
remotehost$

How to Change SSH Port Number in Linux

It is highly recommended to change ssh port number and it is a good security practice for an administrator. By default ssh port number is 22 and you can change with any non usable port numbers. 

[root@
123schools.blogspot.in~]# cat /etc/services

In the above command, services file display all the service names with associated default port numbers. If you are not using that port then you can select any port.

Open the sshd configuration using a vim editor.
vim /etc/ssh/sshd_config

Change from Port 22 to Port 2220 or any other (ex: 2222, 2221, 2028 etc...)
After implementing your required changes, save and quit from the configuration file and restart the ssh service. Service restart as shown below:
[root@123schools.blogspot.in ~]# /etc/init.d/sshd restart

Now you will connect the server using custom ssh port number.
[root@123linuxschool.blogspot.in~]# ssh albin@123schools.blogspot.in -p 2220

Friday, October 12, 2012

VIDEO DOWNLOAD MANAGER

This tutorial can help you how to capture any video links from any site.

You need only 4 things:-
  1. You want one 'Computer Operating System' which you can install Mozilla firefox. example (Windows, Linux, Mac, etc).
  2. Download and install 'Mozilla firefox' in your Operating system.
  3. Add 'Video DownloadHelper' ADD-ONS in Mozilla firefox.
  4. Add 'DownThemAll' ADD-ONS in Mozilla firefox.

steps:-

  • Download 'Mozilla firefox' from www.mozilla.org.
  • Install Mozilla firefox on your computer.
  • add Mozilla firefox add-ons from the following links Video DownloadHelper and DownThemAll .
     
  • Go to any video sites like www.youtube.com.
  • Start video and you will see VideoDownloadHelper dancing in your Navigation Toolbar.

  • Click the down arrow right side of the VideoDownloadHelper.
 
  •  Select your video and DownThemAll. 
  • Check 'save file location' before start downloading.

  • You will see a new window, you just have to press start button and it will start download with a very good speed.


May this video help you 

Tuesday, September 25, 2012

Linux Boot Process

Press the power button on your system, and after few moments you see the Linux login prompt.

Have you ever wondered what happens behind the scenes from the time you press the power button until the Linux login prompt appears?


The following are the stages of a typical Linux boot process.

  1. BIOS

    1. BIOS stands for Basic Input/Output System
    2. Performs some system integrity checks
    3. Searches, loads, and executes the boot loader program.
    4. It looks for boot loader in floppy, cd-rom, or hard drive. You can press a key (typically F12 of F2, but it depends on your system) during the BIOS startup to change the boot sequence.
    5. Once the boot loader program is detected and loaded into the memory, BIOS gives the control to it.
    6. So, in simple terms BIOS loads and executes the MBR boot loader.
  2.  MBR

    1. MBR stands for Master Boot Record.
    2. It is located in the 1st sector of the bootable disk. Typically /dev/hda, or /dev/sda
    3. MBR is less than 512 bytes in size. This has three components 1) primary boot loader info in 1st 446 bytes 2) partition table info in next 64 bytes 3) mbr validation check in last 2 bytes.
    4. It contains information about GRUB (or LILO in old systems).
    5. So, in simple terms MBR loads and executes the GRUB boot loader.
     
  3. GRUB

    1. GRUB stands for Grand Unified Bootloader.
    2. If you have multiple kernel images installed on your system, you can choose which one to be executed.
    3. GRUB displays a splash screen, waits for few seconds, if you don’t enter anything, it loads the default kernel image as specified in the grub configuration file.
    4. GRUB has the knowledge of the filesystem (the older Linux loader LILO didn’t understand filesystem).
    5. Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to this). The following is sample grub.conf of CentOS.
    6. As you notice from the above info, it contains kernel and initrd image.
    7. So, in simple terms GRUB just loads and executes Kernel and initrd images.
  4. Kernel

    1. Mounts the root file system as specified in the “root=” in grub.conf
    2. Kernel executes the /sbin/init program
    3. Since init was the 1st program to be executed by Linux Kernel, it has the process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.
    4. initrd stands for Initial RAM Disk.
    5. initrd is used by kernel as temporary root file system until kernel is booted and the real root file system is mounted. It also contains necessary drivers compiled inside, which helps it to access the hard drive partitions, and other hardware.
  5. Init

    1. Looks at the /etc/inittab file to decide the Linux run level.
    2. Following are the available run levels
      • 0 – halt
      • 1 – Single user mode
      • 2 – Multiuser, without NFS
      • 3 – Full multiuser mode
      • 4 – unused
      • 5 – X11
      • 6 – reboot
    3. Init identifies the default initlevel from /etc/inittab and uses that to load all appropriate program.
    4. Execute ‘grep initdefault /etc/inittab’ on your system to identify the default run level
    5. If you want to get into trouble, you can set the default run level to 0 or 6. Since you know what 0 and 6 means, probably you might not do that.
    6. Typically you would set the default run level to either 3 or 5
  6. Runlevel Programs

    1. When the Linux system is booting up, you might see various services getting started. For example, it might say “starting sendmail …. OK”. Those are the runlevel programs, executed from the run level directory as defined by your run level.
    2. Depending on your default init level setting, the system will execute the programs from one of the following directories.
      • Run level 0 – /etc/rc.d/rc0.d/
      • Run level 1 – /etc/rc.d/rc1.d/
      • Run level 2 – /etc/rc.d/rc2.d/
      • Run level 3 – /etc/rc.d/rc3.d/
      • Run level 4 – /etc/rc.d/rc4.d/
      • Run level 5 – /etc/rc.d/rc5.d/
      • Run level 6 – /etc/rc.d/rc6.d/
    3. Please note that there are also symbolic links available for these directory under /etc directly. So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.
    4. Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.
    5. Programs starts with S are used during startup. S for startup.
    6. Programs starts with K are used during shutdown. K for kill.
    7. There are numbers right next to S and K in the program names. Those are the sequence number in which the programs should be started or killed.
    8. For example, S12syslog is to start the syslog deamon, which has the sequence number of 12. S80sendmail is to start the sendmail daemon, which has the sequence number of 80. So, syslog program will be started before sendmail.

There you have it. That is what happens during the Linux boot process.