Review and Practical Scenarios
مراجعة شاملة وأمثلة عملية على إدارة المستخدمين، الصلاحيات، والتعامل مع الملفات (`grep`, `tar`).
ملخص المحاضرة
🐧 Lecture 1: Introduction to Linux and OS Concepts
This lecture introduces the fundamental concepts of an operating system (OS) and the specific components of the Linux ecosystem.
What is an Operating System?
- An OS is a program that manages all of the computer's software and hardware. It is loaded by a boot program when the computer starts.
- It acts as a communicator between the hardware and the application software, allowing programs to run.
- The system is layered:
- Hardware (The physical components)
- System Software (The OS itself, like Linux, macOS, Windows)
- Application Software (Programs the user interacts with, like Chrome or games)
- The communication flow is: User <-> Applications <-> Operating System <-> Hardware.
The Linux System
- Linux is the general name for an operating system that uses the Linux Kernel.
- The Kernel: This is the core part of the OS. Its main job is to connect to and manage the hardware (like the CPU, Memory/RAM, and VGA).
- Modules: In Linux, hardware drivers (software for devices like the mouse, keyboard, or camera) are called modules. The kernel uses these modules to translate user-level software commands into signals for the hardware.
Core Parts of a Linux System
A complete Linux system (often called a "Distribution") is made of several parts:
- Kernel: The core that manages hardware.
- GNU & Tool Chain: These are the essential libraries and tools that allow applications to run and communicate with the kernel.
- Graphical Environment: This provides the visual desktop, including menus, windows, and icons. Examples include:
- Gnome: The most popular and simple environment.
- KDE (Plasma): Easier to customize but can be more complicated.
- Cinnamon and Mate.
- Package Manager: This is the top layer used to install (setup) and manage applications and programs.
Why Use Linux?
Linux is widely used due to several key advantages:
- Costless: It is free to use and distribute.
- Open Source: The source code is available for anyone to view, modify, and improve.
- Stable & Reliable: Known for running for long periods without crashing.
- Highly Secure: The architecture and open-source nature make it less vulnerable to viruses.
- Extremely Powerful: It is efficient and widely used in servers and supercomputers.
🐧 Lecture 2: Linux Desktop (GUI) and User Management
This lecture explores the components of the Ubuntu graphical user interface (GUI) and introduces the different types of users in Linux, focusing on how to manage them via the GUI.
The Ubuntu Desktop Interface
The Ubuntu desktop is composed of several key areas:
- Top Bar:
- Activities: A button in the top-left corner that, when clicked, allows you to access all your open windows and applications.
- Clock & Calendar: Located in the center. Clicking it shows the current date, a calendar, and a list of appointments and notifications.
- System Information Menu: Located in the top-right. This menu provides quick access to network connections (Wi-Fi), Bluetooth, battery status, sound volume, system preferences (Settings), and the Power Off/Log Out options.
- The Dash (Side Bar):
- This is the vertical bar on the left side that holds program icons.
- Icons can be added, removed, or reordered.
- It includes the Files/Folders icon (to access directories and devices like USBs) and the Shopping Bag icon (to download new software).
Types of Users
Linux has three main types of users, each with different permissions:
- Normal User: Has limited permissions and cannot make significant system changes.
- Privileged User: A user who can use the
sudocommand to temporarily gain root privileges for specific tasks. - Root User: The administrator account that has all privileges and can make any system change.
Managing Users via GUI
You can create, delete, and manage users graphically:
- Access Settings: Click the System Information menu (top-right) and select Settings.
- Open User Panel: In the Settings window, scroll down and click Users.
- Unlock: You must click the Unlock button (top-right) and enter the administrator password to make changes.
- Add User: Click the Add User... button.
- Set Account Type: You can choose between:
- Standard: A limited account (Normal user).
- Administrator: A privileged account.
- Set Details: Fill in the user's Full Name and Username.
- Set Password: You can choose to "Allow user to set a password when they next login" or "Set a password now". You can also click the gear icon to generate a random password.
- Finish: Click Add to create the account.
You can also click on an existing user (after unlocking) to modify their settings, such as changing their name, switching their account type, changing their language, enabling/disabling automatic login, or clicking Remove User... to delete them.
🐧 Lecture 3: Command Line Interface (CLI) Basics
This lecture introduces the Command Line Interface (CLI), explaining what it is, why it's necessary, and how to use its most fundamental commands for navigation and file management.
GUI vs. CLI
- User Interface (UI): The point of human-computer interaction and communication.
- GUI (Graphical User Interface): A digital interface where the user interacts with graphical components like icons, buttons, and menus.
- CLI (Command Line Interface): A text-based user interface used to run programs, manage files, and interact with the computer by typing commands.
- Shell: The program that takes the text commands you type and gives them to the Operating System to perform.
- Terminal: The tool or program that opens a black window and lets you interact with the shell.
Why Use the CLI?
While a GUI is user-friendly, the CLI is essential because:
- Some advanced tasks cannot be done using the GUI.
- It allows you to see more details about background programs and files.
Essential Navigation & Management Commands
This lecture covers the most basic commands for interacting with the system:
whoami: Tells you the name of the user you are currently logged in as.pwd: (Print Working Directory) Shows you the full path of the directory you are currently in.ls: (List) Lists all the files and folders in your current directory.cd: (Change Directory) Used to navigate between directories and folders.cd Downloads: Enters the 'Downloads' folder.cd ..: Goes back up one level to the parent directory.cd /: Goes directly to the root directory of the entire system.
clear: Clears all previous commands and output from the terminal screen.cp: (Copy) Copies files.cp test testcopy: Copies the file 'test' and creates a new file 'testcopy' with the same content.
rm: (Remove) Deletes files.rm testcopy: Deletes the file 'testcopy'.
mkdir: (Make Directory) Creates a new, empty folder.rmdir: (Remove Directory) Deletes an empty folder.man: (Manual) Describes what a command does and how to use it.man ls: Shows the manual for thelscommand.
🐧 Lecture 4: CLI Text File Manipulation and Advanced Commands
This lecture expands on CLI skills, focusing on creating, reading, and editing text files, as well as introducing more powerful commands for searching, viewing, and sorting data.
Basic File Creation and Editing
touch: Creates a new, empty file. You can create multiple files at once (e.g.,touch main.js script.js sum.py).cat: (Concatenate) Shows the entire content of a file on the terminal. If the file is empty, it shows nothing.echo: Used to write text.echo "Text" > file.txt: (Overwrite) Puts the text "Text" intofile.txt. If the file already has content, it will be deleted and replaced.echo "Text" >> file.txt: (Append) Adds the text "Text" to the end offile.txtwithout deleting existing content.
mv: (Move) This command has two functions:- Move:
mv file.txt /path/to/dirmoves the file to a new directory. - Rename:
mv old-name.txt new-name.txtrenames the file.
- Move:
nano: A terminal-based text editor.nano names.txt: Opens the file for editing.- Ctrl + O: Saves the changes (Write Out).
- Ctrl + X: Exits the editor.
Advanced File Interaction
find: Searches for files or directories.find . -name "*.js": Finds all files in the current directory (.) whose names end in.js.find . -name "*.js" -delete: Finds all files ending in.jsand deletes them.
less: An interactive file viewer for large files. It allows you to:- Scroll down (Space) and up (b).
- Go to the end (G) or start (g).
- Search (
/word). - Quit (
q).
more: An older, simpler viewer. It does not allow scrolling backward and exits automatically at the end of the file.head: Shows the first 10 lines of a file by default.head -n 5 file.txt: Shows only the first 5 lines.
tail: Shows the last 10 lines of a file.tail -f file.txt: (Follow) Monitors the file in real-time and shows any new lines as they are added.
grep: Searches for specific text inside files.grep "Nolan" file.txt: Finds lines containing "Nolan".grep -i "nolan" file.txt: Case-insensitive search.grep -n "Nolan" file.txt: Shows the line numbers of matches.grep -c "Nolan" file.txt: Counts how many lines match.
sort: Sorts the lines of a file alphabetically.sort -r file.txt: Sorts in reverse (descending) order.sort -u file.txt: (Unique) Sorts and removes duplicate lines.
🐧 Lecture 5: User and Group Management (CLI)
This lecture provides a detailed guide on how to manage users and groups using only the command line, which is essential for server administration.
User Management Commands
useradd: Creates a new user account.sudo useradd a_user: Creates the user but does not create a/home/a_userdirectory.sudo useradd -m -s /bin/bash a_user: The-mflag creates the user's home directory. The-s /bin/bashsets their default shell.
passwd: Sets or changes a user's password.sudo passwd a_user: Prompts you to enter and retype a new password for 'a_user'.
userdel: Deletes a user account.sudo userdel a_user: Deletes the user, but leaves their home directory and files behind.sudo userdel -r a_user: The-r(recursive) flag deletes the user and their home directory with all its contents.
cat /etc/passwd: This command lists all user accounts on the system, showing their user ID, group ID, and home directory path.
Group Management Commands
Groups are used to control permissions for multiple users at once.
groupadd: Creates a new group.sudo groupadd developers: Creates a group named 'developers'.
groupdel: Deletes a group.sudo groupdel developers: Deletes the 'developers' group.
usermod: Modifies an existing user. It's used to add a user to a group.sudo usermod -aG developers a_user: Adds 'a_user' to the 'developers' group. The-aGflags mean "append" (so you don't remove them from other groups) and "Groups".
gpasswd: Manages group settings.sudo gpasswd -d a_user developers: Removes 'a_user' from the 'developers' group.
groups: Lists all groups a specific user belongs to.groups a_user: Shows all groups for 'a_user'.
cat /etc/group: This command lists all groups on the system and shows which users are members of each.
🐧 Lecture 6: File Permissions (chmod)
This lecture covers the crucial topic of file permissions in Linux, explaining what they mean and how to change them using the chmod command.
The Permission Structure
Every file and directory in Linux has permissions set for three distinct "actors":
- Owner (User): The user who created or owns the file.
- Group: The specific group that has permissions on the file.
- Others (All): Every other user on the system.
Each actor can be granted three types of permissions:
- Read (r): Ability to read a file's contents or list a directory's contents (
ls). - Write (w): Ability to modify or delete a file or add/delete files within a directory.
- Execute (x): Ability to run a file as a program or enter a directory (
cd).
Reading Permissions (ls -l)
When you type ls -l, you see permissions like -rwxr-xr--:
- First Character: The file type.
dis a directory,-is a regular file. - Chars 2-4 (rwx): Permissions for the Owner (User).
- Chars 5-7 (r-x): Permissions for the Group.
- Chars 8-10 (r--): Permissions for Others.
Changing Permissions (chmod)
The chmod command is used to change permissions in two ways:
1. Symbolic Mode (Letters) This mode uses letters to represent who, what action, and what permission.
- Who:
u(user),g(group),o(other),a(all). - Action:
+(add),-(remove),=(set exactly). - Permission:
r(read),w(write),x(execute). - Examples:
chmod g+x file.txt: Adds execute permission for the group.chmod u-w file.txt: Removes write permission from the user.chmod ug-x script.sh: Removes execute permission from both user and group.chmod ugo+r file.txt: Adds read permission for everyone.
2. Absolute Mode (Numbers) This mode uses octal (base-8) numbers to represent the permissions for each actor.
r(read) = 4w(write) = 2x(execute) = 1---(none) = 0
You add the numbers for each actor:
-
rwx= 4 + 2 + 1 = 7 -
rw-= 4 + 2 + 0 = 6 -
r-x= 4 + 0 + 1 = 5 -
r--= 4 + 0 + 0 = 4 -
-wx= 0 + 2 + 1 = 3 -
-w-= 0 + 2 + 0 = 2 -
--x= 0 + 0 + 1 = 1 -
Example 1:
chmod 755 index.html- User: 7 (rwx)
- Group: 5 (r-x)
- Other: 5 (r-x)
- Result:
-rwxr-xr-x
-
Example 2:
chmod 644 index.html- User: 6 (rw-)
- Group: 4 (r--)
- Other: 4 (r--)
- Result:
-rw-r--r--
🐧 Lecture 7: Disk Partitioning (fdisk)
This lecture covers the process of disk partitioning, the different types of partitions, and the step-by-step procedure for creating, formatting, and mounting a new partition using the fdisk command-line tool.
Partitioning Concepts
- Disk Partitioning: The process of dividing a single physical disk into one or more logical areas.
- HDD vs. SSD:
- HDD (Hard Disk Drive): Uses mechanical spinning disks. Best for long-term storage at a low cost.
- SSD (Solid State Drive): Uses flash memory with no moving parts. It's more expensive but provides rapid boot times and fast file access.
- Partition Types:
- Primary: A bootable partition. A disk can have a maximum of four primary partitions.
- Extended: A special type of primary partition that cannot hold data itself but can be subdivided.
- Logical: The partitions created inside an extended partition. This is used to overcome the 4-partition limit.
The 3-Step Process for Using a New Partition
To use a new disk, you must perform three steps:
- Step 1: Create the Partition: Use a tool like
fdiskto define the partition's size and type. - Step 2: Format the Partition: Create a filesystem on the partition so the OS can store data on it (e.g., ext4).
- Step 3: Mount the Partition: Attach the formatted partition to a directory in the filesystem so you can access it.
Using the fdisk Utility
fdisk is a powerful command-line tool for partition management.
fdisk -l: Lists all disks and their current partition tables.fdisk /dev/sdb: Enters thefdiskediting tool for the disksdb.
Inside the fdisk tool, you use single-letter commands:
m: Prints the help menu.p: Prints the current partition table for the disk you are editing.n: Adds a new partition. It will ask you to choose:pfor primaryefor extended- It will then ask for the partition number, first sector (press Enter for default), and last sector or size (e.g.,
+2048Mfor 2GB).
d: Deletes a partition.w: Writes all changes to the disk and exits. This is the save-and-quit command.q: Quits the tool without saving any changes.
Formatting and Mounting
partprobe /dev/sdb: After usingfdisk, this command tells the kernel to re-read the partition table to see the new changes.mkfs -t ext4 /dev/sdb1: Makes a filesystem of typeext4on the newsdb1partition.mkdir -p /mt/sdb1: Creates a directory to be used as the "mount point".mount /dev/sdb1 /mt/sdb1: Attaches thesdb1partition to the/mt/sdb1directory.df hT: Verifies that the partition is successfully mounted and shows its usage.
🐧 Lecture 8: Comprehensive Linux Review (Lectures 1-7)
This lecture serves as a final review of all topics and applies them to practical, real-world scenarios.
Topic 1: Core Linux Concepts (Recap)
- Linux: An operating system based on the Linux Kernel.
- Kernel: The core component that communicates with hardware (RAM, CPU, etc.).
- Distribution (Distro): A complete OS made from the Kernel plus a software collection, including GNU tools, a Package Manager, and a Graphical Environment (like Gnome).
- Shell: The program that takes text commands and gives them to the OS.
- Terminal: The window you type commands into.
- Why Linux?: It is Costless, Stable, Reliable, and Highly Secure.
Topic 2: User and Group Management (Recap)
- User Types: Root (full admin), Privileged (uses
sudo), and Normal (limited). - GUI Management: Done via
Settings > Users > Unlock. - CLI User Commands:
sudo useradd -m name: Creates user with a home directory.sudo useradd name: Creates user without a home directory.sudo passwd name: Sets the user's password.sudo userdel name: Deletes user but keeps their files.sudo userdel -r name: Deletes user AND their files.
- CLI Group Commands:
sudo groupadd name: Creates a new group.sudo usermod -aG group user: Adds a user to a group.groups user: Lists a user's groups.gpasswd -d user group: Removes user from a group.cat /etc/passwd: Lists all users.cat /etc/group: Lists all groups.
Topic 3: File Permissions (Recap)
- Structure:
[File Type] [User (rwx)] [Group (rw-)] [Other (r--)]. - File Type:
-for file,dfor directory. - Command:
chmod. - Symbolic Mode: Uses
u, g, oand+, -, =.- Example:
chmod g+x file(Adds Execute for Group). - Example:
chmod u-x file(Removes Execute from User). - Example:
chmod o-rwx file(Removes All from Other).
- Example:
- Absolute Mode: Uses numbers (r=4, w=2, x=1).
rwx= 4+2+1 = 7rw-= 4+2+0 = 6r-x= 4+0+1 = 5r--= 4+0+0 = 4---= 0+0+0 = 0- Example:
chmod 755 filemeansrwxr-xr-x. - Example:
chmod 670 filemeansrw-rwx---.
Topic 4: Partitioning & Advanced File Commands (Recap)
- Partitioning: Dividing a disk. Max 4 primary partitions. An Extended partition holds Logical partitions (e.g., sda5, sda6).
- 3 Steps: 1) Create (
fdisk /dev/sdb), 2) Format (mkfs -t ext4 /dev/sdb1), 3) Mount (mount /dev/sdb1 /data). fdiskcommands:n(new),p(print),w(write/save),q(quit).find: Searches for files.find . -name "Capsules": Finds a file named "Capsules" in the current directory (.).
grep: Searches inside files.grep Panadol Capsules: Searches for the text "Panadol" inside the "Capsules" file.
tail -f: Follows a file and shows new lines as they are added.echo >>: Appends text to a file.uniq: Removes consecutive duplicate lines from a file.tar: Archives (zips) and extracts (unzips) files.tar -cvf Books.Rar ...: Creates a verbose file (archive).tar -xvf Books.Rar: Extracts a verbose file (archive).