Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Btech Linux Experiment

we covered various Linux commands, system administration tasks, and advanced topics such as Linux installation, scripting, and programming. We discussed basic commands like ls, cd, grep, and awk, as well as system administration tasks like user management, package installation, and backups.

Uploaded by

Shashi shekhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Btech Linux Experiment

we covered various Linux commands, system administration tasks, and advanced topics such as Linux installation, scripting, and programming. We discussed basic commands like ls, cd, grep, and awk, as well as system administration tasks like user management, package installation, and backups.

Uploaded by

Shashi shekhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Lab-1: Familiarization with Unix/Linux Logging, Logout,

and Simple Commands

TOOLS/LIABILITIES/SOFTWARE USED:
 Bash (Bourne Again Shell)
 Linux Distribution(kali)
HARDWARE USED:
 Standard PC
 Virtual box

login

Description:

The login command is used to start a new session by authenticating the


user.

Usage:

To login, simply type login and press Enter.


Example:

$ login
Username: subodh
Password:

logout
Description:
The logout command is used to end the current session and log the
user out of the system.

Usage:

To logout, simply type logout and press Enter.

Example:
You have been successfully logged out.

who
Description:The who command displays information about users who
are currently logged into the system.

Usage:Type who and press Enter to display the list of logged-in users.

Example:

ls
Description:
The ls command lists the files and directories in the current working
directory.

Usage:

Type ls and press Enter to display the list of files and directories in the
current directory

Example:
tree
Description:
The tree command displays the directory structure in a tree-like format,
starting from the specified directory.

Usage:
Type tree [directory] and press Enter to display the directory structure.

Example:

pwd
Description:
The pwd command prints the current working directory.

Usage:

Type pwd and press Enter to display the current working directory.

Example:
cd
The cd command, short for "change directory," is used to navigate
between different directories in a file system. It allows you to move from
one directory to another within the file system hierarchy.

Syntax:

Parameters:

 directory_path: Specifies the path of the directory you want to


navigate to. This can be an absolute path (starting from the root
directory) or a relative path (relative to the current directory).

mkdir
Description:
The mkdir command is used to create new directories.

Usage:
 Type mkdir [directory_name] and press Enter to create a new
directory.
Example:

rmdir
Description:
The rmdir command is used to remove empty directories.

Usage:

 Type rmdir [directory_name] and press Enter to remove an


empty directory.

Example:

cal
Description:
The cal command displays a calendar for the specified month and year.

Usage:

 Type cal [month] [year] and press Enter to display the calendar.

Example:
date
Description:
The date command displays the current date and time.

Usage:

 Type date and press Enter to display the current date and time.

Example:
Lab-2: Familiarize with Vim editor and Linux GUIs

Vi Editor:

There are three types of editors:

 ED
 EX
 VI

Ed and ex are line editors, meaning line numbers are assigned to the
lines in a file. Vi, on the other hand, is a text editor or screen editor. It
displays as much of the file as it can fit on the screen, allowing the user
to view and edit the entire document at once. Creating and editing files
becomes easier using the vi editor.

Disadvantages of Vi:

 There are several self-explanatory error messages in vi if anything


goes wrong.
 No help available in vi.
 Vi operates in three modes (Command, Insert, Ex mode), and the
same key creates different effects in each mode.
 Vi is key-sensitive and handles plain text with no styles by default.
It does not support graphics, only simple text files.

Modes of Operation:

 Command mode: In this mode, keys pressed by the user are


interpreted as editor commands, such as moving the cursor left,
right, up, or down. The keys pressed are not displayed on the
screen.
 Insert mode: This mode permits the insertion of new text and
editing existing text. Insertion mode is also known as input mode.
 Execution mode: It permits giving commands at the command
line. The bottom line of the screen is called the command line.
Starting Vi or to Invoke Vi:

To create a file:

To invoke vi, type vi followed by the filename you want to create. When
you type vi and the filename and press Enter, vi clears the screen and
displays a window where you can enter and edit text.
To Add Text to a File:

