What is an Operating System
An Operating System (OS) is a computer program that manages hardware and software resources, serving as the core foundation of a system. It handles memory management, resource prioritization, I/O device control, networking, and file system management. It also provides a user interface for interaction.
Structure:
- Top Layer: User
- Middle Layer: Operating System (containing applications)
- Bottom Layer: Hardware support (CPU, Memory, Hard Drive)
Mainstream OS Categories:
-
Desktop OS
- Windows: Large user base.
- macOS: Superior detail handling, fewer applications, higher cost.
- Linux: Limited commercial applications.
-
Server OS
- Linux: Secure, stable, free, high market share.
- Windows Server: Paid licensing, lower market share.
-
Embedded OS
- Primarily Linux.
-
Mobile OS
- iOS
- Android (Linux-based)
- HarmonyOS (Linux-based)
Linux History
- 1984: Andrew S. Tanenbaum developed Minix for educational purposes.
- 1989: Minix ported to x86 PC platforms.
- 1990: Linus Torvalds, a student at the University of Helsinki, encountered Minix.
- 1991: Torvalds began writing kernel components for Minix.
- Late 1991: Linux kernel 0.02 source code released (kernel only).
- 1994: Linux 1.0 released under the GPL license.
Linus Torvalds created the kernel as a hobby, finding Minix too restrictive. The initial release had only 10,000 lines of code. He released the source code for free, inviting collaboration. Today, while he maintains control over the kernel evolution, the OS is a collective effort by thousands of developers.
Note on Unix: Developed by Ken Thompson and Dennis Ritchie at Bell Labs (1969). It is a commercial, multi-user, multi-tasking OS. Linux drew heavy inspiration from Unix principles.
Linux Overview
Linux is a free, open-source, Unix-like operating system supporting multi-user, multi-tasking, multi-threading, and multi-CPU architectures. It supports both 32-bit and 64-bit hardware and is widely used in servers, mobile devices, and network infrastructure.
Core Characteristics:
- Everything is a file: Commands, hardware, software, and processes are treated as files.
- Specific purpose software: Every enstalled software has a distinct function.
- Open Source: Users can view, modify, and distribute the source code.
- POSIX Compatible: Can run or emulate many DOS/Windows programs via compatibility layers.
- Multi-user/Multi-tasking: Supports simultaneous users and independent running processes.
- Multi-platform: Runs on x86, ARM, SPARC, etc.
Advantages:
- Open source kernel with a complete toolchain.
- Powerful native networking stack (TCP/IP).
- Highly stable and portable.
Linux vs. Unix & Windows
Linux vs. Unix:
- Cost: Unix is commercial; Linux is free/open.
- Hardware: Unix is often hardware-locked; Linux runs on diverse platforms.
- Nature: Linux is open development; Unix is proprietary.
Linux vs. Windows:
| Feature | Windows | Linux |
|---|---|---|
| Interface | Uniform GUI across apps. | Varied GUI (depends on distro); consistent CLI. |
| Drivers | Frequent updates, vendor-provided. | Community-driven, improving hardware support. |
| Usability | Easy for beginners. | CLI requires learning; GUI is optional. |
| Learning Curve | Complex internals, fast obsolescence. | Simple structure, stable knowledge base. |
| Software | Commercial licenses often required. | Mostly free/open source alternatives. |
| Security | Lower (historical). | Higher (permission model & architecture). |
Distributions
A Linux Distribution (Distro) bundles the kernel with libraries, tools, a desktop environment (like GNOME/KDE), and applications.
Kernel Versions:
- Even second numbers (e.g., 4.18) are stable.
- Odd second numbers are development versions.
Popular Distributions:
- Debian: Community-driven, strictly follows GNU principles. (Branches: Stable, Testing, Unstable).
- Ubuntu: Based on Debian, focused on desktop usability and hardware support.
- Red Hat Enterprise Linux (RHEL): Commercial, enterprise-focused, high stability.
- Fedora: Community project sponsored by Red Hat, featuring cutting-edge software.
- CentOS: (Now shifted to CentOS Stream) Was a free, binary-compatible rebuild of RHEL sources.
- openSUSE: German-origin distro known for the YaST configuration tool.
- Slackware: One of the oldest distros, favoring simplicity and original upstream packages.
Summary for Learning:
- RHEL: Enterprise standard.
- Ubuntu: Best for desktop/laptop use.
- CentOS: (Legacy) Free alternative to RHEL for servers.
Application Fields
Linux dominates in servers (Web, DB, DNS, FTP), embedded systems (routers, firewalls, IoT), and supercomputing. Governments (e.g., China, Brazil, Russia) often adopt Linux for sovereignty and cost-efficiency.
System Management Commands
User Management
Prerequisite: Most commands require root privileges. Switch using su -.
1. Create User
useradd [options] username
Options:
-d: Home directory.-m: Create home directory if missing.-s: Login shell.-G: Supplementary groups.
Example:
useradd dev_user
2. Set Password
passwd username
3. Modify User
usermod -l new_name old_name
4. Delete User
userdel -r username
-r removes home directory and mail spool.
Group Management
1. Create Group
groupadd dev_team
2. Modify Group
groupmod -n new_group_name old_group_name
3. Delete Group
groupdel dev_team
4. Add User to Group
Use gpasswd or usermod:
gpasswd -a dev_user dev_team
5. Check User Groups
groups dev_user
Essential System Commands
Date & Time
date # Show current date
date -s "2023-10-27 14:30:00" # Set date (root)
Concepts: UTC (Coordinated Universal Time), GMT (Greenwich Mean Time), CST (China Standard Time, UTC+8).
User Identity
whoami # Show current username
id # Show UID, GID, groups
logname # Show login name
su - username # Switch user
Privilege Elevation
sudo command # Execute as root (if configured)
Example: sudo vi /etc/sysconfig/network-scripts/ifcfg-ens33
Process Management
- View Processes:
ps aux # Detailed list of all processes top # Dynamic real-time view top -p <PID> # Focus on specific PID - Kill Processes:
kill <PID> # Terminate gracefully kill -9 <PID> # Force kill
System Control
shutdown -h now # Shutdown immediately
shutdown -r +5 "Rebooting in 5" # Reboot in 5 mins
reboot # Restart system
Time Synchronization
timedatectl status # Check time/date status
timedatectl list-timezones # List available zones
timedatectl set-timezone Asia/Shanghai # Set timezone
timedatectl set-ntp true # Enable NTP sync
Session Control
exit # Exit shell
clear # Clear terminal screen
who # Show logged in users
Directory and File Management
Path Concepts
- Absolute Path: Starts from root
/(e.g.,/var/log). - Relative Path: Starts from current directory (e.g.,
../docs).
Navigation & Listing
pwd # Print Working Directory
ls # List files
ls -l # Long format (permissions, owner, size)
ls -a # Include hidden files (starting with .)
cd /path/to/dir # Change directory
cd ~ # Go to home directory
cd .. # Go up one level
File Details (ls -l output):
drwxr-xr-x 2 user group 4096 Oct 27 10:00 dirname
- d: Directory (- for file, l for link).
- rwx: Owner permissions (Read, Write, Execute).
- r-x: Group permissions.
- r-x: Others permissions.
- user: Owner.
- group: Owning group.
CRUD Operations
Create:
mkdir project # Create directory
mkdir -p a/b/c # Create nested directories
touch file.txt # Create empty file
Copy:
cp file.txt backup/ # Copy file to dir
cp -r src_dir dest_dir/ # Copy directory recursively
Move/Rename:
mv old.txt new.txt # Rename file
mv file.txt /tmp/ # Move file
Delete:
rm file.txt # Remove file (prompt if alias)
rm -f file.txt # Force remove
rm -rf dir_name/ # Remove directory recursively (DANGER)
rmdir empty_dir # Remove empty directory only
Permissions Management
Changing Ownership:
chown new_owner file.txt # Change owner
chown new_owner:new_group file.txt # Change owner and group
chgrp new_group file.txt # Change group only
Changing Permissions (chmod):
-
Numeric Method (Recommended):
- r (read) = 4
- w (write) = 2
- x (execute) = 1
rwx= 7,rw-= 6,r-x= 5.chmod 755 script.sh # rwxr-xr-x chmod 644 data.txt # rw-r--r-- -
Symbolic Method:
- u (user/owner), g (group), o (others), a (all).
-
- (add), - (remove), = (set exact).
chmod u+x script.sh # Add execute for owner chmod go-w file.txt # Remove write from group and others
Practical Case: Team Collaboration
Scenario: Create a shared development directory for users alice, bob, and charlie.
-
Create Users:
useradd alice useradd bob useradd charlie -
Create Group:
groupadd devs -
Set Directory:
mkdir /opt/shared_project chown root:devs /opt/shared_project chmod 2770 /opt/shared_project(The 2 sets the SGID bit, ensuring new files inherit the group).
-
Add Users to Group:
usermod -aG devs alice usermod -aG devs bob usermod -aG devs charlie -
Verification: Switch to
alice, create a file in/opt/shared_project. Switch tobob, verify read/write access.