本文根据一份完整的 Linux 基础课程笔记整理而成,适合零基础入门、期末复习或运维入门打底。
内容覆盖:目录与路径、常用命令、文件操作、查找与文本处理、vim/nano、用户权限、软件包管理、网络配置、进程监控、磁盘管理、环境变量、压缩解压、仓库配置、国内镜像源、EPEL、VirtualBox 网络切换与系统监控。
建议边看边在虚拟机或云服务器上敲一遍,Linux 是练出来的。
一、Linux 目录结构与路径
Linux 的目录结构是一棵“树”,只有一个根目录/。Windows 有 C、D、E 盘,而 Linux 没有盘符,所有文件都挂在/下面。
路径分隔符也不同:
Linux:
/usr/local/hello.txtWindows:
D:\data\work\hello.txt
路径分两种:
绝对路径:以
/开头,从根目录出发,如/etc/hosts相对路径:不以
/开头,从当前目录出发,如test/hello.txt
特殊路径符:
| 符号 | 含义 | 示例 |
|---|---|---|
. | 当前目录 | cd ./Desktop |
.. | 上一级目录 | cd .. |
~ | 当前用户 HOME 目录 | cd ~/Desktop |
普通用户的 HOME 目录一般是/home/用户名。
二、命令基础与文件操作
Linux 命令通用格式:
command [-options] [parameter]例如:
ls -l /home/itheima cp -r test1 test21. ls:列出目录内容
ls # 平铺列出当前目录 ls / # 查看根目录 ls -a # 显示隐藏文件,以 . 开头 ls -l # 列表形式,显示权限、大小、日期等 ls -h # 人性化显示大小,通常配合 -l ls -lah # 等价于 ls -a -l -h2. cd 与 pwd
cd /etc # 切换到 /etc cd # 回到 HOME cd ~ # 回到 HOME cd .. # 上一级 pwd # 查看当前工作目录3. 创建目录与文件
mkdir test mkdir -p itcast/good/666 # 自动创建多级父目录 touch hello.txt # 创建空文件4. 查看文件内容:cat、more、less、head、tail
| 命令 | 特点 | 常用场景 |
|---|---|---|
cat | 一次性输出全部 | 小文件 |
more | 分页,只能向下 | 中大文件 |
less | 分页,可上下翻、搜索 | 大文件、日志,最推荐 |
head | 看开头,默认前 10 行 | 看表头、日志头部 |
tail | 看结尾,默认后 10 行,支持-f | 实时监控日志 |
示例:
cat test.txt more /etc/services less -N /etc/services head -n 5 test.txt tail -n 3 test.txt tail -f work.txt # 持续跟踪文件变化less常用操作:空格翻页、b上翻、/关键字搜索、n下一个、q退出。
5. 复制、移动、删除
cp -r test1 test2 # 复制文件夹 mv test.txt Desktop/ # 移动 mv test2.txt test3.txt # 目标不存在时改名 rm -r test # 删除文件夹 rm -rf test # 强制删除,慎用 rm -r test* # 通配符删除⚠️危险警告:
rm -rf /和rm -rf /*等同于格式化系统,绝对不要执行。
三、查找、文本处理与重定向
1. which、find、locate
which cd # 查看命令程序文件位置 find / -name "*centos*" # 按文件名查找 find /usr -size +100M # 按大小查找 locate itcast # 查索引数据库,速度极快 updatedb # 手动更新 locate 数据库,需 rootfind实时遍历磁盘,准确但慢;locate查数据库,快但非实时。
2. grep、wc 与管道符
grep -n "itheima" test.txt wc -l test.txt cat test.txt | grep itcast | wc -l管道符|把左边命令的输出,作为右边命令的输入。
3. echo、反引号与重定向
echo "Hello Linux" echo `pwd` echo "Hello Linux" > itheima.txt # 覆盖 echo "Hello itcast" >> itheima.txt # 追加标准错误重定向:
find / -name "test" > success.log 2> error.log find / -name "test" > all.log 2>&1 find / -name "test" &> all.log find / -name "test" 2> /dev/null>:重定向标准输出2>:重定向标准错误2>&1或&>:合并输出和错误/dev/null:Linux 黑洞,丢弃输出
四、文本编辑器:vim 与 nano
1. vim 三种模式
命令模式:默认模式,按键是命令
输入模式:按
i、a、o等进入,正常打字底线命令模式:按
:进入,用于保存退出
常用操作:
vim hello.txt # 按 i 进入输入模式,输入内容 # 按 Esc 回命令模式 # 输入 :wq 保存退出底线命令:
| 命令 | 作用 |
|---|---|
:wq | 保存退出 |
:q | 退出 |
:q! | 强制退出 |
:w | 仅保存 |
:set nu | 显示行号 |
命令模式常用:dd删除行、yy复制行、p粘贴、u撤销、gg首行、G尾行、/搜索。
2. nano:更简单的编辑器
nano test.txtCtrl+O保存,Ctrl+X退出,Ctrl+W搜索。适合快速改配置文件。
五、用户与权限管理
1. root、su、sudo
su - root # 切换到 root exit # 退出 sudo ls /root # 临时以 root 身份执行为普通用户配置 sudo:
visudo # 添加: itheima ALL=(ALL) NOPASSWD: ALL2. 用户与用户组
groupadd itcast groupdel itcast useradd -g itcast itheima userdel -r itheima id itheima usermod -aG itcast itheima getent passwd getent group passwd itheimapasswd用于设置或修改密码,实际写入/etc/shadow。
3. chmod、chown、chgrp
权限字符:r=4、w=2、x=1。
chmod 755 hello.txt chmod u=rwx,g=rx,o=x hello.txt chown root:itcast hello.txt chgrp itcast hello.txt chown -R root test| 命令 | 作用 |
|---|---|
chmod | 修改读写执行权限 |
chown | 修改所属用户和组,通常 root |
chgrp | 仅修改所属组 |
六、软件包与服务管理
1. yum、apt、dnf、rpm
| 工具 | 适用系统 | 特点 |
|---|---|---|
yum | CentOS 7、RHEL 7 | 自动解决依赖 |
dnf | CentOS 8/9、Rocky、Fedora | yum 升级版 |
apt | Ubuntu、Debian | 自动解决依赖 |
rpm | RPM 系通用 | 底层工具,不自动解决依赖 |
yum -y install wget yum -y remove wget yum search wget yum update wget apt install wget apt remove wget rpm -ivh package.rpm rpm -Uvh package.rpm rpm -e wget rpm -qa | grep wget2. systemctl
systemctl start sshd systemctl stop sshd systemctl status sshd systemctl enable sshd systemctl disable sshd systemctl enable --now chronyd常见服务:NetworkManager、firewalld、sshd、httpd、chronyd。
3. 时间同步:chrony 替代 ntp
CentOS Stream 8/9/10 已废弃ntp,推荐chrony:
yum install chrony -y systemctl enable --now chronyd chronyc -a makestep chronyc tracking七、网络基础与远程连接
1. IP 与特殊地址
ifconfig ip addr127.0.0.1:本机回环地址0.0.0.0:本机或所有 IP,常用于端口绑定和放行规则
2. 主机名与域名解析
hostname hostnamectl set-hostname centos域名解析先查本机 hosts:
Windows:
C:\Windows\System32\drivers\etc\hostsLinux:
/etc/hosts
再查公开 DNS,如114.114.114.114、8.8.8.8。
3. 固定 IP:CentOS Stream + nmcli
nmcli connection show nmcli connection modify "连接名" ipv4.method manual ipv4.addresses 192.168.88.130/24 ipv4.gateway 192.168.88.1 ipv4.dns 114.114.114.114 nmcli connection up "连接名"4. VirtualBox 网络模式简表
| 模式 | 虚拟机上网 | 宿主机直连 | 多虚拟机互通 | 适用 |
|---|---|---|---|---|
| NAT | ✅ | ❌ 端口转发 | ❌ | 单机学习 |
| NAT 网络 | ✅ | ❌ 端口转发 | ✅ | 多机集群 |
| 桥接 | ✅ | ✅ | ✅ | 局域网访问 |
5. 网络工具与端口
ping -c 3 baidu.com wget http://example.com/file.tar.gz curl -O http://example.com/file.tar.gz nmap 127.0.0.1 netstat -anp | grep 22端口范围:
公认端口:1~1023
注册端口:1024~49151
动态端口:49152~65535
八、进程、磁盘与系统监控
1. 进程管理
ps -ef ps -ef | grep tail kill 2339 kill -9 2339top类似 Windows 任务管理器:
top top -p 1234 top -d 2 top -n 3交互按键:P按 CPU 排序、M按内存排序、q退出。
2. 作业管理
tail -f test.txt & Ctrl + Z jobs bg %1 fg %1 kill %1&:后台运行Ctrl+Z:挂起当前前台进程jobs:查看后台作业bg:后台继续运行fg:调回前台
3. 磁盘管理
df -h du -sh /var du -h --max-depth=1 /var iostat -x mount /dev/sdb1 /mnt/usb umount /mnt/usbdf看分区整体使用率,du看目录/文件占用。
4. 网络统计
sar -n DEV 3 2查看网卡收发数据包、流量等。
九、环境变量、压缩与仓库配置
1. 环境变量
env echo $PATH export ITHEIMA=666 source ~/.bashrcPATH决定命令搜索路径。临时生效用export,永久生效写入:
当前用户:
~/.bashrc所有用户:
/etc/profile
2. 上传下载与压缩解压
yum -y install lrzsz rz sz test.txt压缩解压:
tar -zcvf test.tar.gz 1.txt 2.txt tar -zxvf test.tar.gz -C /home/itheima zip -r test.zip test itheima unzip test.zip -d /home/itheima gzip test.txt gunzip test.txt.gz记忆技巧:-f永远紧挨包名,且放在选项最后。
3. Yum/DNF 仓库
仓库配置文件在/etc/yum.repos.d/,以.repo结尾。
yum repolist yum makecache yum clean all yum --disablerepo="仓库ID" install 软件名 yum --enablerepo="仓库ID" install 软件名常见第三方源:EPEL、Remi、Docker、Nginx、MySQL。
十、CentOS Stream 10 配置国内镜像源与 EPEL
适用环境:VirtualBox + CentOS Stream 10。
操作前先切换到 root:su -
1. 备份原有 repo
CentOS Stream 10 默认源文件:
/etc/yum.repos.d/centos.repo /etc/yum.repos.d/centos-addons.repo先备份,出错可恢复:
cp /etc/yum.repos.d/centos.repo /etc/yum.repos.d/centos.repo.bak cp /etc/yum.repos.d/centos-addons.repo /etc/yum.repos.d/centos-addons.repo.bak2. 替换为中科大源
中科大源国内稳定,推荐使用。
覆盖/etc/yum.repos.d/centos.repo:
cat > /etc/yum.repos.d/centos.repo <<'EOF' [baseos] name=CentOS Stream $releasever - BaseOS baseurl=https://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/BaseOS/$basearch/os gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck=1 repo_gpgcheck=0 metadata_expire=6h countme=1 enabled=1 [baseos-debuginfo] name=CentOS Stream $releasever - BaseOS - Debug baseurl=https://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/BaseOS/$basearch/debug/tree/ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck=1 repo_gpgcheck=0 metadata_expire=6h countme=1 enabled=0 [baseos-source] name=CentOS Stream $releasever - BaseOS - Source baseurl=https://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/BaseOS/source/tree/ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck=1 repo_gpgcheck=0 metadata_expire=6h countme=1 enabled=0 [appstream] name=CentOS Stream $releasever - AppStream baseurl=https://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/AppStream/$basearch/os gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck=1 repo_gpgcheck=0 metadata_expire=6h countme=1 enabled=1 [appstream-debuginfo] name=CentOS Stream $releasever - AppStream - Debug baseurl=https://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/AppStream/$basearch/debug/tree/ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck=1 repo_gpgcheck=0 metadata_expire=6h countme=1 enabled=0 [appstream-source] name=CentOS Stream $releasever - AppStream - Source baseurl=https://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/AppStream/source/tree/ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck=1 repo_gpgcheck=0 metadata_expire=6h countme=1 enabled=0 [crb] name=CentOS Stream $releasever - CRB baseurl=https://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/CRB/$basearch/os gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck=1 repo_gpgcheck=0 metadata_expire=6h countme=1 enabled=0 EOF再覆盖/etc/yum.repos.d/centos-addons.repo:
cat > /etc/yum.repos.d/centos-addons.repo <<'EOF' [extras-common] name=CentOS Stream $releasever - Extras packages baseurl=https://mirrors.ustc.edu.cn/centos-stream/SIGs/$releasever-stream/extras/$basearch/extras-common gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512 gpgcheck=1 repo_gpgcheck=0 metadata_expire=6h countme=1 enabled=1 EOF如果你想用清华源,把上面所有:
mirrors.ustc.edu.cn替换成:
mirrors.tuna.tsinghua.edu.cn即可。
3. 清理旧缓存并生成新缓存
dnf clean all dnf makecache如果这一步无报错,说明镜像源配置成功。
如果出现元数据失败,优先检查虚拟机网络和 URL 拼写。
4. 测试安装软件
dnf install -y tree能正常下载安装,就说明源可用。
5. 开启 CRB 仓库
EPEL 很多包依赖 CRB,必须先开启。
dnf config-manager --set-enabled crb等价 yum 写法:
yum config-manager --set-enabled crb6. 安装 EPEL
dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-10.noarch.rpmyum 写法:
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-10.noarch.rpm刷新缓存:
dnf makecache验证 EPEL 是否启用:
dnf repolist enabled | grep epel出现epel仓库即成功。
测试安装:
dnf install -y tree # 或 yum install -y tree7. 检查 EPEL 是否走国内镜像
cat /etc/yum.repos.d/epel.repo如果发现仍然走官方源,可以手动把metalink注释掉,改用中科大 EPEL 的baseurl。
正常情况下,系统源替换为 USTC 后,EPEL 下载速度也会明显改善。
8. 恢复官方源
如果配置出错,可以直接还原备份:
mv /etc/yum.repos.d/centos.repo.bak /etc/yum.repos.d/centos.repo mv /etc/yum.repos.d/centos-addons.repo.bak /etc/yum.repos.d/centos-addons.repo dnf clean all && dnf makecache9. 常见坑
虚拟机网络不通:
VirtualBox 设置 → 网卡 1 → 连接方式选NAT,启动虚拟机后ping mirrors.ustc.edu.cn。SELinux / 防火墙一般不影响 dnf 源,不用特意关闭。
CRB 仓库一定要开,否则 EPEL 很多包会依赖报错。
卸载 EPEL:
dnf remove -y epel-release十一、VirtualBox + CentOS Stream 10 网络模式切换
网卡名:
enp0s3
备份文件:enp0s3.nmconnection.bak
前提:备份文件已存在,里面存的是 NAT 静态 IP 配置192.168.88.130
模式 1:NAT 静态 IP → 桥接 DHCP
目标:从192.168.88.130切换到路由器分配的192.168.20.x。
操作步骤:
关闭虚拟机
VirtualBox 设置 → 网络 → 网卡 1:
连接到:桥接网卡
选择你的 Realtek WiFi 网卡
保存确定
开机启动虚拟机
推荐在虚拟机本地终端执行,不要用 FinalShell,因为执行后会断连
第一次切换时先备份:
sudo cp /etc/NetworkManager/system-connections/enp0s3.nmconnection /etc/NetworkManager/system-connections/enp0s3.nmconnection.bak查看备份:
sudo cat /etc/NetworkManager/system-connections/enp0s3.nmconnection.bak sudo ls -l /etc/NetworkManager/system-connections/确认存在:
enp0s3.nmconnection enp0s3.nmconnection.bak切换为 DHCP 自动获取:
sudo nmcli connection modify enp0s3 ipv4.method auto ipv4.addresses "" ipv4.gateway "" ipv4.dns "" sudo nmcli connection reload sudo nmcli device reapply enp0s3查看 IP:
ifconfig预期拿到路由器分配的192.168.20.x。
如果笔记本 WiFi 桥接失败,可能仍然拿到192.168.88.x。
模式 2:桥接 DHCP → NAT 静态 IP
目标:从桥接 DHCP 切回 NAT 静态 IP192.168.88.130。
操作步骤:
关闭虚拟机
VirtualBox 设置 → 网络 → 网卡 1:
连接到:NAT
保存确定
开机启动虚拟机
在 FinalShell 或虚拟机本地终端执行:
sudo cp /etc/NetworkManager/system-connections/enp0s3.nmconnection.bak /etc/NetworkManager/system-connections/enp0s3.nmconnection sudo nmcli connection reload sudo nmcli device reapply enp0s3回车执行瞬间,当前 SSH 会话会断开,这是正常现象。
然后在 FinalShell 中用旧连接重新连接:
192.168.88.130关键规则
VirtualBox 网卡模式必须和 Linux 内网络配置匹配
VirtualBox 网卡 = NAT → Linux 配置必须是静态
192.168.88.130VirtualBox 网卡 = 桥接 → Linux 配置必须是 DHCP auto
不匹配会导致网络异常:可能能 ping 通外网,但宿主机无法互通。
修改网卡配置的命令都会断 SSH,优先在虚拟机本地终端执行更稳。
备份操作只需要执行一次,后续来回切换不用重复备份。
十二、学习建议与避坑清单
不要长期使用 root,日常用普通用户 +
sudo。rm -rf /绝对禁止,删除前先pwd、ls确认。-f在 tar 中必须放最后,如tar -zcvf test.tar.gz file。CentOS Stream 8/9/10 用 chrony,不用 ntp。
less 比 more 更强,看大文件优先用
less。find 慢但准,locate 快但可能不是最新。
管道符
|是 Linux 的灵魂,多练grep、wc、tail -f组合。权限数字法:
r=4、w=2、x=1,755是常见目录权限。环境变量 PATH 决定命令能否直接执行。
多敲命令,多查
man和--help。
总结
Linux 基础并不只是背命令,而是理解一套“一切皆文件、权限分明、组合小工具完成大任务”的思想。
从ls、cd、cp、rm到grep、find、chmod、systemctl、nmcli,这些命令构成了日常运维和开发的基石。
建议你按本文顺序在虚拟机里完整走一遍:
建目录 → 建文件 → 改权限 → 配网络 → 装软件 → 看进程 → 查磁盘 → 写脚本。
当你能不查笔记就完成这些操作时,Linux 基础就算真正入门了。