Ads 468x60px

วันศุกร์ที่ 21 ตุลาคม พ.ศ. 2554

GRUB bootloader - Full tutorial

http://www.dedoimedo.com/computers/grub.html
GRUB bootloader - Full tutorial


This tutorial focuses on GRUB legacy. If you're looking for a tutorial on GRUB 2, the next generation of the popular bootloader, please take a look at this article.
One of the most frightening things about Linux is the horrible word bootloader. The primary reason for this is the fact that most new Linux users have only ever used Windows operating systems. In the Windows world, they have never bothered with bootloaders. For them, the issue of a system booting was a transparent one. At most, they would use Windows Recovery Console to fix problems for them. Thus, they have been spared the need to learn about the single most important piece of software on a computer - the little program that makes it all work.
This article is supposed to provide you with basic understanding of the GRUB bootloader. If you have read my other Linux articles, you are familiar with partitioning and Linux notation, as well as with the command line. The next logical step is to enhance this knowledge by taking one step further. Understanding how GRUB works and what boot entries in the boot menu mean will help you understand how the operating systems work, how to fix, recover or modify the GRUB menu to suit your needs, and how to setup different work environments with several operating systems.
In the past, we have relied on the installation setups to make the hard work for us. In fact, setting up GRUB, while frightening, is a quite simple and fully reversible procedure. Mastering the GRUB is a very important step in building up the confidence to use Linux.
So if you are ready, read on.
But first of all, a foreword of wisdom. This article is a compilation of sources and examples that will help you learn about GRUB. Of course, it's all out there somewhere, on the Internet. However, new Linux users will probably find the notion of spending hours searching for relevant pieces of information (especially if their PC won't boot) somewhat frustrating. The goal of this guide is to help provide simple and quick solutions to most common problems regarding multi-boot setups and installation of Linux operating systems.

Table of contents


  1. Everything is a file
  2. Introduction
    1. What about LILO?
  3. How does GRUB work?
  4. GRUB notation
  5. GRUB configuration
    1. Installation of GRUB
      1. GRUB on a floppy disk
      2. Installing GRUB natively
      3. Installing GRUB with grub-install
    2. Setting up GRUB manually (after installation)
      1. Backup!
      2. Adding a new operating system to the GRUB menu
  6. Common problems
    1. GRUB got deleted; how to restore?
      1. The easy way
      2. The hard way
    2. Windows is installed on a non-first hard disk (Swapping)
    3. There is more than one Windows operating system installed on one hard disk (Hide/Unhide)
    4. You resized a partition; GRUB is gone
  7. Useful tools
  8. Extras for special geeks
  9. Links
    1. My other articles
    2. General information (by alphabetic order)

Everything is a file

To be able to successfully master the secrets of GRUB, you must understand one of the basic foundations of *NIX-based operating systems. Everything is a file. Even hard disks and partitions are treated as files. There is no magic. If you remember this, you will find the supposedly perilous task of tampering with partitions no different than playing with files using a file explorer (or the command line).
Now that we have established this, we can move on to the more technical parts of grubbing.

Introduction

GNU GRUB is a bootloader (can also be spelled boot loader) capable of loading a variety of free and proprietary operating systems. GRUB will work well with Linux, DOS, Windows, or BSD. GRUB stands for GRand Unified Bootloader.
GRUB is dynamically configurable. This means that the user can make changes during the boot time, which include altering existing boot entries, adding new, custom entries, selecting different kernels, or modifying initrd. GRUB also supports Logical Block Address mode. This means that if your computer has a fairly modern BIOS that can access more than 8GB (first 1024 cylinders) of hard disk space, GRUB will automatically be able to access all of it.
GRUB can be run from or be installed to any device (floppy disk, hard disk, CD-ROM, USB drive, network drive) and can load operating systems from just as many locations, including network drives. It can also decompress operating system images before booting them.
You can learn much, much more from the official GNU GRUB Manual 0.97.

What about LILO?

You may have heard about another Linux bootloader called LILO (stands for LInux LOader). While a sensible option for many Linux users, I believe that GRUB is a better choice, for several reasons:
  • LILO supports only up to 16 different boot selections; GRUB supports an unlimited number of boot entries.
  • LILO cannot boot from network; GRUB can.
  • LILO must be written again every time you change the configuration file; GRUB does not.
  • LILO does not have an interactive command interface.
All in all, it seems that GRUB is the winner. So let's see what this baby can do.

How does GRUB work?

When a computer boots, the BIOS transfers control to the first boot device, which can be a hard disk, a floppy disk, a CD-ROM, or any other BIOS-recognized device. We'll concentrate on hard disks, for the sake of simplicity.
The first sector on a hard is called the Master Boot Record (MBR). This sector is only 512 bytes long and contains a small piece of code (446 bytes) called the primary boot loader and the partition table (64 bytes) describing the primary and extended partitions.
By default, MBR code looks for the partition marked as active and once such a partition is found, it loads its boot sector into memory and passes control to it.
GRUB replaces the default MBR with its own code.
Furthermore, GRUB works in stages.
Stage 1 is located in the MBR and mainly points to Stage 2, since the MBR is too small to contain all of the needed data.
Stage 2 points to its configuration file, which contains all of the complex user interface and options we are normally familiar with when talking about GRUB. Stage 2 can be located anywhere on the disk. If Stage 2 cannot find its configuration table, GRUB will cease the boot sequence and present the user with a command line for manual configuration.
Stage 1.5 also exists and might be used if the boot information is small enough to fit in the area immediately after MBR.
The Stage architecture allows GRUB to be large (~20-30K) and therefore fairly complex and highly configurable, compared to most bootloaders, which are sparse and simple to fit within the limitations of the Partition Table.

