0% found this document useful (0 votes)
20 views

Linux

1. Use GParted to create primary, extended, and logical partitions as well as swap space. Delete partitions using the same tool. 2. Use fdisk or parted command to create, delete, format partitions from the terminal. Commands like mkfs format partitions. 3. Partitions can be primary, extended containing logical partitions, and swap space. The partition table limits total partitions.

Uploaded by

lovepreet1221674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Linux

1. Use GParted to create primary, extended, and logical partitions as well as swap space. Delete partitions using the same tool. 2. Use fdisk or parted command to create, delete, format partitions from the terminal. Commands like mkfs format partitions. 3. Partitions can be primary, extended containing logical partitions, and swap space. The partition table limits total partitions.

Uploaded by

lovepreet1221674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

P age |1

PRACTICAL FILE
OF
LINUX OPERATING SYSTEM

Department of Information Technology


Seth Jai Parkash Mukand Lal Institute of Engineering &
Technology
Radaur – 135133(YamunaNagar)
(Affiliated to Kurukshetra University, Kurukshetra, Haryana,
INDIA)

BATCH : 2020-2024

SUBMITTED BY: SUBMITTED TO:


NAME – Ashwani Kumar Dr. Savita Khurana
ROLL NO. – 1220609 AP (IT Department)
BRANCH – IT
SESSION : 2023

Ashwani kumar
1220609
P age |2

INDEX

S.NO. EXPERIMENT NAME PAGE SIGNATURE


NO.
1. Install Linux on the system dual boot with the window Operating 3-7
system.

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.

4 To Familiarize with Unix/Linux logging/logout and simple 20-30


commands.

31-39
5 To Get Familiarize with Linux File System-Files and Directories

6 Managing Users and Groups in Linux- Adding, Modifying, 40-43


Password aging.

7 Implementation of disk quotas- enabling, creating, mounting, 44-47


configuring, assigning.

8 Using Bash shell write a Program to find the sum of a number. 48

9 Using Bash shell write a Program to check whether the given 49


number is a prime or not.

10 Using Bash shell write a Program that works like a five function 50-51
calculator.

11 To configure Network on Linux through CUI mode. 52

12 Configuring the NFS (Network file system) server on the linux


system by command line. 53-56

13 Configuring the SAMBA server on the linux system by command 57-61


line.

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 5: Select your location and Click the "Continue" button .

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.

By using the GUI (Graphical User Interface)


To create a 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.

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.

To delete the partition


Step 1: Just select the partition and click on the ‘-’ button to delete the selected partition.

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.

By using the terminal or command line interface


You can create or delete the partition by using the terminal or command line interface.
For creating the partition:

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.

To delete the partition:

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

To delete the swap


To delete the swap partition you just only have to select the swap partition and click on the delete
option or ‘-’. And you swap space will be deleted.

By using the terminal or command line interface


You can create or delete the partition by using the terminal or command line interface.
Before we begin, we can check if the system already has some swap space available. It is possible
to have multiple swap files or swap partitions, but generally one should be enough.
Step 1: Open the terminal as the super user and type the folowing command
We can see if the system has any configured swap by typing: sudo swapon –show
If you don't get back any output, this means your system does not have swap space available
currently.
You can verify that there is no active swap using the free utility. free –h
we should check the current disk usage by typing: df -h

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

AIM: To Familiarize with Unix/Linux logging/logout and simple commands.


 whatis
 Display short description of commands.
 The what is 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.
 Syntax :
<command> --help
 man
 provides the documentation for commands
 almost every command has a man “page”
 pages are grouped into “chapters”
 collectively reffered to as linux manual
 syntax :
man [<chapter>] <command>
 info
similar to man, but often more in-depth
run info without args to list all pages
info pages are structured like awebsite
each pages is devided into “nodes”
links to nodes are preceded by “*”
Syntax: info [<command>]

 pwd displays the absolute path to the current directory


the current working directory is the directory in
which you are working
Syntax: pwd
 cd cd changes directories to an absolute or relative path
$ cd /home/Joshua/work
$ cd project/docs

Ashwani kumar
1220609
P a g e | 22

 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
 Syntax:
ls [options] [files_or_dirs]
 eg: $ ls
ls without arguments lists files and directory names in
the current directory
$ ls /
ls lists the contents of directory given as an argument
$ ls -l /usr
use ls -l for more detailed “long” listing
$ ls -ld /usr
ls –d lists directory names
 cp
 it coping files and directories
 Syntax:
cp [option] file destination
more than one file may be copied at a time if the destination
is directory
cp [option] file1 file2 destination

 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

