Sunteți pe pagina 1din 102

Course Outline:

• Basic Commands
• Disk Management
• User Management
• Basic Utilities to monitor and Troubleshoot
• Installing Applications

o Describe the four main hardware components of a desktop


computer
o Describe the three main components of the Linux/Unix operating
system
o Log in to the system by using the GUI and the command line
o Use the GUI

• Gui
• CPU, RAM, HDD, I/O Devices
• Command Line
• root, su, su user, su – user
• changing passwords
• /etc/passwd, /etc/shadow

o Command Line Interface And Commands


The three main components of the Linux OS are:
• The kernel - The kernel is the core of the SunOS operating
system. The kernel, which manages all the physical resources
of the computer, including:
• File systems and structures
• Device management, such as storing data to the hard
disk
• Process management or CPU functions
• Memory management

1
o The shell - The shell is primarily a command interpreter. The
shell accepts commands you enter, interprets these commands,
and then passes them to the kernel for execution.
The 3 most common primary shells are :
Bourne Again (Bash) shell
C shell
Korn shell

The Directory Hiearchy – Show Directory Hiearchy, and how


logically they are below / (root filesystem) and physically they
might be different file systems eg, /usr, /opt etc. In the process
show pwd command etc.

1. List the four main hardware components of a computer.


2. Name the three main components of the Linux operating system.
3. Match the following terms with their descriptions:

Shell - Core of the Solaris OE


kernel - Command interpreter
Hard disk - Storage device

Working in shell & using commands.

Upon completion of this module, you should be able to:


o Understand what is a console and a terminal
o Construct and execute commands from the command line

Console/Terminal and Commands

Console is what is directly connected to the box


Tty – teletypewriter
PTY – Pseudo Terminal

2
These days, the distinction has rather blurred as users run powerful
multi-user multi-tasking OS’s on their PC. I would therefore define
the
CONSOLE as the display used to boot the system, and any other
display as a
terminal. This would be especially true of an X system: in the X GUI
all
the windows are effective terminals; the system console is the text
based
access available via CTRL-ALT-F1.

Command Options Arguments


Commands specify what the system has to do, Specifies how the
command is run (a modifier).
Options start with a dash (-) character. Specifies what is to be
affected (a file, a directory, or text).

Uname, uname –r (release), uname –s (name), uname –r –s


(combining commands), uname –a (all info)

Few other commands cal (cal 12 2006) & date commands.


Ls, ls –l (long listing).

Multiple commands e.g.


uname;date
cal 05 2002;date;uname -rs

man uname (Documentation)

Options using man, spacebar – scroll 1 page, Return Key (Enter)


displays the next line of a man page, b - Moves back one full
screen, /pattern - Searches forward for a pattern, Finds the next
occurrence of a pattern after you have
used /pattern, h – shows scrolling capabilities and q quits the man
pages. Searching man pages (if you don’t know command) for
keywords to find a command man –k calendar. Man –l passwd (to list

3
sections for passwd man page), man –s 4 passwd (to check section 4
of man page).

Exercise :

Logon to Gui, Open a terminal window.

1. Name the two components of command-line syntax that can


enhance the capability of a command.
2. Options and arguments
3. What command displays information about the operating system
and the workstation name?
4. Enter the uname -s command. What information appears?
5. Enter the uname -r command. What information appears?
6. Display the current time and date on your system. What
command did you enter?
7. Display this month’s calendar. What command did you enter?
8. What special character is used to separate commands on a
single command line?
9. Clear the terminal window. What command did you enter?

Pipes, Filters and Streams

| The pipe character

cat /etc/passwd | grep(filter) root

To view a list of all the subdirectories located in the /etc directory,


enter
the following:

$ ls -F /etc | grep "/"

To use the output of the ps command as input for the tail command
and

4
list the word count, enter the following:
$ ps -ef | tail +2 | wc -l
74

The history command displays previously executed commands. By


default, the history command displays the last 16 commands to
standard output.
history – will view commands with their numbers which can be used to
reconstruct commands
history –n to view without nos.
history -4 – To display current command and preceding 4 commands
history –r – Reverse order
history cd ls – to display the most recent cd and ls commands
set history=20
or
HISTSIZE=20
To repeat command type
!n (command number) eg !5

To set history

[abdullah@linuxhost install]#export HISTSIZE=500

File & Shell Basics

Working with directories


Working with files

pwd command – Present working directory

ls /etc

Use the ls -ld command to display detailed information for


the directory only, not its contents.
$ ls -ld dir1
drwxr-xr-x 5 user1 staff 512 Feb 22 14:51 dir1

5
To view a recursive list of the contents of the dir1
directory, enter
ls -R dir1.
$ ls -R dir1

Moving Around the Directory Hierarchy


There are two ways to move around the directory hierarchy.
You can use an absolute path name or you can use a relative
path name. An absolute path name lists all the directories in
the path. A relative path name lists the directories in the
path relative to the current directory.

$ cd
$ cd dir1
$ pwd
/export/home/user1/dir1
$ cd ../dir2
$ pwd
/export/home/user1/dir2
$ cd

To change directories using a relative path name, enter the


cd command with the path name that starts from the current
working directory, user1.

$ cd dir1/coffees

To change directories using an absolute path name, enter


the cd command with the complete path name from the /
(root) directory.

$ cd /export/home/user1/dir1/coffees

Displaying File Types

6
There are two ways to display file types. You can use the ls
-F command or you can use the file command.
#file filename
Will display detailed information of the file

#file

Use the ls -F command to display file types. Table below


shows the symbols used in the ls -F output.

Symbol File Type


/ Directory
* Executable
(none) Plain text file/ASCII
@ Symbolic link

The following is an example of the ls -F command.

$ ls -F

Using the file Command


Use the file command to determine certain file types.
Knowing the file type helps you decide which command or
program you need to use to read the file.

Identifying File Types

The character in the first column identifies each file type, as


follows:

- Regular files – Regular files can hold American Standard


Code for Information Interchange (ASCII) text or binary data,
including image data, database data,
application-related data, and more.

7
d Directories - Directories store information that associates
file names with inode numbers. Unlike regular files, which
can hold many different types of data, directories only hold
file name-to-inode associations. A directory contains entries
for files of all types that are logically found within that
directory.

l Symbolic links - A symbolic link is a special type of file that


points to another file or directory. A symbolic link is a file that
points to another file. Like directories, which contain only
directory information, symbolic links contain only one type of
data. The size of a symbolic link always matches the number
of characters in the path name it contains.

# ln -s file1 link1

b Block-special device files - The file type “b” identifies


block-special device files. For disk devices, block-special
device files call for I/O operations based on a defined block
size. The block size depends on the particular device.

c Character-special device files - The file type “c” identifies


character-special device files. For disk devices, character-
special device files call for input/output (I/O) operations
based on the disk’s smallest addressable unit, a sector. Each
sector is 512 bytes in size.

Regular files, directories, and symbolic links all store one or


more types of data. Device files do not store data. Instead,
device files provide access to devices.

A device file provides access to a device. Unlike regular files,


directories, and symbolic links, device files do not use data
blocks. Instead, the inode information of device files holds
8
numbers that refer to devices. Use the ls -l command to
display these numbers.

# cd /dev
# ls -l

A long listing of a device file shows two numbers, separated


by a comma. These two numbers are called major and minor
device numbers.

[root@localhost dev]# ls -l | more


total 0
crw-rw---- 1 root root 14, 12 Nov 30 00:12 adsp
crw------- 1 root root 10, 175 Nov 30 00:12 agpgart
crw------- 1 root root 14, 4 Nov 30 00:12 audio
drwxr-xr-x 3 root root 60 Nov 30 00:12 bus
lrwxrwxrwx 1 root root 3 Nov 30 00:12 cdrom
-> hdc
lrwxrwxrwx 1 root root 3 Nov 30 00:12 cdrom-hdc
-> hdc
brw-r----- 1 root disk 3, 2 Nov 29 19:12 hda2
brw-rw---- 1 root disk 22, 0 Nov 30 00:12 hdc

Using Hard Links

A hard link is the association between a file name and an


inode. A hard link is not a separate type of file. Every type of
file uses at least one hard link. Every entry in a directory
constitutes a hard link. Think of every file name as a hard link
to an inode. When you create a file, using the touch
command, for example, a new directory entry is created that
links the file name you specified with a particular inode. In
this way, creating a new file creates a hard link.

9
Information in each inode keeps count of the number of file
names associated with it. This is called a link count. In the
output from the ls -l command, the link count appears
between the column of file permissions and the column
identifying the owner. In the following example, the file
called file1 uses one hard link.

# cd dir1
# touch file1
# ls -l
total 0
-rw-r--r-- 1 root other 0 Apr 7 15:26 file1

Creating New Hard Links

A new hard link for a file name increments the link count in
the associated inode. In the following example, inode 1282
now has two hard links, one for file1 and the other for
file2. The ls -li command lists the inode number in the
left-most column. The find -inum command locates files
and directories that have the same inode numbers.
# ln file1 file2
# ls -l
total 0
-rw-r--r-- 2 root other 0 Apr 7 15:26 file1
-rw-r--r-- 2 root other 0 Apr 7 15:26 file2
# ls -li
total 0
1282 -rw-r--r-- 2 root other 0 Apr 7 15:26 file1
1282 -rw-r--r-- 2 root other 0 Apr 7 15:26 file2
# find . -inum 1282
./file1
./file2

The ln command creates new hard links to regular files.

10
Removing Hard Links
Deleting one of the files has no effect on the other file. The
link count decrements accordingly.
# rm file1
# ls -li

11
User Management

Adding Users

useradd – Adds a new user account on the local system


usermod – Modifies a user’s account on the local system
userdel – Deletes a user’s account from the local system
groupadd – Adds a new group entry to the system
groupmod – Modifies a group entry on the system
groupdel – Deletes a group entry from the system

Useradd –d /home/abc –m –s /bin/bash abc


Alternate useradd abc

/etc/skel

# useradd -u 100 -g other -d /export/home/newuser1


-m -s /bin/ksh \
-c "Regular User Account" newuser1
64 blocks

By default a user account just created without a password is in lock mode. To


manually lock a account put a * entry in the /etc/shadow file.

Note : By default Redhat linux UID’s start from 500 by default and are
incremented by 1, everytime a new user is added. In Suse Linux UID’s start
from 1000.

Set the password for the user you created by using the passwd command

# passwd user1

Note: If you want to change a password of a account you are logged into, just
execute the passwd command and press enter and you will be prompted to
enter a new password and confirm it.

12
Passwords are encrypted in the /etc/shadow file using the MD5.

You can find out all shells installed on the OS by using the cat /etc/shells
command and it will display all the shells that are supported by the
distribution.

When you create a account the entries are made into /etc/passwd file and
when you create a password for that user using the passwd user command,
entries are updated in /etc/shadow file.

You can use the stat passwd command to display information of the file, the
date it was modified etc.

13
Modifying a User Account

Use the usermod command to modify a user’s login account on the system.

The usermod Command Format and Options


The following is the command format for the usermod
command:
usermod [ -u uid [ -o ] ] [ -g gid ] [ -G gid [ ,
gid . . . ] ] [ -d dir ] [ -m ] [ -s shell ] [ -c
comment ]
[ -l newlogname] loginname

Key Options for the usermod Command

Option Definition
-o Allows a UID to be duplicated.
-m Moves the user’s home directory to the new location
specified with the -d option.
-l newlogname Changes a user’s login name for the
specified user account.
-f inactive Sets the number of inactive days that are
allowed on a user account. If the account is not logged in to
for the specified number of days, it is locked.
-e expire Sets an expiration date on the user account.
Specifies the date (mm/dd/yy) on which a user can no longer
log in and access the account. After that
date, the account is locked.
loginname Identifies the user’s login name for the current
user account.

The following example changes the login name and home


