Linux 操作系统 – 完整 DevOps 学习笔记
发布: (2026年1月7日 GMT+8 02:50)
7 min read
原文: Dev.to
Source: Dev.to
请提供您希望翻译的具体文本内容,我会在保持原始格式、Markdown 语法和技术术语不变的前提下,将其翻译为简体中文。谢谢!
第1部分 – Linux 基础与架构
1. 什么是 Linux?
Linux 是一个开源、类 Unix 的操作系统内核。在 DevOps 环境中,“Linux” 通常指的是一个 发行版(例如 Ubuntu、CentOS、Alpine),它将内核、系统工具和包管理器打包在一起。
| 组件 | 描述 |
|---|---|
| 内核 | 管理 CPU、内存和 I/O 的核心 |
| Shell | 内核的命令行界面(例如 Bash、Zsh) |
| 用户空间 | 应用程序运行的地方 |
2. Linux 文件系统层次结构
Linux 使用单一的层次树结构,起始于根目录 /。
/
├─ bin & /usr/bin – Essential user binaries (ls, cp, …)
├─ etc – Configuration files (e.g., /etc/nginx/nginx.conf)
├─ home – User home directories (e.g., /home/john)
├─ var – Variable data (logs, spool files, temporary e‑mail)
├─ tmp – Temporary files (cleared on reboot)
└─ proc – Virtual FS exposing process & kernel info
第2部分 – 基本文件管理与权限
1. 文件管理命令
| 类别 | 命令 |
|---|---|
| 导航 | pwd – 打印工作目录 cd – 更改目录 ls -la – 列出所有文件并显示详细信息 |
| 操作 | touch file – 创建空文件 mkdir -p dir/subdir – 创建目录(如有需要创建父目录) cp -r source dest – 递归复制 mv source dest – 移动/重命名 rm -rf path – 强制删除(请谨慎使用) |
| 查看 | cat file less file head file tail file |
2. 文件权限
权限分为 用户 (u)、组 (g) 和 其他 (o)。
| 符号 | 值 |
|---|---|
| r(读取) | 4 |
| w(写入) | 2 |
| x(执行) | 1 |
常用命令
chmod 755 file # u=rwx (7), g=rx (5), o=rx (5)
chmod +x script.sh # add execute bit
chown user:group file # change owner & group
chgrp group file # change group only
第 3 部分 – 用户、组和软件包管理
1. 用户和组管理
| 文件 | 用途 |
|---|---|
/etc/passwd | 用户账户信息 |
/etc/shadow | 安全的密码哈希 |
/etc/group | 组定义 |
常用命令
# Create a user with a home directory and Bash shell
useradd -m -s /bin/bash username
# Add user to the sudo group
usermod -aG sudo username
# Set (or change) password
passwd username
# Show UID/GID information
id username
2. 软件包管理
| 发行版系列 | 包管理器 | 安装命令 | 更新 / 升级命令 |
|---|---|---|---|
| Debian / Ubuntu | apt | apt install <package> | apt update && apt upgrade |
| RHEL / CentOS | yum / dnf | yum install <package> | yum update |
| Alpine | apk | apk add <package> | apk update |
第 4 部分 – 网络与防火墙
1. 网络配置与故障排除
| 命令 | 描述 |
|---|---|
ip addr show | 显示 IP 地址(ifconfig 的现代替代) |
ip route | 显示路由表 |
ping <host> | 测试连通性 |
curl -I <url> | 显示 HTTP 头信息 |
wget <url> | 下载文件 |
nslookup <domain> / dig <domain> | DNS 查询 |
netstat -tulpn 或 ss -tulpn | 列出监听端口(调试服务时很有用) |
2. Linux 防火墙
| 防火墙 | 典型用法 |
|---|---|
| UFW (Ubuntu) | iptables 的简易包装器 |
| firewalld (CentOS/RHEL) | 动态防火墙管理器 |
| iptables | 传统的低层数据包过滤器 |
示例
# UFW
ufw allow 22/tcp # 允许 SSH
ufw enable # 启用防火墙
# firewalld
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
# iptables (basic example)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
第5部分 – 进程、Systemd 与启动
1. 启动过程
- BIOS/UEFI – 硬件自检(POST),加载引导加载程序。
- Bootloader (GRUB) – 加载内核映像。
- Kernel – 挂载根文件系统,启动
init。 - init (systemd) – PID 1,启动用户空间服务。
2. Systemd(服务管理)
systemctl start nginx # start now
systemctl enable nginx # start on boot
systemctl status nginx # view status
journalctl -u nginx # view service‑specific logs
3. 监控与故障排除
| 工具 | 用途 |
|---|---|
top / htop | 实时 CPU 与内存 |
df -h | 磁盘空间使用情况 |
du -sh /path | 特定目录的大小 |
free -m | 内存概览 |
ps aux | grep <process> | 查找进程 |
kill -9 <pid> | 强制终止进程 |
第6部分 – Shell 脚本(Bash)
自动化是 DevOps 的核心。
#!/bin/bash
# -------------------------------------------------
# Example Bash script
# -------------------------------------------------
# Variables
NAME="DevOps Engineer"
DIR="/var/www/html"
# Conditionals
if [ -d "$DIR" ]; then
echo "Directory exists."
else
mkdir -p "$DIR"
echo "Directory created."
fi
# Loops
for i in {1..5}; do
echo "Iteration $i"
done
关键概念
| 概念 | 说明 |
|---|---|
| 退出状态 | $? – 0 = 成功,非零 = 错误 |
| 参数 | $1, $2, … – 位置参数 |
| 重定向 | > 覆盖,>> 追加,` |
第7部分 – SSH 配置与安全
| 文件 | 用途 |
|---|---|
/etc/ssh/sshd_config | SSH 守护进程配置 |
安全最佳实践(加固)
- 禁用 root 登录 – 将
PermitRootLogin no设置为no。 - 禁用密码认证 – 将
PasswordAuthentication no设置为no。 - 更改默认端口(可选) – 例如,使用
Port 2022。
基于密钥的认证
# On the client
ssh-keygen -t rsa -b 4096 # generate key pair
ssh-copy-id user@remote-server # copy public key to server
# On the server (verify permissions)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Source: …
第 8 部分 – 高级概念与云
1. Web 服务器管理
| 服务器 | 典型用途 | 主要配置位置 |
|---|---|---|
| Nginx | 反向代理 / 负载均衡 | /etc/nginx/sites-available/(通过 sites-enabled 链接) |
| Apache | 传统 Web 服务器 | /etc/httpd/conf/httpd.conf(RHEL)或 /etc/apache2/apache2.conf(Debian) |
日志 通常位于 /var/log/nginx/ 或 /var/log/httpd/。
2. 读取访问日志
- 文件:
/var/log/nginx/access.log - 用途: 调试 404/500 错误。
3. 云中的 Linux(AWS / Azure / GCP)
- Cloud‑Init – 实例启动时运行一次,用于安装软件包和写入文件。
- 临时存储 – 某些云磁盘在实例终止后会消失;需注意其瞬时特性。
- 元数据服务 – 在虚拟机内部访问
http://169.254.169.254可获取实例信息(IP、地区等)。
4. 文本处理(“瑞士军刀”)
DevOps 工程师经常使用以下工具解析日志:
-
grep– 搜索文本grep "error" file.log -
awk– 打印特定列awk '{print $1}' file.txt -
sed– 查找并替换文本sed 's/old/new/g' file.txt
查看此资源以进行 DevOps Linux 的高级学习。