OS Lab Record
Operating Systems Lab Manual
Experiment 1: Basic Linux File & Directory Operations
Experiment 2: Viewing & Editing Files 📄
Experiment 3: File Permissions & Ownership 🔐
Experiment 4: Pipes, Redirection & I/O ➡️
Experiment 5: Process Management ⚙️
Experiment 6: User & Group Management 👤
Experiment 7: Scheduling & Automation ⏰
Experiment 8: Network Configuration 🌐
Experiment 9: Shell Scripting Basics 📜
Experiment 10: Advanced Shell Scripting Exercises 📜
Experiment 11: Basic Shell Scripting Exercises 📜
Experiment 12: Package Management 📦
Experiment 13: Filesystem Management 💾
Experiment 14: Systemd Service Management 🚀
Experiment 15: Log Management 🔍
Experiment 1: Basic Linux File & Directory Operations
Objective: To demonstrate proficiency in fundamental Linux command-line operations for managing files and directories.
Tasks:
Navigate to your home directory and find its full path.
Create a new directory named project-files.
Enter the project-files directory.
Inside project-files, create three new, empty files.
Verify that the three files were created.
Create a subdirectory named backups within project-files.
Copy one of the files from project-files into the new backups directory.
Enter the backups directory and rename the copied file.
Return to your home directory.
Delete the project-files directory along with everything it contains.
Solution:
# Navigate to home directory and find its path
cd ~
pwd
# Create a new directory and enter it
mkdir project-files
cd project-files
# Create three new, empty files
touch file1.txt file2.txt file3.txt
# Verify the files were created
ls
# Create a subdirectory
mkdir backups
# Copy a file into the new directory
cp file1.txt backups/
# Enter the new directory and rename the copied file
cd backups
mv file1.txt final_file.txt
# Return to home directory
cd ~
# Delete the directory and its contents
rm -r project-files
Experiment 2: Viewing & Editing Files 📄
Objective: To practice using command-line tools to view and modify the contents of text files.
Tasks:
Create a text file with at least 20 lines of text.
Use a command to display the entire content of the file at once.
Use a command to view the file one screen at a time.
Use a command to view only the first 5 lines of the file.
Use a command to view only the last 10 lines of the file.
Open the file using a command-line editor (nano or vim) and add a new line of text. Save and exit the editor.
Verify that the new line has been added to the file.
Solution:
# Task 1: Create a text file with content
# You can use a single command to create a file with multiple lines
# For example, create a file named 'longfile.txt'
for i in {1..20}; do echo "This is line number $i." >> longfile.txt; done
# Task 2: Display the entire content
cat longfile.txt
# Task 3: View the file one screen at a time
less longfile.txt
# Task 4: View the first 5 lines
head -n 5 longfile.txt
# Task 5: View the last 10 lines
tail -n 10 longfile.txt
# Task 6: Edit the file using nano
nano longfile.txt
# Inside nano: Add a new line, then press Ctrl+X, then Y, then Enter to save and exit.
# Task 7: Verify the new line
tail longfile.txt
Experiment 3: File Permissions & Ownership 🔐
Objective: To master the Linux permission system, including how to view, interpret, and change file and directory permissions and ownership.
Tasks:
Create a new file named secret_data.txt and a directory named private_docs.
Use the ls -l command to view the default permissions and ownership of these two items.
Using the symbolic method, modify the permissions of secret_data.txt so that only the owner has read and write access, while the group and others have no permissions.
Using the octal method, modify the permissions of private_docs so that the owner has all permissions, the group has read and execute permissions, and others have no permissions.
Change the owner of secret_data.txt to another user on the system (e.g., guestuser). This will require sudo.
Change the group of private_docs to a different existing group (e.g., staff). This will also require sudo.
Verify all the changes using ls -l.
Solution:
# Task 1 & 2: Create files and view default permissions
touch secret_data.txt
mkdir private_docs
ls -l
# Task 3: Change permissions using symbolic method
chmod u=rw,go= secret_data.txt
ls -l secret_data.txt
# Task 4: Change permissions using octal method
chmod 750 private_docs
ls -ld private_docs
# Task 5: Change ownership (requires sudo)
# Replace 'guestuser' with an actual user on your system
sudo chown guestuser secret_data.txt
ls -l secret_data.txt
# Task 6: Change group ownership (requires sudo)
# Replace 'staff' with an actual group on your system
sudo chgrp staff private_docs
ls -ld private_docs
# Task 7: Verify all changes
ls -l
ls -ld private_docs
Experiment 4: Pipes, Redirection & I/O ➡️
Objective: To understand how to control the flow of data between commands using pipes and input/output redirection.
Tasks:
Use a command to redirect the output of ls -l into a new file named file_list.txt.
Use a command to append the output of ps (listing processes) to the same file_list.txt.
Use a command to count the number of lines in file_list.txt.
Create a file named my_input.txt with some text. Use a command that reads its input from this file instead of the keyboard.
Use a pipe to send the output of a command that lists a system directory to a command that sorts the list alphabetically.
Solution:
# Task 1: Redirect output to a new file
ls -l > file_list.txt
# Task 2: Append output to the same file
ps >> file_list.txt
# Task 3: Count the lines in the file
wc -l file_list.txt
# Task 4: Create a file and redirect its content as input
echo "This is the first line." > my_input.txt
echo "This is the second line." >> my_input.txt
cat < my_input.txt
# Task 5: Use a pipe to sort a list
ls /etc | sort
Experiment 5: Process Management ⚙️
Objective: To learn how to view, monitor, and control running processes in a Linux environment.
Tasks:
Use a command to list all running processes and their process IDs (PIDs).
Use a command to view a real-time, dynamic list of running processes.
Start a resource-intensive process in the background.
Use a command to view all your background and stopped jobs.
Use a command to bring the background job to the foreground.
Use a command to send the foreground job back to the background.
Terminate a process using its PID.
Solution:
# Task 1: List all running processes
ps -ef
# Task 2: View processes in real time
top
# To exit top, press 'q'
# Task 3: Start a resource-intensive process in the background
# The 'yes' command outputs 'y' infinitely, creating a busy process
yes > /dev/null &
# Task 4: View background jobs
jobs
# Task 5: Bring the job to the foreground
# Replace %1 with the job number shown by the 'jobs' command
fg %1
# Task 6: Send the foreground job back to the background
# Press Ctrl+Z to suspend the process, then use 'bg' to resume it in the background
bg
# Task 7: Terminate a process
# First, find the PID of the 'yes' process using 'ps' or 'top'
ps -ef | grep yes
# Then, kill the process using its PID
kill [PID]
# Example: kill 12345
Experiment 6: User & Group Management 👤
Objective: To learn how to create, modify, and delete user accounts and groups on a Linux system.
Tasks:
Create a new user account with a home directory.
Set a password for the new user.
Create a new group.
Add the new user to the newly created group.
Check that the user and group were created successfully.
Change the user's default shell to /bin/bash (if not already set).
Delete the user account, including their home directory.
Delete the group.
Solution:
# Task 1: Create a new user (requires sudo)
sudo useradd -m newuser
# Task 2: Set a password for the new user (requires sudo)
sudo passwd newuser
# Enter a new password when prompted
# Task 3: Create a new group (requires sudo)
sudo groupadd newgroup
# Task 4: Add the user to the new group (requires sudo)
sudo usermod -aG newgroup newuser
# Task 5: Check user and group creation
id newuser
grep newgroup /etc/group
# Task 6: Change the user's shell (requires sudo)
sudo usermod -s /bin/bash newuser
# Task 7: Delete the user and their home directory (requires sudo)
sudo userdel -r newuser
# Task 8: Delete the group (requires sudo)
sudo groupdel newgroup
Experiment 7: Scheduling & Automation ⏰
Objective: To learn how to use cron to schedule automated tasks.
Tasks:
Create a simple shell script that writes the current date and time to a file.
Make the script executable.
Open the crontab editor for the current user.
Add a new cron job entry to schedule the script to run every minute.
Wait a few minutes to confirm the job is running.
View the output file to see the results.
Remove the cron job entry to stop the automated task.
Solution:
# Task 1: Create a shell script
echo '#!/bin/bash' > my_script.sh
echo 'echo "The current time is $(date)" >> /tmp/cron_output.log' >> my_script.sh
# Task 2: Make the script executable
chmod +x my_script.sh
# Task 3: Open the crontab editor
crontab -e
# If asked, choose an editor (e.g., nano)
# Task 4: Add the cron job entry
# Add the following line to the crontab file and save
# * * * * * /bin/bash /path/to/your/my_script.sh
# Task 5 & 6: Wait and view the output
# Wait for 1-2 minutes.
# Then, view the output file to see the entries.
cat /tmp/cron_output.log
# Task 7: Remove the cron job
crontab -e
# Delete the line you added and save the file.
# Alternatively, to remove all cron jobs:
# crontab -r
Experiment 8: Network Configuration 🌐
Objective: To learn how to use command-line tools to view network settings, test connectivity, and monitor active connections.
Tasks:
View the IP address, subnet mask, and other details for all network interfaces.
Display the system's routing table to see how network traffic is directed.
Test connectivity to a website (e.g., https://www.google.com/url?sa=E&source=gmail&q=google.com).
Trace the path that packets take to reach a remote host.
View a list of all active network connections and listening ports.
Display a summary of network statistics.
Solution:
# Task 1: View IP addresses and interface details
ip addr
# Alternative/Older command: ifconfig
# Task 2: Display the routing table
ip route
# Alternative/Older command: route -n
# Task 3: Test connectivity to a website
ping google.com
# Task 4: Trace the path to a remote host
traceroute google.com
# Task 5: View all active network connections
ss -a
# Alternative/Older command: netstat -a
# Task 6: Display network statistics
ss -s
# Alternative/Older command: netstat -s
Experiment 9: Shell Scripting Basics 📜
Objective: To write simple shell scripts to automate tasks and understand basic scripting concepts.
Tasks:
Create a shell script that prints a personalized greeting to the user.
Create a script that takes a directory name as a command-line argument and creates it.
Create a script that uses a conditional statement to check if a file exists.
Create a script that uses a loop to print numbers from 1 to 5.
Make all scripts executable and run them.
Solution:
# Task 1: Create a script to print a personalized greeting
echo '#!/bin/bash' > greet.sh
echo 'echo "Hello, $USER! Welcome to the Linux lab."' >> greet.sh
chmod +x greet.sh
./greet.sh
# Task 2: Create a script to create a directory
echo '#!/bin/bash' > createdir.sh
echo 'mkdir "$1"' >> createdir.sh
chmod +x createdir.sh
./createdir.sh my_new_dir
ls -d my_new_dir
# Task 3: Create a script to check if a file exists
echo '#!/bin/bash' > checkfile.sh
echo 'if [ -f "$1" ]; then' >> checkfile.sh
echo ' echo "File $1 exists."' >> checkfile.sh
echo 'else' >> checkfile.sh
echo ' echo "File $1 does not exist."' >> checkfile.sh
echo 'fi' >> checkfile.sh
chmod +x checkfile.sh
./checkfile.sh greet.sh
./checkfile.sh non_existent_file.txt
# Task 4: Create a script with a loop
echo '#!/bin/bash' > loop.sh
echo 'for i in {1..5}; do' >> loop.sh
echo ' echo "Number: $i"' >> loop.sh
echo 'done' >> loop.sh
chmod +x loop.sh
./loop.sh
# Task 5: Clean up created files and directories
rm greet.sh createdir.sh checkfile.sh loop.sh
rmdir my_new_dir
Experiment 10: Advanced Shell Scripting Exercises 📜
Objective: To practice creating and executing more complex shell scripts that demonstrate arithmetic, user handling, and file management capabilities.
Tasks:
Create a script that performs basic arithmetic and logical comparisons on two user-provided numbers.
Write a script to display a unique list of all users currently logged into the system.
Develop a script to find and delete common temporary files from specified directories.
Solution:
1.Write a Simple shell script for basic arithmetic and logical calculations.
✅ Shell Script: calc.sh
#!/bin/bash
# Basic Arithmetic and Logical Calculations
echo "Enter first number:"
read a
echo "Enter second number:"
read b
# Arithmetic Operations
sum=$((a + b))
diff=$((a - b))
prod=$((a * b))
div=$((a / b))
mod=$((a % b))
echo "Arithmetic Calculations:"
echo "$a + $b = $sum"
echo "$a - $b = $diff"
echo "$a * $b = $prod"
echo "$a / $b = $div"
echo "$a % $b = $mod"
# Logical Comparisons
echo "Logical Comparisons:"
if [ $a -eq $b ]; then
echo "$a is equal to $b"
else
echo "$a is not equal to $b"
fi
if [ $a -gt $b ]; then
echo "$a is greater than $b"
else
echo "$a is not greater than $b"
fi
if [ $a -lt $b ]; then
echo "$a is less than $b"
else
echo "$a is not less than $b"
fi
💻 How to Run It
1. Save the script to a file named calc.sh.
Make it executable:
chmod +x calc.sh
2. Run it:
./calc.sh
2.Write a shell script to display a list of users currently logged in.
Here's a simple shell script that displays a list of users currently logged into the
system:
#!/bin/bash
# Script to display a list of users currently logged in
echo "Currently logged in users:"
who | awk '{print $1}' | sort | uniq
Explanation:
● who: Shows who is logged on.
● awk '{print $1}': Extracts the username from each line.
● sort: Sorts the usernames alphabetically.
● uniq: Removes duplicate entries.
To use the script:
1. Save the script to a file, for example: logged_in_users.sh
Make it executable:
chmod +x logged_in_users.sh
2. Run it:
./logged_in_users.sh
3. Let me know if you want to include login times or terminals as well.
3.Write a shell script to delete all the temporary files.
#!/bin/bash
# Script to delete all temporary files
# Define directories to search for temporary files
TEMP_DIRS=("/tmp" "$HOME/tmp" "$HOME")
echo "Deleting temporary files..."
# Delete common temp files from these directories
for dir in "${TEMP_DIRS[@]}"; do
if [ -d "$dir" ]; then
echo "Checking $dir ..."
# Delete files ending in ~ or .tmp
find "$dir" -type f \( -name "*.tmp" -o -name "*~" \) -exec rm -f {} \;
fi
done
echo "Temporary files deleted."
Explanation:
● TEMP_DIRS: A list of directories to search for temp files.
● find: Searches for files matching patterns (*.tmp, *~) and deletes them.
● rm -f: Forcefully removes the matched files without prompting.
To use the script:
1. Save it as delete_temp_files.sh
Make it executable:
chmod +x delete_temp_files.sh
2. Run it:
./delete_temp_files.sh
Experiment 11: Basic Shell Scripting Exercises 📜
Objective: To practice creating and executing simple shell scripts that use for loops and handle user input.
Tasks:
Create a script to print numbers from 1 to 10.
Create a script to print the multiplication table for a number provided by the user.
Create a script to print numbers from 25 down to 1.
Solution:
# Script 1: Print Numbers 1-10
# Save this code to a file named 'print_numbers.sh'
#!/bin/bash
for (( i=1; i<=10; i++ ))
do
echo $i
done
# Script 2: Multiplication Table
# Save this code to a file named 'multiplication_table.sh'
#!/bin/bash
echo "Enter the number for the multiplication table:"
read num
for (( i=1; i<=10; i++ ))
do
product=$((num * i))
echo "$num x $i = $product"
done
# Script 3: Print Numbers 25-1
# Save this code to a file named 'print_reverse_numbers.sh'
#!/bin/bash
for (( i=25; i>=1; i-- ))
do
echo $i
done
Execution:
Save each script block to its own file (e.g., print_numbers.sh, multiplication_table.sh, print_reverse_numbers.sh).
Make all the files executable: chmod +x print_numbers.sh multiplication_table.sh print_reverse_numbers.sh
Run the scripts:
./print_numbers.sh
./multiplication_table.sh
./print_reverse_numbers.sh
Experiment 12: Package Management 📦
Objective: To learn how to use the apt package manager to search for, install, update, and remove software on a Debian/Ubuntu-based system.
Tasks:
Update the list of available packages from the repositories.
Search for a specific package (e.g., git).
Install the git package.
Verify that git was installed successfully by checking its version.
Remove the git package.
Clean up any unused dependencies left behind.
Upgrade all installed packages to their latest versions.
Solution:
# Task 1: Update the package list
sudo apt update
# Task 2: Search for a package
apt search git
# Task 3: Install a package
sudo apt install git
# Task 4: Verify the installation
git --version
# Task 5: Remove the package
sudo apt remove git
# Task 6: Clean up unused dependencies
sudo apt autoremove
# Task 7: Upgrade all packages
sudo apt upgrade
.
Experiment 13: Filesystem Management 💾
Objective: To learn how to check disk space and manage file systems. This experiment uses a loopback device to create a virtual filesystem without modifying physical storage.
Tasks:
Use a command to create an empty file that will serve as a virtual disk.
Format this file with a file system.
Create a directory to be the mount point for the file system.
Mount the formatted file to the mount point.
Use a command to check the overall disk space and confirm the new filesystem is listed.
Use a command to check the disk usage of the mounted directory.
Unmount the file system.
Clean up the files and directories created.
Solution:
# Task 1: Create an empty file (e.g., 50MB)
dd if=/dev/zero of=virtual_disk.img bs=1M count=50
# Task 2: Format the file as a filesystem (e.g., ext4)
mkfs.ext4 virtual_disk.img
# Task 3: Create a mount point
mkdir my_mount_point
# Task 4: Mount the virtual filesystem
sudo mount virtual_disk.img my_mount_point/
# Task 5: Check overall disk space
df -h
# Task 6: Check disk usage of the directory
du -sh my_mount_point/
# Task 7: Unmount the filesystem
sudo umount my_mount_point/
# Task 8: Clean up
rm virtual_disk.img
rmdir my_mount_point
Experiment 14: Systemd Service Management 🚀
Objective: To learn how to manage system services and daemons using the systemctl command.
Tasks:
Use a command to check the status of a service (e.g., ssh.service).
Stop the service.
Check the status again to confirm it is inactive.
Start the service.
Check the status to confirm it is running.
Use a command to disable the service so it doesn't start automatically on boot.
Use a command to re-enable the service.
Use a command to list all currently running services.
Solution:
# Task 1: Check the status of a service
systemctl status ssh.service
# Task 2: Stop the service (requires sudo)
sudo systemctl stop ssh.service
# Task 3: Check status again
systemctl status ssh.service
# Task 4: Start the service (requires sudo)
sudo systemctl start ssh.service
# Task 5: Check status to confirm it's running
systemctl status ssh.service
# Task 6: Disable the service from starting at boot (requires sudo)
sudo systemctl disable ssh.service
# Task 7: Enable the service to start at boot (requires sudo)
sudo systemctl enable ssh.service
# Task 8: List all active services
systemctl list-units --type=service --state=active
Experiment 15: Log Management 🔍
Objective: To analyze system logs to diagnose and troubleshoot system issues.
Tasks:
Use a command to view all system logs from the beginning.
Filter the logs to show only entries from a specific service (e.g., ssh.service).
View the most recent logs and follow them in real-time.
View a log file directly from the /var/log directory.
Search for a specific keyword within the system logs.
Solution:
# Task 1: View all logs from the beginning
sudo journalctl | less
# Task 2: Filter logs for a specific service
sudo journalctl -u ssh.service
# Task 3: View recent logs and follow new entries
sudo journalctl -f
# Task 4: View a log file in /var/log
# The file may vary based on your system.
sudo cat /var/log/auth.log
# Or use 'less' to view large files
# sudo less /var/log/syslog
# Task 5: Search for a keyword in the logs
sudo journalctl | grep "error"
Comments
Post a Comment