directory for newuser1 to usera.
# usermod -m -d /export/home/usera -l usera
newuser1

Deleting a user
14
userdel -r login

Note: Similar to the useradd, usermod and userdel command


are the 3 group commands, groupadd, groupmod and
groupdel.

Note: The useradd command writes shell/X window related


files while creation. The template for the shell files are in
the /etc/skel directory.

15
Basic File permissions

View file and directory permissions


Determine file or directory access
Change the permissions
Use the default permissions

r – read, w – write, x – execute and – means denied

The initial permissions for a file are rw-rw-rw-.The initial


permissions for a directory are rwxr-xr-x for root and
rwxrwxr-x

Use the ls -n command to view the UIDs and GIDs.


$ ls -n
total 108
-rw-r--r-- 1 11001 10 0 Feb 22 14:51 brands

16
HL UID GID Size

Understanding Permission Modes

The chmod command can modify permissions specified in


either symbolic mode or octal mode.

17
Symbolic mode uses combinations of letters and symbols to
add or remove permissions for each type of user.
Octal mode uses octal numbers to represent each
permission. Octal mode is also referred to as absolute mode.

The following examples show how to modify permissions on


files and directories using symbolic mode.

$ ls -l dante
-rw-r--r-- 1 user1 staff 1319 Mar 22 14:51 dante
$ chmod o-r dante
$ ls -l dante
-rw-r----- 1 user1 staff 1319 Mar 22 14:51 dante

+ add permissions
- remove permissions
= absolute permission

To remove the read permission for the group, enter the


following:
$ chmod g-r dante
To add an execute permission for the owner (user) and a
read permission for the group and other, enter the following:
$ chmod u+x,go+r dante
$ ls -l dante
-rwxr--r-- 1 user1 staff 1319 Mar 22 14:51 dante
There is no space in u+x,go+r.
To assign read and write permissions for owner, group, and
other, enter the following:
$ chmod a=rw dante
$ ls -l dante
-rw-rw-rw- 1 user1 staff 1319 Mar 22 14:51 dante

Changing Permissions in Octal


Mode

18
Octal Digits for Permission Sets

Combined Values & Perms

To give owner, group, and other read and execute access


only, enter the following:

$ ls -l dante
-rw-rw-rw- 1 user1 staff 1319 Mar 22 14:51 dante
$ chmod 555 dante
$ ls -l dante
-r-xr-xr-x 1 user1 staff 1319 Mar 22 14:51 dante

To change owner and group permissions to include write


access, enter the following:
$ chmod 775 dante
$ ls -l dante
-rwxrwxr-x 1 user1 staff 1319 Mar 22 14:51 dante

19
To change group permission to read and execute only, enter
the following:
$ chmod 755 dante
$ ls -l dante
-rwxr-xr-x 1 user1 staff 1319 Mar 22 14:51 dante

umask

Depending on the shell, the umask value appears as either 0022


(sh), 022 (ksh), or 22 (csh).

The umask utility affects the initial permissions for files and
directories when the files and directories are created. The
umask utility is a three-digit octal value that is associated
with the read, write, and execute permissions. The first digit
determines the default permissions for the owner, the second
digit determines the default permissions for the group, and
the third digit determines the default permissions for other.

The initial permission value specified by the system for a file


creation is
666 (rw-rw-rw-). (subtract 022 umask)
The initial permission value specified by the system for a
directory
creation is 777 (rwxrwxrwx). (subtract 022 umask)

For example, the initial permissions for a new file in symbolic


mode are:
rw-rw-rw-
This corresponds to read-write access for the owner, group,
and other.
This value is represented in octal mode as:
42-42-42- or 666
Use the default umask value of 022 to remove (or deny) write
permission
for group and other.
20
The result in octal mode is:
42-4--4-- or 644
A dash in the permission fields could be represented by the number 0
(zero).
Symbolic Mode Permission Fields

21
You can apply this same process to determine the default
permissions when you create new directories.

For directories, the initial value specified by the system is:


rwxrwxrwx

This corresponds to read, write, and execute access for the


owner, group, and other. This value is represented in octal
mode as:
421421421 or 777

Use the default umask value of 022 to remove (or deny) write
permission for group and other. The result in octal mode is:
4214-14-1 or 755

The result in symbolic mode is derived as shown in

Symbolic Mode Permission Field

You can change the umask value to a new value on the


command line. For example, to change the umask value to
027 and verify the new value, enter the following:
$ umask 027
$ umask
027

22
The new umask value affects only those files and directories
that are created from this point forward. However, if the user
logs out of the system, the new value (027) is replaced by
the old value (022) on subsequent logins because the umask
value was changed using the command line.

System Wide Umask set in /etc/bashrc for the bourne again shell
and /etc/csh.cshrc for the c shell.
User specific umask to be set in their respective profile
/home/abc/.bash_profile

23
#umask
Will display the default umask
#stat /tmp
Will display the directory information, including the default
umask the system took while creating the directory.

#which useradd
Will display the location of the useradd command

#rpm –qf /usr/sbin/useradd


shadow-utils-4.0.3-41.1

Will display which package does the useradd command


belong to.

#rpm –ql shadow-utils


Will display listing of all commands and other files the
shadow-utils program has installed.

24
Vi Editor

By default vi is installed on all redhat systems.

#rpm –qa | grep vim

vim is the enhanced version of vi editor. Though in times of


emergency the traditional vi editor is including with the
package vim-minimal

#which vi

Will display the enhaced vi editor which is a alias to the new


vim editor.

The traditional vi editor is located in /bin/vi

#cd /etc
#vi fstab

The above command will open the file in Normal mode.

You can use the arrow keys to move up, down, left & right or
You can use the j key to move down, k key to move up, l key
to move right and h key to move left.

To use help in vi, press ESC key, then type :help and press
the Enter key.

To quit from help mode press ESC key, and type :q and
press Enter.

To search for a keyword in vi type, ESC key, and type


forward slash /keyword and press Enter.

25
To continue searching for another instance of same
keyword, simply press the n key.

Press CTRL+g to view how many lines are there in the file
and in what position of line the cursor is in..

Press Shift + g to move to the end of file.

To move to a specific line number, simply press the number


eg 45 followed by Shift+g

To delete a character move the cursor to the character and


press the x key.

To undo your action, simply press the u key.

To insert text at a location, move your cursor to the desired


location and press the i key and you will be in insert mode
and can type the text you want.

To delete a line, key the cursor anywhere in the line and


press dd.

To delete more than one line, take your cursor to the first line
from where you wish to delete and press 4dd and it will
delete the 4 lines from the specified location.

To replace a character anywhere, take your cursor to the


desired location of the character which you want to replace
and press the character key on the keyboard you wish to
replace it with.

To delete a word, navigate to the word you wish to delete


and press the dw key.

26
To delete the rest of the line from a specific point, take your
cursor to the desired location and press d$.

If you have changed a entire line and would wish to undo,


press the capital U.

To save changes, press ESC key to leave normal mode and


enter into command mode and press :wq ie write and quit.

To quit without saving, press q!.

Search (Wraped around at end of file):


Search STRING forward : / STRING.
Search STRING backward: ? STRING.

Repeat search: n
Repeat search in opposite direction: N (SHIFT-n)

Replace: Replace OLD with NEW:

First occurrence on current line: :s/OLD/NEW

Globally (all) on current line: :s/OLD/NEW/g

Between two lines #,#:


:#,#s/OLD/NEW/g

Every occurrence in file: :


%s/OLD/NEW/g

Useful Commands

[abdullah@linuxhost install ]#cd $OLDPWD


[abdullah@linuxhost install ]#pwd
[abdullah@linuxhost install ]# set | grep -i pwd
OLDPWD=/home

27
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME
%%.*}:${PWD/#$HOME/~}\007"'
PWD=/root

grep –i (ignores case)

28
GRUB – Grand Unified Boot Loader

GRUB is a GNU boot loader and is capable of loading most operating


systems. RHEL creates 2 entries for booting in grub. There is a entry for the
smp kernel (symmetric multiprocessing) multiprocessor kernel if you have
more than one processor and a uniprocessor kernel for a system with a single
processor.

Its always a good idea to maintain multiple boot entries in the GRUB menu
so that you can boot into the various kernels incase of difficulty.

When the system is powered on the bios detects the necessary hardware
including the RAM. Once the hardware detection process is complete, the
next step is to hand over the boot process to the default hard disk on the
system and to the first sector of that particular hard disk, i.e. the first 512
bytes, the magical area on the hard disk, referred to as the MBR (Master
Boot Record) and it fits perfectly in the boot sector.

Immediately after the grub stage 1 process calls stage 1.5 and entrance stage.
Stage 1.5 provides support for various file systems. We will see that
information later on when the system is up, where that information is stored.
Stage 1.5 has knowledge, the know how and support for various file systems
including ext3, ext2, iso9660 i.e. CDROM etc. Stage 1.5 then invokes stage 2
and stage 2 will eventually hand over the boot process to the actual Linux
kernel vmlinuz residing in /boot.

Working with grub

Press p at the grub screen and we enter into the grub configuration mode.

Pressing e will take us to a next screen showing the 2 kernels which were to
be loaded, if it was to be selected.

Note: Pressing ESC will take you back to the main menu.

29
30
Notice the hd0,0. The hd0 is the first hard disk in the system. This parameter
is passed from the bios of the system to grub. Whatever the bios believes to
be the bootable hard disk will be displayed as hd0. But this does not mean
that grub is restricted to booting from the first hard disk only. Grub is
capable of booting from other hard drives. But the menu item root would be
needed to modified to reflect the altered configuration. Note that the root in
the first entry doesn’t mean its looking into the / root file system of Linux.
Instead it looks for the /boot file system where the kernel is located.

The 2nd zero following the hd0, is the first partition. Recall while installing
RHEL, the /boot filesystem was added to the first partition. The next
directive in the grub list is the kernel. This instructs grub where to find the
kernel.

The kernel version shown in the grub is 2.6.9-5.EL and its mounted in read
only(ro) mode and root label is the forward slash / indicating that this is the
root file system.

The next entry is the initial ram disk initrd-2.6.9-5.EL.img

Grub hands over or attempts to boot the system and loads the kernel in read
only mode as well as the initial ram disk. The initial ram disk really provides
access to memory (RAM). It functions initially in read only mode for the
loading of drivers that are needed for accessing the real /(root) file system.
The idea here for modularity, so that kernel can be of small size and consult
modules during boot time or at run time for providing support for hardware
that are attached to the system such as network card etc.

Once the file system is mounted in rw mode the kernel gets rid of the initial
ram disk that was loading during the boot process.

31
To edit parameters move the cursor using the arrow keys to the entry and
press e. The screen below shows editing of the boot device from hd0,0 to
hd1,0, which means that grub should look into the 2nd hard disk

Note: Grub at this stage has access to the file system. It has the driver to load
ext2 filesystem. The entry for booting another linux kernel can be added or
the current entry modified to load another kernel which exists on the
filesystem.

To modify the kernel, such as the smp kernel to run in single user mode
move the cursor to the entry in grub and press the a key.

The above screen shows adding single to the booting option which passes the
parameter single to grub, which starts the system in a single user mode.
Another way to start linux in single user mode is to type init=1 instead of
single and the system will come up in single user mode, which is init 1.

To work in the command line mode of grub, at the main screen type c, the
grub command line interface will come up. Type help and press enter to see
help related to all commands in the command line mode.

32
In the command line mode tab completion of commands is support. Type re
and press tab and grub will display the information about the available
commands. Type reb and press tab key and grub will complete the command
to reboot.

Running RHEL with CD in rescue mode

1. Reboot the system if the system is running using the reboot command.
The reboot command really runs init with the 6th run level.
2. Boot the system with the RHEL CD1 which is used to install. On the
installation screen press F5 key to enter into rescue mode.
3. Once you see the rescue screen, simply type linux rescue.
4. Once the system runs in rescue mode a linux kernel is booted from the
CD-ROM
5. The kernel will load device drivers, network interface etc and will
prompt to mount the hard disk to /mnt/sysimage, this is the location of
the root file system of the hard drive which needs to be repaired or
modified, etc incase a administrator needs to.
6. Finally the system will display a shell prompt(bourne shell) and the
root file system will now be mounted into /mnt/sysimage for repairing.

33
File System Management Tools

Ext3 is a modern day file system which includes journaling. Ext 3 is the
successor to the Ext2 file system. Because ext3 provides journaling which
provides fast recovery of the operating system in the advent of an unexpected
or unanticipated power failure.
/boot – This partition should be about 100 Mbytes.

To alter the disk partitions in Linux use the fdisk or the parted utility.
Another tool an administrator can use is the system-config-lvm tool to
configure logical volumes.

Note: Do not make changes to a mounted partition. Start the system in single
user mode, backup the data if any and then make changes to active partitions
if necessary.

The default tool which gives you a quick snapshot of various partitions/slices
on your system is called df.

[root@server grub]# which df


/bin/df
[root@server grub]# rpm -qf /bin/df
coreutils-5.2.1-31

When you run df without options it returns the amount of 1k blocks that are
available, the amount used, the amount available, which reflects the
difference between total blocks used.

[root@server grub]# df
Filesystem 1K-blocks Used Available
Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
4483148 2525068 1730344 60% /
/dev/sda1 101086 9132 86735
10% /boot

34
none 128020 0 128020 0%
/dev/shm

df –h option will return the outpout in human readable format i.e. KB, MB,
GB etc.

The /etc/fstab file is the location of mappings between mount point and the
device location.

[root@server grub]# cat /etc/fstab


# This file is edited by fstab-sync - see 'man
fstab-sync' for details
/dev/VolGroup00/LogVol00 /
ext3 defaults 1 1
LABEL=/boot /boot
ext3 defaults 1 2
none /dev/pts
devpts gid=5,mode=620 0 0
none /dev/shm
tmpfs defaults 0 0
none /proc
proc defaults 0 0
none /sys
sysfs defaults 0 0
/dev/VolGroup00/LogVol01 swap
swap defaults 0 0
/dev/hdc /media/cdrecorder
auto
pamconsole,fscontext=system_u:object_r:removable_t,
exec,noauto,managed 0 0
/dev/fd0 /media/floppy
auto
pamconsole,fscontext=system_u:object_r:removable_t,
exec,noauto,managed 0 0

The first column in the /etc/fstab file indicates the device location, the second
column generally locates the mount locations.
35
Fdisk utility

Running the fdisk utility with the –l option lists the currently attached and
mounted disks/partitions

[root@server ~]# fdisk -l /dev/sda

Disk /dev/sda: 5368 MB, 5368709120 bytes


255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks


Id System
/dev/sda1 * 1 13 104391
83 Linux
/dev/sda2 14 652 5132767+
8e Linux LVM

Executing fdisk /dev/sda will allow you to create, edit, delete, print partition
table of the SCSI disk 1, represented as /dev/sda.

[root@server ~]# fdisk /dev/sda

Command (m for help): m


Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
36
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help):