GRUB notation

This section is intended to help you get familiar with GRUB, without touching anything. The next section deals with actual files and manual configuration.
GRUB has its own notation, which is very similar yet somewhat different from the general notation a typical Linux user might be used to.
Now, here's an example of a typical GRUB entry:
(hd0,1)
  • The brackets are a must; all devices listed in GRUB menu must be enclosed in brackets.
  • hd stands for hard disk; alternatively, fd stands for floppy disk, cd stands for CD-ROM etc.
  • The first number (integer for geeks) refers to the physical hard drive number; in this case, the first drive, as they are counted from zero up. For example, hd2 refers to the third physical hard drive.
  • The second number refers to the partition number of the selected hard drive; again, partitions are counted from zero up. In this case, 1 stands for the second partition.
From here, it is evident that GRUB (menu) does not discriminate between IDE or SCSI drives or primary or logical partitions. The task of deciding which hard drive or partition may boot is left to BIOS and Stage 1.
As you see, the notation is very simple.
Primary partitions are marked from 0 to 3 (hd?,0), (hd?,1), (hd?,2), (hd?,3). Logical partitions in the extended partition are counted from 4 up, regardless of the actual number of primary partitions on the hard disk, e.g. (hd1,7).
The entries alone are not enough to boot an operating system though.
GRUB also needs to know what operating system images to load. These are assigned as parameters to each of the called devices, including special flags (switches). For example, Windows Safe Mode is a special flag.
Here is an example of a GRUB menu booting only Ubuntu.
GRUB Ubuntu boot
Here's an example of a GRUB menu from a computer with triple boot (SUSE 10.2, Ubuntu 6.10 and Windows XP). This is the actual menu.lst on one of my real machines.
GRUB triple boot
Let's try to understand what the entries mean. Only uncommented lines count. Comments are marked with #. The lines that have a series of small gray xs showing are in indication that text therein belongs to the line preceding it. In other words, there text simply dropped to another line to visually accommodate the text editor and screen resolution limitations.
default 0
timeout 8
The first line (default 0) means that the first operating system listed will be booted. In this case, it's SUSE 10.2. The second line (timeout 8) tells how much time (seconds) the user has to make his choice before the default entry is loaded. Simple, isn't it?
gfxmenu (hd0,2)/boot/message
The GRUB menu can also be graphic. The fancy stuff needed to present the user with a colorful background and possibly some extras is located on the first physical disk, third partition (hd0,2). This is a primary partition, as we have seen earlier.
title openSUSE 10.2
root (hd0,2)
kernel /boot/vmlinuz-...
initrd /boot/initrd.img-...
This is the first operating system entry in the menu.
  • title is as simple as it sounds. It's merely a string that's meant to help the user read the menu in human terminology.
  • root (hd0,2) tells GRUB where its configuration files are located. In this instance, they can be found under (hd0,2)/boot/grub.
  • kernel /boot/vmlinuz-... boots the actual kernel image. There can be many such images available. The fact there is no device specified before the /boot/vmlinuz indicates the image is located on the same partition as the GRUB itself. This is often the default case for your primary choice of operating system.
  • initrd /boot/initrd.img-... is the temporary file system that makes system preparations - adapts generic kernel image to specific hardware - before the real root is loaded.
The extra switches used after the kernel indicate where the actual root is located, what graphic mode is used and where the swap partition resides. This article will not go into detail regarding the more advanced GRUB configurations.
title Ubuntu, kernel ... (/dev/sda9)
kernel (hd0,8)/boot/vmlinuz-...
initrd (hd0,8)/boot/initrd.img-...
  • Again, title indicates a name, in this case fancily adorned with technical details.
  • kernel (hd0,8)/boot/vmlinuz-... points to the 9th partition on the first hard disk (hd0,8). Accidentally, the root flag (/dev/sda9) indicates that the root partition is the same as the one containing the kernel image. Normally, this is the case, and for the sake of simplicity, you will want this option during your installations.
  • initrd (hd0,8)/boot/initrd.img-... nothing new here.
