1. Introduction to Linux
An operating system (OS) manages computer hardware and software resources. It handles memory allocation, device input/output, network operations, and file systems, providing an interface for user interaction.
Major OS Categories by Domain:
- Desktop OS: Windows (largest user base), macOS (polished UX, fewer apps, higher cost), Linux (limited applications).
- Server OS: Linux (secure, stable, free, high market share), Windows Server (paid, lower share).
- Embedded OS: Linux.
- Mobile OS: iOS, Android (Linux-based), HarmonyOS (Linux-based).
1.1 History of Linux
In 1984, Andrew S. Tanenbaum created Minix, a Unix-like teaching OS. In 1991, Linus Torvalds, a student at the University of Helsinki, built a free kernel after finding Minix too limited. He released version 0.02 publicly and version 1.0 in 1994 under the GPL license, leading to widespread community development.
Unix was developed at Bell Labs in 1969 by Ken Thompson and Dennis Ritchie. It is a commercial, proprietary OS.
1.2 What is Linux?
Linux is a free, open-source, Unix-like OS based on the POSIX standard. It is multi-user, multi-tasking, and supports multi-threading and multi-CPU architectures. It runs on 32-bit and 64-bit hardware and is widely used in servers, embedded systems, and supercomputers.
Core Philosophy:
- Everything is a file.
- Each software has a specific purpose.
Key Features:
- Free and Open Source: Source code can be modified and redistributed.
- POSIX Compatible: Can run many Windows/DOS programs via emulators.
- Multi-user/Multi-tasking: Independent user environments with concurrent program execution.
- Dual Interface: Both command-line (CLI) and graphical (GUI, e.g., X-Window) interfaces.
- Platform Independent: Runs on x86, ARM, SPARC, Alpha, etc.
1.3 Linux vs. Unix
| Aspect | Unix | Linux |
|---|---|---|
| Licensing | Commercial, proprietary | Open source, free |
| Hardware | Bundled with specific hardware (e.g., AIX, HP-UX) | Runs on many platforms including x86 PCs |
| Development | Closed, vendor-controlled | Community-driven, transparent |
1.4 Linux vs. Windows
| Feature | Windows | Linux |
|---|---|---|
| Interface | Unified GUI, consistent menus | Varies by distribution; CLI fundamental |
| Drivers | Vendor-provided, abundant | Community-developed; some hardware support gaps |
| Ease of Use | User-friendly, especially GUI | GUI easy; CLI requires learning |
| Learning Curve | Rapid changes, complex internals | Stable, deep knowledge transferable |
| Software | Commercial, wide selection | Mostly free, fewer choices |
| Security | Lower inherent security | Higher inherent security |
| Open Source | No | Yes |
1.5 Linux Distributions
A distribution packages the Linux kernel with system tools, libraries, application software, and an installer. Major families include:
- Commercial: Red Hat Enterprise Linux (RHEL), SUSE Linux Enterprise.
- Community: Debian, Fedora, CentOS, Ubuntu, Arch Linux.
Notable Distributions:
- Debian: Strictly adheres to GNU principles; basis for Ubuntu.
- Ubuntu: User-friendly desktop, derived from Debian testing branch.
- Red Hat: Enterprise-focused, stable, extensive support.
- Fedora: Sponsored by Red Hat, cutting-edge features.
- CentOS: Free, binary-compatible rebuild of RHEL; community-supported. (Note: CentOS Stream now tracks ahead of RHEL.)
- Slackware: One of the oldest, emphasizes simplicity and stability.
- openSUSE: Community project backed by SUSE/Novell.
For this guide, we use CentOS 7.
2. Installation: VMware and CentOS 7
2.1 Prerequisites
Enable Virtualization in BIOS:
- Restart computer and enter BIOS (e.g., F2, Del, Esc).
- Locate "Virtualization Technology" or "Intel VT-x" and set to Enabled.
- Save and exit.
Without virtualization enabled, VMware will display an error during VM creation.
2.2 Download and Install VMware Workstation
- Download from VMware official site.
- Install the software. During installation, two virtual network adapters are created:
- VMnet1 (Host-Only mode)
- VMnet8 (NAT mode)
2.3 Download CentOS ISO
Choose a mirror from a domestic source for speed:
Recommended version: CentOS-7-x86_64-DVD-1810.iso (or later 7.x).
2.4 Create a Virtual Machine
- Open VMware, select File > New Virtual Machine.
- Choose Typical (recommended).
- Select I will install the operating system latter.
- Guest OS: Linux, Version: CentOS 7 64-bit.
- Name the VM (e.g., "CentOS792207") and choose a storage location with sufficient disk space (>4 GB).
- Set disk size (e.g., 50 GB). Choose Store virtual disk as a single file.
- Customize Hardware:
- Memory: 2 GB or more.
- Processors: 1-2 cores.
- New CD/DVD: Use ISO image file, select the downloaded CentOS ISO.
- Network Adapter: NAT mode (default) for internet access via host.
- Click Finish.
2.5 Install CentOS 7
- Start the VM.
- Select Install CentOS 7.
- Choose language (e.g., 中文 (Chinese) or English).
- Configure:
- Date & Time: Set region (e.g., Asia/Shanghai).
- Keyboard: English (US).
- Software Selection: Select GNOME Desktop for GUI.
- Installation Destination:
- Select the disk, choose I will configure partitioning.
- Create partitions:
/boot– 1 GB (ext4 or xfs)swap– 2-8 GB (equal to or double RAM size)/(root) – remaining space (xfs)
- Network & Host Name:
- Set hostname (e.g.,
hadoop100, no spaces). - Configure network interface (e.g.,
ens33) to ON.
- Set hostname (e.g.,
- KDUMP: Disable (if learning) to save resources.
- Security Policy: Keep default (none).
- Click Begin Installation.
- Set Root Password (e.g.,
root). - Create a user (e.g.,
itcastwith password). - After installation, reboot.
- Accept the license agreement.
- Complete initial setup (language, keyboard, privacy, timezone, skip online accounts, create user if not done).
2.6 Network Configuration (Static IP)
Edit the network script to set a static IP, avoiding DHCP changes.
# Open the configuration file
vi /etc/sysconfig/network-scripts/ifcfg-ens33
Modify as follows:
TYPE="Ethernet"
BOOTPROTO="static" # Change from dhcp
ONBOOT="yes"
IPADDR=192.168.88.100 # Desired static IP
GATEWAY=192.168.88.2 # Gateway (VMware NAT gateway, usually .2)
DNS1=192.168.88.2 # DNS server (can be 8.8.8.8)
Restart network service:
systemctl restart network
# Or: service network restart
Troubleshooting:
- VM cannot ping host: Check host firewall.
- VM cannot ping internet: Verify gateway and DNS.
- If
systemctl status networkshows errors, recheck configuration file.
2.7 Host Name and Hosts File
Set hostname:
hostnamectl set-hostname hadoop100
Edit /etc/hosts:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.88.100 hadoop100
192.168.88.101 hadoop101
...
Edit Windows hosts file (as Administrator):
Path: C:\Windows\System32\drivers\etc\hosts
Add same IP-hostname mappings.
2.8 Remote Access with SecureCRT (or Xshell, FinalShell)
- Create a new session.
- Protocol: SSH2.
- Hostname: The static IP (e.g.,
192.168.88.100). - Port: 22.
- Username:
rootor your custom user. - Set character encoding to UTF-8 to avoid Chinese garbled text.
3. File System and Directory Structure
Linux uses a hierarchical tree structure with / as root. All devices and processes are represented as files.
Key Directories:
| Directory | Description |
|---|---|
/bin |
Essential user commands (e.g., ls, cat). |
/sbin |
System administration commands (e.g., fdisk, ifconfig). |
/home |
User home directories (e.g., /home/user1). |
/root |
Home directory for the root user. |
/lib |
Shared libraries needed for boot and core system. |
/etc |
Configuration files (e.g., /etc/passwd, /etc/fstab). |
/usr |
User applications and utilities (similar to Program Files). |
/boot |
Kernel and boot loader files. |
/proc |
Virtual filesystem for kernel and process information (do not modify). |
/tmp |
Temporary files (typically cleared on reboot). |
/dev |
Device files (e.g., /dev/sda, /dev/cdrom). |
/mnt |
Temporary mount point for external filesystems. |
/opt |
Optional add-on software (e.g., Oracle). |
/var |
Variable data: logs, databases, spools. |
/run |
Runtime variable data for running processes. |
4. Vi/Vim Editor
Vim is an enhanced version of vi. It has three modes:
- Normal Mode: Default; navigate, delete, copy, paste.
- Insert Mode: Edit text.
- Command-Line Mode: Save, quit, search, replace.
4.1 Entering Insert Mode
| Key | Action |
|---|---|
i |
Insert before cursor |
a |
Insert after cursor |
o |
Open a new line below |
I |
Insert at beginning of line |
A |
Insert at end of line |
O |
Open a new line above |
Press Esc to return to Normal mode.
4.2 Common Commands in Command-Line Mode
| Command | Function |
|---|---|
:w |
Save file |
:q |
Quit |
:wq! |
Force save and quit |
/pattern |
Search forward for pattern; n next, N previous |
:set nu |
Show line numbers |
:set nonu |
Hide line numbers |
:%s/old/new/g |
Replace all old with new in file |
5. Essential Shell Commands
5.1 File and Directory Operations
| Command | Usage | Example |
|---|---|---|
pwd |
Print working directory | pwd → /root |
ls [options] |
List directory contents; -a show hidden, -l long format |
ls -la |
cd [dir] |
Change directory; ~ home, - previous, .. parent |
cd /var/log |
mkdir [-p] dir |
Create directory; -p creates parents |
mkdir -p a/b/c |
rmdir dir |
Remove empty directory | rmdir emptydir |
touch file |
Create empty file or update timestamp | touch newfile.txt |
cp [options] src dest |
Copy; -r for directories |
cp -r dir1/ dir2/ |
rm [options] file |
Remove; -r recursive, -f force |
rm -rf olddir/ |
mv src dest |
Move or rename | mv file1.txt /tmp/ |
cat [-n] file |
Concatenate and display file content; -n line numbers |
cat -n /etc/passwd |
more file |
View file page by page (space next, q quit) | more longfile.log |
less file |
View file with backward navigation and search | less /var/log/messages |
head [-n] file |
Show first 10 lines (or -n lines) |
head -20 data.csv |
tail [-n] [-f] file |
Show last 10 lines; -f follow updates |
tail -f /var/log/syslog |
ln -s target link_name |
Create symbolic (soft) link | ln -s /usr/bin/python3 /usr/bin/py |
5.2 File Permissions
Permissions are represented by a 10-character string (e.g., -rwxr-xr--).
- First character:
-file,ddirectory,llink. - Next 9 characters: three groups of
rwxfor owner, group, and others.
Change Permissions:
# Symbolic mode
chmod u+x file.sh # Add execute for owner
chmod g-w file.sh # Remove write for group
chmod o=r file.sh # Set read-only for others
chmod a+rx script.sh # Add read+execute for all
# Numeric mode (r=4, w=2, x=1)
chmod 755 script.sh # Owner: rwx, Group: r-x, Others: r-x
chmod -R 644 dir/ # Recursive set
Change Owner/Group:
chown user:group file
chown -R user:group folder/
chgrp group file
5.3 Searching Files
| Command | Description |
|---|---|
find path -name "pattern" |
Locate files by name |
find path -user username |
Find files owned by username |
find path -size +100M |
Find files larger than 100 MB |
locate filename |
Fast search using pre-built database; run updatedb first |
grep "pattern" file |
Search inside files; -n show line numbers |
# Find all .txt files under /var
find /var -name "*.txt"
# Search for 'error' in log files, case-insensitive
grep -i "error" /var/log/messages
5.4 Archiving and Compression
| Command | Example |
|---|---|
gzip file → file.gz |
gzip archive.tar |
gunzip file.gz |
gunzip archive.tar.gz |
zip -r archive.zip folder/ |
zip -r backup.zip /home/user/ |
unzip archive.zip [-d target] |
unzip data.zip -d /tmp/ |
tar -czvf archive.tar.gz folder/ |
tar -czvf project.tar.gz ./src/ |
tar -xzvf archive.tar.gz [-C dir] |
tar -xzvf downloads.tar.gz -C /opt/ |
5.5 Disk Usage
| Command | Description |
|---|---|
df -h |
Show disk partition usage in human-readable format |
du -sh dir |
Show total size of a directory |
du -h --max-depth=1 dir |
Show sizes of immediate subdirectories |
lsblk |
List block devices (disks and partitions) |
fdisk -l |
Display partition table |
mount device mountpoint |
Mount a filesystem |
umount mountpoint |
Unmount |
Mounting an ISO image example:
mkdir /mnt/cdrom
mount -t iso9660 /dev/cdrom /mnt/cdrom
# Or mount an ISO file directly
mount -o loop /path/to/image.iso /mnt/iso
5.6 Process Management
| Command | Description |
|---|---|
ps aux |
Show all processes (BSD style) |
ps -ef |
Show full process list with parent PID |
top |
Real-time process monitoring (press P CPU, M memory, q quit) |
htop |
Enhanced top (install with yum install htop) |
kill -9 PID |
Force terminate a process |
killall process_name |
Kill all processes with name |
pstree [-p] [-u] |
Display process tree |
netstat -tunlp |
Show listening ports and associated processes |
ss -tunlp |
Modern alternative to netstat |
Example: Find process using port 8080:
netstat -tunlp | grep :8080
5.7 System Management
Services (systemd):
systemctl start | stop | restart | status service_name
systemctl enable | disable service_name # Enable/disable at boot
systemctl list-unit-files # List all services and their boot status
Runlevels (CentOS 7):
multi-user.target(no GUI, equivalent to runlevel 3)graphical.target(with GUI, runlevel 5)
systemctl get-default # Current runlevel
systemctl set-default multi-user.target # Set to CLI-only
Shutdown and Reboot:
sync # Flush disk cache
shutdown -h now # Halt immediately
shutdown -r +5 "System will restart in 5 minutes" # Scheduled reboot
reboot
poweroff
5.8 User and Group Management
Adding/Removing Users:
useradd newuser
passwd newuser
userdel [-r] username # -r removes home directory
Group Commands:
groupadd dev
groupdel dev
groupmod -n newname oldname
gpasswd -a username groupname # Add user to group
gpasswd -d username groupname # Remove user from group
Granting sudo Privileges:
Edit /etc/sudoers with visudo and add:
username ALL=(ALL) ALL
# Or without password:
username ALL=(ALL) NOPASSWD: ALL
5.9 Disk Partitioning with fdisk
- Add a new virtual disk in VMware settings.
- Reboot VM, then:
fdisk /dev/sdb # New disk device
- Inside fdisk:
n→ new partition (primaryp, number 1, accept defaults).w→ write changes and exit.
- Format partition:
mkfs.xfs /dev/sdb1 # Or mkfs.ext4
- Mount it:
mkdir /mnt/newdisk
mount /dev/sdb1 /mnt/newdisk
- Make permanent: add entry to
/etc/fstab:
/dev/sdb1 /mnt/newdisk xfs defaults 0 0
5.10 Cron Jobs
crontab -e # Edit tasks for current user
Format: minute hour day month weekday command
# Run script.sh every day at 2:30 AM
30 2 * * * /path/to/script.sh
# Log CPU usage every 5 minutes
*/5 * * * * /usr/bin/top -b -n1 >> /var/log/cpu_usage.log
6. Package Management: RPM and YUM
6.1 RPM (Red Hat Package Manager)
| Command | Description |
|---|---|
rpm -qa |
List all installed packages |
rpm -e package_name |
Remove package (add --nodeps to force) |
rpm -ivh package.rpm |
Install with verbose and hash progress |
Example:
rpm -qa | grep firefox
rpm -ivh /mnt/cdrom/Packages/firefox-*.rpm
6.2 YUM (Yellowdog Updater, Modified)
YUM resolves dependencies automatically from configured repositories.
| Command | Description |
|---|---|
yum install package |
Instal package (add -y to skip prompts) |
yum remove package |
Uninstall |
yum update package |
Update to latest version |
yum search keyword |
Search for packages |
yum list installed |
List installed |
Configuring a Faster Mirror:
# Backup original repo
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# Download Aliyun repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# Clean cache and generate new
yum clean all
yum makecache
7. Cloning a Virtual Machine
- Shutdown the source VM.
- Right-click VM → Manage > Clone.
- Select current state.
- Choose Create a full clone.
- Set name and location.
After cloning, start the new VM and change:
- Hostname:
hostnamectl set-hostname newname - IP address: edit
/etc/sysconfig/network-scripts/ifcfg-ens33
8. Common Troubleshooting
Virtualization error: Ensure VT-x is enabled in BIOS.
Network issues:
pingfails to host: disable Windows firewall.pingfails to internet: check DNS and gateway settings.- If
systemctl restart networkfails, runsystemctl stop NetworkManager && systemctl disable NetworkManager.
Package conflicts: Use yum instead of rpm to auto-resolve dependencies.
9. Interview Questions
Q: List essential Linux commands.
A: find, df, tar, ps, top, netstat, grep, chmod. Prefer high-level commands.
Q: How to check memory, disk, IO, ports, and processes? A:
- Memory:
top,free -h - Disk:
df -h,du -sh - IO:
iotop -o(install with yum) - Ports:
netstat -tunlp | grep <port> - Processes:
ps aux,ps -ef