You are on page 1of 50

INDEX

Sr. No. Name of Experiment Date Signature


1. Installation of Linux on
system dual boot with
Windows Operating system.

2. Familiarize with Unix/Linux


logging/logout and simple
commands.

3. Familiarize with vi editors.

4. Unix File System- Files and


Directories.

5. Managing users and groups in


Linux- adding, modifying and
password aging.

6. Create, remove and resize


various types of partitions
through GUI as well as
Command line.

7. Introduction to shell.

8. Using Bash shell develop


simple shell programs.

PRACTICAL-1
AIM: Install Linux on the system dual boot with the windows operating
system.

Hardware Requirements

• At least 4MB of RAM.

• Memory is speed, so if we have more RAM we'll thank us for it later. If we are a
power user, 8MB should be more than enough for most applications. If we want
to run XP Window, our system will require at least 8MB of RAM.

• A hard drive with space available for installing Linux. The amount of space
required depends on the amount of software we are installing and how much free
space we wish to leave for us. We can install Linux in very small amounts of disk
space, but a realistic minimum is about 150MB. The complete installation can use
up 250MB, with more useful for data files.

• A Hercules, CGA, EGA, VGA, or Super VGA video card and monitor. In
general, if our video card and monitor work under MS-DOS or Microsoft
Windows, then Linux should be able to use them without any problem.

PROCEDURE: -

 Preparing the Computer.


• Boot the system with the Red Hat Enterprise Linux CD (disk1) in the
CDROM drive.
• Enter the BIOS setup during boot.
• Set system’s boot order to CDROM, A, C.
• Save and Exit the BIOS setup.

 Installing Red Hat Enterprise Linux in Graphical mode.


• Boot up system using CD.
• Press Enter at the boot prompt.
• Choose the appropriate language (English).
• Press Enter at the OK prompt.
• Choose the appropriate keyboard (US).
• Press Enter at the OK prompt.
• Choose NFS image for the installation method.
• Configure TCP/IP. Select use dynamic IP configuration (BOOTP/DHCP)
• Press Enter on the OK prompt.
• Enter the appropriate information for an NFS installation :
NFS method:

NFS server name: 192.168.0.254

NFS mount point: /var/ftp/pub

• At this point Anaconda (the installer) will retrieve the necessary


installation image and probe the system for its monitor and mouse type and
finally present us with the welcome screen. Click Next.
• Choose the appropriate mouse for our system. Click Next.
• Manually partition our system using disk druid using the following
partitioning scheme (delete any pre-existing partitions):
/boot 100M

/ 400M

/usr 1256M

/swap 512M

/var 400M

• Format all partitions, but do not check for bad blocks.


• Use the default Boot loader settings and do not create a boot loader
password.
• Choose DHCP for networking and activate on boot
• Choose enable Firewall and allow ssh. Leave SELinux at the default state
Active.
• Select the appropriate language support.
• Set the time zone as appropriate for our location.
• Set the root password to redhat.
• Select Customize the set of packages to be installed and click Next.
• Unselect ALL selections except the X Window System and click Next.
• We should now be at the About to Install Screen .Click Next to begin.
• Track the progress of file system formatting by switching to tty5 (Ctrl–
Alt–F5 will take us there; Alt-F7 will return us to the installer).
• After the reboot following the installation, complete the initial setup tool,
do not register the machine with Red Hat Network. Select “Tell Me Why”
followed by “Remind Me Later”.
PRACTICAL-2

AIM: Familiarize with Unix/Linux logging/logout and simple


commands.

 whatis
 Display short description of commands.

 The whatis tool provides an easier way of getting quick.

 Explanation of what another command does


use a database that is updated nightly.

 --help
 display usage summary and argument list
 usage statement that describe the command’s
syntax
and list of options it accepts and what they do.

 man
 provides the documentation for commands
 almost every command has a man “page”
 pages are grouped into “chapters”
 collectively referred to as Linux manual

 info
 Similar to man, but often more in-depth
 Run info without args to list all pages
 Info pages are structured like a website
 Each pages is divided into “nodes”
 Links to nodes are preceded by “*”
 pwd
Displays the absolute path to the current directory
the current working directory is the directory in
which you are working.

 cd
cd changes directories to an absolute or relative
path

$ cd /home/Joshua/work

$ cd project/docs

 to move your home directory


$ cd

 to move to the parent of your current directory


$ cd . .

 to go in home directory of current user


$ cd~

 ls
 Lists the content of the current directory or a
specified directory
 ls without arguments lists files and directory
names in the current directory
 ls lists the contents of directory given as an
argument

 cp
 it coping files and directories

 mv
Move and/or rename files and directories
 touch
Create empty files or update file timestamps

 rm
Use for removing files

 mkdir
 makes a directory
 mkdir dirname

 rmdir
 Remove an empty directory

 cat
 Contents of the files are displayed
sequentially with no break
 Some useful options use with cat
-A –show all characters. Including control
characters and non printing characters
-b -number each (non-blank) line of output

 less
Viewing text page by page

Scroll with arrows/pg up/pg down

 history
Use to see list of “remembered” commands

 parameter/variables( $ )
Line substitute the value of the variable in a
command

 Pipe( | )
Pipe the standard output of one command in to the
standard input of another command

| command1 | command2

Pipes the standard output of command into the

standard input of command2

 chmod
 Use to change the access modes by symbolic method or