Note regarding kernel images and root partitions: On older computers with BIOS that do not support access to more than the first 1024 cylinders, you might setup a boot partition that contains the kernel image, while the root itself is located elsewhere. But people with computers younger than the botched Y2K crisis need not worry.
Another thing you might notice is that the Ubuntu entry is fairly detailed. This feature is called Multi-boot Compliance; openSUSE recognizes Ubuntu and can accurately call its images (including special switches) and mount the partitions. However, most operating systems are only partially multi-boot compliant.
A little later on, I will show you how the Ubuntu entry could have been treated differently, with the same results.
title Windows
rootnoverify (hd0,0)
chainloader (hd0,0)+1
  • rootnoverify (hd0,0) means that openSUSE cannot understand Windows operating system, i.e. no multi-boot compliance. Therefore, the operating system is called without any fore-knowledge of the kernel. GRUB assumes that the relevant boot images will be found on the target partition and mounted by the other operating system bootloader. As you can see, Windows was installed on the first partition of the first hard disk. This is the most convenient option.
  • chainloader (hd0,0)+1 feature is used for operating systems that cannot be booted directly. Not surprisingly, Windows operating systems cannot be booted directly. They are booted by the method of chainloading. As the name implies, GRUB passes the control of the boot sequence to another bootloader, located on the device to which the menu entry points. This can be a Windows operating system, but also any other, including Linux.
Back to Ubuntu. You remember that I have told we could have booted Ubuntu in an alternative way? Indeed, we could have simply chainloaded it, just like Windows.
title Ubuntu
root (hd0,8)
chainloader (hd0,8)+1
This would have worked equally well.
The last option in the menu allows you to boot SUSE in the failsafe mode (sort of Safe Mode, no graphics) and does not contribute to our knowledge, therefore we will skip it at the moment.
Let's see some more examples.
The example below is actually written in the Ubuntu menu.lst (commented, of course).
title Windows 95/98/NT/2000
root (hd0,0)
makeactive
chainloader +1

title Linux
root (hd0,1)
kernel /vmlinuz root=/dev/hda3 ro
By now, you should be able to "read" GRUB language with ease. We begin with the title. root specifies the partition where we expect to find the Windows kernel and mounts it (rootnoverify would leave this job to the Windows bootloader). makeactive command sets the active partition on the root disk (above) to GRUB root device. This means that the next command, chainloader, is executed without the target partition specified (as the target partition is the same, now).
The second entry is even simpler. We name a Linux, we call its partition and we boot the kernel. In this case, we see the very interesting case where the kernel image and root (/) partition of the operating system are NOT located on the same partition. This would be a very likely case for older computers - or ones with a specific boot partition.
Finally, the crazy stuff:
A GRUB menu booting 100+ systems of DOS, Windows, Linux, BSD, and Solaris
You see, it's very simple! Once you get the hang of it, it actually becomes fun.
For more information, you must read the GNU GRUB Manual 0.97.
In particular, you need to familiar with The list of command line and menu entry commands.

GRUB configuration

Most of the time, you will not want or need to touch GRUB. When you install operating systems, especially in the right sequence (inconsiderate OS first - like Windows, flexible OS later - Linux and family), the GRUB will be installed automatically and relevant entries appended.
Nevertheless, you should know when and what to do if things go wrong.

Installation of GRUB

GRUB can be installed to a variety of devices. Most people will be interested in setting up GRUB on their hard disk. Nevertheless, it does not hurt to know learn about other options. Of course, you can skip forward.
Before we install anything, we need to know where our files are.
If installed, GRUB menu is located on the root partition under:
/boot/grub/menu.lst
Always, always back this file before making any changes!
The GRUB files can be found in the image of your operating system (usually a CD), under:
/usr/lib/grub/i386-pc
You should also be aware of the basic Linux commands regarding the hard disks and partitions. To this end, you might want to try my article Highly useful Linux commands & configurations.
Nevertheless, for those who do not fancy reading yet another article, here's a brief version:
You can display your PC environment information either through terminal or a text editor. Files through text editor are invoked by specifying the text editor and a target file (with su or sudo for system files).
sudo vi /etc/fstab
OR
sudo gedit /etc/fstab
Alternatively, you can simply print the contents of the files in the terminal by using the cat (concatenate) command.
(sudo) cat /etc/fstab
Some useful commands that one might need when dealing with boot, hard drives and partitions:
Display the partition table:
fdisk -l
Display the mounted partitions:
cat /etc/fstab
Now that we know what we need and where to find it, it's time to install GRUB.

GRUB on a floppy disk

To create GRUB on a floppy disk, we must copy the Stage 1 and Stage 2 files from the image directory to the first and second sector of the floppy disk. We'll use the dd command, which copies information sector by sector.
Here's the set of commands that you need to execute (taken from the Manual):
cd /usr/lib/grub/i386-pc
dd if=stage1 of=/dev/fd0 bs=512 count=1
dd if=stage2 of=/dev/fd0 bs=512 seek=1
Just a short explanation: if stands for input file, of stands for output file, /dev/fd0 refers to the first mounted floppy device, bs specifies the block size (in bytes), count instructs how many blocks should be copied, and seek tells how many blocks should be skipped before writing. Not surprisingly, since we need to write the files to the first two sectors of the floppy disk, we write stage1 to the first sector, and copy stage2 to the second (skip first, then write).
Now that we have taken this deep fancy for the command line, I'll leave you with the nuances of different commands for homework. More on dd on Wikipedia.
Regarding the GRUB on floppy, that's it. Since we have copies the stages from OS image, all of the required information is there. Of course, you can always make manual adjustments. But that's the next chapter.