mv [optio] file1 file2 destination


 touch
create empty files or update file timestamps
syntax:
touch file

 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

scroll with arrows/pg up/pg down


syntax:
less [options] [filename]
less options:
-c clear before displaying. Screen is redrawn
instead of scrolled between screens
-e exit second time it reaches the end of the line
-r displays raw control character
-m verbose prompt, like more(by default it uses a colon)
 history
use to see list of “remembered” commands
syntax: $ history
 parameter/variables( $ )
line substitute the value of the variable in a command
$ cd $HOME/public_html
 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
Useful pipe Targets
Less displays input one page at a time
ls –l | less
mail sends input via email:
is –l | mail –s “files “ [email protected]
lpr sends input to the printer
ls -l | lpr
 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

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

floppy must be manually mounted and umounted


syntax :
mount [device] [mountpoint]

device points to filesystem to mount


mountpoint is the directory under which the file on
the filesystem will be located
syntax:
umount device| mount_point
eg:
mount /dev/fd0 /media/floppy
umount /media/floppy
 tar
use to creat archive
Syntax:
tar cvf archive_name files…
c creates a new archive
v produces verbose messages
f archive name is name of new file
 use to inspective archive
 Syntax:
tar tvf archive_name.tar
use to extracting an archive
Syntax:
tar xvf archive_name.tar
 PATH
 Colon separated list of locations where commands can be
found
 Eg:
$ echo $PATH

/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

use -w for only word count


use -c for only character count
 sort
 sort text to stdout – original file unchanged
$ sort [options ] file
 common options
-r reverses sort to sort descending
-n numeric sort
-f ignore (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
 eg:
sort -t : -k 3 -n /etc/passwd
In this case, the no. before the dot is the field no. and the no. after the dot is the character
within that field with which to begin the sort
 cut
 displas specific columns of file data
$ cut -f4 file
-f specifies field or column
-d specifies field delimiter
$ cut -f3 -d: /etc/passwd
/bin/bash
 also used as filter in pipelines
$ rpm –qa | grep bind
 sed

 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

-mtime: when file data last changed


-ctime: when file metadata last changed
 eg:
$ find -mtime -10
files modified less than 10 dayes ago
 ps

 display process information


 syntax: ps [options]
 useful options:
-a processes by all user
-x processes by all terminal
-u show processes owner
sending signal to processes
Syntax:
kill [-signal] pid
kill [-signal] %jobID
default signal is TERM
 kill –l lists all available signals
 use <ctrl- c> to terminate the processes
 at
scheduling a process to execute later
Syntax:
at time
<commands>
atq [user] -lists current at jobs pending
atrm [user/atjobID] – is used to remove pending at jobs
commands will be executed at the time indicated
command can be entered one per line, with the 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

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

Syntax : rpm -ivh --aid pakage-file.i386.rpm


rpm -e package name
-I use to install the package
v print the package name
h print hash marks which show that package is
installing
use –aid for automatic installation of dependent packages
-e use to remove the package
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
This command lists the files in the package
rpm –qi package_name
This give the package information.

Ashwani kumar
1220609
P a g e | 34

Practical 5

AIM: To Get Familiarize with 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 differentplace,
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."

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.

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
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.

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

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.
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

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!)
Ashwani kumar
1220609
P a g e | 38

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

Ashwani kumar
1220609
P a g e | 39

-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 typingits
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 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.

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

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.

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

Ashwani kumar
1220609
P a g e | 41

-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 execute 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.

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

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.

Ashwani kumar
1220609
P a g e | 43

Practical 6

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 superuser (i.e. root)
can access any file it wants to access. Superuser 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.
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.
To see changes use the command “cat /etc/passwd” passwd is the file stores the information
about the users.
 Group Management
To create a new group or to modify the existing group following commands are used.
Ashwani kumar
1220609
P a g e | 44

Creating a Group: groupadd groupname options


Removing a Group: groupdel groupname options
Modifying a group:  groupmod groupname options
To see changes use the command “cat /etc/group” group is the file stores the information about
the users group.

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 Delete a group you have to give the cmd “usergroup groupname”.


Adding new user to the the existing group use cmd “useradd –G groupname usrrname”. This will
make a new user and add to the group.

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

Aim: Implementation of disk quotas- enabling, creating, mounting,


configuring, assigning.
Disk quotas:
If you are the only person who uses the disk, there is no need to implement quota at all. But if
there are multiple users who use the same disk, quotas are the best ways to control the individual
users from monopolizing entire disk space. A user limited by disk quotas cannot use additional
disk space beyond his limit. For example suppose there are four users; user a, user b, user c and
user d. Without quota any user can use entire disk space, leaving no space for other users. This
situation is very common in shared environment such as web hosting, ISPs, file server, ftp server
etc. But if disk quota is enabled, no user can use disk space beyond his limit.
How to configure the disk quota