Press the ' I ' key to enter insert mode.

To add text to a file, follow these steps:

1. Press the 'I' key.


2. Type the text (plus Enter to begin on a fresh line). The text typed
goes into a place in memory as a buffer.
3. Press the Esc key after finishing the text to leave insert mode and
return to command mode.

To Save the File to Disk:

To save the file and quit from vi, hold the Shift key and press ZZ (Shift +
ZZ). Another way to save a file is to use :wq for saving and quitting.
These commands are given in Ex command mode. You can combine
two commands to save and quit.
To Edit a File:

To edit an existing file, you can add, change, and delete text. Before
performing these tasks, you must load the file from the disk into the
buffer and then move to the part of the file you want to edit.

Steps for Editing a File:

1. Loading the file: vi filename


2. Move the cursor to the point on the screen where you want to
begin the correction. Use the following keys:
 h: Moves the cursor one character to the left.
 l: Moves the cursor one character to the right.
 j: Moves the cursor down one line.
 k: Moves the cursor up one line.
3. Instead of pressing a motion command key multiple times, you can
use a command like 2l to move two spaces to the right.

Editing Text in an Existing File:

To add text to the end of the file:

1. Position the cursor on the last character in the file using cursor
movement keys.
2. Press 'a' to switch from command to text input mode (where 'a'
stands for append).
3. Press the Esc key.

To add text in the middle of existing text:

1. Position the cursor where you wish to add the text.


2. Press 'I' to indicate that you wish to insert text before the current
position ('I' stands for insert).
3. Vi will accept text until you press the Esc key.

Editing Text in an Existing File:

Move the cursor to the character you want to delete and press the 'X'
key (where 'X' is used to delete a character). To erase three characters,
give the command 3X.
To Overwrite Text:

1. Position the cursor at the character from where you want to begin
overwriting.
2. Press 'R' to indicate that whatever you type next should overwrite
the existing text.
3. Press the Esc key when you finish replacing the text.

Undoing Changes:

 u (small 'u'): Undo the most recent change or deletion.


 U (capital 'U'): Undo all changes to the current line since you
moved the cursor to that line.

Quitting from Vi:

1. To save changes and quit from vi: Shift + ZZ in command mode,


:wq in Ex mode.
2. To quit vi without saving changes: q! in Ex mode.

Some Commands Related to Vi:

 O (capital 'O'): Used for opening a new line or a blank line.


 Find and Replace Commands:
 :s/Str1/Str2 : Replace the first occurrence of Str1 with Str2 in
the current line.
 :/Str1/Str2/g: Replace all occurrences of Str1 with Str2 in the
current line.

Basic Rules of Using Vim Editor:

 Press 'i' to write anything in the text file.


 Press Esc to move to read-write command mode.
 Press :w to save the written text.
 Press :q to quit.
 In visual mode, press 'v' to copy data, then press 'y'. In normal
mode, press 'p' to paste.
 Press 'u' in normal mode to undo changes.
Lab-3: Using Bash Shell to Develop Simple Shell Programs

1. Arithmetic Operations:

Description: This script performs basic arithmetic operations such as


addition, subtraction, multiplication, and division on two numbers entered
by the user.

Commands:

 echo: Prints messages or variables.


 read: Reads input from the user.
 expr: Evaluates expressions.
 \*: Escapes the asterisk (*) character for multiplication.
 /: Division operator.

Script:
OUTPUT

2. Finding the Factorial of a Number Without Using :

Description: This script calculates the factorial of a number entered by


the user without using the bc command for floating-point arithmetic.

Commands:

 echo: Prints messages or variables.


 read: Reads input from the user.
 expr: Evaluates expressions.
 while: Looping construct.

Script:
Explanation:

1. The user is prompted to enter a number.


2. The script initializes the factorial fact to 1 and the loop counter i to
2.
3. A while loop is used to calculate the factorial iteratively.
4. In each iteration of the loop, the value of fact is updated by
multiplying it with the current value of i.
5. The loop counter i is incremented by 1 in each iteration.
6. Once the loop condition ($i -le $num) is no longer satisfied, the loop
terminates.