Press l to see the list of supported file systems supported by the Linux
system. A p will print the current partition table.

37
[root@server ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition
table, nor Sun, SGI or OSF disklabel Building a new
DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of
course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4


will be corrected by w(rite)

Command (m for help): n


Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130,
default 130): +1024M

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes


255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks


Id System
/dev/sdb1 1 125 1004031
5 Extended

Command (m for help):

38
In the above example a Scsi disk 2 was loaded in fdisk and a new extended
partition was created. Inside a extended partition logical partitions can be
created. The example below shows created logical partitions in extended
partition

[root@server ~]# fdisk /dev/sdb

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes


255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks


Id System
/dev/sdb1 1 130 1044224+
5 Extended

Command (m for help): n


Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130,
default 130): +500M

Command (m for help): n


Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (63-130, default 63):
Using default value 63
Last cylinder or +size or +sizeM or +sizeK (63-130,
default 130):
39
Using default value 130

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes


255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks


Id System
/dev/sdb1 1 130 1044224+
5 Extended
/dev/sdb5 1 62 497983
83 Linux
/dev/sdb6 63 130 546178+
83 Linux

Command (m for help):

The above example shows we created 2 logical partitions in the extended


partition of our drive sdb.

Deleting a partition

[root@server ~]# fdisk /dev/sdb

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes


255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks


Id System
/dev/sdb1 1 130 1044224+
5 Extended

40
/dev/sdb5 1 62 497983
83 Linux
/dev/sdb6 63 130 546178+
83 Linux

Command (m for help): d


Partition number (1-6): 5

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes


255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks


Id System
/dev/sdb1 1 130 1044224+
5 Extended
/dev/sdb5 63 130 546178+
83 Linux

Command (m for help):

Note: We deleted partition 5, but partition 5 is still listed. What fdisk does is
once it deletes a lower numbered partition; the higher numbered partition is
decremented, in the above case partition 6 automatically decremented to 5
once partition 5 was deleted.

Now that our partition is created, we can list the partition