numeric method
Where mode is:

u, g or o for user, group and other

+ or - for grant or deny

r, w or x for read, write or execute

 Permissions are calculated by adding:


4(for read)

2(for write)

1(for execute)

 df
 reports total kilobytes, kilobytes used, kilobytes free
per file system
 -h displays sizes in easier to read units
 Du
 Reports kilobytes used per directory
also take –h option

 mount and un-mount


use to mount the device and removable media
before accessing media must be mounted
before removing media must be un-mounted
by default, non-root users may only mount certain
devices(CD, DVD, floppy, usb etc)
floppy must be manually mounted and un-mounted.
 tar

 Use to create archive

-c creates a new archive

-v produces verbose messages

-f archive name is name of new file

 Use to inspective archive

 PATH
 Colon separated list of locations where commands
can be found

 Which
 Command showing location in the PATH of an
executable

 su
Su means switch user .this command is use to change
identities

To change your password, run passwd

 head
Display few lines of text in a file

 tail
 Displays last few lines of text in a file
 Use –n or --lines to change no. of lines displayed
 Use –f option to continue to display the file in “real
time”

 wc
“word count” – also count lines and characters

$ wc story.txt

19 237 1901 story.txt

 sort
 sort text to stdout – original file unchanged
 common options

-r reverses sort to sort descending

-n numeric sort

-f ignores (fold) case of characters in strings

-u unique (remove duplicates lines in output)

-t use as a field separator

-k POS1 – sort from field POS1

 uniq
 remove successive duplicates lines in a file
 can use in conjunction with sort to remove all
duplicates

 cut
 displays specific columns of file data

-f specifies field or column

-d specifies field delimiter


 grep (General Regular
Expression Process)

 prints the lines of the files where a pattern is


matched
 also used as filter in pipelines
 sed
 stream editor
 read a file or stream of data ; writes out the data ,
performing search and replace instruction(s)
 multiple sed instructions

 find
find [directory…] [criteria…]

 searches directory trees in real-time

 all files are matched if no criteria given

 can execute commands on found files

 criteria are ANDed together by default


 can be OR’ed or negated with –o and –not
 find can match by inode timestamps

 ps
 display process information
 useful options:
-a processes by all user

-x processes by all terminal


-u show processes owner

 at
 Scheduling a process to execute later

 Command will be executed at the time indicated

 Command can be entered one per line, with the output


terminated with a <ctrl- d> on a line by itself

 The time arguments has many formats which are


illustrated for the following examples:
at 8:00pm December 7

at 7 am Thursday

at now + 5 minutes

at midbight + 23 minutes

 cron
 this is also used for schedule processes
 user need not be logged on
 cron jobs are listed in crontab file
 must edit and install your crontable file
 cron table file cannot be edited directly
 edit the file and then install with crontab or
edit the file through crontab

crontab file format

Entry consist of five space-delimited fields followed by


command line

Fields are minute, hour, day of month, month, and day of


week entry fields can be separated by any number of tab
or spaces. Valid field values are as follows:
minute 0 – 59

hour 0 – 23

day of month 1 – 31

month 1 – 12

day of week 0–6

 installing and
removing software

 Installed package options:

rpm –qa

 This command lists all installed packages

rpm –qf filename

 This command shows that particular file is part of


which package

rpm –ql package_name


PRACTICAL – 3

AIM: Familiarize with vi editors.

vi editor is similar to a text editor. For text editing, vi uses 24 of the 25 lines that are
normally available in a terminal. The three modes in which vi editor operates for sharing
the workload are explained as below:

1. Command Mode

The default mode of the editor where every key pressed is interpreted as a command to
run on text. You’ll have to be in this mode to copy and delete text. Unnecessary pressing
of [Esc] in this mode sound a beep but also confirms that you are in this mode.

2. Input Mode

Every key pressed after switching to this mode actually shows up as text. This mode is
invoked by pressing key i, a, I, A, o, O, r, R, s, and S. Pressing [Esc] in this mode takes vi
to command mode.

3. ex Mode or Last Line Mode

The mode used to handle files (like for saving) and perform substitution (replacing one
string with another). Pressing a : in the Command Mode invokes this mode. You then
enter an ex Mode command followed by [Enter]. After the command is run, you are back
to the default Command Mode.
Common vi Commands

Have a look at this list of common vi commands (there are many more, but these will at
least allow you to get some basic work done). Then we'll do one more exercise before
moving on.

Note: As with all of Linux, vi commands are case sensitive.

Positioning the Cursor

Move cursor one space right.

Move cursor one space left.

Move cursor up one line.

Move cursor down one line.

ctrl-F Move forward one screen.

ctrl-B Move backward one screen.

$ Move cursor to end of line.

^ Move cursor to beginning of line.

:1 Move to first line of file

:$ Move to last line of file

/ Search for a character string.

? Reverse search for a character string.

x Delete the character at the cursor position.

dd Delete the current line.

p Paste data that was cut with x or dd commands.

u Undo.

Entering Input Mode

a Add text after the cursor.


i Insert text before the cursor.

R Replace text starting at the cursor.

o Insert a new line after the current one.

Entering Command Mode

Esc Switch from Input mode to Command mode.

Exiting or Saving Your File

:w Write file to disk, without exiting editor.

ZZ Save the file and exit.

:q! Quit without saving.

Navigation

k Moves cursor up