Installing GRUB natively

Native install means placing GRUB Stage 1 in the first sector of the hard disk (MBR or Partition Table). This means you will be able to boot without a secondary device, like a floppy disk (which have become a rarity nowadays). However, this also means that if you install an inconsiderate OS later on (like Windows) or try to repair the MBR for some reason (by running fdisk /MBR from DOS prompt), you will erase the GRUB and render all systems listed in the menu.lst unbootable.
To install GRUB in MBR, you will need to boot from external media (floppy, live Linux CD). Then, once you reach the GRUB prompt, execute the following commands:
Find the GRUB root device:
find /boot/grub/stage1
GRUB will search for all available Stages 1 and present them. If you have more than one operating system image present (e.g. SUSE, Kubuntu, Mandriva), you will have more than one stage1 available.
Example:
Let's say the computer has the following operating systems installed on different partitions:
  • SUSE on (hd0,1)
  • Kubuntu on (hd0,2)
  • Mandriva on (hd0,4)
All these will be returned as potential roots for GRUB device (as each OS has its own files).
If you wish to use SUSE GRUB, then you will setup the GRUB root device to (hd0,1):
root (hd0,1)
If you want Mandriva's GRUB, then:
root (hd0,4)
If you know in advance what you want to do, you can skip the find command.
Once you have decided on the root, you need to write the information to the MBR:
setup (hd0)
Finally, quit the GRUB prompt:
quit
As a sequence, the commands that you need are:
find /boot/grub/stage1 (optional)
root (hdX,Y)
setup (hd0)
quit
You can also setup GRUB on another drive or partition, but then you will have to chainload GRUB to another bootloader for this to work.
That's it. Easy peasy orange squeasy!

Installing GRUB with grub-install

This method is considered less safe (according to the Manual), as it guesses the mapping. Still, for total newbs in need of dire help, this might be the preferred method.
You only need to invoke a single command - namely, where to install the bootloader. Furthermore, this command can be written in several ways, all equivalent:
grub-install /dev/hda

grub-install /dev/hd0

grub-install '(hd0)'

grub-install hd0
After you have installed the GRUB, your operating systems should boot. Once booted, you can once again start playing with GRUB, manually changing settings - adding and removing entries, chainloading other bootloaders, or even hiding and unhiding partitions.

Setting up GRUB manually (after installation)

You can reconfigure or reinstall GRUB at any moment.

Backup!

First, before you make any changes to the GRUB configuration file, it is most warmly recommended that you backup the existing menu. You might even consider copying to another machine or printing the menu, in case things go bunkerous.
cp /boot/grub/menu.lst /boot/grub/menu.lst_backup
To access GRUB, execute the following command in Linux terminal:
sudo grub
After a few moments, the GRUB command line should show up. You can identify it by the grub> prompt.
GRUB prompt
Alternatively, you can reach the GRUB command line during bootup. When the GRUB menu loads, press C on the keyboard.

Adding a new operating system to the GRUB menu

Let's say you have installed yet another operating system on your machine, Sabayon. During the installation, you skip the GRUB setup. This means that the original GRUB remains untouched - and it does not contain an entry about Sabayon. For all practical purposes, Sabayon is not bootable.
Note: GRUB menu entries are called stanzas (probably a twist of instance?).
So, we need to add Sabayon to the list.
If you know where Sabayon is installed, you just need to add its entry to the menu.lst.
title Sabayon
root (hdX,Y)
chainloader +1
That's it.
You can also do this while booting the computer, without editing the menu. When the original GRUB comes up, press C to reach the command line. And then:
root (hdX,Y)
chainloader +1
Optionally, you will use the find command to get around.
Basically, this is the whole of GRUB magic. As you can see, it's very very simple. But for people who have never heard of GRUB and see long lists of strange commands, the prospect can be daunting.
Now that we have covered the basics of grubbing, it's time for extras and some more common problems.

Common problems

GRUB got deleted; how to restore?

This will often happen if you install Windows after Linux. Windows assumes it's the only operating system in the world and does not try to live with existing information present in the MBR; it overwrites it. For people with dual-boot and very little knowledge of Linux, this is a disaster. Luckily, it's very easily recoverable one.

The easy way

Use Super Grub Disk. I have written about this tool in detail in my article A (cool) list of Linux tools, under Rescue.

The hard way

  • Boot from floppy disk or CD (any Linux live CD should do).
  • Get to the grub command line.
find /boot/grub/stage1 (optional)
root (hdX,Y)
setup (hd0)
quit
The same as before!

Windows is installed on a non-first hard disk (Swapping)

GRUB cannot directly boot Microsoft operating systems. And sometimes, even chainloading may not work, especially if Windows is not installed on the first hard disk. Once again, you should remember that you should always install Windows first, on the first hard disk, on the first partition (the rule of three first). Nevertheless, even if you have Windows installed on a separate disk, you can solve the problem by swapping.
You need to perform a virtual swap between hard disks. Like this:
map (hd0) (hd1)
map (hd1) (hd0)
After you add these two lines, you should be able to boot into Windows (or DOS, for that matter).

