From a1aacd06b72716df3ed0318bc64a81186230477c Mon Sep 17 00:00:00 2001 From: FNS Service Date: Tue, 21 Apr 2026 11:27:00 +0800 Subject: [PATCH] Update from Sync Service --- Lunix/Linux系统管理指南.md | 298 +++++++++++++++++++++++++++++++++++++ 1 file changed, 298 insertions(+) create mode 100755 Lunix/Linux系统管理指南.md diff --git a/Lunix/Linux系统管理指南.md b/Lunix/Linux系统管理指南.md new file mode 100755 index 0000000..7443c98 --- /dev/null +++ b/Lunix/Linux系统管理指南.md @@ -0,0 +1,298 @@ +--- +title: Linux 系统管理指南 +tags: + - Linux + - Shell + - Bash + - 系统管理 + - Ubuntu +created: 2026-04-21 +--- + +# Linux 系统管理指南 + +## 基础命令 + +### 文件与目录 + +```bash +# 目录操作 +ls -la # 列出所有文件(含隐藏) +cd /path # 切换目录 +pwd # 显示当前目录 +mkdir -p a/b/c # 递归创建目录 +rm -rf directory # 强制删除目录 + +# 文件操作 +cp file1 file2 # 复制 +mv file1 file2 # 移动/重命名 +rm file # 删除 +touch file # 创建空文件 + +# 查看文件 +cat file # 全文显示 +head -n 20 file # 前20行 +tail -n 50 file # 后50行 +tail -f log # 实时跟踪日志 +less file # 分页查看 + +# 搜索 +grep "pattern" file +find / -name "*.log" +locate filename +``` + +### 用户与权限 + +```bash +# 用户管理 +sudo useradd -m -s /bin/bash username +sudo passwd username +sudo usermod -aG sudo username +sudo deluser username + +# 权限 +chmod 755 file # 数字形式 +chmod +x script.sh # 添加执行权限 +chown user:group file # 修改所有者 + +# 权限数字 +# 4 = r, 2 = w, 1 = x +# 755 = rwxr-xr-x (所有者全部, 其他读+执行) +# 644 = rw-r--r-- +# 600 = rw------- +``` + +### 进程管理 + +```bash +# 查看进程 +ps aux | grep nginx +top # 实时监控(退出按 q) +htop # 更友好的 top(需安装) + +# 管理进程 +kill PID # 温柔终止 +kill -9 PID # 强制终止 +killall nginx # 按名字终止 + +# 后台运行 +nohup command & # 忽略挂断信号后台运行 +command & # 后台运行 +jobs # 查看后台任务 +fg %1 # 调到前台 +``` + +## 软件安装 + +### APT(Debian/Ubuntu) + +```bash +# 更新源 +sudo apt update +sudo apt upgrade + +# 安装软件 +sudo apt install nginx +sudo apt install docker.io + +# 搜索 +apt search nginx + +# 卸载 +sudo apt remove nginx +sudo apt autoremove +``` + +### YUM/DNF(CentOS/RHEL) + +```bash +sudo dnf install nginx +sudo dnf update +sudo dnf remove nginx +``` + +### 系统服务(systemd) + +```bash +# 管理服务 +sudo systemctl start nginx +sudo systemctl stop nginx +sudo systemctl restart nginx +sudo systemctl status nginx +sudo systemctl enable nginx # 开机自启 +sudo systemctl disable nginx # 禁用开机自启 + +# 查看服务 +systemctl list-units --type=service --state=running +``` + +## 网络命令 + +```bash +# 查看网络 +ip addr show +ip link set eth0 up/down +ss -tuln # 查看监听端口 +netstat -tuln + +# 网络测试 +ping -c 4 google.com +curl -I https://example.com +wget https://example.com/file + +# 端口转发 +sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000 + +# 防火墙 +sudo ufw allow 22 # 开放SSH +sudo ufw allow 80/tcp # 开放HTTP +sudo ufw allow 443/tcp # 开放HTTPS +sudo ufw enable +sudo ufw status +``` + +## 磁盘管理 + +```bash +# 查看磁盘使用 +df -h +du -sh /var/log/* + +# 挂载 +sudo mount /dev/sdb1 /mnt/usb +sudo umount /mnt/usb + +# 分区 +sudo fdisk /dev/sdb +# 或使用 parted + +# 创建文件系统 +sudo mkfs.ext4 /dev/sdb1 +sudo mkfs.xfs /dev/sdb1 +``` + +## Shell 脚本 + +```bash +#!/bin/bash + +# 变量 +NAME="World" +echo "Hello, $NAME!" +echo "当前目录: $(pwd)" + +# 条件 +if [ -f "file.txt" ]; then + echo "文件存在" +elif [ $age -ge 18 ]; then + echo "成年" +else + echo "未成年" +fi + +# 循环 +for file in *.txt; do + echo "处理: $file" +done + +# 函数 +function greet() { + echo "Hello, $1!" +} +greet "张三" + +# 常用判断 +# -f file 文件存在 +# -d dir 目录存在 +# -z string 字符串为空 +# -n string 字符串非空 +# -eq, -ne 数字相等/不等 +# -lt, -gt 数字小于/大于 + +# 读取输入 +read -p "请输入名称: " name +echo "你输入了: $name" + +# 函数返回状态 +function check() { + if [ $1 -gt 10 ]; then + return 0 # 成功 + else + return 1 # 失败 + fi +} +``` + +## 日志查看 + +```bash +# 系统日志 +journalctl -u nginx # 查看nginx服务日志 +journalctl -f # 实时跟踪 +journalctl --since "1 hour ago" + +# 应用日志 +tail -f /var/log/nginx/access.log +tail -f /var/log/syslog + +# 历史命令 +history | grep apt +``` + +## 常用配置 + +### SSH + +```bash +# 生成密钥 +ssh-keygen -t ed25519 -C "your_email@example.com" + +# 复制公钥到服务器 +ssh-copy-id user@hostname + +# SSH 配置 (~/.ssh/config) +Host myserver + HostName 192.168.1.100 + User ubuntu + Port 22 + IdentityFile ~/.ssh/id_ed25519 +``` + +### Crontab 定时任务 + +```bash +# 编辑 crontab +crontab -e + +# 格式: 分 时 日 月 周 命令 +# 示例: +# 0 9 * * 1-5 # 每周一到周五9点 +# */15 * * * * # 每15分钟 +# 0 2 * * * # 每天凌晨2点 +# @reboot # 开机运行 +``` + +## Docker(Linux) + +```bash +# 安装 Docker +curl -fsSL https://get.docker.com | sudo sh +sudo usermod -aG docker $USER + +# Docker Compose +sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" \ + -o /usr/local/bin/docker-compose +sudo chmod +x /usr/local/bin/docker-compose +``` + +## 故障排查 + +| 问题 | 命令 | +|------|------| +| 端口占用 | `lsof -i :8080` 或 `ss -tulpn \| grep 8080` | +| 磁盘满 | `df -h` + `du -sh /*` | +| 内存满 | `free -h` + `top` | +| CPU 高 | `top` 或 `htop` | +| 服务起不来 | `systemctl status xxx` + `journalctl -u xxx` | +| 网络不通 | `ping 8.8.8.8` + `curl -I url` |