j Moves cursor down

h Moves cursor left

l Moves cursor right

Editing Text

Dd Delete a line

yy Copy a line

p Moving text

J Joining lines
PRACTICAL-4

AIM: The Linux File System-Files and Directories

Files an Overview: -

The most basic concept of a file, and one we may already be familiar with from other
computer systems, defines a file as a distinct chunk of information that is found
on our hard drive. Distinct means that there can be many different files, each with
its own particular contents. To keep files from getting confused with each other,
every file must have a unique identity. In Linux, we identify each file by its name
and location. In each location or directory, there can be only one file by a
particular name. So, for instance, if we create a file called novel, and we get a
second great idea, we will either have to call it something different, such as
novel2, or put it in a different place, to keep from overwriting the contents already
in our original novel.

Common Types of Files

Files can contain various types of information. The following three types will become the
most familiar to us:

• User data: Information that we create and update. The very simplest user data is
plain text or numbers. We learn to create these simple files later. More
complicated user data files might have to be interpreted by another program to
make sense. For instance, a spreadsheet file looks like gibberish if we look at it
directly. To work with a spreadsheet, we have to start up the spreadsheet program
and read in the spreadsheet file.

• System data: Information, often in plain text form, that is read and used by the
Linux system - to keep track of which users are allowed on the system, for
instance. As a system administrator, we are responsible for changing system data
files. For instance, when we create a new user, we modify the file /etc/passwd,
which contains the user information. Ordinary users of the system are usually not
concerned with system data files, except for their private startup files.

• Executable files: These files contain instructions that our computer can perform.
This set of instructions is often called a program. When we tell the computer to
perform them, we're telling it to execute the instructions given to it. To human
eyes, executable files contain meaningless gibberish—obviously our computer
doesn't think the way we do! Creating or modifying executable files takes special
tools. We learn how to use these programming tools in Part V, "Linux for
Programmers."

Filenames

Linux allows filenames to be up to 256 characters long. These characters can be lower-
and uppercase letters, numbers, and other characters, usually the dash (-), the underscore
(_), and the dot (.).

They can't include reserved meta-characters such as the asterisk, question mark,
backslash, and space, because these all have meaning to the shell. We met some meta-
characters when we discussed wildcards.

Directories An Overview

Linux, like many other computer systems, organizes files in directories. We can think of
directories as file folders and their contents as the files. However, there is one
absolutely crucial difference between the Linux file system and an office filing
system. In the office, file folders usually don't contain other file folders. In Linux,
file folders can contain other file folders. In fact, there is no Linux "filing
cabinet"—just a huge file folder that holds some files and other folders. These
folders contain files and possibly other folders in turn, and so on.
Parent Directories and Subdirectories: -
Imagine a scenario in which we have a directory A, which contains another directory B.
Directory B is then a subdirectory of directory A, and directory A is the parent directory
of directory B.

The Root Directory: -


In Linux, the directory that holds all the other directories is called the root directory. This
is the ultimate parent directory; every other directory is some level of subdirectory.

From the root directory, the whole structure of directory upon directory springs and
grows like some electronic elm. This is called a tree structure because, from the single
root directory, directories and subdirectories branch off like tree limbs.

How Directories Are Named: -


Directories are named just like files, and they can contain upper- and lowercase letters,
numbers, and characters such as -, ., and _.

The slash (/) character is used to show files or directories within other directories. For
instance, usr/bin means that bin is found in the usr directory. Note that we can't tell, from
this example, whether bin is a file or a directory, although we know that usr must be a
directory because it holds another item—namely, bin. When we see usr/bin/grep, we
know that both usr and bin must be directories, but again, we can't be sure about grep.
The ls program shows directories with a following /—for example, fido/. This notation
implies that we could have, for instance, fido/file; therefore, fido must be a directory.

The root directory is shown simply by the symbol / rather than mentioned by name. It's
very easy to tell when / is used to separate directories and when it's used to signify the
root directory. If / has no name before it, it stands for the root directory. For example, /usr
means that the usr subdirectory is found in the root directory, and /usr/bin means that bin
is found in the usr directory and that usr is a subdirectory of the root directory.
Remember, by definition the root directory can't be a subdirectory.
The Home Directory: -
Linux provides each user with his or her own directory, called the home directory. Within
this home directory, users can store their own files and create subdirectories. Users
generally have complete control over what's found in their home directories. Because
there are usually no Linux system files or files belonging to other users in our home
directory, we can create, name, move, and delete files and directories as we see fit.

Navigating the Linux File System

Fortunately, navigating the Linux file system is simple. There are only two commands to
be learned, and one of them has absolutely no options or parameters!

The pwd Command Where Am I?


Type pwd at the Linux command prompt. We see

darkstar:~$ pwd

/home/fido

darkstar:~$

This tells you that we're currently in the directory /home/fido. (If we are logged in under
a different user name, we will see that name in place of fido.) This is our home directory.
When we log in, Linux always places us in our home directory.

The letters "pwd" stand for "print working directory." Again, a command's name or
function has been cut down to a few easy-to-type characters. (We will often see the term
current directory used in place of working directory.)

We might be wondering what "working directory" or "being in a directory" really means.