[root@server ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 1073 MB, 1073741824 bytes


255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

41
Device Boot Start End Blocks
Id System
/dev/sdb1 1 130 1044224+
5 Extended
/dev/sdb5 1 62 497983
83 Linux
/dev/sdb6 63 130 546178+
83 Linux
[root@server ~]#

To check which file systems are supported by the kernel type

[root@server ~]# cat /proc/filesystems


nodev sysfs
nodev rootfs
nodev bdev
nodev proc
nodev sockfs
nodev binfmt_misc
nodev usbfs
nodev usbdevfs
nodev futexfs
nodev tmpfs
nodev pipefs
nodev eventpollfs
nodev devpts
ext2
nodev ramfs
nodev hugetlbfs
iso9660
nodev mqueue
nodev selinuxfs
ext3
nodev rpc_pipefs
nodev autofs
[root@server ~]#

42
The file /proc/filesystems houses the filesystem that are supported by the
kernel.

43
The mke2fs utility is used to format and create filesystems to be mounted
and used.

[root@server ~]# which mke2fs


/sbin/mke2fs
[root@server ~]# rpm -qf /sbin/mke2fs
e2fsprogs-1.35-11.6.EL4
[root@server ~]# rpm -ql e2fsprogs
/lib/evms
/lib/evms/libe2fsim.1.2.1.so
/lib/libblkid.so.1
/lib/libblkid.so.1.0
/lib/libcom_err.so.2
/lib/libcom_err.so.2.1
/lib/libe2p.so.2
/lib/libe2p.so.2.3
/lib/libext2fs.so.2
/lib/libext2fs.so.2.4
/lib/libss.so.2
/lib/libss.so.2.0
/lib/libuuid.so.1
/lib/libuuid.so.1.2
/sbin/badblocks
/sbin/blkid
/sbin/debugfs
/sbin/dumpe2fs
/sbin/e2fsck
/sbin/e2image
/sbin/e2label
/sbin/findfs
/sbin/fsck
/sbin/fsck.ext2
/sbin/fsck.ext3
/sbin/logsave
/sbin/mke2fs
/sbin/mkfs.ext2
/sbin/mkfs.ext3
44
/sbin/tune2fs
/usr/bin/chattr
/usr/bin/lsattr
/usr/bin/uuidgen
/usr/sbin/ext2online
/usr/sbin/filefrag
/usr/sbin/mklost+found
(Output truncated).

mke2fs utility creates the ext3 filesystems, which incorporate journaling.

45
To create the ext2 filesystem, execute the following command

[root@server ~]# mke2fs /dev/sdb5


mke2fs 1.35 (28-Feb-2004)
max_blocks 509931520, rsv_groups = 62248, rsv_gdb =
256
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
124928 inodes, 497980 blocks
24899 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
61 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801,
221185, 401409

Writing inode tables: done


inode.i_blocks = 4610, i_size = 67383296
Writing superblocks and filesystem accounting
information: done

This filesystem will be automatically checked every


24 mounts or
180 days, whichever comes first. Use tune2fs -c or
-i to override.

Note: When executing mke2fs without any options, the default set of options
are applied. The above command created ext2 filesystem which is the
default.

46
47
Finally once the filesystem is ready to be used, a mount point needs to be
created. To check which slices on hard disks are available on your system
exectute

[root@server /]# mount /dev/hdb


for IDE hard disk
[root@server /]# mount /dev/sdb
for SCSI hard disk, where a is hard disk 2.

[root@server /]# mkdir backup


[root@server /]# mount -t ext2 /dev/sdb5 /backup
[root@server /]# mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
none on /proc type proc (rw)
none on /sys type sysfs (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
usbfs on /proc/bus/usb type usbfs (rw)
/dev/sda1 on /boot type ext3 (rw)
none on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc
(rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs
(rw)
/dev/sdb5 on /backup type ext2 (rw)

The above procedure created a temporary mount point ie /backup and


mounted the filesystem /dev/sdb5 to it. If no options are given with mount
command, the mount command is smart enough to detect whether the
filesystem to be mounted is ext2 or ext3 filesystem.

[root@server /]# df -h
Filesystem Size Used Avail Use% Mounted
on
/dev/mapper/VolGroup00-LogVol00
4.3G 2.5G 1.7G 60% /
/dev/sda1 99M 9.0M 85M 10% /boot
48
none 126M 0 126M 0%
/dev/shm
/dev/sdb5 471M 2.3M 445M 1% /backup

To create a ext3 journal based filesystem

[root@server /]# mke2fs -j /dev/sdb6


mke2fs 1.35 (28-Feb-2004)
max_blocks 139821056, rsv_groups = 4267, rsv_gdb =
33
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
68320 inodes, 136544 blocks
6827 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=142606336
5 block groups
32768 blocks per group, 32768 fragments per group
13664 inodes per group
Superblock backups stored on blocks:
32768, 98304

Writing inode tables: done


inode.i_blocks = 800, i_size = 4243456
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting
information: done

This filesystem will be automatically checked every


27 mounts or
180 days, whichever comes first. Use tune2fs -c or
-i to override.

[root@server /]# mkdir /backupext3

49
[root@server /]# mount -t ext3 /dev/sdb6
/backupext3
[root@server /]# mount | grep sdb
/dev/sdb5 on /backup type ext2 (rw)
/dev/sdb6 on /backupext3 type ext3 (rw)

Note: Our first partition is ext2 and the second is ext3 journal based
filesystem.

Converting a ext2 filesystem to ext3 filesystem

Note: While creating a filesystem with the mke2fs, we can execute –t ext3
option to create a ext3 filesystem which supports journaling.

To convert an existing ext2 filesystem to ext3 filesystem use the tune2fs


utility.

Unmount the ext2 /backup filesystem using the umount command

[root@server /]# umount /backup


[root@server /]# tune2fs -j /dev/sdb5
tune2fs 1.35 (28-Feb-2004)
Creating journal inode: done
This filesystem will be automatically checked every
24 mounts or
180 days, whichever comes first. Use tune2fs -c or
-i to override.

[root@server /]# mount /dev/sdb5 /backup


[root@server /]# mount | grep sdb
/dev/sdb6 on /backupext3 type ext3 (rw)
/dev/sdb5 on /backup type ext3 (rw)

Both the filesystems are now ext3 journal based filesystem.

50
Note: Converting a mounted ext2 filesystem to an ext3 journal filesystem
will convert the filesystem, but the mount command will yet display the
status of the filesystem as ext2 filesystem, until the filesystem is remounted.

Over head of ext3 filesystem as compared to ext2 filesystem

[root@server /]# mount /dev/sdb5 /backup


[root@server /]# mount /dev/sdb6 /backupext3
[root@server /]# mount | grep sdb
/dev/sdb5 on /backup type ext2 (rw)
/dev/sdb6 on /backupext3 type ext3 (rw)
[root@server /]# df -h | grep sdb
/dev/sdb5 471M 2.3M 445M 1% /backup
/dev/sdb6 471M 11M 437M 3%
/backupext3

The above example shows 2 newly created ext2 and ext3 filesystem,
where /backup is ext2 and /backupext3 is ext3 journaling filesystem. Both
the partitions were created to 500 MB and then were formatted with mke2fs
and mounted. The ext3 filesystem shows a 3% usage which is actually being
used for journaling, which means a little overhead using ext3.

The difference between creating a journal based filesystem and converting a


filesystem using tune2fs is that, the tune2fs adds a journal file to the
directory mount point of the partition.

[root@server backupext3]# ls -lah


total 21K
drwxr-xr-x 3 root root 1.0K Dec 11 20:54 .
drwxr-xr-x 26 root root 4.0K Dec 11 20:02 ..
drwx------ 2 root root 12K Dec 11 20:54
lost+found

The backupext3 directory is the mount point for /dev/sdb6, which was
created with the –j option, creating a journal based ext3 file system. Below
tune2fs –j is used to modify a ext2 filesystem to a journal based ext3

51
filesystem. Once the conversion process is complete a .journal file is added
to the directory which is the mount point for /dev/sdb5.

[root@server backupext3]# tune2fs -j /dev/sdb5


tune2fs 1.35 (28-Feb-2004)
Creating journal inode: done
This filesystem will be automatically checked every
39 mounts or
180 days, whichever comes first. Use tune2fs -c or
-i to override.
[root@server backupext3]# umount /backup
[root@server backupext3]# mount /dev/sdb5 /backup
[root@server backupext3]# cd /backup
[root@server backup]# ls -lah
total 8.1M
drwxr-xr-x 3 root root 1.0K Dec 11 21:04 .
drwxr-xr-x 26 root root 4.0K Dec 11 20:02 ..
-rw------- 1 root root 8.0M Dec 11 21:04 .journal
drwx------ 2 root root 12K Dec 11 20:54
lost+found

Note: Modifying size of filesystems and remounting it, might make mount
command to report size which was mounted earlier. To make change reflect,
reboot the system and recheck with mount command.

Mounting filesystems automatically when the system is rebooted

In the previous example we mounted a partition /dev/sdb5 to backup


directory. The /backup directory is a temporary mount point and once the
system is rebooted the /dev/sdb5 will not be mounted. To automatically
mount the /dev/sdb5 during every system reboot, we need to edit the
/etc/fstab filesystem table file. The 3 programs that make use of the /etc/fstab
file are mount, fsck and umount.

The /etc/fstab is the default repository to mount filesystems when the


systems comes up.

52
[root@server ~]# cat /etc/fstab
# This file is edited by fstab-sync - see 'man
fstab-sync' for details
s file is edited by fstab-sync - see 'man fstab-
sync' for details
/dev/VolGroup00/LogVol00 /
ext3 defaults 1 1
LABEL=/boot /boot
ext3 defaults 1 2
none /dev/pts
devpts gid=5,mode=620 0 0
none /dev/shm
tmpfs defaults 0 0
none /proc
proc defaults 0 0
none /sys
sysfs defaults 0 0
/dev/VolGroup00/LogVol01 swap
swap defaults 0 0

/dev/hdc /media/cdrecorder
auto
pamconsole,fscontext=system_u:object_r:removable_t,
exec,noauto,managed 0 0
/dev/fd0 /media/floppy
auto
pamconsole,fscontext=system_u:object_r:removable_t,
exec,noauto,managed 0 0

The following describes the content of the fstab file:

1. Anything specified with the # sign in the fstab file is a comment


2. The first field in the file specifies the device, generally the block
device. There are some special descriptors such as LABEL=/boot that
are recognized by the system. But usually in the 1st field you will
specify the location within the /dev directory, such as /dev/sdb5. But

53
the kernel recognizes the LABEL=/boot as such, it is an alias to
wherever the /boot filesystem is.
3. The 2nd field in the file specifies the mount point of the filesystem.
4. The 3rd field contains the type of filesystem.
5. The 4th field refers to the permissions on the filesystem. Incase of
defaults it defaults to read writable mount, it allows all users to interact
with the filesystem. But generally it is read write permission.
6. The 5th field specified whether the filesystem should be dumped.
7. The 6th field specifies.

A detailed explanation of the fields is given below.

1st and 2nd columns: Device and default mount point

The first and second columns should be pretty straightforward. They tell the
mount command exactly the same things that you tell mount when you
mount stuff manually: what is the device or partition, and what is the mount
point. The mount point specified for a device in /etc/fstab is its default
mount point. That is the directory where the device will be mounted if you
don't specify any other mount point when mounting the device.

Most Linux distribution create special directories for mount points. Most
distribution create them under /mnt, but some (RHEL)) under /media. As
you probably noticed when looking at the example fstab, I have used
RHEL AS 4’s mount points as an example.

What does all this mean? If I type the following command:


# mount /dev/fd0
... my floppy will be mounted in /media/floppy, because that's the
default mount point specified in /etc/fstab. If there is no entry for
/dev/fd0 in my fstab when I issue the command above, mount gets
very confused because it doesn't know where to mount the floppy.

You can freely change the default mount points listed in /etc/fstab if
you're not satisfied with the defaults your Linux distribution has given you.
Just make sure the mount point is a directory that already exists on your
system. If it doesn't, simply create it.
54
Some partitions and devices are also automatically mounted when your
Linux system boots up. For example, have a look at the example below.
There are lines that look like this many a times:
/dev/hda2 / ext2 defaults 1 1
/dev/hdb1 /home ext2 defaults 1 2

These lines mean that /dev/hda2 will be mounted to / and /dev/hdb1 to


/home. This is done automatically when your Linux system boots up... if it
wouldn't, you'd have a hard time using your cool Linux system because all
the programs you use are in / and you wouldn't be able to run them if / wasn't
mounted! But how does the system know where you want to mount
/dev/hda2 and /dev/hdb1? By looking at the /etc/fstab file of
course.

3rd column: Filesystem type

The third column in /etc/fstab specifies the filesystem type of the


device or partition. Many different filesystems are supported but we'll take
look at the most common ones only.

ext2 and ext3 Very likely your Linux partitions are Ext3. Ext2 used to be the
standard filesystem for Linux, but these days, Ext3 and ReiserFS are usually
the default filesystems for almost every new Linux distribution. Ext3 is a
newer filesystem type that differs from Ext2 in that it's journaled, meaning
that if you turn the computer off without properly shutting down, you
shouldn't lose any data and your system shouldn't spend ages doing
filesystem checks the next time you boot up.

reiserfs Your Linux partitions may very well be formatted as ReiserFS. Like
Ext3, ReiserFS is a journaled filesystem, but it's much more advanced than
Ext3. Many Linux distributions (including SuSE) have started using
ReiserFS as their default filesystem for Linux partitions.

swap The filesystem name is self-explanatory. The filesystem type "swap" is


used in your swap partitions.

55
vfat and ntfs Your Windows partitions are probably either Vfat or NTFS.
The 9x series (95, 98, ME) all use Vfat (more widely known as FAT32), and
the NT series (NT, 2000, XP) use NTFS. In 2000 and XP you can choose the
filesystem type, so 2000 and XP partitions may be formatted as Vfat, too. If
you want to be able to write to your Windows partitions from Linux, I
suggest formatting them as Vfat, because Linux's support for writing to
NTFS partitions is a bit shabby at this moment, though it does and might
work.

auto No, this isn't a filesystem type :-) The option "auto" simply means that
the filesystem type is detected automatically. If you take a look at the
example fstab above, you'll see that the floppy and CD-ROM both have
"auto" as their filesystem type. Why? Their filesystem type may vary. One
floppy might be formatted for Windows and the other for Linux's Ext2.
That's why it's wise to let the system automatically detect the filesystem type
of media such as floppies and cdroms.

4th column: Mount options

The fourth column in fstab lists all the mount options for the device or
partition. This is also the most confusing column in the fstab file, but
knowing what some of the most common options mean, saves you from a big
headache. Yes, there are many options available, but I'll take a look at the
most widely used ones only. For more information, check out the man page
of mount.

auto and noauto With the auto option, the device will be mounted
automatically (at bootup, just like I told you a bit earlier, or when you issue
the mount -a command). auto is the default option. If you don't want the
device to be mounted automatically, use the noauto option in
/etc/fstab. With noauto, the device can be mounted only explicitly.

user and nouser These are very useful options. The user option allows
normal users to mount the device, whereas nouser lets only the root to
mount the device. nouser is the default, which is a major cause of
headache for new Linux users. If you're not able to mount your cdrom,

56
floppy, Windows partition, or something else as a normal user, add the user
option into /etc/fstab.

exec and noexec exec lets you execute binaries that are on that partition,
whereas noexec doesn't let you do that. noexec might be useful for a
partition that contains binaries you don't want to execute on your system, or
that can't even be executed on your system. This might be the case of a
Windows partition.

exec is the default option, which is a good thing. Imagine what would
happen if you accidentally used the noexec option with your Linux root
partition...

ro Mount the filesystem read-only.

rw Mount the filesystem read-write. Again, using this option might cure the
headache of many new Linux users who are tearing their hair off because
they can't write to their floppies, Windows partitions, or something else.

sync and async How the input and output to the filesystem should be done.
sync means it's done synchronously. If you look at the example fstab,
you'll notice that this is the option used with the floppy. In plain English, this
means that when you, for example, copy a file to the floppy, the changes are
physically written to the floppy at the same time you issue the copy
command.

However, if you have the async option in /etc/fstab, input and output
is done asynchronously. Now when you copy a file to the floppy, the changes
may be physically written to it long time after issuing the command. This
isn't bad, and may sometimes be favorable, but can cause some nasty
accidents: if you just remove the floppy without unmounting it first, the
copied file may not physically exist on the floppy yet!

async is the default. However, it may be wise to use sync with the floppy,
especially if you're used to the way it's done in Windows and have a
tendency to remove floppies before unmounting them first.

57
defaults Uses the default options that are rw, suid, dev, exec, auto, nouser,
and async.

5th and 6th columns: Dump and fsck options

Dump? Well, dump is a backup utility and fsck is a filesystem check utility. I
won't discuss them in great length here, but I'll mention them, because
otherwise you'd spend the rest of the day wondering what on God's green
Earth do these things mean.

The 5th column in /etc/fstab is the dump option. Dump checks it and
uses the number to decide if a filesystem should be backed up. If it's zero,
dump will ignore that filesystem. If you take a look at the example fstab,
you'll notice that the 5th column is zero in most cases.

The 6th column is a fsck option. fsck looks at the number in the 6th column
to determine in which order the filesystems should be checked. If it's zero,
fsck won't check the filesystem.

Example /etc/fstab entries

As an example, we'll take a look at a couple of fstab entries that have been
a source of endless frustration for new Linux users: floppy and CD-ROM
(although these days floppies aren't that important anymore).
/dev/fd0 /media/floppy auto rw,noauto,user,sync 0 0

This line means that the floppy is mounted to /media/floppy by default


and that its filesystem type is detected automatically. This is useful because
the type of the floppy may wary. Note especially the rw and user options:
they must be there if you want to be able to mount and write to the floppy as
a normal user. If you have trouble with this, check your fstab file to see if
these options are there. Also note the sync option. It can be async just as
well, but it's sync because of reasons discussed a bit earlier.
/dev/cdrom /media/cdrom auto ro,noauto,user,exec 0
0

58
Note, again, the user option that enables you to mount the CD as a normal
user. The CD-ROM has the ro option because it's no use mounting a CD-
ROM read-write because you wouldn't be able to write to it anyway. Also
note the exec option. It's especially useful if you'd like to be able to execute
something from your CD.

Also note that the noauto option is used with the floppy and CD-ROM. This
means that they won't be automatically mounted when your Linux system
boots up. This is useful for removable media, because sometimes there won't
be any floppy or CD-ROM when you boot up your system, so there isn't any
reason to try to mount something that doesn't even exist.

What is /dev/shm and its practical usage

/dev/shm is nothing but implementation of traditional shared memory


concept. It is an efficient means of passing data between programs. One
program will create a memory portion, which other processes (if permitted)
can access. This will result into speeding up things on Linux.

If you type mount command you will see /dev/shm as a tempfs file system.
Therefore, it is a file system, which keeps all files in virtual memory.
Everything in tmpfs is temporary in the sense that no files will be created on
your hard drive. If you unmount a tmpfs instance, everything stored therein is
lost. By default almost all distribution configured to use /dev/shm.

Nevertheless, where can I use /dev/shm?


You can use /dev/shm to improve the performance of application software or
overall Linux system performance. On heavily loaded system, it can make
tons of difference. For example VMware workstation/server can be
optimized to improve your Linux host’s performance (i.e. improve the
performance of your virtual machines).

For example, if you have 8GB RAM then remount /dev/shm as follows:
# mount -o remount,size=8G /dev/shm

To be frank if you have more than 2GB RAM and if you running multiple
Virtual machines this hack always improves performance.

59
To add our backup filesystem to vfstab add the following lines:

/dev/sdb5 /backup ext3


defaults 0 2

Mounting Floppy Disks and Removable Media

To read floppy disks and other removable media, you need to mount the
device. If RHEL detects your hardware, it adds the proper settings to
/etc/fstab and default mount points to the /mnt directory. The default mount
points are straightforward; for example, floppy disks are mounted on
/mnt/floppy, CDs are mounted on /mnt/cdrom. If you're mounting an MS-
DOS-formatted floppy, you can do so with the following command:
# mount -t vfat /dev/fd0 /mnt/floppy

The -t switch specifies the type of filesystem (vfat). The device file /dev/fd0
represents the first floppy drive; /mnt/floppy is the directory through which
you can access the files on the floppy after mounting.

But the command that you actually need may be simpler. Take a look at your
/etc/fstab configuration file. You'll probably see something like the following
line:
/dev/fd0 /mnt/floppy auto
noauto,owner,kudzu 0 0

This /etc/fstab line sets the default configuration for the first floppy drive
(/dev/fd0). Normally, it's mounted on the /mnt/floppy directory. The format
is auto, which means that the mount command searches through the
/etc/filesystems configuration file. As long as vfat is part of this file, you do
not need to specify the filesystem type. The mount command reads the
filesystem on the floppy and mounts it with the correct filesystem
automatically. So all you really need to mount the floppy is the following
command:
# mount /dev/fd0 /mnt/floppy

60
But wait-if you know something about /etc/fstab, you know that the device
and mount points are effectively linked. If you specify either, mount looks
through /etc/fstab for the appropriate device, label, or mount point.
Therefore, all you actually need to mount the floppy is either one of the
following two commands:
# mount /dev/fd0

# mount /mnt/floppy

Similarly, the device for your CD-ROM is normally /dev/cdrom. To mount


an ISO 9660 CD-ROM, run the following command:
# mount -rt iso9660 /dev/cdrom /mnt/cdrom

Now you can read the contents of /mnt/cdrom as if it were a normal


filesystem on your system. You don't have to use the /mnt/floppy or
/mnt/cdrom directories. They're part of the standard Linux filesystem as a
matter of convenience. You can mount a floppy or CD on an empty directory
of your choice.

But as with a floppy disk, the actual command that you need is simpler. Take
a look at the relevant default line from /etc/fstab:
/dev/cdrom /mnt/cdrom udf,iso9660
noauto,owner,kudzu,ro 0 0

As you can see, iso9660 is already specified as the default CD filesystem,


and CDs are mounted as read-only (ro) by default. Therefore, all you actually
need to mount the CD is either one of the following two commands:
# mount /dev/cdrom
# mount /mnt/cdrom

To unmount a floppy or CD-ROM, use the umount command with the


mount point as an argument. Remember, the command is umount, not
unmount.

The following commands unmount both our floppy and our CD-ROM:
61
# umount /mnt/floppy
# umount /mnt/cdrom

It is important you unmount floppy disks before removing them. Otherwise,


the data that you thought you wrote to the disk might still be in the cache. In
that case, you would lose that data.

The Automounter

Once you run the mount command on a partition, it stays mounted until you
unmount it or shut down or reboot your computer. This can cause problems.
For example, if you've mounted a floppy and then physically removed the
disk, Linux may not have had a chance to write the file to the disk. This
situation also applies to Zip or other 'hot-swap' removable drives.

Another example is with mounted NFS directories. If the remote computer


fails or the connection is lost, your system may become slow or even hang as
it looks for the mounted directory.

This is where the Automounter can help. It relies on the autofs daemon to
mount configured directories as needed, on a temporary basis. In RHEL, the
relevant configuration files are /etc/auto.master and /etc/auto.misc. If you use
the Automounter, keep the /misc directory free. Red Hat configures
automounts on this directory by default, and they won't work if there are
local files or directories stored there.

/etc/auto.master

The standard /etc/auto.master file includes just a series of comments, with


one sample command:
# /misc /etc/auto.misc --timeout=60

You can activate the Automounter by activating this command (or adding a
similar command). As you should already know as a Linux expert, the pound
sign (#) is a comment character. When you delete the #, the command is
activated. This command:

• Configures automounted filesystems on the /misc directory


62
• Points to /etc/auto.misc for detailed configuration
• Sets a timeout of 60 seconds; if the automounted directory is not used
or accessed in this time, it is automatically unmounted

You can set up the Automounter on other directories. One popular option is
to set up the Automounter on the /home directory. In this way, you can
configure user home directories on remote servers, mounted on demand.
Users are given access to their home directories upon login, and with the
following command in /etc/auto.master, the home directory is automatically
unmounted 60 seconds after that user logs off the system:
# /home /etc/auto.home --timeout=60

This works only if you don't have a /home directory on your computer.

/etc/auto.misc

Red Hat conveniently provides standard automount commands in comments


in the /etc/auto.misc file. It's helpful to analyze this file in detail. I use the
default RHEL 3 version of this file. The first four lines are comments, which
I skip. The first command is:
cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

In RHEL, this command is active by default. In other words, if you have a


CD in the /dev/cdrom drive, you can access its files through the Automounter
with the ls /misc/cd command. The Automounter accesses it using the
ISO9660 filesystem. It's mounted read-only (ro); set user ID permissions are
not allowed (nosuid), and devices on this filesystem are not used.

With the command from /etc/auto.master, the CD is unmounted 60 seconds


after the last time it's accessed. There are a number of other sample
commands in comments. The first command allows you to set up a
/misc/linux mount point from a shared NFS directory, /pub/linux, on the
ftp.example.org computer.
#linux -ro,soft,intr
ftp.example.org:/pub/linux

63
The next command assumes that the /boot directory is stored on the
/dev/hda1 partition. With this command, you don't need to mount /boot when
you start Linux. Instead, this command allows you to automount it with the
mount /misc/boot command.
#boot -fstype=ext2 :/dev/hda1

The following three commands apply to a floppy disk drive on your


computer. The first command searches through /etc/filesystems to try to
match what's on your floppy. The last two commands assumes that the
floppy is formatted to the Second Extended (ext2) filesystem.
#floppy -fstype=auto :/dev/fd0
#floppy -fstype=ext2 :/dev/fd0
#e2floppy -fstype=ext2 :/dev/fd0

The next command points to the first partition on the third SCSI drive. The
'jaz' at the beginning suggests this is suitable for an Iomega type 'Jazz' drive.
#jaz -fstype=ext2 :/dev/sdc1

Finally, the last command assumes that you want to apply the Automounter
to the IDE drive connected as the slave on the secondary controller. The
'removable' at the beginning suggests this is suitable for removable hard
drives.
#removable -fstype=ext2 :/dev/hdd

With the possible exception of the floppy commands, you'll probably need to
modify these lines before activating them for your own hardware.

Activating the Automounter

Once you've configured the /etc/auto.master and related /etc/auto.misc


configuration files, you can activate the Automounter. As it is governed by
the autofs daemon, you can activate it with the following command:
# service autofs restart

64
With the default command in the /etc/auto.misc file, you should now be able
to mount a CD on the /misc/cd directory, automatically, just by accessing the
configured directory. Once you have a CD in the drive, the following
command should work:
# ls /misc/cd

If you were to make /misc/cd your current directory, the Automounter would
ignore any timeouts. Otherwise, /misc/cd is automatically unmounted
according to the timeout, which according to the command in
/etc/auto.master, is 60 seconds.

65
Swap File System

Linux Swap Space

Linux uses the swap space configured on one or more hard drive partitions to
store infrequently used programs and data. Swap space can extend the
amount of effective RAM on your system. However, if you don't have
enough actual RAM, Linux may use the swap space on your hard drive as
virtual memory for currently running programs. Because hard drive access
can be 1/100,000th the speed of RAM, this can cause significant
performance problems.

But you can't just buy extra RAM and eliminate swap space. Linux moves
infrequently used programs and data to swap space even if you have
gigabytes of RAM.

Normally, Linux (on a 32-bit Intel-style computer) can use a maximum 4GB
of swap space, in partitions no larger than 2GB. This 4GB can be spread over
a maximum of eight partitions. The typical rule of thumb suggests that swap
space should be two to three times the amount of RAM. However, at larger
amounts of RAM, the amount of swap space that you need is debatable.

The way Red Hat assigns default swap space is based on the amount of RAM
on your system and the space available in your hard drive. Red Hat requires
at least 256MB of RAM on RHEL 4. If there's room available on your hard
drives, Anaconda configures a swap partition of at least 512MB. For Intel
32-bit systems, Red Hat requires a swap partition at least equal to the amount
of RAM on your system.

Red Hat RAM and swap space requirements vary if you're installing RHEL 4
on computers with non-Intel 32-bit CPUs.

In any case, you want to make the swap space you create as efficient as
possible. Swap partitions near the front of a hard disk, thus on a primary
partition, have faster access times. Swap partitions on different hard drives
attached to separate disk controllers gives Linux flexibility on where to send
swap data. Linux can start a program through one hard drive controller, and

66
move files to and from swap space on a separate hard drive controller
simultaneously.

To check your current swap space and its usage run the free command.

[root@server /]# free


total used free shared buffers
cached
Mem: 256044 252352 3692 0 13836
149268
-/+ buffers/cache: 89248 166796
Swap: 524280 0 524280

67
To create a swap filesystem, create a new partition for swap.

[root@server /]# fdisk /dev/sdb

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes


255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks


Id System
/dev/sdb1 1 130 1044224+
5 Extended

Command (m for help): n


Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130,
default 130): +512M

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes


255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks


Id System
/dev/sdb1 1 130 1044224+
5 Extended
/dev/sdb5 1 63 506015+
83 Linux
68
Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux
swap)

Command (m for help): w


The partition table has been altered!

Calling ioctl() to re-read partition table.


Syncing disks.

[root@server /]# locate mkswap


/usr/share/man/man8/mkswap.8.gz
/sbin/mkswap
[root@server /]# mkswap /dev/sdb5
Setting up swapspace version 1, size = 518152 kB

[root@server /]# swapon -s


Filename Type
Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition
524280 0 -1

The command swapon is used to enable the swap filesystem. However with
the –s option it can list the current swap filesystems being used by the
system. Swapon –a will enable all swap filesystems listed in the /etc/fstab
file.

[root@server /]# swapon /dev/sdb5


[root@server /]# swapon -s
Filename Type
Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition
524280 0 -1

69
/dev/sdb5 partition
506004 0 -2

To disable the swap filesystem execute

[root@server /]# swapoff /dev/sdb5 && echo $?


0

To create a mount of swap filesystems across systems reboot to be permanent


add a entry to the /etc/fstab file.

[root@server /]# cat /etc/fstab | grep swap


/dev/VolGroup00/LogVol01 swap
swap defaults 0 0
/dev/sdb5 swap
swap defaults 0 0
[root@server /]# swapon -a
[root@server /]# swapon -s
Filename Type
Size Used Priority
/dev/mapper/VolGroup00-LogVol01 partition
524280 0 -1
/dev/sdb5 partition
506004 0 -3

70
RAID Configuration and Data Recovery

A Redundant Array of Independent Disks (RAID) is a series of disks that can


save your data even if there is a catastrophic failure on one of the disks.
While some versions of RAID make complete copies of your data, others use
the so-called parity bit to allow your computer to rebuild the data on lost
disks.

Linux RAID has come a long way. A substantial number of hardware RAID
products support Linux, especially from name brand PC manufacturers.
Dedicated RAID hardware can ensure the integrity of your data even if there
is a catastrophic physical failure on one of the disks.

Depending on your definitions, RAID has nine or ten different levels, which
can accommodate different levels of data redundancy. Only three levels of
RAID are supported directly by RHEL: levels 0, 1, and 5. Hardware RAID
uses a RAID controller connected to an array of several hard disks. A driver
must be installed to be able to use the controller. Most RAID is hardware
based; when properly configured, the failure of one drive in a RAID 1 or
RAID 5 array does not destroy the data in the array. Linux, meanwhile,
offers a software solution to RAID. Once RAID is configured on a sufficient
number of partitions, Linux can use those partitions just as it would any other
block device. However, to ensure redundancy, it's up to you in real life to
make sure that each partition in a Linux software RAID array is configured
on a different physical hard disk.

Note: The RAID md device is a meta device. In other words, it is a


composite of two or more other devices such as /dev/hda1 and /dev/hdb1 that
might be components of a RAID array.

The following sections describe the basic RAID levels supported on Red Hat
Enterprise Linux.

71
RAID 0 (Stripping without parity, Zero Fault Tolerance)

This level of RAID makes it faster to read and write to the hard drives.
However, RAID 0 provides no data redundancy. It requires at least two hard
disks.

Reads and writes to the hard disks are done in parallel, in other words, to two
or more hard disks simultaneously. All hard drives in a RAID 0 array are
filled equally. But since RAID 0 does not provide data redundancy, a failure
of any one of the drives will result in total data loss. RAID 0 is also known as
'striping without parity.'

RAID 1 (Disk Mirroring)

This level of RAID mirrors information to two or more other disks. In other
words, the same set of information is written to two different hard disks. If
one disk is damaged or removed, you still have all of the data on the other
hard disk. The disadvantage of RAID 1 is that data has to be written twice,
which can reduce performance. You can come close to maintaining the same
level of performance if you also use separate hard disk controllers. That
prevents the hard disk controller from becoming a bottleneck.

And it is expensive. To support RAID 1, you need an additional hard disk for
every hard disk worth of data. RAID 1 is also known as disk mirroring.

RAID 4 (1 Disk Parity)

While this level of RAID is not directly supported by current Linux


distributions associated with Red Hat, it is still supported by the current
Linux kernel. RAID 4 requires three or more disks. As with RAID 0, data
reads and writes are done in parallel to all disks. One of the disks maintains
the parity information, which can be used to reconstruct the data. Reliability
is improved, but since parity information is updated with every write
operation, the parity disk can be a bottleneck on the system. RAID 4 is
known as disk striping with parity.

72
RAID 5 (Disk Stripping with Parity across the disks)

Like RAID 4, RAID 5 requires three or more disks. Unlike RAID 4, RAID 5
distributes, or 'stripes,' parity information evenly across all the disks. If one
disk fails, the data can be reconstructed from the parity data on the remaining
disks. RAID does not stop; all data is still available even after a single disk
failure. RAID level 5 is the preferred choice in most cases: the performance
is good, data integrity is ensured, and only one disk's worth of space is lost to
parity data. RAID 5 is also known as disk striping with parity.

Note: Hardware RAID systems should be 'hot-swappable.' In other words, if


one disk fails, the administrator can replace the failed disk while the server is
still running. The system will then automatically rebuild the data onto the
new disk. If you configure different partitions from the same physical disk
for a software RAID system, the resulting configuration can easily fail.
Alternatively, you may be able to set up 'spare disks' on your servers; RAID
may automatically rebuild data from a lost hard drive on properly configured
spare disks.

Visit the following URL for more information about RAID

http://www.pcguide.com/ref/hdd/perf/raid/levels/singleLevel0-c.html

RAID in Practice

RAID is associated with a substantial amount of data on a server. It's not


uncommon to have a couple dozen hard disks working together in a RAID
array. That much data can be rather valuable.

Creating RAID Arrays

During the Red Hat Installation and Configuration, it's generally easier to do
as much as possible during the installation process. If you're asked to create a
RAID array, it's easiest to do so with Disk Druid, which only works during
installation. You can create RAID arrays once RHEL is installed, but as
you'll see in the following instructions, it is more time consuming, and
involves a process that is more difficult to remember.

73
However, if you're required to create a RAID array during your exam and
forget to create it during the installation process, not all is lost. You can still
use the tools describe here to create and configure RAID arrays. And the
skills you learn here can serve you well through your career.

All four drives (hda, hdb, hdc, hdd) should be approximately the same size.

This first example shows how to mirror both the /home and the /var
directories (RAID 1) on Drive 2 and Drive 3, leaving Drive 4 as a spare.

You need to create nearly identically sized partitions on Drives 2 and 3. In


this example, four disks are configured with four partitions of the same size.
If you use the Linux fdisk program, use the t command to toggle the drive ID
type. You can then set the partition to type fd, which corresponds to the
Linux raid autodetect filesystem. You'll get to test it out for yourself shortly
in an exercise, as well as a lab at the end of this chapter.

In the partition table of the first drive is /dev/hda3 (currently mounted as


/home) and /dev/hda4 (currently mounted as /var). The second drive includes
74
/dev/hdb3 and /dev/hdb4. The third drive is set up with /dev/hdc3 and
/dev/hdc4, while the last drive has /dev/hdd3 and /dev/hdd4. All of these
partitions have been marked with partition IDs of type 0xFD.

Note: If you need to create a raidtab configuration file, it may be faster to


start with one of the sample raidtab configuration files. There are several
available in the following directory: /usr/share/doc/raidtools-1.00.3.

Next, update the configuration file /etc/raidtab. As shown in the following


code, you'll see two different RAID 1 arrays (/dev/md0 and /dev/md1):
raiddev /dev/md0
raid-level 1

nr-raid-disks 2
nr-spare-disks 1
persistent-superblock 1
chunk-size 4

device /dev/hdb3
raid-disk 0
device /dev/hdc3
raid-disk 1
device /dev/hdd3
spare-disk 0

raiddev /dev/md1
raid-level 1
nr-raid-disks 3
nr-spare-disks 1
persistent-superblock 1
chunk-size 4
device /dev/hdb4
raid-disk 0
device /dev/hdc4
raid-disk 1
device /dev/hdd4

75
spare-disk 0

The table below shows what some of the commands are, along with a brief
description of what they do. If you haven't already done so, it's time to
format these partitions and convert them to the default ext3 filesystem.

Commands in raidtab
Command Description
nr-raid-disks Number of RAID disks to use
nr-spare-disks Number of spare disks to use
persistent-superblock Required for autodetection
chunk-size Amount of data to read/write
parity-algorithm How RAID 5 should use parity

Note: Take special note that raid-disks and spare-disks start counting at 0;
nr-raid-disks and nr-spare-disks are the correct number of drives. For
example, if nr-raid-disks = 3, then the raid-disks are 0, 1, and 2.

The Linux format command is mkfs; with the right switch, you can
automatically set it up to ext3 with a journal. For example, the following
command formats the /dev/hda4 partition:
# mkfs -j /dev/hda4

If the partitions in /etc/raidtab are new, repeat this command for all of those
other partitions.

Partitions from older Linux computers may be formatted to the ext2


filesystem, which is essentially the same as ext3 without a journal. You can
add journaling to an older partition with a command such as:
# tune2fs -j /dev/hda4

When a journal is added to the ext2 filesystem, it upgrades that partition to


the ext3 filesystem.

76
Note: There are advantages to the ext3 journaling filesystem. If your system
suffers a sudden power failure, it does not have to check every inode for file
data; the information is already available in the journal.

The aforementioned /etc/raidtab file includes two RAID devices, /dev/md0


and /dev/md1. To start RAID 1 on those devices, run the following
commands:
# mkraid -R /dev/md0
# mkraid -R /dev/md1

If it works, you'll see the result in the dynamic /proc/mdstat file. You can
now mount the device and format it with the appropriate mkfs command,
and finally mount it on the Linux directory of your choice. You can even set
it up to be automatically mounted through /etc/fstab.

Note: Yes, when you configure a RAID device, you're formatting the same
space twice. First, you format the partitions that make up the array. Once
you've made RAID devices such as /dev/md0, you can then format those
devices as if they were new partitions.

For a RAID 5 array on the /var partition (in order to preserve mail, print
spools, and log files), the /etc/raidtab file should be modified as follows:
raiddev /dev/md0
raid-level 5
nr-raid-disks 3
nr-spare-disks 1
persistent-superblock 1
chunksize 32

parity-algorithm right-symmetric
device /dev/hda4
raid-disk 0
device /dev/hdb4
raid-disk 1
device /dev/hdc4
raid-disk 2
77
device /dev/hdd4
spare-disk 0

Now you can run mkraid /dev/md0 to initialize this RAID 5 device. You
can then format and mount this RAID array on the Linux directory of your
choice.

Formatting the RAID Array

Now you can run the mkfs command to format each RAID array. It's fairly
simple; now that you've created arrays such as /dev/md0 and /dev/md1, you
can work with them as if they were any other hard drive partition. For
example, you can format these arrays to the ext3 filesystems with the
following commands:
# mkfs -j /dev/md0
# mkfs -j /dev/md1

The process is straightforward-for example, if you wanted to mount the


/home/mj directory on the first RAID array, you'd run the following
commands (assume the /hometmp directory exists):
# cp -r /home/mj /hometmp
# mount /dev/md0 /home/mj
# cp -r /hometmp /home/mj

Setting up RAID on a critical set of files such as a /boot directory partition is


a bit trickier. Because of the importance of this data, manually copy the
contents of the /boot directory (as well as the boot loader file, /etc/grub.conf
or /etc/lilo.conf) to a different physical drive.

Implementing the RAID Array

But that's not the last step. You may not get full credit for your work on the
exam unless the directory gets mounted on the RAID array when you reboot
your Linux computer. Based on a standard RHEL 3 /etc/fstab configuration
file, you might add the following line to that file:

78
LABEL=/home/mj /home/mj ext3 defaults 1
2

Before this line can work, you'll need to set the label for this directory with
the following command:
# e2label /dev/md0 /home/mj

Mirror the /home Partition with Software RAID

Don't do this exercise on a production computer. If you have a computer with


Red Hat Enterprise Linux already installed with several different physical
hard drives that you can use for testing, that is best. One alternative is to use
virtual machine technology such as VMWare, which can allow you to set up
these exercises with minimal risk to a production system. You can also set up
several IDE and SCSI hard disks on a VMWare machine. When you're
ready, use the Linux fdisk techniques discussed earlier to configure the
following two-drive partition scheme:
Drive 1:
hda1 256 /
hda2 64 swap
hda3 500 /home
hda4 256 /var

Drive 2:
hdb1 1200 /usr
hdb2 64 swap
hdb3 100 /tmp
hdb4 500 (not allocated)

Now with the following steps, you can create a mirror of hda3, which stores
the /home directory, to the hdb4 partition. (The partition sizes do not have to
be identical.)

If you're making fdisk changes on a production computer, back up the data in


the /home partition first. Otherwise, all data on the current /dev/hda3 will be
lost.

79
1. Mark the two partition IDs as type FD using the Linux fdisk utility.
# fdisk /dev/hda
Command (m for help) : t
Partition number (1-4) 3
Partition ID (L to list options): FD
Command (m for help) : w
# fdisk /dev/hdb
Command (m for help) : t
Partition number (1-4) 4
Partition ID (L to list options): FD
Command (m for help) : w

2. Update the configuration file /etc/raidtab with these lines of code:


# vi /etc/raidtab
raiddev /dev/md0
raid-level 1

nr-raid-disks 2
nr-spare-disks 0
persistent-superblock 1
chunk-size 4
device /dev/hda3
raid-disk 0
device /dev/hdb4
raid-disk 1

3. Now make the RAID device file md0 and format it this way:
# mkraid -R /dev/md0
# mkfs -j /dev/md0

4. All that's left is to restore the files to the device and mount it.
5. However, you may not get full credit for your work unless your Linux
system mounts the directory on the RAID device. Make sure to do so in
the /etc/fstab configuration file. Run the e2label command as required

80
to make sure that the LABEL that you add to /etc/fstab is read properly
the next time you boot Linux.

81
Process Management

The ps command enumerates the processes running on your system. The ps


command is similar to the Task Manager in Windows OS, in that it lists the
running processes (programs).

[root@server ~]# ps
PID TTY TIME CMD
2980 pts/1 00:00:00 bash
2997 pts/1 00:00:00 ps
[root@server ~]# which ps
/bin/ps

As per existing standards required binaries are to exist in /bin directory and
ps is one such binary.

[root@server ~]# rpm -ql procps-3.2.3-7EL


/bin/ps
/lib/libproc-3.2.3.so
/sbin/sysctl
/usr/bin/free
/usr/bin/pgrep
/usr/bin/pkill
/usr/bin/pmap
/usr/bin/skill
/usr/bin/slabtop
/usr/bin/snice
/usr/bin/tload
/usr/bin/top
/usr/bin/uptime
/usr/bin/vmstat
/usr/bin/w
/usr/bin/watch
/usr/share/doc/procps-3.2.3
(Output filtered)

The ps with the –e option will list all the processing running on the system
82
[root@server ~]# ps -e
PID TTY TIME CMD
1 ? 00:00:00 init
2 ? 00:00:00 ksoftirqd/0
3 ? 00:00:00 events/0
4 ? 00:00:00 khelper
5 ? 00:00:00 kacpid
18 ? 00:00:00 kblockd/0
28 ? 00:00:00 pdflush
29 ? 00:00:00 pdflush
31 ? 00:00:00 aio/0
19 ? 00:00:00 khubd
30 ? 00:00:00 kswapd0
105 ? 00:00:00 kseriod
176 ? 00:00:00 scsi_eh_0
188 ? 00:00:00 kmirrord/0
197 ? 00:00:00 kjournald
1042 ? 00:00:00 udevd
The command ps –aux or ps aux (BSD Style) will list all process and their
ids and will provide detailed information about the processes running on the
system.

[root@server ~]# ps -aux


Warning: bad syntax, perhaps a bogus '-'? See
/usr/share/doc/procps-3.2.3/FAQ
USER PID %CPU %MEM VSZ RSS TTY STAT
START TIME COMMAND
root 1 0.0 0.2 1664 556 ? S
07:36 0:00 init [5]
root 2 0.0 0.0 0 0 ? SN
07:36 0:00 [ksoftirqd/0]
root 3 0.0 0.0 0 0 ? S<
07:36 0:00 [events/0]
root 4 0.0 0.0 0 0 ? S<
07:36 0:00 [khelper]

83
root 5 0.0 0.0 0 0 ? S<
07:36 0:00 [kacpid]
root 18 0.0 0.0 0 0 ? S<
07:36 0:00 [kblockd/0]
root 28 0.0 0.0 0 0 ? S
07:36 0:00 [pdflush]
root 29 0.0 0.0 0 0 ? S
07:36 0:00 [pdflush]
root 31 0.0 0.0 0 0 ? S<
07:36 0:00 [aio/0]
root 19 0.0 0.0 0 0 ? S
07:36 0:00 [khubd]
root 30 0.0 0.0 0 0 ? S
07:36 0:00 [kswapd0]
root 105 0.0 0.0 0 0 ? S
07:36 0:00 [kseriod]
root 176 0.0 0.0 0 0 ? S
07:36 0:00 [scsi_eh_0]
root 188 0.0 0.0 0 0 ? S<
07:36 0:00 [kmirrord/0]
root 197 0.0 0.0 0 0 ? S
07:36 0:00 [kjournald]
root 1042 0.0 0.1 2452 448 ? S<s
07:37 0:00 udevd
root 3022 0.8 1.1 5484 3040 pts/2 S
07:53 0:07 /usr/lib/vmware-tools/bin32/vmware
root 3087 0.0 0.3 2808 772 pts/1 R+
08:07 0:00 ps -aux

-a all w/ tty, including other users -t by tty


-u by effective user ID (supports names)
-x processes w/o controlling ttys

PROCESS STATE CODES

Here are the different values that the s, stat and state output specifiers (header
"STAT" or "S") will display to describe the state of a process.
84
D Uninterruptible sleep (usually IO)
R Running or runnable (on run queue)
S Interruptible sleep (waiting for an event to complete)
T Stopped, either by a job control signal or because it is being traced.
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z Defunct ("zombie") process, terminated but not reaped by its parent.

For BSD formats and when the stat keyword is used, additional characters
may be displayed:
< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group

Note: Use ps –aux | less command to display output one page at a time.

[root@server ~]# ps -aux | grep sshd


Warning: bad syntax, perhaps a bogus '-'? See
/usr/share/doc/procps-3.2.3/FAQ
root 2037 0.0 0.6 5892 1652 ? Ss
07:37 0:00 /usr/sbin/sshd
root 3152 0.0 0.1 2444 352 pts/1 R+
08:18 0:00 grep sshd

The above command filters the ps –aux and lists only the sshd process.

[root@server ~]# ps -aux | grep tty


Warning: bad syntax, perhaps a bogus '-'? See
/usr/share/doc/procps-3.2.3/FAQ
root 2199 0.0 0.1 3112 408 tty1 Ss+
07:37 0:00 /sbin/mingetty tty1
root 2201 0.0 0.1 2972 404 tty2 Ss+
07:37 0:00 /sbin/mingetty tty2

85
root 2202 0.0 0.1 2192 408 tty3 Ss+
07:37 0:00 /sbin/mingetty tty3
root 2203 0.0 0.1 2476 408 tty4 Ss+
07:37 0:00 /sbin/mingetty tty4
root 2452 0.0 0.1 1476 408 tty5 Ss+
07:37 0:00 /sbin/mingetty tty5
root 2525 0.0 0.1 2464 408 tty6 Ss+
07:37 0:00 /sbin/mingetty tty6
root 3154 0.0 0.2 5028 672 pts/1 S+
08:19 0:00 grep tty

The above command filters all processes and lists only tty (Tele Type
Terminals) processes.

[root@server ~]# ps -t tty1


PID TTY TIME CMD
2199 tty1 00:00:00 mingetty
[root@server ~]# ps -t tty2
PID TTY TIME CMD
2201 tty2 00:00:00 mingetty

The above command lists only -tty processes, -t option.

The who command also displays information about tty/pts terminals.

[root@server ~]# who


root :0 Dec 20 07:51
root pts/1 Dec 20 07:51 (:0.0)
root pts/2 Dec 20 07:52 (:0.0)
[root@server ~]# who -a
Dec 20 07:37
216 id=si term=0 exit=0
system boot Dec 20 07:37
run-level 5 Dec 20 07:37
last=S
Dec 20 07:37
1497 id=l5 term=0 exit=0
86
Dec 20 07:37
2199 id=1
Dec 20 07:37
2201 id=2
LOGIN tty3 Dec 20 07:37
2202 id=3
Dec 20 07:37
2203 id=4
Dec 20 07:37
2452 id=5
Dec 20 07:37
2525 id=6
Dec 20 07:37
2571 id=x
root ? :0 Dec 20 07:51 ?
2829
root + pts/1 Dec 20 07:51 .
2978 (:0.0)
root + pts/2 Dec 20 07:52 00:30
2978 (:0.0)

The pstree utility

[root@server ~]# which pstree


/usr/bin/pstree
[root@server ~]# rpm -qf /usr/bin/pstree
psmisc-21.4-4
[root@server ~]# rpm -ql psmisc-21.4-4
/sbin/fuser
/usr/bin/killall
/usr/bin/pstree
/usr/bin/pstree.x11
/usr/share/locale/de/LC_MESSAGES/psmisc.mo
/usr/share/locale/en/LC_MESSAGES/psmisc.mo
/usr/share/locale/fr/LC_MESSAGES/psmisc.mo
/usr/share/locale/it/LC_MESSAGES/psmisc.mo
87
/usr/share/locale/pt/LC_MESSAGES/psmisc.mo
/usr/share/locale/sv/LC_MESSAGES/psmisc.mo
/usr/share/man/man1/fuser.1.gz
/usr/share/man/man1/killall.1.gz
/usr/share/man/man1/pstree.1.gz

The pstree command displays the processes in a tree like view.

Process Selections

The free command lists the amount of total memory, used, free, shared,
buffers, cached and the swap usage.

[root@server ~]# free


total used free shared
buffers cached
Mem: 256044 247204 8840 0
15440 140916
-/+ buffers/cache: 90848 165196
Swap: 524280 0 524280
[root@server ~]#
88
The free command with the –m option returns the memory and swap in
megabytes.

[root@server ~]# free -m


total used free shared
buffers cached
Mem: 250 241 8 0
15 137
-/+ buffers/cache: 88 161
Swap: 511 0 511

Use the free command with the –m and –s (seconds) to update the memory
usage every 3 seconds.

[root@server ~]# free -m -s 3


total used free shared
buffers cached
Mem: 250 241 8 0
15 137
-/+ buffers/cache: 89 160
Swap: 511 0 511

total used free shared


buffers cached
Mem: 250 241 8 0
15 137
-/+ buffers/cache: 89 161
Swap: 511 0 511

Use the top command to display information about the processes running on
the system. The top command updates the information displayed every 3
seconds.

[root@server ~]# top

89
top - 09:02:30 up 1:25, 3 users, load average:
0.05, 0.11, 0.15
Tasks: 76 total, 1 running, 73 sleeping, 2
stopped, 0 zombie
Cpu(s): 6.0% us, 5.6% sy, 0.0% ni, 88.4% id,
0.0% wa, 0.0% hi, 0.0% si
Mem: 256044k total, 247976k used, 8068k
free, 15540k buffers
Swap: 524280k total, 0k used, 524280k
free, 140992k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM


TIME+ COMMAND
2675 root 15 0 38508 15m 5096 S 6.8 6.0
1:09.98 X
2978 root 15 0 38348 14m 8680 S 2.9 5.6
0:17.09 gnome-terminal
24855 root 16 0 2612 940 756 R 0.7 0.4
0:00.12 top
3022 root 15 0 5484 3056 2496 S 0.3 1.2
0:29.69 vmware-toolbox-
1 root 16 0 1664 556 480 S 0.0 0.2
0:00.86 init
2 root 34 19 0 0 0 S 0.0 0.0
0:00.12 ksoftirqd/0
3 root 5 -10 0 0 0 S 0.0 0.0
0:00.22 events/0
4 root 5 -10 0 0 0 S 0.0 0.0
0:00.02 khelper
5 root 15 -10 0 0 0 S 0.0 0.0
0:00.00 kacpid
18 root 5 -10 0 0 0 S 0.0 0.0
0:00.14 kblockd/0
28 root 20 0 0 0 0 S 0.0 0.0
0:00.00 pdflush
29 root 15 0 0 0 0 S 0.0 0.0
0:00.44 pdflush
90
31 root 8 -10 0 0 0 S 0.0 0.0
0:00.00 aio/0
19 root 15 0 0 0 0 S 0.0 0.0
0:00.00 khubd
30 root 15 0 0 0 0 S 0.0 0.0
0:00.12 kswapd0
105 root 25 0 0 0 0 S 0.0 0.0
0:00.00 kseriod
176 root 22 0 0 0 0 S 0.0 0.0
0:00.00 scsi_eh_0
188 root 6 -10 0 0 0 S 0.0 0.0
0:00.00 kmirrord/0
197 root 16 0 0 0 0 S 0.0 0.0
0:00.87 kjournald

Pressing SHIFT+f will bring up a menu to select more options in the top
command.

Note: zombie processes tend to be in deep sleep.

[root@server ~]# top --help


top: procps version 3.2.3
usage: top -hv | -bcisS -d delay -n iterations [-u
user | -U user] -p pid [,pid ...]

The top command with the –u username option will display usage of a
particular user.

[root@server ~]# top -u root

top - 09:20:53 up 1:44, 3 users, load average:


0.07, 0.05, 0.06
Tasks: 77 total, 1 running, 73 sleeping, 3
stopped, 0 zombie
Cpu(s): 2.8% us, 3.8% sy, 0.0% ni, 93.4% id,
0.0% wa, 0.0% hi, 0.0% si

91
Mem: 256044k total, 248564k used, 7480k
free, 15648k buffers
Swap: 524280k total, 0k used, 524280k
free, 141020k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM


TIME+ COMMAND
2675 root 15 0 38508 15m 5112 S 3.3 6.0
1:21.08 X
2978 root 15 0 38216 14m 8680 S 1.6 5.6
0:21.18 gnome-terminal
3022 root 15 0 5484 3056 2496 S 1.0 1.2
0:38.82 vmware-toolbox-
24886 root 17 0 3612 944 760 R 0.7 0.4
0:00.04 top
2189 root 16 0 6524 4004 1612 S 0.3 1.6
0:10.54 hald
2956 root 25 10 32156 18m 10m S 0.3 7.5
0:06.61 rhn-applet-gui
2972 root 15 0 21324 9224 6936 S 0.3 3.6
0:03.83 mixer_applet2
1 root 16 0 1664 556 480 S 0.0 0.2
0:00.93 init
2 root 34 19 0 0 0 S 0.0 0.0
0:00.12 ksoftirqd/0
3 root 5 -10 0 0 0 S 0.0 0.0
0:00.25 events/0
4 root 5 -10 0 0 0 S 0.0 0.0
0:00.02 khelper
5 root 15 -10 0 0 0 S 0.0 0.0
0:00.00 kacpid
18 root 5 -10 0 0 0 S 0.0 0.0
0:00.15 kblockd/0
28 root 20 0 0 0 0 S 0.0 0.0
0:00.00 pdflush
29 root 15 0 0 0 0 S 0.0 0.0
0:00.47 pdflush
92
31 root 8 -10 0 0 0 S 0.0 0.0
0:00.00 aio/0
19 root 15 0 0 0 0 S 0.0 0.0
0:00.00 khubd
30 root 15 0 0 0 0 S 0.0 0.0
0:00.12 kswapd0
105 root 25 0 0 0 0 S 0.0 0.0
0:00.00 kseriod
176 root 22 0 0 0 0 S 0.0 0.0
0:00.00 scsi_eh_0
188 root 6 -10 0 0 0 S 0.0 0.0
0:00.00 kmirrord/0
197 root 15 0 0 0 0 S 0.0 0.0
0:00.92 kjournald

Changing Priority (renice)

1. Open a new terminal and run the top command.


2. Open a new terminal and run the sequence command seq 1000000 >
seqmilrecord.txt
3. Go back to terminal one where you ran top and sort the process in order
of high cpu usage.
4. Press n to change priority of a process.
5. Enter the PID of the process in our case it was seq.
6. Enter new renice value to increase or decrease priority of the seq
command. Where -19 is the highest value where process gets a highest
priority and 20 is the lowest value, meaning the lowest priority.

Killing a Process

Press k in the top menu to kill a process and type in the PID of the process
you want to kill (shut).

The kill command.

93
The kill command is used to kill a process by sending signal to the process
using the Process ID(PID).

kill –l

Will list all signals supported by the kill command.

kill processid

Will kill the process whose id was given to kill.

Killall command

The killall command kills all the processes of the given process name.

#killall seq

Will kill all seq processes running in other shells (in background &).

Note: The information listed by ps, top etc is pulled from a filesystem /proc
which is mounted in memory. Below is a example of cpuinfo kept in the
/proc filesystem.

[root@server ~]# cat /proc/cpuinfo


processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 3
model name : Intel(R) Pentium(R) 4 CPU 2.40GHz
stepping : 8
cpu MHz : 2389.200
cache size : 1024 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
94
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce
cx8 apic mtrr pge mca cmov pat pse36 clflush dts
acpi mmx fxsr sse sse2 ss pni ds_cpl
bogomips : 4702.20

The uptime command displays information about the time since the system
has been up and running.

[root@server ~]# uptime


09:04:26 up 1:27, 3 users, load average: 0.02,
0.08, 0.14

The jobs command is used to display the jobs running in the background.
The fg (Foreground) commands is used to display foreground processes
running in the shell. Both are inbuilt inside the shell and are not separate
binary files.

95
Adding and Removing RPM Packages

The rpm command makes it easy to add and remove software packages to
your system. It maintains a database regarding the proper way to add,
upgrade, and remove packages. This makes it relatively simple to add and
remove software with a single command.

Install Mode

The install mode, as its name suggests, is used to install RPM packages on
your system. You can install an RPM package with the -i option. For
example, the following command installs the hypothetical penguin RPM:
# rpm -i penguin-3.4.5-26.i386.rpm

If the package is available on a remote FTP server, you could install it


directly from that server. For example, the following command installs it
from a hypothetical ftp.rpmdownloads.com server in its /pub directory.
# rpm -i ftp://ftp.rpmdownloads.com/pub/penguin-
3.4.5-26.i386.rpm

Before installing the package, RPM performs several checks. First, it makes
sure the package you're trying to install isn't already installed-normally, RPM
won't let you install a package on top of itself. It also checks to make sure
you aren't installing an older version of the package. Next, RPM does a
dependency check. Some programs won't work unless others are already
installed. In this example, you've just downloaded the latest RPM version of
the Penguin utilities, and you now want to install it.
# rpm -i penguin-3.4.5-26.i386.rpm
failed dependencies:
iceberg >>= 7.1 is needed by penguin-3.26.i386.rpm

This error tells you that rpm did not install the Penguin package because it
requires the iceberg RPM package, version 7.1 or later. You'll have to find
and install the iceberg package, and any packages iceberg may require.

96
Finally, RPM checks to see if it would overwrite any configuration files
when it installs a package. RPM tries to make intelligent decisions about
what to do in this situation. If RPM chooses to replace an existing
configuration file, it gives you a warning like:
# rpm -i penguin-3.26.i386.rpm
warning: /etc/someconfig saved as
/etc/someconfig.rpmsave

It's up to you to look at both files and determine what, if any, modifications
need to be made.

Note: If you've already customized a package and upgraded it with rpm, go


to the saved configuration file. Use it as a guide to change the settings in the
new configuration file. Since you may need to make different changes to the
new configuration file, you should test the result in every way that package
may be used in a production environment.

Upgrade Mode

The -U switch is used to upgrade existing packages. For example, if Penguin


utilities, version 3.25, is already installed, the following command:
# rpm -U penguin-3.26.i386.rpm

upgrades the old version of the package with the new one. In fact, if you've
never installed this package before, the -U switch works just like -i. The
package is simply installed for the first time.

Remove Mode

The rpm -e command removes a package from your system. But before
removing a package, RPM checks a few things first. It does a dependency
check to make sure no other packages need what you're trying to remove. If
it finds dependent packages, rpm -e fails with an error message identifying
these packages.
97
If you have modified any of the configuration files, RPM makes a copy of
the file, adds an .rpmsave extension to the end of the filename, and then
erases the original. Finally, after removing all files from your system and the
RPM database, it removes the package name from the database.

Note: Be very careful about which packages you remove from your system.
Like many other Linux utilities, RPM may silently let you shoot yourself in
the foot. For example, if you were to remove the packages for /etc/passwd or
the kernel, that would devastate your system.

Seeing What Packages Are Installed

Without RPM, you'd need to search around your filesystems to figure out
whether a particular software package is installed. RPM makes it easy for
you to figure out what RPM packages are installed and get information about
those packages.

Running RPM Queries

The -q switch is used to query packages. When you use RPM's query mode,
you can learn if a specific package is installed or identify the files associated
with that package. The rpm -q packagename command will return the
installed version of a specified package. For example, to find the version
number of an installed mutt text e-mail reader, run the following command:
# rpm -q mutt
lynx-1.4.1-3

If you want to see which installed package owns a file, use the -f modifier.
Here we want to identify the package that owns /etc/passwd:
# rpm -qf /etc/passwd
setup-2.5.27-1

Likewise, if you want to generate a list of files belonging to a certain


package, use the -l modifier.
# rpm -ql setup
/etc/bashrc
98
/etc/csh.cshrc
/etc/csh.login
/etc/exports
/etc/filesystems
/etc/group
/etc/gshadow
/etc/host.conf
/etc/hosts.allow
/etc/hosts.deny
/etc/inputrc
/etc/motd
/etc/passwd
/etc/printcap
/etc/profile
/etc/profile.d
/etc/protocols
/etc/securetty
/etc/services
/etc/shells
/usr/share/doc/setup-2.5.27
/usr/share/doc/setup-2.5.27/uidgid
/var/log/lastlog

One of the most common modifiers to -q is -a, a query for all installed
packages on your system. As configured, my RHEL 3 system includes 700
packages. Here's a truncated output:
# rpm -qa
ghostscript-fonts-5.50-9
libmng-1.0.4-3
libtiff-3.5.7-13
ncurses-5.3-9.3
arts-1.1.3-2.2
...
commons-modeler-1.0-5
sendmail-cf-8.12.10-1

99
Note: It's common to use the rpm -qa command as a searchable database.
All you need to do is pipe the output with a search term through grep. For
example, the rpm -qa | grep kde command makes it easy to identify all
installed packages related to the K Desktop Environment.

For even more information about a specific package, use the -i (information)
modifier. The table below lists some of the most important entries from the
package output. Run the rpm -qi packagename command on the RPM of
your choice and see the result for yourself.

Information from the rpm -qi packagename Command


Tag Description
Name The name of the package.
Version The version of the package.
Release The number of times this package has
been released using the same version
of the software.
Install Date When this package was installed on
your system.
Group Your RPM database is divided into
groups, which describe the
functionality of the software. Every
time you install a package, it will be
grouped accordingly.
Size The total size in bytes of all the files
in the package.
License The license under which the original
software was released.

100
The Red Hat Package Management Utility

You can update packages with a graphical tool. The Red Hat Package
Management utility includes several advantages. It includes an interface
similar to the Anaconda Custom Installation screen. It allows you to install
more than one package at a time. It automatically installs any other
dependent packages that may be needed.

There are two ways to start the Package Management utility. In the GNOME
or KDE desktop, click Main Menu | System Settings | Add/Remove
Applications. Alternatively, in a GUI text command window, run the
system-config-packages command. Either action opens the Package
Management utility shown in below.

You can use it to add the package groups of your choice. The list shown is
the same as what you saw during the installation process. Once you've
101
selected a package group, you're allowed to customize the installation by
clicking the associated Details link.

102

S-ar putea să vă placă și