ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Linux 基础学习全景笔记:从命令入门到 CentOS Stream 10 环境实战

Linux 基础学习全景笔记:从命令入门到 CentOS Stream 10 环境实战 本文根据一份完整的 Linux 基础课程笔记整理而成适合零基础入门、期末复习或运维入门打底。内容覆盖目录与路径、常用命令、文件操作、查找与文本处理、vim/nano、用户权限、软件包管理、网络配置、进程监控、磁盘管理、环境变量、压缩解压、仓库配置、国内镜像源、EPEL、VirtualBox 网络切换与系统监控。建议边看边在虚拟机或云服务器上敲一遍Linux 是练出来的。一、Linux 目录结构与路径Linux 的目录结构是一棵“树”只有一个根目录/。Windows 有 C、D、E 盘而 Linux 没有盘符所有文件都挂在/下面。路径分隔符也不同Linux/usr/local/hello.txtWindowsD:\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 与 pwdcd /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、locatewhich 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 21 find / -name test all.log find / -name test 2 /dev/null重定向标准输出2重定向标准错误21或合并输出和错误/dev/nullLinux 黑洞丢弃输出四、文本编辑器vim 与 nano1. 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.txtCtrlO保存CtrlX退出CtrlW搜索。适合快速改配置文件。五、用户与权限管理1. root、su、sudosu - root # 切换到 root exit # 退出 sudo ls /root # 临时以 root 身份执行为普通用户配置 sudovisudo # 添加 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权限字符r4、w2、x1。chmod 755 hello.txt chmod urwx,grx,ox hello.txt chown root:itcast hello.txt chgrp itcast hello.txt chown -R root test命令作用chmod修改读写执行权限chown修改所属用户和组通常 rootchgrp仅修改所属组六、软件包与服务管理1. yum、apt、dnf、rpm工具适用系统特点yumCentOS 7、RHEL 7自动解决依赖dnfCentOS 8/9、Rocky、Fedorayum 升级版aptUbuntu、Debian自动解决依赖rpmRPM 系通用底层工具不自动解决依赖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. systemctlsystemctl 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 替代 ntpCentOS Stream 8/9/10 已废弃ntp推荐chronyyum 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域名解析先查本机 hostsWindowsC:\Windows\System32\drivers\etc\hostsLinux/etc/hosts再查公开 DNS如114.114.114.114、8.8.8.8。3. 固定 IPCentOS Stream nmclinmcli 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后台运行CtrlZ挂起当前前台进程jobs查看后台作业bg后台继续运行fg调回前台3. 磁盘管理df -h du -sh /var du -h --max-depth1 /var iostat -x mount /dev/sdb1 /mnt/usb umount /mnt/usbdf看分区整体使用率du看目录/文件占用。4. 网络统计sar -n DEV 3 2查看网卡收发数据包、流量等。九、环境变量、压缩与仓库配置1. 环境变量env echo $PATH export ITHEIMA666 source ~/.bashrcPATH决定命令搜索路径。临时生效用export永久生效写入当前用户~/.bashrc所有用户/etc/profile2. 上传下载与压缩解压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。操作前先切换到 rootsu -1. 备份原有 repoCentOS 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.repocat /etc/yum.repos.d/centos.repo EOF [baseos] nameCentOS Stream $releasever - BaseOS baseurlhttps://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/BaseOS/$basearch/os gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck1 repo_gpgcheck0 metadata_expire6h countme1 enabled1 [baseos-debuginfo] nameCentOS Stream $releasever - BaseOS - Debug baseurlhttps://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/BaseOS/$basearch/debug/tree/ gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck1 repo_gpgcheck0 metadata_expire6h countme1 enabled0 [baseos-source] nameCentOS Stream $releasever - BaseOS - Source baseurlhttps://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/BaseOS/source/tree/ gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck1 repo_gpgcheck0 metadata_expire6h countme1 enabled0 [appstream] nameCentOS Stream $releasever - AppStream baseurlhttps://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/AppStream/$basearch/os gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck1 repo_gpgcheck0 metadata_expire6h countme1 enabled1 [appstream-debuginfo] nameCentOS Stream $releasever - AppStream - Debug baseurlhttps://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/AppStream/$basearch/debug/tree/ gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck1 repo_gpgcheck0 metadata_expire6h countme1 enabled0 [appstream-source] nameCentOS Stream $releasever - AppStream - Source baseurlhttps://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/AppStream/source/tree/ gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck1 repo_gpgcheck0 metadata_expire6h countme1 enabled0 [crb] nameCentOS Stream $releasever - CRB baseurlhttps://mirrors.ustc.edu.cn/centos-stream/$releasever-stream/CRB/$basearch/os gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial gpgcheck1 repo_gpgcheck0 metadata_expire6h countme1 enabled0 EOF再覆盖/etc/yum.repos.d/centos-addons.repocat /etc/yum.repos.d/centos-addons.repo EOF [extras-common] nameCentOS Stream $releasever - Extras packages baseurlhttps://mirrors.ustc.edu.cn/centos-stream/SIGs/$releasever-stream/extras/$basearch/extras-common gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Extras-SHA512 gpgcheck1 repo_gpgcheck0 metadata_expire6h countme1 enabled1 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. 安装 EPELdnf 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 很多包会依赖报错。卸载 EPELdnf remove -y epel-release十一、VirtualBox CentOS Stream 10 网络模式切换网卡名enp0s3备份文件enp0s3.nmconnection.bak前提备份文件已存在里面存的是 NAT 静态 IP 配置192.168.88.130模式 1NAT 静态 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查看 IPifconfig预期拿到路由器分配的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组合。权限数字法r4、w2、x1755是常见目录权限。环境变量 PATH 决定命令能否直接执行。多敲命令多查man和--help。总结Linux 基础并不只是背命令而是理解一套“一切皆文件、权限分明、组合小工具完成大任务”的思想。从ls、cd、cp、rm到grep、find、chmod、systemctl、nmcli这些命令构成了日常运维和开发的基石。建议你按本文顺序在虚拟机里完整走一遍建目录 → 建文件 → 改权限 → 配网络 → 装软件 → 看进程 → 查磁盘 → 写脚本。当你能不查笔记就完成这些操作时Linux 基础就算真正入门了。
返回列表