It simply means that Linux commands, by default, perform their actions in our working
directory. For instance, when we run ls, we are shown only the files in our working
directory. If we want to create or remove files, they will be created or removed in our
working directory.
Creating and Deleting Files: -
Linux has many ways to create and delete files. In fact, some of the ways are so easy to
perform that we have to be careful not to accidentally overwrite or erase files!
Return to the home directory by typing cd. Make sure we're in your /home/<user>
directory by running pwd.

We created a file by typing ls -l /bin > test. Remember, the > symbol means "redirect all
output to the following filename." Note that the file test didn't exist before we typed this
command. When we redirect to a file, Linux automatically creates the file if it doesn't
already exist.

What if we want to type text into a file, rather than some command's output? The quick
and dirty way is to use the command cat.

Creating Directories: -
To create a new directory, use the mkdir command. The syntax is mkdir <name>, where
<name> is replaced by whatever we want the directory to be called. This creates a
subdirectory with the specified name in our current directory:

darkstar:~$ ls

anotherfile newfile thirdfile

darkstar:~$ mkdir newdir

darkstar:~$ ls

anotherfile newdir/ newfile thirdfile

The mkdir command is already familiar to us if we have used MS-DOS systems. In MS-
DOS, we can abbreviate mkdir as md. We might think that md would work in Linux,
because, after all, most of the commands we've seen have extremely concise names.
However, Linux doesn't recognize md; it insists on the full mkdir.
If we frequently switch between Linux and MS-DOS, we might want to use mkdir for
both systems. However, be warned that we might start typing other Linux commands in
MS-DOS—for example, typing ls instead of dir!

Removing Files and Directories: -

Now that we know how to create files and directories, it's time to learn how to undo our
handiwork.

To remove (or delete) a file, use the rm command (rm is a very terse spelling of remove).
The syntax is rm <filename>. For instance:

darkstar:~$ rm dead_duck

removes the file dead_duck from your current directory.

darkstar:~$ rm /tmp/dead_duck

removes the file dead_duck from the /tmp directory.

darkstar:~$ rm *

removes all files from your current directory. (Be careful when using wildcards!)

