Linux Manual
OS Labs
Introduction to Linux
What is Linux?
● Linux is an open-source, Unix-like operating system developed by Linus
Torvalds in 1991. It is free to use, modify, and distribute.
● It is modular and consists of the kernel, which interacts directly with hardware,
and user space, which contains system utilities and programs.
● Linux is at the heart of many modern technologies, from web servers to
smartphones, thanks to its stability, flexibility, and security.
Introduction to Linux
Key Linux Distributions
● Ubuntu: Known for its user-friendliness and ease of use. It’s perfect for
beginners and is widely used in academic environments.
● CentOS/RHEL: These are enterprise-grade Linux distributions widely used
for server environments.
● Debian: Highly stable, often used for servers and cloud applications.
● Kali Linux: Focused on penetration testing and security auditing.
● Arch Linux: For advanced users who want to build their own Linux system
from the ground up.
Introduction to Linux
Basic Linux Terminology
● Kernel: The core part of the Linux system, responsible for managing
hardware and system resources.
● Shell: The command-line interface that allows users to interact with the
system. Common shells are bash and zsh.
● Terminal: A program that emulates a console, allowing users to interact with
the shell.
● Package Manager: Tool for installing, updating, and removing software
packages. Examples: apt (Ubuntu), dnf (Fedora), yum (CentOS).
Linux Architecture
The Boot Process
● BIOS/UEFI: The computer’s firmware loads the bootloader from disk.
● Bootloader: GRUB (Grand Unified Bootloader) loads the Linux kernel.
● Kernel Initialization: The kernel initializes the hardware and sets up
essential processes like init or systemd.
● User Space Initialization: Once the kernel is loaded, systemd (or init) starts
system services and mounts the file system.
Installation of Linux
Pre-Installation Setup
● Before installing Linux, ensure that the machine meets the following minimum
requirements:
○ RAM: At least 2GB.
○ Disk Space: At least 10GB of free space.
○ Processor: 1 GHz or faster.
Installation Steps:
● Download Linux ISO: Choose a distribution (e.g., Ubuntu, CentOS) and
download the ISO file.
● Create a Bootable USB: Use tools like Rufus or UNetbootin to create a
bootable USB drive.
● Boot from USB: Insert the USB into the machine and boot from it. Follow the
on-screen prompts to install Linux.
Installation of Linux
Partitioning the Disk
During installation, you'll be asked to partition your disk.
● Primary partition: The main partition that contains the OS.
● Swap space: Virtual memory used when RAM is full.
Recommended Partition Scheme for Ubuntu:
● /: Root partition, contains the operating system.
● /home: User data partition.
● swap: Swap partition for virtual memory.
Example
● sudo fdisk /dev/sda
Linux File System
File System Hierarchy
The Linux file system follows a hierarchical structure, with a single root directory /
at the top. Everything else is part of this hierarchy, including devices, files, and
user directories.
Common Directories
Directory Purpose
/ The root directory. All files are located under /.
/boot Essential command binaries for system booting.
/sbin System binaries for administrative tasks.
/home User directories. Each user has their own subdirectory here.
Linux File System
Common Directories
Directory Purpose
/etc Configuration files for system services.
/var Variable files, such as logs.
/tmp Temporary files.
/dev Device files that represent hardware (e.g., /dev/sda for the hard drive).
Linux File System
File Permissions
● Permissions define who can read, write, or execute files and directories.
● Linux use symbolic representations for permissions.
● Symbolic:
○ r (read), w (write), x (execute)
● Example
○ chmod 755 file.txt # rwxr-xr-x
○ chmod u+x file.txt # Adds execute permission to the user
Basic Linux Commands
File Compression
Navigating Directories
● tar: Create and extract compressed
● pwd: Shows the current directory. archives.
● ls: Lists the contents of a directory. Use ○ tar -cvf archive.tar /path/to/directory
(Create)
ls -l for detailed listing.
○ tar -xvf archive.tar (Extract)
● cd: Change the current directory.
● cd ~: Goes to the home directory. Searching Files
Working with Files ● find: Search for files and directories.
○ find /home/user -name "*.txt" (Find all .txt
● cat: Display the content of a file. files in the directory)
● cp: Copy files from one location to ● grep: Search the contents of files.
another. ○ grep "pattern" file.txt (Search for a pattern in
a file)
● mv: Move or rename files.
● rm: Remove files or directories.
User and Group Management
Navigating Directories Viewing Users and Groups
● Adding a user ● List users:
○ sudo adduser username ○ cat /etc/passwd
● Changing a user's password ● List groups:
○ sudo passwd username ○ cat /etc/group
● Deleting a user:
○ sudo deluser username
Managing Groups
● Creating a group
○ sudo groupadd groupname
● Adding a user to a group
○ sudo usermod -aG groupname username
Process Management
Viewing Processes
● ps: Shows the currently running processes.
○ ps aux: Show all processes.
○ ps -ef: Show processes with more detail.
● top: Real-time process monitoring tool.
Managing Processes
● Kill a process:
○ kill -9 PID # Use the PID to kill a specific process
● Send a process to the background
○ command &
● Bring a process back to the foreground
○ fg %1 # %1 represents the job number
Shell Scripting
Introduction to Scripting
● Shell scripting allows automating repetitive tasks like backups, file
management, etc.
● Scripts begin with a shebang (#!/bin/bash) to specify the interpreter.
Variables and Control Flow
Variables If-Else Conditional: For Loop:
name="Linux"
echo "Hello, $name"
Scheduling Tasks
Cron Jobs for Automated Backups
● You can automate backups by creating cron jobs to run backup commands at scheduled intervals.
* * * * * /path/to/script.sh
|||||
| | | | +--- Day of the week (0-6)
| | | +----- Month (1-12)
| | +------- Day of the month (1-31)
| +--------- Hour (0-23)
+----------- Minute (0-59)
● Command: cronetab -e
Networking Basics
Networking Tools:
● ifconfig: View network interface configuration.
● ping: Test network connectivity.
○ ping ip address/domain name
● ssh: Secure shell access to remote machines.
○ ssh user@remote_host
● scp: Secure copy for transferring files.
○ scp file.txt user@remote:/path/to/destination
Getting Started with C++
Your First C++ Program
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
Compiling and Running
● Save as hello.cpp
● Compile: g++ hello.cpp -o hello
● Run: ./hello (Linux/Mac) or hello.exe (Windows)
Basic C++ Concepts
Variables and Data Types Input/Output
int age = 25; #include <iostream>
int main() {
float price = 19.99; int number;
std::cout << "Enter a number: ";
double pi = 3.1415926535; std::cin >> number;
std::cout << "You entered: " << number << std::endl;
char grade = 'A';
return 0;
bool is_valid = true; }
Control Structures // For loop
for (int i = 0; i < 5; i++) {
// If-else
std::cout << i << " ";
if (x > 10) {
}
std::cout << "x is greater than
10"; // While loop
} else { int i = 0;
std::cout << "x is 10 or less"; while (i < 5) {
} std::cout << i << " ";
i++;
}
Functions
#include <iostream>
// Function declaration // Function definition
int add(int a, int b); int add(int a, int b) {
int main() { return a + b;
int result = add(5, 3); }
std::cout << "5 + 3 = " << result <<
std::endl;
return 0;
}
Object-Oriented Programming Basics
2 public:
1 #include <iostream>
// Constructor
#include <string>
Person(std::string n, int a) : name(n), age(a) {}
class Person {
// Method
private:
void introduce() {
std::string name;
std::cout << "Hello, my name is " << name
int age;
<< " and I'm " << age << " years old." <<
std::endl;
3 int main() {
}
Person person1("Alice", 30);
};
person1.introduce();
return 0; }
Process Creation with Fork()
#include <iostream>
#include <unistd.h> // for fork(), getpid(), getppid()
#include <sys/wait.h> // for wait()
int main() {
std::cout << "Main process started (PID: " << getpid() << ")\n";
// Create a child process
pid_t pid = fork();
if (pid < 0) {
// Error occurred
std::cerr << "Fork failed!\n";
return 1; }
Process Creation with Fork()
else if (pid == 0) {
// Child process
std::cout << "Child process (PID: " << getpid() << ", Parent PID: " << getppid() << ")\n";
// Child-specific work
for (int i = 1; i <= 5; i++) {
std::cout << "Child counting: " << i << "\n";
sleep(3);
std::cout << "Child process exiting\n";
exit(0); // Terminate child
}
Process Creation with Fork()
else {
// Parent process
std::cout << "Parent process (PID: " << getpid() << ", Child PID: " << pid << ")\n";
// Wait for child to complete
int status;
waitpid(pid, &status, 0);
std::cout << "Child process exited with status: " << WEXITSTATUS(status) << "\n";
std::cout << "Parent process exiting\n";
return 0;