Disk quota can be configured in four steps

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.

3 File system File system type of device.


Mount options which control the mount process. To enable user quota add
4 Options
usrquota option and to enable group quota add grpquota option.

Ashwani kumar
1220609
P a g e | 49

Number Filed Description


Dump
5 To enable the dump on this device use 1. Use 0 to disable the dump.
support

Whether this device should be checked while mounting or not. To disable


Automatic
6 use 0, to enable use 1 (for root partition) or 2 (for all partitions except root
check
partition).

Remounting file system

If partition is not used by any process, we can remount it with following command.

#mount –o remount [partition]

Following figure illustrate this operation

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.

Creating quota files


In third step we will run following command.
#quotacheck –cug [partition where quota is enabled]
This command will create necessary files for quota. Let’s understand this command in detail.
quotacheck :- This command is used to check the quota implementation in partition.
c :- This option is used to create the quota files in specified partition.
u :- This option is used to check the user quota.
g :- This option is used to check the group quota.
Basically this command will check quota entries in specified partition. If aquota.user and
aquota.group files are not available in specified partition, it will create them.
We need to run above command only once for each partition where we want to configure the disk
quota.

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

Configuring quota policies

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.

To understand quota policies practically, let's create some dummy requirements.

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

Above command will open user quota configuration file.

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:

/dev/md0 / ext3 defaults 11


LABEL=/boot /boot ext3 defaults 12
none /dev/pts devpts gid=5,mode=620 0 0
LABEL=/home /home ext3 defaults,usrquota,grpquota 1 2
none /proc proc defaults 00
none /dev/shm tmpfs defaults 00
/dev/md1 swap swap defaults 00

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:

Disk quotas for user ed (uid 500):


Filesystem blocks soft hard inodes soft hard
/dev/md3 6618000 0 0 17397 0 0

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:

Disk quotas for user ed (uid 500):


Filesystem blocks soft hard inodes soft hard
/dev/md3 6617996 6900000 7000000 17397 0 0

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"

enter a positive number: 8


addition of first 8 numbers is 36

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

Enter a Positive Number: 8


8 is not a prime number

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

enter the operation to be performed: 5


enter first no.: 19
enter second no.: 5
mod = 4
Want to perform some other opertaion: no

Ashwani Kumar
1220609
P a g e | 57

Practical 11

Aim: - To configure Network on Linux through CUI mode.

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.

Setup NFS Server and NFS Client in Linux

Benefits of NFS

1. NFS allows local access to remote files.


2. It uses standard client/server architecture for file sharing between all *nix based machines.
3. With NFS it is not necessary that both machines run on the same OS.
4. With the help of NFS we can configure centralized storage solutions.
5. Users get their data irrespective of physical location.
6. No manual refresh needed for new files.
7. Newer version of NFS also supports acl, pseudo root mounts.
8. Can be secured with Firewalls and Kerberos.

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.

Important Files for NFS Configuration

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

Setup and Configure NFS Mounts on Linux Server

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.

[root@nfsserver ~]# yum install nfs-utils nfs-utils-lib


[root@nfsserver ~]# yum install portmap (not required with NFSv4)
[root@nfsserver ~]# apt-get install nfs-utils nfs-utils-lib

Now start the services on both machines.

[root@nfsserver ~]# /etc/init.d/portmap start


[root@nfsserver ~]# /etc/init.d/nfs start
[root@nfsserver ~]# chkconfig --level 35 portmap on
[root@nfsserver ~]# chkconfig --level 35 nfs on

After installing packages and starting services on both the machines, we need to configure both
the machines for file sharing.

Setting Up the NFS Server

First we will be configuring the NFS server.

Configure Export directory


For sharing a directory with NFS, we need to make an entry in “/etc/exports” configuration file.
Here I’ll be creating a new directory named “nfsshare” in “/” partition to share with client server,
you can also share an already existing directory with NFS.

[root@nfsserver ~]# mkdir /nfsshare


Now we need to make an entry in “/etc/exports” and restart the services to make our directory
shareable in the network.

[root@nfsserver ~]# vi /etc/exports


/nfsshare 192.168.0.101(rw,sync,no_root_squash)

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.

[root@nfsclient ~]# showmount -e 192.168.0.100

Export list for 192.168.0.100:


/nfsshare 192.168.0.101