3. Finding the Largest Among Three Numbers:

Description: This script compares three numbers entered by the user


and determines which one is the largest.

Commands:

 echo: Prints messages or variables.


 read: Reads input from the user.
 test: Evaluates conditional expressions.
 if, elif, else, fi: Conditional constructs for branching.

Script:

Lab-4: Develop Advanced Shell Programs using awk and


grep

1. Grep Commands:

Description: This script demonstrates the usage of the grep command


to search for a particular pattern of characters in a shashi and display all
lines that contain that pattern.

Commands:

 grep: Searches a shashi for a particular pattern of characters.

Options:
 -c: Prints only a count of the lines that match a pattern.
 -h: Displays the matched lines, but does not display the
 -i: Ignores case distinctions in patterns and input data.
 -l: Displays list of shashinames only.
 -n: Display the matched lines and their line numbers.
 -v: Prints out all the lines that do not match the pattern.
 -e exp: Specifies expression with this option. Can be used
multiple times.
 -f shashi: Takes patterns from shashi, one per line.
 -w: Match whole word.
 -o: Print only the matched parts of a matching line, with each
such part on a separate output line.
 -A n: Prints searched line and n lines after the result.
 -B n: Prints searched line and n line before the result.
 -C n: Prints searched line and n lines after the result.

Script:

Text file
COMMANDS:
2. Using Awk for Data Manipulation and Reporting:

Description: This script showcases the usage of the awk command for
manipulating data and generating reports.

Commands:

 awk: A scripting language used for manipulating data and


generating reports.
 Syntax: awk options 'selection_criteria {action }' input-shashi
> output-shashi

Script:

# Example usage of awk command

# Print the first column of a CSV file


awk -F',' '{print $1}' data.csv

# Print lines where the second column is greater than 50


awk '$2 > 50' data.txt

# Print lines containing the word "example"


awk '/example/' file.txt

# Calculate the sum of numbers in the second column


awk '{sum += $2} END {print "Sum of column 2:", sum}' data.txt

Output :

 Print the first column of a CSV file:


shashi
sourav
sanand

 Print lines where the second column is greater than 50:


Shashi 60
sourav 62
sanand 75

 Print lines containing the word "example":

This line contains the word example


Another example line

 Calculate the sum of numbers in the second column:

Sum of column 2: 210

These scripts demonstrate the powerful functionalities of grep and awk


commands in shell scripting, allowing users to efficiently search for
patterns in shashis and perform data manipulation and reporting tasks.
They serve as valuable tools for processing and analyzing data in
various Unix-like environments.
Lab-5: Compile and Debug Various C Programs Using
Different Options

Objective: The objective of this lab is to familiarize yourself with compiling and debugging C
programs using different options available in the compiler. By the end of this lab, you should
be comfortable compiling C programs and using debugging options to identify and resolve
errors.

Tools/Software Required:

 Text editor (e.g., Vim, Emacs, Sublime Text)

Lab Exercises:

Creating c file in vi:


1. Compiling a Simple C Program

Description: Compile a simple "Hello, World!" C program without any optimization flags.

Commands:

gcc -o hello hello.c

Expected Output: If there are no syntax errors in the program, the compiler should
generate an executable file named "hello".

2. Debugging a C Program

Description: Introduce an error in the "Hello, World!" program and attempt to compile it with
debugging symbols.

Commands:

gcc -o hello_debug -g hello.c

Expected Output: The compiler should generate an executable file named "hello_debug"
with debugging symbols included.
3. Enabling All Warnings

Description: Compile the "Hello, World!" program with all warning messages enabled to
catch potential issues.

Commands:

gcc -o hello_warnings -Wall hello.c

Expected Output: The compiler should display warning messages if there are any potential
issues in the code.

4. Optimizing Compilation

Description: Compile the "Hello, World!" program with optimization flags enabled.

Commands:

gcc -o hello_optimized -O3 hello.c