There is more than one Windows operating system installed on one hard disk (Hide/Unhide)

Again, Windows can cause problems if there's more than one instance present on a hard disk, especially if installed on primary partitions. When you use the chainloader command, the control is transferred to the Windows boot loader, but which one?
The problem is easily solved by hiding and unhiding partitions. If GRUB hides a partition, it will not be visible by Windows and prevent a possible confusion. Vice versa, if you unhide a partition, Windows will detect it and be able to boot from it, without getting confused.
Here's the example taken from the Manual that demonstrates this point.
Let's say we have Windows installed on the first and the second partition of your hard disk. We wish to boot the first instance of Windows. Then, we need to execute the following set of commands to make it all work:
unhide (hd0,0)
hide (hd0,1)
rootnoverify (hd0,0)
chainloader +1
makeactive

You resized a partition; GRUB is gone

This is an interesting case. It can happen after you use a partitioning or an imaging software to make changes to the Partition Table. Usually, this will happen when you make such a change in Windows. Linux will not be informed of the change, because Windows is blind to anything else on the machine save Microsoft thingies. Consequently, GRUB will suffer.
The solution is that most likely the filesystem is damaged and needs to be repaired.
Boot from a live CD and execute the following commands, assuming you know where Linux partitions are.
Check the filesystem:
fsck.ext2 /dev/hdXY
Replace X and Y with hard disk letter and partition number that you want to check (hda2, hdb3, etc.).
Create the ext2/3 filesystem parameters again.
tune2fs -j /dev/hdXY
Now mount the root filesystem on /mnt/sysimage and run grub:
mount -t ext2 /dev/hdXY /mnt/sysimage
OR
mount -t ext2 /dev/hdXY /mnt/sysimage

cd /mnt/sysimage/sbin
grub
You're back to the familiar grounds.
For more information on Linux commands, including fsck.ext2, tune2fs and others, see Manual pages on Linux Reviews.
Finally, some more tricky questions answered in Ubuntu wiki:
How to restore GRUB menu after Windows installation
How to identify the name of the boot drive (hd0, hd1, hd2, etc)
How to add Windows entry into GRUB menu
How to boot into Windows installed on a separate SATA drive

Useful tools

GParted
GParted is a Gnome Partition Editor. Although Linux-based, it fully supports the NTFS file system. It runs from a CD.
SystemRescueCD
This is a live CD specifically geared toward rescue and recovery. The tools package includes some of the most important tools available for Linux user, like GParted, QTParted, Partimage, Grub, Lilo, sfdisk, security tools, network tools, and more.
Super Grub Disk
Super Grub Disk is intended to run from a floppy disk or CD and is used for system rescue. Most importantly, it can be used to restore boot loaders, including GRUB, LILO and even Windows boot loader.
TestDisk
TestDisk is a utility specially designed to recover lost partitions and make non-booting disks bootable again. This highly useful tool can be found on a number of live CDs, including Knoppix and GParted. It supports NTFS partitions and also comes in Windows flavor.

Extras for special geeks

Multiboot with GRUB Mini-HOWTO
How To Multi-boot Operating Systems
How to boot Ubuntu from the Windows Bootloader
Filesystems and Mounting

Links:

GNU GRUB
GNU GRUB FAQ
GNU GRUB Manual 0.97
The list of command line and menu entry commands
GNU GRUB on Wikipedia
A GRUB menu booting 100+ systems of DOS, Windows, Linux, BSD, and Solaris

My other articles:

Highly useful Linux commands & configurations
A (cool) list of Linux tools

General information (by alphabetic order):

cat
dd
initrd
LILO
Logical Block Address
Manual pages (man) on Linux Reviews
NTLDR
That's it. Enjoy!

วันพุธที่ 19 ตุลาคม พ.ศ. 2554

การ Re-installing GRUB จาก Rescue System

http://www.susethailand.com/suseforum/index.php?topic=1645.0
จากสถานการณ์ที่ GRUB boot loader เกิดความเสียหายด้วยเหตุผลบางอย่าง เช่น ระหว่าง fsck ผู้ใช้อาจจะกดปุ่ม reset ที่เครื่องคอมพิวเตอร์ หรือ เกิดจากไฟฟ้าติดๆ ดับๆ โดยคอมพิวเตอร์ไม่ได้เชื่อมต่อกับเครื่องสำรองไฟ (UPS) ซึ่งจำเป็นต้องติดตั้ง Boot loader ใหม่

การ Re-installing GRUB จาก Rescue System

1. บูตจากแผ่น DVD (SLED หรือ SLES 11)

2. เลือก "Rescue System"

3. ล็อคอินด้วย root

4. พิมพ์คำสั่ง grub

Code:
opensuse:~ # grub

    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)
 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename. ]

grub> find /boot/grub/stage1
 (hd0,0)

grub> root (hd0,0)
 Filesystem type is reiserfs, partition type 0xfd

grub> setup (hd0)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/reiserfs_stage1_5" exists... yes
 Running "embed /boot/grub/reiserfs_stage1_5 (hd0)"...  18 sectors are embedded.