Above command shows that a directory named “nfsshare” is available at “192.168.0.100” to


share with your server.

Mount Shared NFS Directory


To mount that shared NFS directory we can use following mount command.
[root@nfsclient ~]# mount -t nfs 192.168.0.100:/nfsshare /mnt/nfsshare
The above command will mount that shared directory in “/mnt/nfsshare” on the client server.
You can verify it following command.

[root@nfsclient ~]# mount | grep nfs

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

Ashwani Kumar
1220609
P a g e | 61

nfsd on /proc/fs/nfsd type nfsd (rw)


192.168.0.100:/nfsshare on /mnt type nfs (rw,addr=192.168.0.100)

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“.

[root@nfsclient ~]# vi /etc/fstab


Add the following new line as shown below.
192.168.0.100:/nfsshare /mnt nfs defaults 0 0
Test the Working of NFS Setup
We can test our NFS server setup by creating a test file on the server end and check its availability at nfs
client side or vice-versa.
At the nfsserver end
I have created a new text file named “nfstest.txt’ in that shared directory.
[root@nfsserver ~]# cat > /nfsshare/nfstest.txt
This is a test file to test the working of NFS server setup.
At the nfsclient end

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.

root@nfsclient ~]# umount /mnt/nfsshare


You can see that the mounts were removed by then looking at the filesystem again.
[root@nfsclient ~]# df -h -F nfs
You’ll see that those shared directories are not available any more.
Important commands for NFS
Some more important commands for NFS.

1. showmount -e : Shows the available shares on your local machine


2. showmount -e <server-ip or hostname>: Lists the available shares at the remote server
3. showmount -d : Lists all the sub directories

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.

Step 1: Installing Samba Server


Our current testing environment consists of two RHEL 7 boxes and one Windows 8 machine, in
that order:

1. Samba / NFS server [box1 (RHEL 7): 192.168.0.18],


2. Samba client #1 [box2 (RHEL 7): 192.168.0.20]
3. Samba client #2 [Windows 8 machine: 192.168.0.106]

install the following packages:


# yum update && yum install samba samba-client samba-common
# yum update && yum install samba samba-client samba-common cifs-utils
Once the installation is complete, we’re ready to configure our share.

Step 2: Setting Up File Sharing Through Samba

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

Step 3: Configuring SELinux and Firewalld

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

In addition, we must ensure that Samba traffic is allowed by the firewalld.


# firewall-cmd --permanent --add-service=samba
# firewall-cmd –reload

Step 4: Configure Samba Share


Now it’s time to dive into the configuration file /etc/samba/smb.conf and add the section for our
share: we want the members of the finance group to be able to browse the contents of /finance,
and save / create files or subdirectories in it (which by default will have their permission bits set
to 0770 and finance will be their group owner):
smb.conf
[finance]
comment=Directory for collaboration of the company's finance team
browsable=yes
path=/finance
public=no
valid users=@finance
write list=@finance
writeable=yes
create mask=0770
Force create mode=0770
force group=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:

Test Samba 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.

Step 5: Adding Samba Users

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.

Step 6: Mounting the Samba Share in Linux.


First, make sure the Samba share is accessible from this client:
# smbclient –L 192.168.0.18 -U user2
Mount Samba Share on Linux
(repeat the above command for user1)

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

Mount Samba Network Share


(where /media/samba is an existing directory)
or permanently, by adding the following entry in /etc/fstab file:
fstab
//192.168.0.18/finance /media/samba cifs credentials=/media/samba/.smbcredentials,defaults 0 0
Where the hidden file /media/samba/.smbcredentials (whose permissions and ownership have
been set to 600 and root:root, respectively) contains two lines that indicate the username and
password of an account that is allowed to use the share:

.smbcredentials

username=user1
password=PasswordForUser1
Finally, let’s create a file inside /finance and check the permissions and ownership:
# touch /media/samba/FileCreatedInRHELClient.txt

Create File in Samba Share


As you can see, the file was created with 0770 permissions and ownership set to user1:finance.
Step 7: Mounting the Samba Share in Windows
To mount the Samba share in Windows, go to My PC and choose Computer, then Map network
drive. Next, assign a letter for the drive to be mapped and check Connect using different
credentials (the screenshots below are in Spanish, my native language):

Ashwani Kumar
1220609
P a g e | 66

Mount Samba Share in Windows


Finally, let’s create a file and check the permissions and ownership:

Create Files on Windows Samba Share


# ls -l /finance
This time the file belongs to user2 since that’s the account we used to connect from the Windows
client.

Ashwani Kumar
1220609

You might also like