🐧Linux命令教程大全-从入门到精通
目录
Linux 系统概述
Linux 文件系统结构
终端与 Shell 基础
文件和目录操作命令
文件查看与编辑命令
用户和权限管理
进程管理命令
磁盘与存储管理
网络相关命令
软件包管理
系统信息与监控
压缩与归档
文本处理三剑客
Shell 脚本基础
实用技巧与快捷键
1. Linux 系统概述 什么是 Linux? Linux 是一个开源的类 Unix 操作系统内核,由 Linus Torvalds 于 1991 年首次发布。如今,Linux 已成为服务器领域的主导操作系统,也是 Android 系统的底层核心。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 mindmap root((Linux 系统)) 内核 Kernel 进程管理 内存管理 文件系统 设备驱动 网络协议栈 Shell Bash Zsh Fish 应用层 命令行工具 GUI 桌面 服务守护进程
主流 Linux 发行版
发行版
包管理器
适用场景
特点
Ubuntu
apt
桌面/服务器
用户友好,社区活跃
CentOS/RHEL
yum/dnf
企业服务器
稳定可靠,长期支持
Debian
apt
服务器
极度稳定,自由软件
Arch Linux
pacman
极客/开发
滚动更新,高度定制
Kali Linux
apt
安全测试
预装渗透测试工具
Alpine
apk
容器/Docker
极致轻量,安全
2. Linux 文件系统结构 目录树结构 Linux 采用单一根目录 的树状结构,所有文件和设备都从 / 开始。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 / # 根目录 ├── /bin # 基本系统命令(ls, cp, mv 等) ├── /boot # 启动文件(内核、grub) ├── /dev # 设备文件(硬盘、终端等) ├── /etc # 系统配置文件 │ ├── /ssh/ # SSH 配置 │ ├── /nginx/ # Nginx 配置 │ └── /systemd/ # Systemd 服务配置 ├── /home # 用户主目录 │ ├── /alice/ │ └── /bob/ ├── /lib # 系统库文件 ├── /media # 挂载点(U盘、光盘) ├── /mnt # 临时挂载点 ├── /opt # 第三方软件 ├── /proc # 进程和内核信息(虚拟文件系统) ├── /root # root 用户主目录 ├── /run # 运行时临时文件 ├── /sbin # 系统管理命令 ├── /srv # 服务数据目录 ├── /sys # 系统硬件信息(虚拟文件系统) ├── /tmp # 临时文件(重启清空) ├── /usr # 用户程序和数据 │ ├── /bin/ # 用户命令 │ ├── /lib/ # 用户库 │ ├── /local/ # 本地编译安装的软件 │ └── /share/ # 共享数据 └── /var # 可变数据(日志、缓存) ├── /log/ # 日志文件 ├── /cache/ # 缓存 └── /spool/ # 队列任务
路径类型 1 2 3 4 5 6 7 8 9 10 /home/alice/Documents/file.txt /etc/nginx/nginx.conf ./file.txt ../file.txt ../../etc/config ~ ~/Downloads
3. 终端与 Shell 基础 什么是 Shell? Shell 是用户与 Linux 内核之间的命令解释器 。你输入命令,Shell 将其翻译给内核执行,然后将结果返回给你。
1 2 3 4 5 6 7 flowchart LR A[👤 用户] -->|输入命令| B[🐚 Shell] B -->|系统调用| C[⚙️ Linux 内核] C -->|操作| D[💾 硬件] D -->|返回| C C -->|返回| B B -->|显示结果| A
常用 Shell 1 2 3 4 5 6 7 8 echo $SHELL cat /etc/shells chsh -s /bin/zsh
命令基本格式 1 2 3 4 5 command [-options] [arguments]ls -l /home tar -xzvf file.tar.gz
获取帮助 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 man ls ls --help info coreutils whatis ls apropos network tldr tar
4. 文件和目录操作命令 目录导航 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 pwd cd /etc cd ~ cd - cd .. cd ../.. cd ls ls -l ls -a ls -la ls -lh ls -lt ls -ltr ls -R ls -S ls *.txt
目录操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 mkdir mydir mkdir -p a/b/c/d mkdir -m 755 mydir rmdir emptydir rm file.txt rm -r dir / rm -rf dir / rm -i file.txt rm -v file.txt rm *.log
⚠️ 警告 :rm -rf 非常危险,没有回收站,删除后无法恢复!
文件操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 touch newfile.txt touch file{1..10}.txt cp source.txt dest.txt cp -r source_dir/ dest_dir/ cp -i source.txt dest.txt cp -v source.txt dest.txt cp -p source.txt dest.txt cp -a source_dir/ dest_dir/ mv oldname.txt newname.txt mv file.txt /home/alice/ mv -i old new mv *.txt archive/ ln -s /path/to/target linkname ln /path/to/target linkname
软链接 vs 硬链接 1 2 3 4 5 6 7 8 flowchart LR subgraph 软链接 A[软链接文件] -.->|指向路径| B[原始文件数据] end subgraph 硬链接 C[硬链接文件] -->|指向同一 inode| D[文件数据块] E[原始文件] -->|指向同一 inode| D end
1 2 3 4 5 6 7 ln -s /usr/bin/python3 /usr/bin/pythonln /home/alice/file.txt /home/alice/hardlink
5. 文件查看与编辑命令 文件内容查看 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 cat file.txt cat -n file.txt cat file1.txt file2.txt cat file1.txt > combined.txt less largefile.txt more file.txthead file.txt head -n 20 file.txt head -c 100 file.txt tail file.txt tail -n 20 file.txt tail -f /var/log/syslog tail -F /var/log/syslog wc file.txt wc -l file.txt wc -w file.txt wc -c file.txt nl file.txt
使用 Vim 编辑文件 1 2 3 4 5 6 7 8 vim file.txt vi file.txt
快捷键
模式
功能
i
Normal → Insert
在光标前插入
a
Normal → Insert
在光标后插入
o
Normal → Insert
在下一行插入
Esc
Insert → Normal
返回普通模式
:w
Normal → Command
保存
:q
Normal → Command
退出
:wq / ZZ
Normal → Command
保存并退出
:q!
Normal → Command
强制退出不保存
dd
Normal
删除当前行
yy
Normal
复制当前行
p
Normal
粘贴
u
Normal
撤销
Ctrl+r
Normal
重做
/search
Normal
搜索文本
:set nu
Normal → Command
显示行号
使用 Nano 编辑文件
6. 用户和权限管理 用户管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 whoami id id alice who w last last -n 10 sudo useradd alice sudo useradd -m -s /bin/bash bob sudo passwd alice sudo usermod -aG sudo alice sudo userdel alice sudo userdel -r alice
文件权限系统 Linux 文件权限基于三种角色和三种权限:
1 2 3 4 5 6 - rw- r-- r-- alice staff 1024 Jun 27 19:00 file.txt | | | | | | | └── 其他用户权限 (other) | | └─────── 用户组权限 (group) | └──────────── 所有者权限 (owner) └─────────────── 文件类型 (-文件, d目录, l链接)
权限
数字
文件
目录
r (read)
4
读取文件内容
列出目录内容
w (write)
2
修改文件内容
创建/删除目录中文件
x (execute)
1
执行文件
进入目录
权限操作命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 chmod 755 script.sh chmod u+x script.sh chmod g-w file.txt chmod o+r file.txt chmod -R 755 dir / sudo chown alice file.txt sudo chown alice:staff file.txt sudo chown -R alice:staff dir / sudo chgrp staff file.txt
sudo 权限 1 2 3 4 5 6 7 8 9 10 11 12 sudo command sudo su -sudo -isudo visudo alice ALL=(ALL) NOPASSWD: ALL
7. 进程管理命令 进程查看 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ps ps aux ps -ef ps -ef | grep nginx top htop pgrep nginx pgrep -u alice pgrep -f "python app" pidof sshd
进程控制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 kill PID kill -9 PID kill -15 PID kill -l pkill nginx pkill -f "python app" killall chromecommand & Ctrl+Z jobs fg %1 bg %1 nohup command & nohup python train.py > output.log 2>&1 &
8. 磁盘与存储管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 df -h df -hT df -i du -sh * du -sh /home/alice du -h --max-depth=1 du -sh * | sort -hr mount mount /dev/sdb1 /mnt mount -t nfs server:/share /mnt umount /mnt lsblk lsblk -f sudo fdisk -l sudo fdisk /dev/sdb blkid
9. 网络相关命令 网络配置查看 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ip addr ip addr show eth0 ip link ip route ifconfig ifconfig eth0 ping google.com ping -c 4 google.com ping -i 0.5 google.com ss -tlnp ss -tlnp | grep :80 ss -an netstat -tlnp netstat -an | grep 3306
网络请求与传输 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 curl https://api.example.com curl -X POST https://api.example.com/data curl -H "Content-Type: application/json" \ -d '{"key":"value"}' url curl -O https://example.com/file.zip curl -o myfile.zip https://... curl -I https://example.com curl -v https://example.com curl -L https://short.url curl -u user:pass https://api.example.com wget https://example.com/file.zip wget -c https://example.com/file.zip wget -r -np https://example.com/docs/ scp file.txt user@server:/path/ scp user@server:/path/file.txt ./ scp -r dir / user@server:/path/ scp -P 2222 file.txt user@server:/path/ rsync -avz source / dest/ rsync -avz --delete source / dest/ rsync -avz -e "ssh -p 2222" source / user@server:/dest/
防火墙(ufw) 1 2 3 4 5 6 7 8 9 10 sudo ufw status sudo ufw enable sudo ufw disable sudo ufw allow 22 sudo ufw allow 80/tcp sudo ufw allow 443 sudo ufw allow from 192.168.1.100 sudo ufw delete allow 80 sudo ufw reset
10. 软件包管理 Debian/Ubuntu (apt) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 sudo apt updatesudo apt upgradesudo apt full-upgrade sudo apt install nginxsudo apt install vim git curl -y apt search keyword apt-cache search keyword apt show nginxsudo apt remove nginx sudo apt purge nginx sudo apt autoremove sudo apt clean sudo apt autoclean apt list --installed dpkg -l
CentOS/RHEL/Fedora (yum/dnf) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 sudo yum search nginxsudo yum install nginx -ysudo dnf install nginx -ysudo yum updatesudo yum upgradesudo yum remove nginxsudo yum clean all yum list installed yum repolist
从源码编译安装 1 2 3 4 5 6 7 ./configure --prefix=/usr/local/myapp make -j$(nproc ) sudo make installsudo make uninstall
11. 系统信息与监控 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 uname -a uname -r uname -m hostnamectl hostnamectl set-hostname myserver lsb_release -acat /etc/os-release uptime free -h free -m lscpu lsblk lspci | grep -i network lspci | grep -i vga lsusbsudo dmidecode -t memory sudo dmidecode -t system cat /proc/loadavg w
日志查看 1 2 3 4 5 6 7 8 9 10 11 12 13 journalctl -u nginx journalctl -u nginx -f journalctl -u nginx --since today journalctl -u nginx -n 50 journalctl --since "2026-06-27" --until "2026-06-28" cat /var/log/syslog cat /var/log/messages tail -f /var/log/auth.log tail -f /var/log/nginx/access.log tail -f /var/log/nginx/error.log
12. 压缩与归档 tar - 归档与压缩 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 tar -czvf archive.tar.gz dir / tar -cjvf archive.tar.bz2 dir / tar -cJvf archive.tar.xz dir / tar -xzvf archive.tar.gz tar -xjvf archive.tar.bz2 tar -xJvf archive.tar.xz tar -xvf archive.tar -C /target/ tar -tzvf archive.tar.gz
其他压缩工具 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 gzip file.txt gunzip file.txt.gz gzip -d file.txt.gz gzip -9 file.txt zip -r archive.zip dir / unzip archive.zip unzip -l archive.zip 7z a archive.7z dir / 7z x archive.7z 7z l archive.7z
13. 文本处理三剑客 grep - 文本搜索 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 grep "pattern" file.txt grep -i "pattern" file.txt grep -v "pattern" file.txt grep -r "pattern" /path/to/dir/ grep -n "pattern" file.txt grep -c "pattern" file.txt grep -l "pattern" *.txt grep -E "pattern1|pattern2" file.txt grep -A 3 "pattern" file.txt grep -B 3 "pattern" file.txt grep -C 3 "pattern" file.txt ps aux | grep nginx history | grep git grep -r "TODO" src/ grep -v "^#" config.conf | grep -v "^$"
sed - 流编辑器(文本替换) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 sed 's/old/new/' file.txt sed 's/old/new/g' file.txt sed 's/old/new/gi' file.txt sed -i 's/old/new/g' file.txt sed '5d' file.txt sed '5,10d' file.txt sed '/pattern/d' file.txt sed -n '5p' file.txt sed -n '5,10p' file.txt sed '5i\New line before' file.txt sed '5a\New line after' file.txt sed -i 's/\r$//' file.txt sed -i '/^$/d' file.txt sed 's/[[:space:]]*$//' file.txt
awk - 文本分析工具 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 awk '{print $1}' file.txt awk '{print $1, $3}' file.txt awk -F: '{print $1, $7}' /etc/passwd awk -F, '{print $2}' data.csv awk '$3 > 100' file.txt awk '$1 == "error"' log.txt awk 'NR>=5 && NR<=10' file.txt awk '{print NR, $0}' file.txt awk '{print NF, $0}' file.txt awk 'END {print NR}' file.txt awk '{sum+=$3} END {print sum}' data.txt awk '{if($3>80) print $1,"优秀"; else print $1,"继续努力"}' score.txtdf -h | awk 'NR>1 {print $5, $6}'
14. Shell 脚本基础 第一个脚本 1 2 3 4 5 6 7 #!/bin/bash echo "Hello, World!" echo "当前用户: $(whoami) " echo "当前目录: $(pwd) " echo "当前时间: $(date) "
1 2 3 4 chmod +x script.sh ./script.sh bash script.sh
变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #!/bin/bash name="Alice" age=25echo "Name: $name " echo "Age: ${age} " echo "Hello, ${name} !" echo "脚本名: $0 " echo "第一个参数: $1 " echo "第二个参数: $2 " echo "参数个数: $# " echo "所有参数: $@ " echo "退出状态: $?" echo "当前进程PID: $$" current_date=$(date ) file_count=$(ls | wc -l)echo "Today is $current_date " echo "Files: $file_count "
条件判断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #!/bin/bash if [ "$name " = "Alice" ]; then echo "Welcome, Alice!" elif [ "$name " = "Bob" ]; then echo "Welcome, Bob!" else echo "Who are you?" fi if [ -f "file.txt" ]; then echo "file.txt exists" fi if [ -d "/etc" ]; then echo "/etc is a directory" fi if [ -x "script.sh" ]; then echo "script.sh is executable" fi if [ "$age " -gt 18 ]; then echo "Adult" fi if [ -z "$var " ]; then echo "var is empty" fi if [ -n "$var " ]; then echo "var is not empty" fi if [ -f "a.txt" ] && [ -f "b.txt" ]; then echo "Both exist" fi if [ -f "a.txt" ] || [ -f "b.txt" ]; then echo "At least one exists" fi
循环 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #!/bin/bash for i in 1 2 3 4 5; do echo "Number: $i " done for i in {1..10}; do echo "Count: $i " done for file in *.txt; do echo "Processing: $file " done count=1while [ $count -le 5 ]; do echo "Count: $count " ((count++))done while read line; do echo "Line: $line " done < file.txt
函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash greet () { echo "Hello, $1 !" } greet "Alice" greet "Bob" add () { result=$(($1 + $2 )) echo $result }sum =$(add 5 3)echo "5 + 3 = $sum "
15. 实用技巧与快捷键 终端快捷键
快捷键
功能
Ctrl+C
终止当前命令
Ctrl+D
退出当前终端 / EOF
Ctrl+Z
暂停当前进程
Ctrl+L
清屏(等同于 clear)
Ctrl+A
光标移到行首
Ctrl+E
光标移到行尾
Ctrl+U
删除光标前所有内容
Ctrl+K
删除光标后所有内容
Ctrl+W
删除前一个单词
Ctrl+R
搜索命令历史
Ctrl+Shift+C
复制(终端中)
Ctrl+Shift+V
粘贴(终端中)
Tab
自动补全
!!
执行上一条命令
!$
上一条命令的最后一个参数
输入输出重定向 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 command > file.txt command >> file.txt command 2> error.log command &> all.log command > /dev/null command < input.txtwc -l < file.txt command1 | command2 | command3cat access.log | grep "404" | awk '{print $1}' | sort | uniq -c | sort -rn
常用一行命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 find / -type f -exec du -Sh {} + | sort -rh | head -n 10 find . -emptyfor f in *.txt; do mv "$f " "prefix_$f " ; done find . -name "*.py" | xargs wc -l | tail -1 grep -rl "TODO" src/ find . -type f | sed 's/.*\.//' | sort | uniq -ccp file.txt{,.bak} openssl rand -base64 16cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 16history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10 ps aux | grep process_name | awk '{print $2}' | xargs kill -9
别名与配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 alias ll='ls -alF' alias la='ls -A' alias ..='cd ..' alias ...='cd ../..' alias gs='git status' alias gp='git pull' alias df ='df -h' alias du ='du -sh' alias myip='curl ifconfig.me' alias ports='ss -tlnp' source ~/.bashrc . ~/.bashrc
定时任务 (Crontab) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 crontab -e crontab -l 0 3 * * * /backup.sh */5 * * * * /path/to/script.sh 0 0 1 * * /monthly.sh @reboot /startup.sh
总结 这份教程涵盖了 Linux 最核心和最常用的命令。建议按以下路径学习:
✅ 先掌握文件目录操作 (ls, cd, cp, mv, rm)
✅ 学会查看文件内容 (cat, less, head, tail)
✅ 理解权限系统 (chmod, chown)
✅ 熟练使用管道和重定向
✅ 掌握grep 进行文本搜索
✅ 学会进程管理 (ps, top, kill)
✅ 最后学习 Shell 脚本 自动化任务
💡 提示 :最好的学习方式就是多用!在虚拟机或云服务器上搭建一个 Linux 环境,每天用命令行完成日常操作,很快就能熟练。
参考资源 :