succeeded
 Running "install /boot/grub/stage1 (hd0) (hd0)1+18 p (hd0,0)/boot/grub/stage2 /boot/
grub/menu.lst"... succeeded
Done.

grub> quit

จากตัวอย่างด้านบน รูทพาร์ติชันคือ (hd0,0) ได้จากการใช้คำสั่งค้นหา (find /boot/grub/stage1)

5. รีบูตเครื่อง (เป็นอันเสร็จ)

วันศุกร์ที่ 7 ตุลาคม พ.ศ. 2554

8 ขั้นตอนง่าย ๆ ในการเปลี่ยน desktop ให้เป็น Ubuntu Server

เอาไว้สำหรับเป็นโปรเจคเล็กๆ เล่นยามว่าง ในที่นี้สมมุติว่าคุณมี Ubuntu 7.10 "Gutsy" (เวอร์ชั่น desktop) ติดตั้งในเครื่องแล้วนะ แล้วจะช้าอยู่ใย ลงมือกันเลยดีกว่า
ขั้นตอนที่ 0: จะทำเองหรือเสียตังค์? - จริงๆ แล้วมีวิธีการรันเซิฟเวอร์แอปพริเคชั่นได้หลายวิธี ด้วยสนนราคาที่ไม่แพงนัก เช่น Linode หรือ Bytemark ก็สามารถนำมาทำ "virtual root" ของเซิฟเวอร์ลีนุ๊กซ์ได้ในราคาประมาณ 600 บาทต่อเดือน โดยใช้ลีนุ๊กซ์แมชชีนเช่น User-Mode Linux (UML), Xen, และ VMWare.
แต่ถ้าไม่อยากเสียตังค์ก็เอาเครื่องของเราเองนี้แห ล่ะมาทำได้ แต่ต้องเป็นโปรเจคเล็กๆ นะ วิธีการที่จะทำกันต่อจากนี้สามารถนำไปประยุกต์ใช้ได้กับ 'เวอร์ชวลรูทโฮสติ้งเซอร์วิส (virtual root hosting service)' ทั้งนี้เนื่องจากว่ายังไงคุณก็ต้องติดตั้งซอฟแวร์ของเซิฟเวอร์แบบเดียวกัน
ขั้นตอนที่ 1: หาทางเชื่อมต่อกับเซิฟเวอร์ - คุณต้องแน่ใจว่าคุณมี stable IP address และโดเมนเนมที่จะใช้ในการเชื่อมต่อไปยังเครื่องของคุณ คุณอาจต้องคุยกับผู้ดูแลระบบ หรือซื้อโดเมนของคุณเอง มีหลายๆ บริษัทให้บริการจดโดเมนในราคาไม่แพงนัก หรือคุณอาจจะหาโดเมนเนมจากบริษัทที่ให้บริการฟรีก็ได้ ถ้าคุณต้องการทราบไอพีแอดเดรสและโดเมนเนมของคุณเอง ก็สามารถใช้คำสั่งเช่น "ifconfig" และ "nslookup" หรือไปยังเวปไซต์พวก
whatismyip.com และ samspade.com ก็ได้ ถึงตอนนี้เมื่อคุณได้รับไอพีแอดเดรสและโดเมนเนมแล้ว ให้ตั้งโดเมนเนมสำหรับเครื่องของคุณโดยการแก้ไขไฟล์ /etc/hostname:
sudo nano /etc/hostname
แล้วใส่โฮสเนมในบันทัดเดียวกันหมด:
my.domain.name.th
พิมพ์ control-X เพื่อออกมาแล้ว saveไฟล์ คุณอาจเพิ่มข้อมูลอื่นๆ เกี่ยวกับไอพีแอดเดรสและโฮสเนมไปยังไฟล์ /etc/hosts เช่นถ้าไอพีของคุณคือ 123.123.123.123 ให้เพิ่มบันทัดต่อไปนี้ไปยังไฟล์ (โดยการใช้ "sudo nano /etc/hosts"):
123.123.123.123 your.domain.name.th
ขั้นตอนที่ 2: ตั้งพาสเวิร์สให้ยากๆ หน่อย - ถ้าตั้งพาสเวิอร์ดยาวๆ มีทั้งตัวเล็ก ตัวใหญ่ ผสมกับหมายเลข และสัญญลักษณ์ได้จะดีมาก เป็นเรื่องสำคัญมากที่ต้องพยายามทำให้ได้ คุณอาจทำได้ง่ายๆ โดยไปที่ System->Administration->Users and Groups หรือที่ command line (กรณีใช้ Terminal) ให้รันคำสั่ง:
passwd
ตัวเลือกเพิ่มเติม: คุณสามารถตั้งพาสเวิอร์ดสำหรับ bios ของคุณและสำหรับ grub bootloader ใน BIOS ของคุณก็เช่นกันคุณสามารถตั้งลำดับการบูทเครื่องโดยให้ใช้ฮาร์ดไดร์เป็น ลำดับแรก เพื่อเป็นการป้องกันไม่ให้คนอื่นเอา CD มาควบคุมระบบของคุณได้
ขั้นตอนที่ 3: ติดตั้งเซิฟเวอร์ SSH - เพื่อที่ว่าคุณจะได้ล๊อคอินไปยังคอมพิวเตอร์ทางไกล ผ่านการเชื่อมต่อแบบ encrypted ได้ ถ้าคุณใช้ command line ของ shell application ของ Terminal คุณสามารถติดตั้งแบบนี้ก็ได้:
apt-get install ssh openssh-server
หรือมิฉะนั้นคุณสามารถใช้ Synaptic package manager เพื่อค้นหา "ssh" และติดตั้ง package เดียวกัน
ตัวเลือกเพิ่มเติม: แก้ไขไฟล์ /etc/ssh/sshd_config เพื่อเปลี่ยนพอร์ตของ ssh ที่ใช้สำหรับเชื่อมต่อ จากค่าปกติที่ 22 เป็นค่าอื่น เปลี่ยนที่บันทัดที่มีคำว่า "Port 22" ทั้งนี้เพราะพอร์ต 22 ง่ายต่อการถูกโจมตี (แต่ยังไงก็ตามที คุณควรจะรัน firewall และหมั่นดูแลระบบให้ทันสมัย และที่สำคัญควร backed up ข้อมูลบนเซิฟเวอร์บ่อยๆ เช่นกัน)
sudo nano /etc/ssh/sshd_config
รีสตาร์ท ssh
sudo invoke-rc.d ssh restart
ทดลองใช้คอมพิวเตอร์เครื่องอื่น เชื่อมต่อไปยังเซิฟเวอร์ของคุณโดยใช้ ssh client (อาทิเช่น "ssh" บน command line หรือ Putty บน
windows เป็นต้น) เช่น ถ้าคุณเปลี่ยนค่าพอร์ตเป็น 789 และ username ของคุณคือ "superman" ดังนั้นคำสั่งสำหรับการเชื่อมต่อคือ:
ขั้นตอนที่ 4: ติดตั้ง LAMP software - Apache 2 webserver, PHP 5,
& MySQL 5 วิธีการติดตั้งแบบง่ายๆ คุณอาจทำโดยใช้ tasksel (บน command line) หรือ Synaptic ในการทำแบบ Synaptic ให้เลือก Edit->Mark
Packages by Task...และเลือก LAMP server และติดตั้ง หรือมิฉะนั้นที่ command line ให้ run
sudo tasksel
แล้วเลือก LAMP server ถ้าคุณไม่ได้ติดตั้ง tasksel ก็ให้ run "sudo apt-get install tasksel" ตอนนี้ก็จะเห็นว่ามีการติดตั้ง Apache, MySQL, และ PHP และควรมีการถามหา root password เพื่อใช้กับ MySQL ก็เช่นเดียวกันควรเลือกพาสเวิร์ดที่ดี ดังที่กล่าวไว้แล้วในขั้นตอนที่ 2 และก็ควรตั้งค่า MySQL ไม่ให้ตอบรับการติดต่อใดๆ
ยกเว้นจากเครื่องของคุณ (local machine) ตัวไฟล์หลักในการจัดการ Apache อยู่ที่:
/etc/apache2/apache2.conf