darkstar:~$ rm /tmp/*duck

removes all files ending in duck from the /tmp directory.

Removing Directories: -
The command normally used to remove (delete) directories is rmdir. The syntax is rmdir
<directory>.

Before we can remove a directory, it must be empty (the directory can't hold any files or
subdirectories). Otherwise, we see
rmdir: <directory>: Directory not empty

This is as close to a safety feature as we will see in Linux!


File Permissions and Ownership:

All Linux files and directories have ownership and permissions. We can change permissions, and
sometimes ownership, to provide greater or lesser access to our files and directories. File
permissions also determine whether a file can be executed as a command.

If you type ls -l or dir, we see entries that look like this:

-rw-r—r— 1 fido users 163 Dec 7 14:31 myfile

The -rw-r—r— represents the permissions for the file myfile. The file's ownership includes fido
as the owner and users as the group.

File and Directory Ownership: -


When we create a file, we are that file's owner. Being the file's owner gives us the privilege of
changing the file's permissions or ownership. Of course, once we change the ownership to
another user, we can't change the ownership or permissions anymore!

File owners are set up by the system during installation. Linux system files are owned by IDs
such as root, uucp, and bin. Do not change the ownership of these files.

Use the chown (change ownership) command to change ownership of a file. The syntax is chown
<owner> <filename>. In the following example, we change the ownership of the file myfile to
root:

darkstar:~$ ls -l myfile

-rw-r—r— 1 fido users 114 Dec 7 14:31 myfile

darkstar:~$ chown root myfile

darkstar:~$ ls -l myfile
-rw-r—r— 1 root users 114 Dec 7 14:31 myfile

To make any further changes to the file myfile, or to chown it back to fido, we must use su or log
in as root.

Files (and users) also belong to groups. Groups are a convenient way of providing access to files
for more than one user but not to every user on the system. For instance, users working on a
special project could all belong to the group project. Files used by the whole group would also
belong to the group project, giving those users special access. Groups normally are used in larger
installations. We may never need to worry about groups.

The chgrp command is used to change the group the file belongs to. It works just like chown.

File Permissions: -
Linux let us specify read, write, and execute permissions for each of the following: the owner,
the group, and "others" (everyone else).

read permission enables us to look at the file. In the case of a directory, it let us list the
directory's contents using ls.

write permission enables us to modify (or delete!) the file. In the case of a directory, we must
have write permission in order to create, move, or delete files in that directory.

execute permission enables you to execute the file by typing its name. With directories, execute
permission enables you to cd into them.

For a concrete example, let's look at myfile again:

-rw-r—r— 1 fido users 163 Dec 7 14:31 myfile

The first character of the permissions is -, which indicates that it's an ordinary file. If this were a
directory, the first character would be d. There are also some other, more exotic classes. These
are beyond the scope of this chapter.
The next nine characters are broken into three groups of three, giving permissions for owner,
group, and other. Each triplet gives read, write, and executes permissions, always in that order.
Permission to read is signified by an r in the first position, permission to write is shown by a w in
the second position, and permission to execute is shown by an x in the third position. If the
particular permission is absent, its space is filled by -.

In the case of myfile, the owner has rw-, which means read and write permissions. This file can't
be executed by typing myfile at the Linux prompt.

The group permissions are r—, which means that members of the group "users" (by default, all
ordinary users on the system) can read the file but not change it or execute it.

Likewise, the permissions for all others are r—: read-only.

File permissions are often given as a three-digit number—for instance, 751. It's important to
understand how the numbering system works, because these numbers are used to change a file's
permissions. Also, error messages that involve permissions use these numbers.

The first digit codes permissions for the owner, the second digit codes permissions for the group,
and the third digit codes permissions for other (everyone else).

The individual digits are encoded by summing up all the "allowed" permissions for that
particular user as follows:

read permission 4

write permission 2

execute permission 1

Therefore, a file permission of 751 means that the owner has read, write and execute permission
(4+2+1=7), the group has read and execute permission (4+1=5), and others have execute
permission (1).
If we play with the numbers, we quickly see that the permission digits can range between 0 and
7, and that for each digit in that range there's only one possible combination of read, write, and
execute permissions.

Changing File Permissions: -


To change file permissions, use the chmod (change [file] mode) command. The syntax is chmod
<specification> file.

There are two ways to write the permission specification. One is by using the numeric coding
system for permissions:

darkstar:~$ ls -l myfile

-rw-r—r— 1 fido users 114 Dec 7 14:31 myfile

darkstar:~$ chmod 345 myfile

darkstar:~$ ls -l myfile

—wxr—r-x 1 fido users 114 Dec 7 14:31 myfile

darkstar:~$ chmod 701 myfile

darkstar:~$ ls -l myfile

-rwx——x 1 root users 114 Dec 7 14:31 myfile

This method has the advantage of specifying the permissions in an absolute, rather than relative,
fashion. Also, it's easier to tell someone "Change permissions on the file to seven-five-five" than
to say "Change permissions on the file to read-write-execute, read-execute, read-execute."

We can also use letter codes to change the existing permissions. To specify which of the
permissions to change, type u (user), g (group), o (other), or a (all). This is followed by a + to
add permissions or a - to remove them. This in turn is followed by the permissions to be added or
removed. For example, to add execute permissions for the group and others, we would type

darkstar:~$ chmod go+r myfile

Other ways of using the symbolic file permissions are described in the chmod man page.

Changing Directory Permissions: -


We change directory permissions with chmod, exactly the same way as with files. Remember
that if a directory doesn't have executed permissions, we can't cd to it.

Any user who has write permission in a directory can delete files in that directory, whether or not
that user owns or has write privileges to those files. Most directories, therefore, have permissions
set to drwxr-xr-x. This ensures that only the directory's owner can create or delete files in that
directory. It is especially dangerous to give write permission to all users for directories!

Important Directories in the Linux File System: -


Most of the directories that hold Linux system files are "standard." Other UNIX systems will
have identical directories with similar contents. This section summarizes some of the
more important directories on our Linux system.

This is the root directory. It holds the actual Linux program, as well as subdirectories. Do not
clutter this directory with our files!

/home

This directory holds users' home directories. In other UNIX systems, this can be the /usr or /u
directory.
/bin

This directory holds many of the basic Linux programs. bin stands for binaries, files that are
executable and that hold text only computers could understand.

/usr

This directory holds many other user-oriented directories. Some of the most important are
described in the following sections. Other directories found in /usr include

docs Various documents, including useful Linux information

man The man pages accessed by typing man <command>

games The fun stuff!

/usr/bin

This directory holds user-oriented Linux programs.

/var/spool

This directory has several subdirectories. mail holds mail files, spool holds files to be printed,
and uucp holds files copied between Linux machines.

/dev

Linux treats everything as a file! The /dev directory holds devices. These are special files that
serve as gateways to physical computer components. For instance, if we copy to /dev/fd0, we're
actually sending data to the system's floppy disk. Our terminal is one of the /dev/tty files.
Partitions on the hard drive are of the form /dev/hd0. Even the system's memory is a device!
A famous device is /dev/null. This is sometimes called the bit bucket. All information sent to
/dev/null vanishes—it's thrown into the trash.

/usr/sbin

This directory holds system administration files. If we do an ls -l, we see that we must be the
owner, root, to run these commands.

/sbin

This directory holds system files that are usually run automatically by the Linux system.

/etc

This directory and its subdirectories hold many of the Linux configuration files. These files are
usually text, and they can be edited to change the system's configuration (if we know what we're
doing!).
PRACTICAL-5

AIM: Managing Users and Groups in Linux- Adding, Modifying, Password


aging.

Linux is designed to serve many users at the same time as well as provide an interface between
the users and the computer with its storage media, such as hard disks and tapes .Users have their
own shells through which they interact with the operating system .As a system administrator, we
can manage user logins on our system .We can add or remove users, as well as add and remove
groups. A particular user can only access the files created by him. Whereas super user (i.e. root)
can access any file it wants to access. Super user has the unlimited access to all the files, devices,
and programs on the system. Each time when a user logs in, two profile scripts are executed. A
system profile script (located in /etc directory) is the same for every user, and each user has
the .bash_profile script in their home directory. As a super user, we can edit the profile script and
put in any commands we want to execute for each user when he logs in. Following is the list of
some directories related to the users.

Directory Description

/home Location of the user’s own home directory.

/etc/skel Holds the default initialization files for the login shell,such as .bash_profile,

.bashrc, and .bash_logout.

/etc/shells Holds the login shells, such as BASH or TCSH.

/etc/passwd Holds the password for a user.

/etc/group Holds the group to which the user belongs.

/etc/shadow Encrypted password file.

/etc/gshadow Encrypted password files for groups.

/etc/login.defs Default login definitions for users.


 User Management

To add, modify, and setting a password of a user following commands are used.

Adding a user: à useradd username options

Modifying a user: à usermod username options

Setting a Password: à passwd username.

When we add a user, an entry for that user is made in the /etc/passwd file, commonly known as
password file. Each entry takes up one line that has several fields separated by colons. The fields
are as follows:

Field Description

Username Login name of the user.

Password Encrypted password for the user’s account.

User ID Unique number assigned by the system.

Group ID Number used to identify the group to which the user belongs.

Comment Any user information, such as user’s full name.

Home directory The user’s home directory.

Login shell Shell to run when the user logs in; default shell is /bin/bash.

Options that we can include with the useradd and usermod commands are:

Option Description

-c str Adds a comment to the user’s entryin system password file: /etc/ passwd.

-d dir Set the home directory of the new user.


-D Displays defaults for all settings.

-e mm/dd/yy Set an expiration date for the account (None by default). Specify by
month/day/ year.

-f days Sets the number of days an account remains active after its password expires.

-g group Sets a group.

-G group Sets additional groups.

-m Creates user’s home directory if it does not exist.

-p password Supplies an encrypted password (crypt or MD5).

-s shell Sets the login shell of new user.

-u userid Sets the userid of the new user.

 Group Management

To create a new group or to modify the existing groups following commands are used.

Creating a Group: àgroupadd groupname options

Removing a Group: àgroupdel groupname options

Modifying a group: à groupmod groupname options

The system file that holds the group enteries is /etc/group.The file consists of group records with
one record per line and its fields separated by colons. A group record consists of following fields.

Field Description

Groupname Name of the group; name must be unique.

Password Encrypted password for the user’s account.

Group ID Number used by the system to identify the group.


Users List of the users belong to the group.

Options that we can include with the groupadd and groupmod commands are:

Option Description

-g gid Changes a group ID.

-n groupname Changes a group name.

-f Detect if group already exists.

-r Creates a system group, one lower than the group minimum specified in
login.defs.

Practical-6
AIM: Create, Remove, and Resize various types of partitions through
GUI as well as command line.

Partitioning is a means to divide a single hard drive into many logical drives. A partition is a
contiguous set of blocks on a drive that are treated as an independent disk. A partition table is an
index that relates sections of the hard drive to partitions.

Partition Types

A partition is labeled to host a certain kind of file system. Such a file system could be the Linux
standard ext2 file system or Linux swap space, or even foreign file systems like (Microsoft)
NTFS or (Sun) UFS. There is a numerical code associated with each partition type. For example,
the code for ext2 is 0x83 and Linux swap is 0x82.

 Foreign Partition Types

The partition type codes have been arbitrarily chosen (we can't figure out what they
should be) and they are particular to a given operating system. Therefore, it is
theoretically possible that if we use two operating systems with the same hard drive, the
same code might be used to designate two different partition types.

OS/2 marks its partitions with a 0x07 type and so does Windows NT's NTFS. MS-DOS
allocates several type codes for its various flavors of FAT file systems: 0x01, 0x04 and
0x06 are known. DR-DOS used 0x81 to indicate protected FAT partitions, creating a type
clash with Linux/ Minix at that time, but neither Linux/Minix nor DR-DOS are widely
used any more.

 Primary Partitions
The number of partitions on an Intel-based system was limited from the very beginning:
The original partition table was installed as part of the boot sector and held space for only
four partition entries. These partitions are now called primary partitions.

 Logical Partitions

One primary partition of a hard drive may be sub-partitioned. These are logical partitions.
This effectively allows us to skirt the historical four partition limitation.

The primary partition used to house the logical partitions is called an extended partition
and it has its own file system type (0x05). Unlike primary partitions, logical partitions
must be contiguous. Each logical partition contains a pointer to the next logical partition,
which implies that the number of logical partitions is unlimited. However, Linux imposes
limits on the total number of any type of partition on a drive, so this effectively limits the
number of logical partitions. This is at most 15 partitions total on an SCSI disk and 63
total on an IDE disk.

 Swap Partitions

Every process running on our computer is allocated a number of blocks of RAM. These
blocks are called pages. The set of in-memory pages which will be referenced by the
processor in the very near future is called a "working set." Linux tries to predict these
memory accesses (assuming that recently used pages will be used again in the near
future) and keeps these pages in RAM if possible.

If we have too many processes running on a machine, the kernel will try to free up RAM
by writing pages to disk. This is what swap space is for. It effectively increases the
amount of memory we have available. However, disk I/O is very slow compared to
reading from and writing to RAM.
Required Partitioning Software

fdisk - a command line tool for partitioning.

sfdisk - a command line-version of sfdisk.

Disk druid – GUI partition editor.

 Partitioning with fdisk in Command Prompt

fdisk utility is used to partition a hard disk of any capacity through command line. Linux
allows only 4 primary partitions and any one partition can be further subdivided to form
any number of logical partitions. Only one of the primary partitions can be sub-divided.

fdisk is started by typing (when logged in as root) fdisk device at the command prompt.
"device" might be like /dev/hda or /dev/sda. The basic fdisk commands we need are:

p – to print the partition table

n – To create a new partition

d – To delete a partition

q – To quit without saving changes

w– To write the new partition table and exit

Changes made to the partition table do not take effect until we issue the write (w)
command.

When a partition is created and changes made to the partition table are saved, then to
activate the partition without rebooting the system, use the command partprobe otherwise
it will be activated automatically after reboot.

After activating the created partition, it must be formatted. To format the partition type
the following commands and press enter.
(1.) mke2fs or mkfs.ext2 – if we want the ext2 file system.
(2.) mke2fs – j or mkfs.ext3 - if we want the ext3 file system (j is for journaling).

 Partitioning with Disk Druid in GUI

The partitioning tool used in Red Hat Linux is Disk Druid. With the exception of certain
esoteric situations, Disk Druid can handle the partitioning requirements for a typical Red
Hat Linux installation.
Partition Fields

Each line in the "Partitions" section represents a disk partition. Each line in this section has
five different fields:

Mount Point: A mount point is the location within the directory hierarchy at which a
volume exists. The volume is said to be mounted at this location. This
field indicates where the partition will be mounted. If a partition exists, but
is "not set" we need to define its mount point. Double-click on the
partition or use the Edit key.

Device: This field displays the partition's device name.

Requested: This field shows the partition's original size. To re-define the size, we must

delete the current partition and recreate it using the Add button.

Actual: This field shows the space currently allocated to the partition.

Type: This field shows the partition's type (such as Linux Native or DOS).
Drive Summaries

Each line in the Drive Summaries section represents a hard disk on our system. Each line
has the following fields:

Drive: This field shows the hard disk's device name.


Geom [C/H/S]: This field shows the hard disk's geometry. The geometry consists of three
numbers representing the number of cylinders, heads and sectors as
reported by the hard disk.

Total: This field shows the total available space on the hard disk.

Free: This field shows how much of the hard disk's space is still unallocated.

Used: These fields show how much of the hard disk's space is currently allocated to
partitions, in megabytes and percentage.
The Drive Summaries section is displayed only to indicate our computer's disk
configuration. It is not meant to be used as a means of specifying the target hard drive for
a given partition.
Adding Partitions:à To add a new partition, select the Add button. A dialog box will appear

• Mount Point: Highlight and enter the partition's mount point. For example, if this
• partition should be the root partition, enter /; enter /boot for the /boot
o Partition, and so on. We can also use the pull-down menu to choose the
o Correct mount point for our partition.

Size (Megs): Enter the size (in megabytes) of the partition. Note this field starts with a "1"
in it; unless changed we'll end up with a 1 MB partition.

Use remaining space: This check box indicates if the size we entered in the previous field
is to be considered the partition's exact size, or its minimum size.
When selected, the partition will grow to fill all available space on
the hard disk. The partition's size will expand and contract as other
partitions are modified. We can make multiple partitions growable;
if we do, the additional free space will be shared among all
growable partitions.

Partition Type: This field contains a list of different partition types (such as Linux Native
or DOS). Select the appropriate partition type by using the mouse.

Allowable Drives: This field contains a list of the hard disks installed on our system. If a
hard disk's box is highlighted, then a desired partition can be created on
that hard disk. If the box is not checked, then the partition will never be
created on that hard disk. By using different check box settings, we can
direct Disk Druid to place partitions as we see fit, or let Disk Druid
decide where partitions should go.

Ok: Select OK once you're satisfied with the settings, and wish to create the partition.

• Cancel: Select Cancel if we don't want to create the partition.

Removing Partitions: à To delete a partition, select the delete button after selecting the
partition we want to delete or make free. A dialog box will
appear that will confirm the deletion of the selected
partition. If OK is pressed the selected partition will be
removed and If Cancel is pressed the partitions will remain
as it is i.e. partition table will not be altered.

PRACTICAL-7
AIM: Introduction to shell.

Using bash: -
Shells in a Nutshell: -
What is a shell, anyway? It seems to be a word used all the time in Linux, but the exact meaning
is vague for many new users (and some veterans). This section explains exactly what a
shell program is and why it is so important when using Linux.

What Is a Shell?
The shell is a program used to interface between you (the user) and Linux (or, more accurately,
between you and the Linux kernel). Figure 10.1 illustrates the relationship between the user, the
shell, and the Linux kernel. Every command we type at a prompt on your screen is interpreted by
the shell, then passed to the Linux kernel.

The shell is a command-language interpreter. It has its own set of built-in shell commands. The
shell can also make use of all of the Linux utilities and application programs that are available on
the system.

Whenever you enter a command it is interpreted by the Linux shell. For example, when we were
introduced to the Linux file- and directory-manipulation commands, all of the sample commands
entered at the command prompt were interpreted by whichever Linux shell we were using.

Some of the commands, such as the print working directory (pwd) command, are built into the
Linux bash shell. Other commands, such as the copy command (cp) and the remove command
(rm), are separate executable programs that exist in one of the directories in the filesystem. As
the user, we don't know (or probably care) if the command is built into the shell or is a separate
program.

The shell tries to find these application programs by looking in all of the directories that are in
your search path. The path is a list of directories where executable programs can be found. If the
command that was entered is not an internal shell command and it is not an executable file in
your path, an error message will be displayed.

As the last step in a successful command, the shell's internal commands and all of the application
programs are eventually broken down into system calls and passed to the Linux kernel.

Another important aspect of the shell is that it contains a very powerful interpretive
programming language. This language is similar in function to the MS-DOS interpreted
language, but is much more powerful. The shell programming language supports most of the
programming constructs found in high-level languages, such as looping, functions, variables, and
arrays.

How the Shell Gets Started: -


The shell is the main method by which a user interacts with the Linux kernel. But how does this
program get initialized to do so? The shell is started after you successfully log into the system,
and it continues to be the main method of interaction between the user and the kernel until we
log out.

Each user on our system has a default shell. The default shell for each user is specified in the
system password file, called /etc/passwd. The system password file contains, among other things,
each person's user ID, an encrypted copy of each user's password, and the name of the program
to run immediately after a user logs into the system. The program specified in the password file
does not have to be one of the Linux shells, but it almost always is.

The Most Common Shells: -


Several different kinds of shells are available on Linux and UNIX systems. The most common
are the Bourne shell (called sh), the C shell (csh), and the Korn shell (ksh). Each of these three
shells has its own advantages and disadvantages.

The Bourne shell was written by Steven Bourne. It is the original UNIX shell and is available on
every UNIX system in existence. The Bourne shell is considered to be very good for UNIX shell
programming, but it does not handle user interaction as well as some of the other shells available.
The C shell, written by Bill Joy, is much more responsive to user interaction. It supports features
such as command-line completion that are not in the Bourne shell. The C shell's programming
interface is thought by many not to be as good as that of the Bourne shell, but it is used by many
C programmers because the syntax of its programming language is similar to that of the C
language. This is also why it is named the C shell.

The Korn shell (ksh) was written by Dave Korn. He took the best features of both the C shell and
the Bourne shell and combined them into one that is completely compatible with the Bourne
shell. ksh is efficient and has both a good interactive interface and a good programming
interface.

In addition to these shells, many other shell programs took the basic features from one or more of
the existing shells and combined them into a new version. The three newer shells are tcsh (an
extension of csh), the Bourne Again Shell (bash, an extension of sh), and the Public Domain
Korn Shell (pdksh, an extension of ksh). Bash is the default shell on most Linux systems.

The Bourne Again Shell: -

The Bourne Again Shell (bash), as its name implies, is an extension of the Bourne shell. bash is
fully backward-compatible with the Bourne shell, but contains many enhancements and extra
features that are not present in the Bourne shell. bash also contains many of the best features that
exist in the C and Korn shells. bash has a very flexible and powerful programming interface, as
well as a user-friendly command interface.

Why use bash instead of sh? The biggest drawback of the Bourne shell is the way that it handles
user input. Typing commands into the Bourne shell can often be very tedious, especially if we
are using it on a regular basis and typing in a large number of commands. bash provides several
features that make entering commands much easier.

PRACTICAL-8

AIM: Using Bash shell develop simple shell programs.


# Program to find the sum of a number.
clear

echo -e "enter a positive number: \c"

read n

j=0

i=1

while [ $i -le $n ]

do

j=`expr $j + $i`

i=`expr $i + 1`

done

echo "addition of first $n numbers is $j"

enter a positive number: 8

addition of first 8 numbers is 36

[root@localhostprog]#
# Program to check whether the given number is a prime or not.

clear

echo -e "Enter a Positive Number: \c"

read num

i=2

while [ $i -le `expr $num - 1` ]

do

if [ `expr $num % $i` -eq 0 ]

then

echo "$num is not a prime number"

break

fi

i=`expr $i + 1`

done

if [ $i -eq $num ]

then

echo "$num is a prime number"

fi

Enter a Positive Number: 8

8 is not a prime number

[root@localhost mm]#
# Program to find the factorial of a Number.

clear

echo -e "enter a positive number: \c"

read n

prod=1

i=2

if [ $n -gt 1 ]

then

while [ $i -le $n ]

do

prod=`expr $prod \* $i`

i=`expr $i + 1`

done

echo "factorial of $n is $prod"

else

echo "factorial of $n is $n"

fi

enter a positive number: 5

factorial of 5 is 120

[root@localhostmm]#
# Program that works like a five function calculator.

clear

echo "Menu is shown below:"

echo "1. ADDITION "

echo "2. SUBTRACTION"

echo "3. MULTIPLICATION"

echo "4. DIVISION"

echo "5. MODE"

ch='y'

while [ $ch = 'y' ]

do

echo

echo -e "enter the operation to be performed: \c"

read op

echo

echo -e "enter first no.: \c"

read a

echo -e "enter second no.: \c "

read b

echo

if [ $op -eq 1 ]

then

c=$(($a + $b))

echo "addition = $c"

elif [ $op -eq 2 ]

then
c=$(($a - $b))

echo "subtraction = $c"

elif [ $op -eq 3 ]

then

c=$(($a * $b))

echo "multiplication = $c"

elif [ $op -eq 4 ]

then

c=$(($a / $b))

echo "divison = $c"

elif [ $op -eq 5 ]

then

c=$(($a % $b))

echo "mod = $c"

else

echo "wrong choice"

fi

echo -e "Want to perform some other opertaion: \c"

read ch

done
Menu is shown below:

1. ADDITION

2. SUBTRACTION

3. MULTIPLICATION

4. DIVISION

5. MODE

enter the operation to be performed: 3

enter first no.: 15

enter second no.: 6

multiplication = 90

Want to perform some other opertaion: y

enter the operation to be performed: 5

enter first no.: 19

enter second no.: 5

mod = 4

Want to perform some other opertaion: ns

[root@localhost mm]#

You might also like