Expected Output: The compiler should generate optimized machine code for better
performance.

5. Static Analysis with AddressSanitizer

Description: Compile the "Hello, World!" program with AddressSanitizer enabled for
detecting memory errors.

Commands:

Expected Output: The compiler should generate an executable file with AddressSanitizer
enabled for runtime memory error detection.

6. Profiling Compilation

Description: Compile the "Hello, World!" program with profiling flags enabled for
performance analysis.

Commands:

gcc -o hello_asan -fsanitize=address hello.c


Expected Output: The compiler should generate an executable file with profiling support for
analyzing program execution time.

Conclusion: In this lab, you learned how to compile and debug C programs using various
options available in the compiler. Understanding these options is essential for efficient
development and debugging of C programs.
LAB-6: Learning of Installation and Upgradation of Linux
Operating System

Objective: The objective of this lab is to familiarize yourself with the process of installing
and upgrading a Linux operating system. By the end of this lab, you should be able to
perform a fresh installation of Linux and upgrade an existing Linux installation.

Tools/Software Required:

 Linux distribution ISO file (e.g., Ubuntu, Fedora, CentOS)


 Virtualization software (e.g., VirtualBox, VMware)
 Bootable USB drive (optional)
 Internet connection (for updates and upgrades)

Lab Exercises:

1. Preparing for Installation

Description: Download the ISO file of the Linux distribution you want to install. Optionally,
create a bootable USB drive using tools like Rufus (for Windows) or dd command (for Linux).
If you're using a virtual machine, skip this step.

Commands:

Expected Output: The ISO file is downloaded and/or the bootable USB drive is created
successfully.

2. Installing Linux

Description: Boot your computer from the bootable USB drive or start your virtual
machine. Follow the on-screen instructions to begin the installation process. Choose
appropriate options such as language, keyboard layout, disk partitioning, and installation
type (e.g., minimal, desktop). Complete the installation process by setting up a user account
and configuring network settings.

Commands: No specific commands, as the installation process is mostly GUI-based.

Expected Output: A successful installation of the Linux operating system.

3. Performing System Updates

Description: After installing Linux, perform system updates to ensure that your system is
up-to-date with the latest security patches and software updates. Use the package manager
provided by your Linux distribution to update the system.
Commands:

# Update package repositories

Expected Output: The system updates are successfully installed.

4. Upgrading Linux Distribution

Description: If you're using an existing Linux installation and want to upgrade to a newer
version, follow the upgrade process provided by your distribution. This may involve using
package managers like apt or dnf to upgrade packages, or running specific commands
provided by the distribution.

Commands:

# Upgrade to a newer version

sudo apt full-upgrade -y

Expected Output: The Linux distribution is successfully upgraded to the newer version.

Conclusion: In this lab, you learned the process of installing and upgrading a Linux
operating system. By following these steps, you can set up a new Linux installation or
upgrade an existing one with ease.
Lab 7 :Install Linux Alongside an Existing Operating
System

Objective: The objective of this lab is to guide you through the process of installing Linux on
VirtualBox, a virtualization software, without the need for a bootable USB drive. By the end
of this lab, you should have a fully installed Linux system running within VirtualBox.

Tools/Software Required:

 VirtualBox (Download and install from virtualbox.org)


 Linux distribution ISO file (e.g., Ubuntu, Fedora, CentOS)

Lab Exercises:

1. Download Linux ISO


 Description: Download the ISO file of the Linux distribution you want to
install from its official website or a trusted source.
 Commands/Steps:
 Go to the official website of the Linux distribution.
 Download the ISO file for your desired distribution.
2. Install VirtualBox
 Description: Download and install VirtualBox, a virtualization software that
allows you to run guest operating systems on your host machine.
 Commands/Steps:
 Go to virtualbox.org.
 Download the appropriate version of VirtualBox for your operating
system.
 Install VirtualBox by following the on-screen instructions.
3. Create a New Virtual Machine
 Description: Use VirtualBox to create a new virtual machine for installing