/etc/apache2/sites-enabled/000-default
ในกรณีที่คุณอยากแก้ไขใดๆ
root folder สำหรับเอกสาร HTML ของ webserver HTML ของคุณคือ:
/var/www
แก้ไขไฟล์ index.html ตัวอย่างเช่นคุณต้องการเปลี่ยนค่า homepage
ให้ตรวจดูเวปเซิฟเวอร์ของคุณที่ http://your.domain.name.th/
ขั้นตอนที่ 5: ติดตั้ง Firewall - อันนี้เป็นสิ่งจำเป็นเช่นเดียวกัน คุณสามารถบล๊อกการติดต่อทั้งหมดจากภายนอกได้ ยกเว้นพอร์ตที่คุณต้องการจะเปิดไว้ เวปเซิฟเวอร์จะใช้พอร์ต 80 และ ssh ใช้ พอร์ต อะไรก็ได้ที่คุณตั้งใน /etc/ssh/sshd_config (ค่าปกติอคือ 22 )

สำหรับ firewall ใน Ubuntu จะมีให้เลือกอยู่ 2 ตัว ถ้าคุณยังคงใช้เครื่องนี้เป็น desktop ต่อไปด้วย คุณอาจใช้ FireStarter เป็น graphical firewall manager แต่ถ้าคุณใช้เครื่องนี้เป็นเซิฟเวอร์อย่างเดียว คุณอาจเลือก Shorewall เพราะคุณสามารถแก้ไขจาก command line (ไม่แน่ใจว่า Firestarter จะสามารถใช้ command line ได้หรือเปล่าไม่เคยลอง) ในการติดตั้ง Shorewall ให้พิมพ์บันทัดต่อไปนี้:
sudo apt-get install shorewall
จากนั้นพิมพ์คำสั่งต่อไปนี้:
cd /usr/share/doc/shorewall/default-config sudo cp interfaces /etc/shorewall/ sudo cp policy /etc/shorewall/ sudo cp rules /etc/shorewall/ sudo cp zones /etc/shorewall/
โดย การใช้คำสั่งในการแก้ไข (เช่น sudo nano หรือ sudo gedit) คุณต้องแก้ไขไฟล์ 4 ไฟล์ ให้เพิ่มบันทัดนี้ก่อนบันทัดสุดท้ายของ /etc/shorewall/interfaces:
net eth0 detect
เพิ่มบันทัดนี้ก่อนบันทัดสุดท้ายของ /etc/shorewall/zones:
net ipv4
คุณจะต้องมีบันทัดที่เขียนว่า "fw firewall" อยู่ใน zones file ให้ใส่บันทัดต่อไปนี้ก่อนบันทัดสุดท้ายของ /etc/shorewall/policy:
fw net ACCEPT net all DROP info all all REJECT info
และเพิ่มบันทัดเหล่านี้ไปยังไฟล์ /etc/shorewall/rules ของคุณหลังจาก SECTION NEW line:
#change "22" to the port you used for ssh if not the default one: ACCEPT net fw tcp 22 ACCEPT net fw tcp www,https
เปลี่ยนจาก 22 ไปเป็นพอร์ตที่คุณจะใช้สำหรับ ssh ให้เอาคำว่า ",https" ออก ถ้าคุณไม่คิดว่าจะติดตั้ง ssh module ใดๆ สำหรับ apache
webserver (เพื่อความปลอดภัยในการทำธุรกรรมซื้อขาย) สุดท้ายแก้ไขไฟล์ /etc/default/shorewall โดยเปลี่ยนบันทัดที่มีคำว่า startup=0 เป็น:
startup=1
คุณสามารถเปิด firewall โดยใช้คำสั่ง:
sudo invoke-rc.d shorewall start
ส่วนตัวเลือกอื่นๆ คุณสามารถผ่านไปยัง invoke-rc.d รวมถึงการ start, stop, และ restart
ขั้นตอนที่ 6: รักษาระบบให้ทันสมัยอยู่เสมอ - Run คำสั่ง
sudo apt-get update sudo apt-get upgrade
เพื่อให้ซอฟแวร์ update สม่ำเสมอ จำเป็นมากทีเดียวเพราะโดยทั่วไปแล้วตัวที่ update จะมีการแก้ไขข้อผิดพลาดด้านความปลอดภัยของระบบ คุณสามารถ run คำสั่งข้างต้นได้ทุกวัน หรือแม้แต่ใช้ crontab ก็ได้ มีเวป สอนการใช้ crontabอยู่หลายๆ เวป
ขั้นตอนที่ 7: หมั่นดูแลตรวจสอบ Site ของคุณ - มีเครื่องไม้เครื่องมือมากมายที่คุณสามารถติดตั้ง และใช้ในการตรวจสอบ site ของคุณเช่น webalizer (apache
statistics), webmin (ตัวนี้ใช้ เป็นweb-based control panel สำหรับเซิฟเวอร์ของคุณ) เป็นต้น ตัว crontab ผสมผสานกับ shell scripts (ใน bash, python, perl, และอื่นๆ) สามารถนำมาใช้ในการส่งอีเมล์ถึงคุณเมื่อเซิฟเวอร์มีปัญหาหรือล่ม หรือคุณอาจสั่งให้รีสตาร์ทอัตโนมัติได้
ขั้นตอนที่ 8: Backup backup และก็ยัง backup นั่นแหล่ะ - คุณควรทำการเก็บ backups ไฟล์บนเซฟเวอร์ของคุณอย่างสม่ำเสมอ คุณอาจ backup ไปยังฮาร์ดไดร์สำรอง หรือรีโมทคอมพิวเตอร์ก็ได้ คุณสามารถตกแต่ง RAID configuration เผื่อว่าการทำไดร์หลายๆ ไดร์จะช่วยเก็บข้อมูลซ้ำๆ กันได้ โดยการ copy และ compress backup ไปยังอีกไดร์ คุณสามารถ run คำสั่งดังต่อไปนี้เช่น:
sudo tar cpzf /media/path/to/backup/folder/my-backup.tar.gz
--exclude=/proc/* --exclude=/lost+found/* --exclude=/dev/*
--exclude=/mnt/* --exclude=/media/* --exclude=/sys/* --exclude=/tmp/*
--exclude=/var/cache/apt/* /
สำหรับ ในแง่เซิฟเวอร์ จะเป็นการดีถ้าใช้บางอย่างที่เหมือน rsync สามารถจัดแต่งเพื่อใช้ในการ backup อะไรก็ตามที่มีการเปลี่ยนแปลง แม้เพียงครั้งเดียวหลังจากครั้งสุดท้ายที่คุณทำการ back up (incremental backups) โปรดดูตัวอย่างได้ที่:
ควรเรียนรู้การกู้ไฟล์จาก backup ของคุณด้วยเช่นกัน รายละเอียดเพิ่มเติม ดูได้ที่:
แหล่งข้อมูล Quickly Setting Up and Securing an Ubuntu Server