Experiment 01
Introduction to Linux
Linux is fully customizable free operating system and is supported by all kinds of
hardware components. Due to advent open source feature of Linux, everyone can
download the source code of Linux and modify it. On the server side Linux is a renowned
OS for its stability and reliability. Day by day Linux is getting more popular and user
friendly. Modern Linux is not only supported by high end server, workstations and home
PC but can also run on various gadgets.
Linux is totally UNIX kernel but not fully UNIX OS because it does not include
applications like file system, Window system, text editor, Compiler and so on.. Whole
Linux distribution is a collection of Linux Kernel, GUI, GNU C/C++ compiler, text
editors and various applications under GPL. Linux uses only POSIX (Portable Operating
System Interface Standards) of UNIX. which are standards specified by IEEE computer
society for maintaining compability between OS.
It needs standard GNU C compiler and GPL (General Purpose License). GNU is
collection of computer software that can be used in building an OS like UNIX or
Linux.and makes it free to use for everyone. Various GNU components are used to
develop Linux like GNU compiler, debugger and hardware. Linux was licensed under the
GPL. Without paying any charge, you can download source code of Linux to use, share
and modify it. GPL (General Public License) uses free license software which provides
freedom to run, share, modify and study it.
Before we discuss Linux in brief, mentioned below are some of its advantages: Open
Source
-Run on both low and high end hardware
-Compatible with others OS
-More powerful
-Low failure time
-Well supported
-Well Secure
-Fully customizable in all its components
1
Features of Linux
Linux is fully featured OSand adopts various features from different UNIX
versions. Multithreading
Linux is supported by multithreading environment. More than one thread can run at the
same time in Linux. Thread is a small code within the process. I will briefly explain about
thread in another tutorial.
Monolithic kernel
Linux kernel is Monolithic kernel. It is an operating system architecture where entire OS
runs in kernel mode instead of user mode. When program or applications want to use
hardware, send request to kernel mode by system call. Kernel evaluates request from
system call and interacts with proper hardware. All operating services like memory
management, file management, process management are working in kernel.
File System
Whole Linux kernel is based on file system architecture. Linux supports many file types
such as a directory file, link, pipe, named pipe, special file and block device file. Each file
type has its own characteristics and features. File system is not only holding the data of
file but also contains the structure and informationof the file.
Multiuser Capability
Linux OS shares the same resources like memory, hard disk etc. for multi users. Multi
users can access same resource using individual terminal (i.e. client PC).
2
Experiment 02
Directory structure of Linux
1. / – Root
Every single file and directory starts from the root directory.
Only root user has write privilege under this directory.
Please note that /root is root user’s home directory, which is not same as /.
2. /bin – User Binaries
Contains binary executables.
Common linux commands you need to use in single-user modes are located under
this directory.
Commands used by all the users of the system are located here.
For example: ps, ls, ping, grep, cp.
3. /sbin – System Binaries
Just like /bin, /sbin also contains binary executables.
But, the linux commands located under this directory are used typically by system
aministrator, for system maintenance purpose.
For example: iptables, reboot, fdisk, ifconfig, swapon
4. /etc – Configuration Files
Contains configuration files required by all programs.
This also contains startup and shutdown shell scripts used to start/stop individual
programs.
For example: /etc/resolv.conf, /etc/logrotate.conf
5. /dev – Device Files
Contains device files.
These include terminal devices, usb, or any device attached to the system.
For example: /dev/tty1, /dev/usbmon0
6. /proc – Process Information
Contains information about system process.
This is a pseudo filesystem contains information about running process. For
example: /proc/{pid} directory contains information about the process with that
particular pid.
3
This is a virtual filesystem with text information about system resources. For
example: /proc/uptime
7. /var – Variable Files
var stands for variable files.
Content of the files that are expected to grow can be found under this directory.
This includes — system log files (/var/log); packages and database files (/var/lib);
emails (/var/mail); print queues (/var/spool); lock files (/var/lock); temp files needed
across reboots (/var/tmp);
8. /tmp – Temporary Files
Directory that contains temporary files created by system and users.
Files under this directory are deleted when system is rebooted.
9. /usr – User Programs
Contains binaries, libraries, documentation, and source-code for second level
programs.
/usr/bin contains binary files for user programs. If you can’t find a user binary
under /bin, look under /usr/bin. For example: at, awk, cc, less, scp
/usr/sbin contains binary files for system administrators. If you can’t find a
system binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd,
useradd, userdel
/usr/lib contains libraries for /usr/bin and /usr/sbin
/usr/local contains users programs that you install from source. For example,
when you install apache from source, it goes under /usr/local/apache2
10. /home – Home Directories
Home directories for all users to store their personal files.
For example: /home/john, /home/nikita
11. /boot – Boot Loader Files
Contains boot loader related files.
Kernel initrd, vmlinux, grub files are located under /boot
For example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic
12. /lib – System Libraries
Contains library files that supports the binaries located under /bin and /sbin
Library filenames are either ld* or lib*.so.*
For example: ld-2.11.1.so, libncurses.so.5.7
4
13. /opt – Optional add-on Applications
opt stands for optional.
Contains add-on applications from individual vendors.
add-on applications should be installed under either /opt/ or /opt/ sub-directory.
14. /mnt – Mount Directory
Temporary mount directory where sysadmins can mount filesystems.
15. /media – Removable Media Devices
Temporary mount directory for removable devices.
For examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives;
/media/cdrecorder for CD writer
16. /srv – Service Data
srv stands for service.
Contains server specific services related data.
For example, /srv/cvs contains CVS related data.
5
Experiment 03
VI Editor
The VI editor is the most popular and classic text editor in the Linux family. Below, are
some reasons which make it a widely used editor –
It is available in almost all Linux Distributions
It works the same across different platforms and Distributions
It is user-friendly. Hence, millions of Linux users love it and use it for their
editing needs
Nowadays, there are advanced versions of the vi editor available, and the most popular
one is VIM which is Vi Improved. Some of the other ones are Elvis, Nvi, Nano, and
Vile. It is wise to learn vi because it is feature-rich and offers endless possibilities to edit
a file.
To work on VI editor, you need to understand its operation modes. They can be divided
into two main parts.
Command mode:
The vi editor opens in this mode, and it only understands commands
In this mode, you can, move the cursor and cut, copy, paste the text
This mode also saves the changes you have made to the file
Commands are case sensitive. You should use the right letter case.
Insert mode:
This mode is for inserting text in the file.
You can switch to the Insert mode from the command mode by pressing 'i' on
the keyboard
Once you are in Insert mode, any key would be taken as an input for the file on
which you are currently working.
To return to the command mode and save the changes you have made you need to
press the Esc key
Starting the vi editor
To launch the VI Editor -Open the Terminal (CLI) and type
vi <filename_NEW> or <filename_EXISTING>
6
& If you specify an existing file, then the editor would open it for you to edit. Else, you
can create a new file.
7
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
8
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
vi Editing commands
Note: You should be in the "command mode" to execute these commands. VI editor
is case-sensitive so make sure you type the commands in the right letter-case.
Keystrokes Action
I Insert at cursor (goes into insert mode)
A Write after cursor (goes into insert mode)
A Write at the end of line (goes into insert mode)
ESC Terminate insert mode
U Undo last change
U Undo all changes to the entire line
O Open a new line (goes into insert mode)
dd Delete line
3dd Delete 3 lines.
D Delete contents of line after the cursor
C Delete contents of a line after the cursor and insert new text. Press ESC key to end
insertion.
dw Delete word
4dw Delete 4 words
Cw Change word
X Delete character at the cursor
R Replace character
R Overwrite characters from cursor onward
S Substitute one character under cursor continue to insert
S Substitute entire line and begin to insert at the beginning of the line
~ Change case of individual character
Make sure you press the right command otherwise you will end up making undesirable
changes to the file. You can also enter the insert mode by pressing a, A, o, as required.
9
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
Moving within a file
You need to be in the command mode to move within a file. The default keys for
navigation are mentioned below else; You can also use the arrow keys on the
keyboard.
Keystroke Use
K Move cursor up
J Move cursor down
H Move cursor left
L Move cursor right
Saving and Closing the file
You should be in the command mode to exit the editor and save changes to the file.
Keystroke Use
Shift+zz Save the file and quit
:w Save the file but keep it open
:q Quit without saving
:wq Save the file and quit
10
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
Experiment 4
Steps Create & execute shell script
#!/bin/bash
echo This is a Simple Shell Script
To execute Script
(1) #chmod 755 simplescript
(2) #./simplescript
Output
11
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
Experiment 5
Shell script for performing mathematical operations
#!/bin/bash
a=10
b=5
echo Addition
c=expr $a+$b
echo Substraction
d=expr $a-$b
echo Multiplication
e=expr $a*$b
echo Division
f=expr $a/$b
Output
12
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
Experiment 6
Shell script for performing mathematical operations
using inputs
#!/bin/bash
echo Enter First Number
read a
echo Enter Second Number
read b
echo Addition
c=expr $a+$b
echo Substraction
d=expr $a-$b
echo Multiplication
e=expr $a*$b
echo Division
f=expr $a/$b
Output
13
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
Experiment 7
Shell script to print table
#!/bin/bash
echo Table
echo Enter a number
read n
for((i=1; j<=10; j++))
do
echo $n \* $j = $(expr $n \* $j)
done
Output
14
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
Experiment 8
Shell script to implement nested loop
#!/bin/bash
echo Steps
for((i=1; i<=5; i++))
do
for((j=1; j<=5; j++))
do
echo -n “|”
done
echo “_”
done
Output
15
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
Experiment 9
Shell script to print system information
#!/bin/bash
echo Current User Information
echo My username is $(who)
echo My mechine name is $(hostname)
echo My current Directory is $(pwd)
Output
16
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
Experiment 10
Shell Script to print file details
#!/bin/bash
echo Enter file name to process
read file
echo The number of line in $file is :
wc –l $file
echo The number of word in $file is :
wc –w $file
echo end
Output
17
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
Experiment 11
Red Hat Enterprise Linux: RHEL3 / RHEL4
Enabling Telnet and FTP Services
Linux is configured to run the Telnet and FTP server, but by default, these
services are not enabled. To enable the telnet service, login to the server as
the root user account and run the following commands:
# chkconfig telnet on
# service xinetd reload
Reloading configuration: [ OK ]
Starting with the Red Hat Enterprise Linux 3.0 release (and in CentOS
Enterprise Linux), the FTP server (wu-ftpd) is no longer available with
xinetd. It has been replaced with vsftp and can be started from
/etc/init.d/vsftpd as in the following:
# /etc/init.d/vsftpd start
Starting vsftpd for vsftpd: [ OK ]
If you want the vsftpd service to start and stop when recycling (rebooting)
the machine, you can create the following symbolic links:
# ln -s /etc/init.d/vsftpd /etc/rc3.d/S56vsftpd
# ln -s /etc/init.d/vsftpd /etc/rc4.d/S56vsftpd
# ln -s /etc/init.d/vsftpd /etc/rc5.d/S56vsftpd
Allowing Root Logins to Telnet and FTP Services :Now before
getting into the details of how to configure Red Hat Linux for root logins,
keep in mind that this is VERY BAD security. Make sure that you NEVER
configure your production servers for this type of login.
Configure Telnet for root logins :Simply edit the file /etc/securetty
and add the following to the end of the file:
pts/0
pts/1
pts/2
pts/3
18
ASCT BHOPAL Linux & Shell Programming (CS - 5006)
pts/4
pts/5
pts/6
pts/7
pts/8
pts/9
This will allow up to 10 telnet sessions to the server as root.
Configure FTP for root logins Edit the files /etc/vsftpd.ftpusers and
/etc/vsftpd.user_list and remove the 'root' line from each file.
19