Linux.
 Commands/Steps:
 Open VirtualBox.
 Click on "New" to create a new virtual machine.
 Follow the wizard to set up the virtual machine, including specifying
the OS type and version.
4. Allocate Resources
 Description: Allocate resources such as RAM and virtual hard disk space for
the virtual machine.
 Commands/Steps:
 Customize the virtual machine settings, including RAM size, CPU
cores, and virtual disk size.
5. Mount the Linux ISO
 Description: Mount the Linux ISO file as a virtual optical drive in VirtualBox.
 Commands/Steps:
 In the virtual machine settings, navigate to the "Storage" tab.
 Select the empty optical drive and choose "Choose a disk file".
 Browse and select the Linux ISO file you downloaded.
6. Start the Virtual Machine
 Description: Start the virtual machine and boot from the mounted Linux ISO
file.
 Commands/Steps:
 Start the virtual machine.
 It should boot from the mounted ISO file and launch the Linux
distribution's installation process.
7. Install Linux
 Description: Follow the on-screen instructions to install Linux on the virtual
machine.
 Commands/Steps:
 Follow the installation wizard, choosing language, keyboard layout,
and installation type.
 Partition the virtual disk and select the appropriate options for your
installation.
8. Complete Installation
 Description: After the installation is complete, restart the virtual machine and
configure your Linux system.
 Commands/Steps:
 Restart the virtual machine to complete the installation process.
 Follow the initial setup prompts to configure your Linux system.

Conclusion: In this lab, you learned how to install Linux on VirtualBox without the need
for a bootable USB drive. By creating a virtual machine and mounting the Linux ISO file, you
were able to install and configure Linux within a virtualized environment.
Lab-8 : System Administration Tasks and Scripting

Objective: The objective of this lab is to familiarize you with various system
administration tasks such as user account management, package installation, backups, and
scripting for file and user management. Additionally, you will learn how to create startup and
shutdown scripts using tools like at and cron.

Tools/Software Required:

 Linux distribution (e.g., Ubuntu, CentOS)


 Terminal or command line interface

Lab Exercises:

1. User Account Management

Description: As a supervisor, you will create and maintain user accounts on the system.

Commands/Steps:

# Create a new user


# Delete a user

Expected Output: New user account is created and old user account is deleted
successfully.

2. Package Installation

Description: Install packages using package managers like apt (for


Debian-based systems) or yum (for Red Hat-based systems).

Commands/Steps:

# Update package lists

sudo apt update


# Install a package

sudo apt install package_name

Expected Output: Package is installed successfully.

3. Taking Backups

Description: Create backups of important files or directories using tools like tar or rsync.

Commands/Steps:

# Create a tar archive of a directory

tar -czvf backup.tar.gz /path/to/directory


Expected Output: Backup is created successfully.
4. Scripting for File and User Management

Description: Write shell scripts to automate file and user management tasks.

Script Example:

#!/bin/bash

# Create a new user

sudo adduser newuser

# Backup important files

tar -czvf backup.tar.gz /important/directory

# Clean up temporary files

rm -rf /tmp/*

Expected Output: Script executes successfully, creating a new user, taking backups, and
cleaning up temporary files.

5. Startup and Shutdown Scripts with at and cron

Description: Schedule tasks to run at startup and shutdown using at and cron.

Commands/Steps:

# Schedule a task to run at startup

echo "backup_script.sh" | sudo tee -a /etc/init.d/startup_script.sh

sudo update-rc.d startup_script.sh defaults

# Schedule a task to run at shutdown

echo "cleanup_script.sh" | sudo tee -a /etc/init.d/shutdown_script.sh

sudo update-rc.d shutdown_script.sh stop 20 0 6 .


Expected Output: Tasks are scheduled to run at startup and shutdown as configured.

Conclusion: In this lab, you learned essential system administration tasks such as user
account management, package installation, backups, and scripting for automation.
Additionally, you learned how to schedule tasks to run at startup and shutdown using at and
cron. These skills are fundamental for effective system administration and automation in a
Linux environment.

You might also like