Linux
Linux
PRACTICAL FILE
OF
LINUX OPERATING SYSTEM
BATCH : 2020-2024
Ashwani kumar
1220609
P age |2
INDEX
2. Create and remove various type of partition through GUI as well as 8-15
command line.
3 Creating, removing the swap spaces as well as swap files through 16-19
command line as well as GUI.
31-39
5 To Get Familiarize with Linux File System-Files and Directories
10 Using Bash shell write a Program that works like a five function 50-51
calculator.
Ashwani kumar
1220609
P age |3
Practical 1
Aim: Install Linux on the system dual boot with the window Operating system.
Minimum requirements to install the linux:
CPU Type: Pentium 4 or higher; 2 GHz or higher
Memory/RAM: 1 GB minimum, up to the system limit
Hard Disk: 5 GB minimum
Step 1. There are several ways to boot the installation system. Some of the very popular ways are
, Booting from a CD ROM, Booting from a USB memory stick. As your computer starts, press
the DEL, ESC, F1, F2, F8 or F10 during the initial startup screen. Depending on the BIOS
manufacturer, a menu may appear. However, consult the hardware documentation for the exact
key strokes. In my machine, its DEL key as shown in following screen-shot. Find the Boot option
in the setup utility. Its location depends on your BIOS. Select the Boot option from the menu, you
can now see the options Hard Drive, CD-ROM Drive, Removable Devices Disk etc.Change the
boot sequence setting so that the CD-ROM is first. See the list of “Item Specific Help” in right
side of the window and find keys which is used to toggle to change the boot sequence.
Step 2. Insert the Ubuntu Disk in CD/DVD drive.Save your changes. Instructions on the screen
tell you how to save the changes on your computer. The computer will restart with the changed
settings. Machine should boot from CD ROM, Wait for the CD to load...
Ashwani kumar
1220609
P age |4
Step 3: In a few minutes installation wizard will be started. Select your language and click the
"Install Ubuntu" button to continue. Optionally, you can choose to download updates while
installing and/or install third party software, such as MP3 support. Be aware, though, that if you
select those options, the entire installation process will be longer.
Step 4: Since we are going to create partitions manually, select Something else, then click
Continue. Keep in mind that even if you do not want to create partitions manually, it is better to
select the same option as indicated here. This would insure that the installer will not overwrite
your Windows , which will destroy your data. The assumption here is that sdb will be used just
for Ubuntu 12.04, and that there are no valuable data on it.
Ashwani kumar
1220609
P age |5
Step 6: Select your keyboard layout and UK (English) and Click on “Continue” button
Ashwani kumar
1220609
P age |6
Step 7: Fill in the fields with your real name, the name of the computer, username, and the
password. Also at this step, there's an option called "Log in automatically." If you check it, you
will automatically be logged in to the Ubuntu desktop without giving the password. Option
"Encrypt my home folder," will encrypt your home folder. Click on the "Continue" button to
continue.
Step 8: Now Ubuntu 12.04 LTS (Precise Pangolin) operating system will be installed. It will take
approximately 10-12 minutes (depending on computer's speed), a pop-up window will appear,
notifying you that the installation is complete, and you'll need to restart the computer in order to
use the newly installed Ubuntu operating system. Click the "Restart Now" button.
Ashwani kumar
1220609
P age |7
Step 9: Please remove the CD and press the "Enter" key to reboot. The computer will be restarted.
In a few seconds, you should see Windows 7′s boot menu with two entires listed – Windows 7
and Ubuntu 12.04 (LTS). Then you may choose to boot into Windows 7 or Ubuntu 12.04 using
the UP/Down arrow key. Please select Ubuntu 12.04 (LTS) and press Enter to boot the machine
in Ubuntu 12.04 Linux. Here you can see the users on the machine, Click on the user name and
enter the password and press Enter key to login.
Ashwani kumar
1220609
P age |8
Practical 2
Aim: Create and remove various type of partition 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.
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.
Ashwani kumar
1220609
P age |9
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.
After clicking the disk option this type of screen will occur which display about all the free space
all the partition type of the disk, and all the created partition.
Ashwani kumar
1220609
P a g e | 10
Step 2: For creating the partition click on the ‘+’ button and the below type of interface will occur
here you can choose how much type of space you want to allot to your disk and which type of
disk you want to make i.e. NTFS or ext or lvm and here you can also assign the name to the disk.
Step 3: Here we have name the drive cc and it is of ext type after tis just click on create option
and your disk is listed in the disk menu.
Ashwani kumar
1220609
P a g e | 11
Step 4: Now select the disk and click on the play button highlighted by rectangle to mount the
disk and your disk is ready to use to see your drive just go to explorer here our disk ‘cc’ is working.
Ashwani kumar
1220609
P a g e | 12
Step 2: A warning box will appear on the screen and to delete the partition you just have to click
the delete option and your partition has been deleted to check you can again check your explorer
the created partition has been deleted.
Ashwani kumar
1220609
P a g e | 13
Step 1: Open the terminal as the super user and type the command “fdisk /dev/sda” this will list
all the command that you can do on your disk and the partitions in this you have to remain carefull
when using these commands. The following menu will appear on your screen.
Step 2: Now type n to create the new partition and after this it will ask about the type of disk i.e
logical or primary, you have to select it by typing ‘l’ or ‘p’ and press enter and then it will ask
about the first and the last sector values i.e. the size of the disk you have to give the size and then
press the enter your disk has been created.
Ashwani kumar
1220609
P a g e | 14
Step 3: To see the disk type the ‘p’ that will print all the partition. Here the partition 10 has been
create of type Linux and size of 6gb.
Step 4: To save the changes you have to press the ‘w’ command this will alter your partition table
and add the created partition to the table.
Ashwani kumar
1220609
P a g e | 15
Step 1: Open the terminal as the super user and type the command “fdisk /dev/sda” this will list
all the command that you can do on your disk and the partitions in this you have to remain careful
when using these commands. The following menu will appear on your screen.
Step 2: Now type ‘d’ to delete the partition and after this it will ask about the partition you want
to delete you have to specify the partition that you want to delete. And again press the ‘w’
command to save the changes and exit, this will alter your partition table and your partition will
be deleted form the table.
Step 3: You can see the changes by again using the cmd ‘fdisk /dev/sda’ and giving cmd ‘p’ to
print the partition table and you will see that the the partition has been deleted.
Ashwani kumar
1220609
P a g e | 16
Practical 3
Aim: Creating, removing the swap spaces as well as swap files through
command line as well as GUI.
Swap is an area on a hard drive that has been designated as a place where the operating system
can temporarily store data that it can no longer hold in RAM. Basically, this gives you the ability
to increase the amount of information that your server can keep in its working "memory", with
some caveats. The swap space on the hard drive will be used mainly when there is no longer
sufficient space in RAM to hold in-use application data. The information written to disk will be
significantly slower than information kept in RAM, but the operating system will prefer to keep
running application data in memory and use swap for the older data. Overall, having swap space
as a fall back for when your system's RAM is depleted can be a good safety net against out-of-
memory exceptions on systems with non-SSD storage available.
By using the GUI (Graphical User Interface)
To create swap partition:
Step 1: Firstly at the left most of the screen go to application and select utility option and select
the disk option.
After clicking the disk option this type of screen will occur which display about all the free space
all the partition type of the disk, and all the created partition.
Step 2: Select the free space and click the ‘+’ or add new button to create the swap space.
Ashwani kumar
1220609
P a g e | 17
After this this type will appear on the screen you have .
You have to change the type of the partition and gave a size to the disk it can be equal to the ram
or double of the ram of the computer and change the type Ext4 to swap.
Here you can see the swap partition has been created of 1GB
Ashwani kumar
1220609
P a g e | 18
Ashwani kumar
1220609
P a g e | 19
Step 2: Now give the command “sudo fallocate -l 1G /swapfile” or “sudo dd if=/dev/zero of=/swap
bs=1GB count=2”. Now that we know our available hard drive space, we can go about creating a
swap file within our filesystem. We will create a file of the swap size that we want called swapfile
in our root (/) directory. Permissions change by typing: "sudo chmod 600 /swap".
Permission change verify “ls -lh /swap”
We can now mark swap space by typing: "sudo mkswap /swap"
Step 3: After marking the file, we can enable the swap, allowing our system to start utilizing it:
"sudo swapon /swap"
We can verify that the swap is available by typing: "sudo swapon --show or -s"
And we can see the free spaces by using cmd “free”
Remove the Swap Space by the following command.
swapoff /dev/sda
Ashwani kumar
1220609
P a g e | 20
Ashwani kumar
1220609
P a g e | 21
Practical 4
Ashwani kumar
1220609
P a g e | 22
mv
move and/or rename files and directories
syntax:
mv [option] file destination
more than one file may be moved at a time if the destination
is directory
Ashwani kumar
1220609
P a g e | 23
rm
use for removing files
syntax: rm [option] filenames….
Options are:
-i – it will ask you before removing the file
-r – it can remove file as well as directory
eg:
$ rm -ri file
$ rm -r file
mkdir
makes a directory
mkdir dirname
rmdir
remove an empty directory
syntax:
rmdir dirname
cat
contents of the files are displayed sequentially with no break
syntax:
cat [option] [file..]
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
Ashwani kumar
1220609
P a g e | 24
Ashwani kumar
1220609
P a g e | 25
examples:
ugo+ r grant read access to all
o-wx deny write and execute to other
chmod u+w, go-w .bashrc grant write access to owner
but denies it to group and
others.
permissions are calculated by adding:
4(for read)
2(for write)
1(for execute)
example :
chmod 664 file
read and write permissions set for the owner and
group; read only for others
chmod 660 file
read and write permissions set for the owner and group
permissions for others
df
reports total kilobytes,kiloytes used , kilobytes free per file
system
-h displays sizes in easier to read units
eg:
$ df -h /opt
du
reports kilobytes used per directory
also take –h option
eg:
$ df –h /opt
mount and umount
use to mount the device and removable media
before accessing media must be mounted
before removing media must be umounted
by default, non-root users may only mount certain
devices(cd,dvd,floppy,usb etc)
Ashwani kumar
1220609
P a g e | 26
/usr/local/bin:/usr/bin:/bin:/usr/X11r6/bin:/home/digby/bi
n
Ashwani kumar
1220609
P a g e | 27
Which
Command showing location in the PATH of an executable
Eg:
$ which less
/usr/bin/less
su
Su means switch user .this command is use to change identities
To change your password, run passwd
Syntax:
su – username
head
display few lines of text in a file
Syntax:
head file
use –n or –lines parameter to change no. of lines displayed
syntax:
head –n 20 file
tail
displays last few lines of text in a file
Syntax:
tail file
use –n or --lines to change no. of lines displayed
Syntax:
tail –n 5 file
use –f option to continue to display the file in “real rime”
Syntax:
tail –f file
wc
“word count” – also count lines and characters
$ wc story.txt
19 237 1901 story.txt
use –l for only line count
Ashwani kumar
1220609
P a g e | 28
stream editor
read a file or stream of data ; writes out the data , performing
search and replace instruction(s)
eg: $ sed ‘s/cat/dog/’ pets
$ sed ‘s/cat/dog/g’ pets
$ sed ‘1,50s/dog/cat/g’ pets
multiple sed instructions
$ sed -e ‘s/dog/cat/’ -e ‘s/hi/lo/’ pets
Ashwani kumar
1220609
P a g e | 29
find
find [directory…] [criteria…]
searches directory trees in realtime
all files are matched if no criteria given
can execute commands on found files
: eg
$ find -name snow.png
searches for files name snow.png
$ find –iname snow.png
case-insentive search for files named snow.png, Snow.png,
SNOW.PNG
$ find -user joe -group joe
searches for files owned by the user joe and group joe
criteria are ANDed together by default
can be OR’ed or negated with –o and –not
eg:
$ find –user joe -not -group joe
$ find -user joe -o -user jane
can match the ownership by name or id
$ find / -owner joe -gid 500
can match numeric or symbolic permission
$ find -perm 755
$ find -perm +o+w
$ find –size 10M
$ files with a size of exactly 10 megabytes
$ find -size -10M
$ files with a size less than 10 megabytes
$ find -size +10M
$ files with a size over 10 megabyes
find can match by inode timestamps
-atime: when file was last read
Ashwani kumar
1220609
P a g e | 30
Ashwani kumar
1220609
P a g e | 31
at 8:00pm December 7
Ashwani kumar
1220609
P a g e | 32
at 7 am Thursday
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
Syntax: crontab [-u user ] file crontab [-l| -
r| -e ]
-l option displays the current crontab file
-r option removes the crontab file
-e option is used to set the job for login user
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 weekentry
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
A user’s crontab may look like the following:
#min hour doM month doW command
0 4 * * 1,3,5 ps –a <abc.txt
This command will run on every Monday, Wednesday & Friday of every month at
4:00 am
installing and
removing software
Ashwani kumar
1220609
P a g e | 33
Ashwani kumar
1220609
P a g e | 34
Practical 5
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."
Ashwani kumar
1220609
P a g e | 35
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.
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.
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
Ashwani kumar
1220609
P a g e | 36
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.
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
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.
Ashwani kumar
1220609
P a g e | 37
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
darkstar:~$ ls
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
darkstar:~$ rm /tmp/dead_duck
darkstar:~$ rm *
removes all files from your current directory. (Be careful when using wildcards!)
Ashwani kumar
1220609
P a g e | 38
darkstar:~$ rm /tmp/*duck
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
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.
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 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
darkstar:~$ ls -l myfile
Ashwani kumar
1220609
P a g e | 39
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 typingits
name. With directories, execute permission enables you to cd into them.
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 execute 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.
Ashwani kumar
1220609
P a g e | 40
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.
There are two ways to write the permission specification. One is by using the numeric coding
system for permissions:
darkstar:~$ ls -l myfile
darkstar:~$ ls -l myfile
darkstar:~$ ls -l myfile
Ashwani kumar
1220609
P a g e | 41
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
Other ways of using the symbolic file permissions are described in the chmod man page.
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!
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.
Ashwani kumar
1220609
P a g e | 42
/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
/usr/bin
/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.
Ashwani kumar
1220609
P a g e | 43
Practical 6
To add a user go to the terminal and enter as the super user and type the cmd useradd username
and press enter to add the user to your system.
To view the added user type the cmd cat /etc/passwd and press enter after this kind of screen will
appear and it will show the added user in the last. Here we have added user name sharma.
Ashwani kumar
1220609
P a g e | 45
To set a password for the user you have to give the cmd “passwd username”.
Then you have to enter the password 2 times.
To Delete a user you have to give the cmd “userdel username”.
To add a group go to the terminal and enter as the super user and type the cmd “groupadd
groupname” and press enter to add the group to your system.
To view the added user type the cmd cat /etc/group and press enter after this kind of screen will
appear and it will show the added group in the last. Here we have added group name aksharma.
Ashwani kumar
1220609
P a g e | 46
To view the added user to the group use the cmd “cat /etc/group”.
Ashwani kumar
1220609
P a g e | 47
Ashwani kumar
1220609
P a g e | 48
Practical 7
1. Enable quota
2. Remount file system
3. Create quota files
4. Configure quota policy
Enabling quota
Linux uses /etc/fstab configuration file to mount all partitions in file system at boot time. This file
contains all necessary information about the partition such as partition location at disk, mount
point, attributes and other control options which are required to mount a partition. Each entry in
this file has six fields.
Number Filed Description
What to Device which we want to mount. We can use device name, UUID and
1
mount label in this filed to represent the device.
Where to The directory in main Linux File System where we want to mount the
2
mount device.
Ashwani kumar
1220609
P a g e | 49
If partition is not used by any process, we can remount it with following command.
If partition is remounted without any error or warning, use mount | grep [partition] command to
confirm that quota options are successfully applied.
Some common reasons which trigger errors here are typing mistake in fstab file, mount point
unavailable, file system is not formatted and wrong partition is selected.
Ashwani kumar
1220609
P a g e | 50
Once necessary files are created, following command is used to sync the disk quota database table
with current disk usages.
# quotacheck -avug
In this command
a :- This option is used to check all quota enabled partitions
v :- This option is used to print real time updates as command proceeds
u :- This option is used to check user disk quota information
g :- This option is used to check group disk quota information
To configure quota policies, we have to define three values; soft limit, hard limit and grace period.
Soft limit: - This limit is flexible. User or group is allowed to cross this limit temporary.
Hard limit: - This is fixed limit. User or group is not allowed to cross this limit.
Grace period: - This is the time period in which user or group is allowed to use additional space
beyond the soft limit.
User Block Size Soft Block Size Hard Grace Inode Soft Inode Hard Grace
/Group Limit Limit period Limit limit period
a 100Mb 200Mb 2 Minutes 10 10 Nil
b 200Mb 200Mb Nil 10 20 5 hours
quotatest 1000Mb 1500Mb 1 Day 100 150 5 days
Nil: - If both soft limit and hard limit are same, there is no need to configure this value. To
configure quota edqota command is used. To configure quota for user a use following command
#edquota a
Ashwani kumar
1220609
P a g e | 51
Modifying /etc/fstab
Using the text editor of your choice, simply add the usrquota and/or grpquota options to the file
systems that require quotas:
we can see that the /home file system has both user and group quotas enabled. At this point you
must remount each file system whose fstab entry has been modified. You may be able to simply
umount and then mount the file system(s) by hand, but if the file system is currently in use by any
processes, reboot the system.
Assigning Quotas
The mechanics of assigning disk quotas are relatively simple. The edquota program is used to edit
a user or group quota:
edquota uses a text editor (which can be selected by setting the EDITOR environment variable to
the full pathname of your preferred editor) to display and change the various settings. Note that
any setting left at zero means no limit:
In this example, user ed (who is currently using over 6GB of disk space) has a soft limit of 6.9GB
and a hard limit of 7GB. No soft or hard limit on inodes has been set for this user.
Ashwani kumar
1220609
P a g e | 52
Practical 8
AIM: Using Bash shell write a 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"
Ashwani kumar
1220609
P a g e | 53
Practical 9
AIM: Using Bash shell write a 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
Ashwani Kumar
1220609
P a g e | 54
Practical 10
AIM: Using Bash shell write a 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 ]
Ashwani Kumar
1220609
P a g e | 55
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
Ashwani Kumar
1220609
P a g e | 56
Ashwani Kumar
1220609
P a g e | 57
Practical 11
1. Enter ifconfig
This command will list all the network related services.
2. Enter system_config_network_tui
This command will list the following:
IP Address = 192.168.0.5
Netmask Address = 255.255.255.0
Gateway = 19.168.0.254
DNS = 192.168.0.254
3. Enter service network restart
This Command will start the Network Service.
Ashwani Kumar
1220609
P a g e | 58
Practical 12
Aim: Configuring the NFS (Network file system) server on the linux system by command
line.
NFS (Network File System) is basically developed for sharing of files and folders between
Linux/Unix systems by Sun Microsystems in 1980. It allows you to mount your local file
systems over a network and remote hosts to interact with them as they are mounted locally on the
same system. With the help of NFS, we can set up file sharing between Unix to Linux system
and Linux to Unix system.
Benefits of NFS
NFS Services
Its a System V-launched service. The NFS server package includes three facilities, included in
the portmap and nfs-utils packages.
1. portmap : It maps calls made from other machines to the correct RPC service (not required
with NFSv4).
2. nfs: It translates remote file sharing requests into requests on the local file system.
3. rpc.mountd: This service is responsible for mounting and unmounting of file systems.
1. /etc/exports : Its a main configuration file of NFS, all exported files and directories are defined
in this file at the NFS Server end.
2. /etc/fstab : To mount a NFS directory on your system across the reboots, we need to make an
entry in /etc/fstab.
3. /etc/sysconfig/nfs : Configuration file of NFS to control on which port rpc and other services
are listening.
Ashwani Kumar
1220609
P a g e | 59
To setup NFS mounts, we’ll be needing at least two Linux/Unix machines. Here in this tutorial,
I’ll be using two servers.
1. NFS Server: nfsserver.example.com with IP-192.168.0.100
2. NFS Client : nfsclient.example.com with IP-192.168.0.101
Installing NFS Server and NFS Client
We need to install NFS packages on our NFS Server as well as on NFS Client machine. We can
install it via “yum” (Red Hat Linux) and “apt-get” (Debian and Ubuntu) package installers.
After installing packages and starting services on both the machines, we need to configure both
the machines for file sharing.
Ashwani Kumar
1220609
P a g e | 60
In the above example, there is a directory in / partition named “nfsshare” is being shared with
client IP “192.168.0.101” with read and write (rw) privilege, you can also use hostname of the
client in the place of IP in above example.
NFS Options
Some other options we can use in “/etc/exports” file for file sharing is as follows.
1. ro: With the help of this option we can provide read only access to the shared files i.e client
will only be able to read.
2. rw: This option allows the client server to both read and write access within the shared
directory.
3. sync: Sync confirms requests to the shared directory only once the changes have been
committed.
4. no_subtree_check: This option prevents the subtree checking. When a shared directory is the
subdirectory of a larger file system, nfs performs scans of every directory above it, in order to
verify its permissions and details. Disabling the subtree check may increase the reliability of
NFS, but reduce security.
5. no_root_squash: This phrase allows root to connect to the designated directory.
For more options with “/etc/exports“, you are recommended to read the man pages for export.
Setting Up the NFS Client
After configuring the NFS server, we need to mount that shared directory or partition in the client
server.
Mount Shared Directories on NFS Client
Now at the NFS client end, we need to mount that directory in our server to access it locally. To
do so, first we need to find out that shares available on the remote server or NFS Server.
Ashwani Kumar
1220609
P a g e | 61
The above mount command mounted the nfs shared directory on to nfs client temporarily, to
mount an NFS directory permanently on your system across the reboots, we need to make an
entry in “/etc/fstab“.
Go to that shared directory in client server and you’ll find that shared file without any manual
refresh or service restart.
[root@nfsclient]# ll /mnt/nfsshare
total 4
-rw-r--r-- 1 root root 61 Sep 21 21:44 nfstest.txt
root@nfsclient ~]# cat /mnt/nfsshare/nfstest.txt
This is a test file to test the working of NFS server setup.
Removing the NFS Mount
If you want to unmount that shared directory from your server after you are done with the file
sharing, you can simply unmount that particular directory with “umount” command. See this
example below.
Ashwani Kumar
1220609
P a g e | 62
Practical 13
Aim: Configuring the SAMBA server on the linux system by command line.
Samba is an open-source software suite that runs on Unix/Linux based platforms but is able to
communicate with Windows clients like a native application. So Samba is able to provide this
service by employing the Common Internet File System (CIFS).
At the heart of this CIFS is the Server Message Block (SMB) protocol. Samba does this by
performing these 4 key things -
File & print services
Authentication and Authorization
Name resolution
Service announcement (browsing)
Samba can be run on many different platforms including Linux, Unix, OpenVMS and operating
systems other than Windows and allows the user to interact with a Windows client or server
natively. It can basically be described as the Standard Windows interoperability suite of programs
for Linux and Unix.
One of the reason why Samba is so relevant is because it provides file and print services to
SMB/CIFS clients, which causes those clients to see the server as if it was a Windows system (I
must admit I tend to get a little emotional while writing about this topic as it was my first setup as
a new Linux system administrator some years ago).
Adding system users and setting up permissions and ownership
To allow for group collaboration, we will create a group named finance with two users (user1
and user2) with useradd command and a directory /finance.
Ashwani Kumar
1220609
P a g e | 63
We will also change the group owner of this directory to finance and set its permissions to 0770
(read, write, and execution permissions for the owner and the group owner):
# groupadd finance
# useradd user1
# useradd user2
# usermod -a -G finance user1
# usermod -a -G finance user2
# mkdir /finance
# chmod 0770 /finance #
chgrp finance /finance
In preparation to configure /finance as a Samba share, we will need to either disable SELinux or
set the proper boolean and security context values as follows (otherwise, SELinux will prevent
clients from accessing the share):
# setsebool -P samba_export_all_ro=1 samba_export_all_rw=1
# getsebool –a | grep samba_export
# semanage fcontext –at samba_share_t "/finance(/.*)?"
# restorecon /finance
Ashwani Kumar
1220609
P a g e | 64
Save the file and then test it with the testparm utility. If there are any errors, the output of the
following command will indicate what you need to fix. Otherwise, it will display a review of your
Samba server configuration:
Should you want to add another share that is open to the public (meaning without any
authentication whatsoever), create another section in /etc/samba/smb.conf and under the new
share’s name copy the section above, only changing public=no to public=yes and not including
the valid users and write list directives.
Next, you will need to add user1 and user2 as Samba users. To do so, you will use the smbpasswd
command, which interacts with Samba’s internal database. You will be prompted to enter a
password that you will later use to connect to the share:
# smbpasswd -a user1
# smbpasswd -a user2
Finally, restart Samba, enable the service to start on boot, and make sure the share is actually
available to network clients:
# systemctl start smb
# systemctl enable smb
# smbclient -L localhost –U user1
# smbclient -L localhost –U user2
Verify Samba Share
At this point, the Samba file server has been properly installed and configured. Now it’s time to
test this setup on our RHEL 7 and Windows 8 clients.
As any other storage media, you can mount (and later unmount) this network share when needed:
# mount //192.168.0.18/finance /media/samba -o username=user1
Ashwani Kumar
1220609
P a g e | 65
.smbcredentials
username=user1
password=PasswordForUser1
Finally, let’s create a file inside /finance and check the permissions and ownership:
# touch /media/samba/FileCreatedInRHELClient.txt
Ashwani Kumar
1220609
P a g e | 66
Ashwani Kumar
1220609