Notes on Learning Linux
This book uses CentOS 7/8 as the practical environment, focusing on practical Linux skills, progressing from basic commands to service deployment, making it a high-quality guide for beginners to learn Linux. The following summarizes the core content by chapter, highlighting practical focuses and knowledge difficulties.
Chapter 1: Overview of Linux System and Environment Setup
Core Knowledge Points
- • Characteristics of Linux System: Open-source, multi-user, multi-tasking, strong stability, suitable for server scenarios; CentOS, as the community version of RHEL, combines compatibility and stability, making it the enterprise-level choice.
- • Environment Setup Methods: Virtual Machine: Recommended VMware Workstation, pay attention to memory allocation (at least 2GB), hard disk (at least 20GB), and select “Bridged Mode” for easy communication between host and virtual machine;
- • Cloud Server: Alibaba Cloud/Tencent Cloud lightweight application server, directly select the CentOS system image for quick access to a production-level environment.
First Login: The default root user is the super administrator, and the password is not displayed when entered. It is recommended to change the password immediately after logging in (using the <span>passwd</span> command).
Note: Starting from CentOS 7, systemd is used to manage services by default, which differs from the traditional initd. Subsequent service management requires special attention.
Chapter 2: Basics of Linux Command Line
Common Commands and Practical Skills
| Command | Function | Example |
<span>ls</span> |
List directory contents | <span>ls -l</span> (long format, shows permissions, size, etc.);<span>ls -a</span> (shows hidden files) |
<span>cd</span> |
Change directory | <span>cd /home</span> (absolute path);<span>cd ../</span> (go back to the previous level);<span>cd ~</span> (return to the current user’s home directory) |
<span>pwd</span> |
Display current working directory | No parameters, just execute directly |
<span>mkdir</span> |
Create directory | <span>mkdir -p /data/logs</span> (recursively create multi-level directories) |
<span>rm</span> |
Delete file/directory | <span>rm -f file.txt</span> (force delete file);<span>rm -rf dir/</span> (force delete directory and contents, use with caution) |
<span>cp/mv</span> |
Copy/Move files | <span>cp -r src/ dest/</span> (copy directory);<span>mv oldname.txt newname.txt</span> (rename file) |
Efficient Command Line Operations
- • Tab key auto-completion: Enter the first few characters of a command/path and press the Tab key for quick completion, reducing input errors;
- • Command history: Use the
<span>↑↓</span>keys to switch between historical commands, or use the<span>history</span>command to view all history; - • Command alias: Use
<span>alias ll='ls -l'</span>to set an alias, simplifying common commands (to make it permanent, write it into the<span>~/.bashrc</span>file).
Chapter 3: File Permissions and User Management
File Permission Analysis
Linux permissions are divided into <span>r</span> (read, 4), <span>w</span> (write, 2), <span>x</span> (execute, 1), corresponding to owner (u), group (g), and other users (o). An example of long format display:<span>-rwxr-xr--</span>, meaning as follows:
- • The first character
<span>-</span>: file type (<span>-</span><code> for regular file, <code><span>d</span>for directory); - • The 2nd-4th characters
<span>rwx</span>: owner permissions (read, write, execute, corresponding permission value 7); - • The 5th-7th characters
<span>r-x</span>: group permissions (read, execute, corresponding permission value 5); - • The 8th-10th characters
<span>r--</span>: other users’ permissions (read, corresponding permission value 4).
Command to modify permissions:<span>chmod</span>, e.g., <span>chmod 755 file.txt</span> (owner rwx, others r-x); command to modify group ownership:<span>chown user:group file.txt</span>.
User and Group Management
- • Create user:
<span>useradd username</span>, automatically creates a home directory with the same name (/home/username); - • Set password:
<span>passwd username</span>, no echo when entering the password; - • Create group:
<span>groupadd groupname</span>; - • Add user to group:
<span>usermod -aG groupname username</span>(-aG means append to group); - • Delete user:
<span>userdel -r username</span>(-r also deletes the home directory).
Note: The root user has the highest privileges. In production environments, it is recommended to create regular users and temporarily obtain administrator privileges using the <span>sudo</span> command (configuration is required in the <span>/etc/sudoers</span> file).
Chapter 4: Package Management
RPM and YUM/DNF Tools
- • RPM: Red Hat Package Manager, suitable for installing single rpm packages, command: Install:
<span>rpm -ivh package.rpm</span>(-i for install, -v for verbose, -h for progress bar); - • Query:
<span>rpm -qa | grep package</span>(query installed packages); - • Uninstall:
<span>rpm -e package</span>(must uninstall dependent packages first).
YUM: Yellowdog Updater Modified, automatically resolves dependencies, the default tool for CentOS 7: Install:<span>yum install -y package</span> (-y for automatic confirmation);
Update:<span>yum update -y package</span>;
Query:<span>yum search package</span>;
Uninstall:<span>yum remove -y package</span>.
DNF: The successor to YUM, the default tool for CentOS 8+, commands are basically the same (just replace yum with dnf), with better performance.
Configure Domestic YUM Source
The default YUM source is slow, and can be replaced with Alibaba Cloud source:
- 1. Backup the original YUM source:
<span>mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak</span>; - 2. Download Alibaba Cloud source: Execute the corresponding command based on the CentOS version (e.g., CentOS 7:
<span>wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo</span>); - 3. Clean cache and generate new cache:
<span>yum clean all && yum makecache</span>.
Chapter 5: Service Management and Firewall
systemd Service Management
CentOS 7+ uses systemd to replace the traditional initd, with service-related commands:
| Command | Function |
<span>systemctl start service_name</span> |
Start service |
<span>systemctl stop service_name</span> |
Stop service |
<span>systemctl restart service_name</span> |
Restart service |
<span>systemctl enable service_name</span> |
Set to start on boot |
<span>systemctl disable service_name</span> |
Cancel start on boot |
<span>systemctl status service_name</span> |
View service status |
firewalld Firewall Configuration
- • Start and set to start on boot:
<span>systemctl start firewalld && systemctl enable firewalld</span>; - • Open port:
<span>firewall-cmd --zone=public --add-port=80/tcp --permanent</span>(–permanent takes effect permanently, requires firewall restart); - • Close port:
<span>firewall-cmd --zone=public --remove-port=80/tcp --permanent</span>; - • Restart firewall:
<span>firewall-cmd --reload</span>; - • View open ports:
<span>firewall-cmd --zone=public --list-ports</span>.
Chapter 6: Practical Case: LAMP Environment Setup
LAMP (Linux+Apache+MySQL+PHP) is a classic web service architecture, steps are as follows:
- 1. Install Apache:
<span>yum install -y httpd</span>, start and set to start on boot:<span>systemctl start httpd && systemctl enable httpd</span>; open port 80:<span>firewall-cmd --add-port=80/tcp --permanent && firewall-cmd --reload</span>. - 2. Install MySQL/MariaDB: CentOS 7 defaults to MariaDB (a MySQL branch), install:
<span>yum install -y mariadb-server mariadb</span>, start and set to start on boot:<span>systemctl start mariadb && systemctl enable mariadb</span>; initialize database:<span>mysql_secure_installation</span>(set root password, delete anonymous users, etc.). - 3. Install PHP:
<span>yum install -y php php-mysql</span>, restart Apache to activate PHP:<span>systemctl restart httpd</span>. - 4. Test Environment: Create a file named
<span>info.php</span>in the<span>/var/www/html/</span>directory, with the content<span><?php phpinfo(); ?></span>, access it via browser at<span>http://server_IP/info.php</span>, if PHP information is displayed, the setup is successful.
Summary and Learning Suggestions
- • Core Logic: This book focuses on “practical operations”; each knowledge point is accompanied by a case study. When learning, practice synchronously with a virtual machine/cloud server to avoid just reading without hands-on experience;
- • Key Breakthroughs: Command line operations, file permissions, and systemd service management are the basics of Linux, which must be mastered proficiently;
- • Advanced Directions: After mastering the basics, you can delve into learning Shell scripting and automation tools (like Ansible), gradually advancing to enterprise-level operations.