以 Windows 11 24H2 与 Ubuntu 24.04 LTS (Noble) / RHEL 9 为参考基准。
| 官方工具 | ||
| U 盘写入 | sudo dd if=ubuntu-24.04.iso of=/dev/sdX bs=4M status=progress conv=fsync | |
| 校验完整性 | certutil -hashfile ISO SHA256 | sha256sum ubuntu-24.04.iso |
| 多合一引导 |
⚠️
dd写入目标是整盘/dev/sdX而非分区/dev/sdX1,写错盘会丢数据。先lsblk确认盘符。
/boot/efi | |||
/ | |||
pagefile.sys | swap | ||
/home/var 独立分区 |
lvextend -l +100%FREE /dev/vg0/root && resize2fs /dev/vg0/root先装 Windows → 再装 Linux → GRUB 自动检测 Windows Boot Manager
若 Windows 大版本更新后覆盖了 GRUB:
# Live USB 启动后修复
sudo mount /dev/nvme0n1p3 /mnt # 根分区
sudo mount /dev/nvme0n1p1 /mnt/boot/efi # EFI 分区
for d in dev proc sys run; do
sudo mount --bind /$d /mnt/$d
done
sudo chroot /mnt
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ubuntu
update-grub
Windows(PowerShell 管理员):
# 安装 PSWindowsUpdate 模块
Install-Module PSWindowsUpdate -Force -AllowClobber
Get-WindowsUpdate -Install -AcceptAll -AutoReboot
# 包管理器 winget(Win11 24H2 内置)
winget install --id Git.Git Microsoft.VisualStudioCode Microsoft.PowerShell 7zip.7zip VideoLAN.VLC
winget upgrade --all # 一键升级所有已装软件
Linux(Ubuntu/Debian):
# 换国内镜像源(Ubuntu 24.04 新格式 deb822)
sudo sed -i 's|http://archive.ubuntu.com|https://mirrors.tuna.tsinghua.edu.cn|g' \
/etc/apt/sources.list.d/ubuntu.sources
sudo sed -i 's|http://security.ubuntu.com|https://mirrors.tuna.tsinghua.edu.cn|g' \
/etc/apt/sources.list.d/ubuntu.sources
sudo apt update && sudo apt full-upgrade -y && sudo apt autoremove --purge -y
# 基础开发工具
sudo apt install -y build-essential git vim curl wget htop \
ca-certificates gnupg lsb-release net-tools dnsutils unzip
RHEL 9 / Rocky 9:
# 换阿里云镜像
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^# baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i /etc/yum.repos.d/rocky*.repo
sudo dnf upgrade -y && sudo dnf autoremove -y
sudo tee /etc/sysctl.d/99-tuning.conf <<'EOF'
# 内存 — 减少 swap 倾向
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
# 网络 — 提高连接队列与吞吐
net.core.somaxconn = 65535
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.core.netdev_max_backlog = 16384
# 文件描述符
fs.inotify.max_user_watches = 524288
fs.file-max = 2097152
EOF
sudo sysctl --system # 立即加载并验证
sudo tee /etc/ssh/sshd_config.d/99-hardening.conf <<'EOF'
PermitRootLogin prohibit-password
PasswordAuthentication no
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 0
AllowUsers myuser
EOF
sudo sshd -t # 测试配置语法
sudo systemctl reload sshd # 热重载(不断开现有连接)
Set-NetFirewallProfile -Enabled True | |||
ufw allow 22/tcp && ufw enable | |||
firewall-cmd --permanent --add-service=ssh && firewall-cmd --reload |
# Ubuntu UFW 示例
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp comment 'SSH'
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'
sudo ufw enable
sudo ufw status verbose
Windows:
# 自动更新组策略
Set-ItemProperty "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" `
-Name AUOptions -Value 4 # 4 = 自动下载并安装
# 查看已安装补丁
wmic qfe list brief /format:table
# 卸载特定补丁
wusa /uninstall /kb:5031356 /quiet /norestart
Linux(自动安全更新):
# Ubuntu — unattended-upgrades
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
# 配置文件 /etc/apt/apt.conf.d/50unattended-upgrades
# 默认只装 security 源更新,生产环境建议保持默认
# 确认定时器已激活
systemctl status apt-daily.timer apt-daily-upgrade.timer
# 手动检查可升级包
apt list --upgradable
| 就地升级 | setup.exe → 保留文件和程序 | sudo do-release-upgrade |
| 跳版本 | ||
| 回滚 |
# Ubuntu LTS 版本升级(仅限 LTS→LTS)
sudo apt install -y update-manager-core
sudo do-release-upgrade # 交互式向导
# 无人值守(不推荐,需充分测试)
sudo do-release-upgrade -f DistUpgradeViewNonInteractive
# 查看当前内核
uname -r # 当前运行内核
dpkg --list | grep linux-image # 已安装内核
# 安装 HWE 内核(新版硬件支持)
sudo apt install linux-generic-hwe-24.04
# 锁定内核版本(防止自动升级)
sudo apt-mark hold linux-image-6.8.0-31-generic
sudo apt-mark unhold linux-image-6.8.0-31-generic
# 清理旧内核(仅保留当前+1)
sudo apt autoremove --purge
# RHEL 9 列出可用内核
dnf list --available kernel
Windows:
# 系统文件完整性校验
sfc /scannow
DISM /Online /Cleanup-Image /ScanHealth
DISM /Online /Cleanup-Image /RestoreHealth
# 磁盘健康
Get-PhysicalDisk | Select FriendlyName, MediaType, HealthStatus, OperationalStatus
chkdsk C: /scan # 只读扫描,不锁定卷
# 启动耗时分析
Get-CimInstance Win32_OperatingSystem | Select LastBootUpTime
Linux:
# 系统文件完整性(dpkg 包装数据库)
sudo dpkg --audit # 检查损坏/未配置的包
sudo apt install -f # 修复依赖断裂
# 启动分析
systemd-analyze blame # 各服务启动耗时
systemd-analyze critical-chain # 关键路径
# 本次启动的严重日志
journalctl -p err -xb # err 及以上,本次启动
journalctl -p 3 --since today # 今日错误日志
# 硬件健康
sudo smartctl -a /dev/nvme0n1 # SSD SMART(需 smartmontools)
sudo dmesg -T | grep -iE 'error|fail|warn'
| 存储 | .evtxC:\Windows\System32\winevt\Logs\) | journal/var/log/ 文本 |
| 查看工具 | Get-WinEvent | journalctltail -f /var/log/syslog |
| 按级别过滤 | -Level 1,2,3 | -p err-p warning |
| 按时间 | -MaxEvents 50 | --since "1 hour ago"--since today |
| 日志轮转 | logrotate/etc/logrotate.conf) |
# Windows:查看最近 50 条 System 错误
Get-WinEvent -FilterHashtable @{LogName='System';Level=1,2} -MaxEvents 50 |
Select TimeCreated, Id, ProviderName, Message | Format-Table -Wrap
# Linux:查看最近 1 小时内所有 error
journalctl -p err --since "1 hour ago" --no-pager
# 持续监控
journalctl -f -p warning
# Linux 磁盘用量分析
df -hT # 各挂载点文件系统+用量
du -sh /* 2>/dev/null | sort -rh | head -10 # 根目录最大占用
ncdu / # 交互式分析
# Btrfs 文件系统(若使用)
sudo btrfs filesystem usage / # 用量统计
sudo btrfs scrub start / # 数据校验(定期执行)
# SSD TRIM
sudo fstrim -av # 手动 TRIM
systemctl status fstrim.timer # 定时 TRIM(每周自动)
# LVM 在线扩容
sudo lvextend -l +100%FREE /dev/vg0/root
sudo resize2fs /dev/vg0/root # ext4
sudo xfs_growfs / # XFS
# Windows 磁盘
Optimize-Volume -DriveLetter C -Trim # SSD TRIM
Optimize-Volume -DriveLetter D -Defrag # HDD 碎片整理
Get-Volume | Format-Table DriveLetter,FileSystemLabel,SizeRemaining,HealthStatus
Get-Service Name | systemctl status name | |
Start-Service Name | systemctl start name | |
Stop-Service Name | systemctl stop name | |
Set-Service Name -StartupType Automatic | systemctl enable name | |
Set-Service Name -StartupType Disabled | systemctl disable name | |
systemctl mask name | ||
Get-Service | Where Status -eq Stopped | systemctl list-units --failed | |
systemctl list-dependencies name |
# win_baseline.yml
-name:Windows基线配置
hosts:windows
gather_facts:yes
tasks:
-name:安装基础软件
win_winget:
name:"{{ item }}"
state:present
loop:
-Git.Git
-7zip.7zip
-Microsoft.VisualStudioCode
-name:关闭遥测
win_regedit:
path:HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection
name:AllowTelemetry
data:0
type:dword
-name:启用防火墙所有配置文件
ansible.windows.win_firewall:
profiles:[Domain,Private,Public]
state:enabled
-name:安装所有可用更新
ansible.windows.win_updates:
category_names:[SecurityUpdates,CriticalUpdates]
state:installed
reboot:yes
# linux_baseline.yml
-name:Linux基线配置
hosts:all
become:true
vars:
ssh_port:22
allowed_ports:[22,80,443]
tasks:
-name:全量升级
apt:
update_cache:yes
upgrade:full
autoremove:yes
autoclean:yes
-name:安装基础工具
apt:
name:[vim,git,curl,htop,ufw,unattended-upgrades,fail2ban]
state:present
-name:内核参数优化
sysctl:
name:"{{ item.key }}"
value:"{{ item.value }}"
sysctl_file:/etc/sysctl.d/99-tuning.conf
reload:yes
loop:
-{key:'vm.swappiness',value:'10'}
-{key:'net.core.somaxconn',value:'65535'}
-name:启用自动安全更新
copy:
dest:/etc/apt/apt.conf.d/20auto-upgrades
content:|
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
-name:配置UFW防火墙
community.general.ufw:
rule:allow
port:"{{ item }}"
proto:tcp
loop:"{{ allowed_ports }}"
-name:启用UFW
community.general.ufw:
state:enabled
policy:deny
direction:incoming
-name:SSH加固
lineinfile:
path:/etc/ssh/sshd_config.d/99-hardening.conf
line:"{{ item }}"
create:yes
loop:
-'PermitRootLogin prohibit-password'
-'PasswordAuthentication no'
-'MaxAuthTries 3'
notify:reloadsshd
handlers:
-name:reloadsshd
systemd:
name:ssh
state:reloaded
# 执行(需先配置 inventory)
ansible-playbook -i hosts.ini linux_baseline.yml --check # dry-run
ansible-playbook -i hosts.ini linux_baseline.yml # 正式执行
| 系统还原点 | ||
| 补丁回滚 | wusa /uninstall /kb:xxxx | apt downgradednf downgrade |
| 内核回滚 | apt purge 新内核 | |
| 文件级备份 | rsyncborg / restic | |
| 系统镜像 | /Capture-Image / Macrium | ddborgbackup |
# Btrfs 快照回滚(推荐)
sudo snapper create -c timeline # 拍快照
sudo snapper rollback 42 # 回滚到快照 #42
# 重启即生效,无需重装系统
# Timeshift(用户友好)
sudo timeshift --create --comment "更新前备份"
sudo timeshift --list
sudo timeshift --restore --snapshot '2026-07-29_12-00-00'
| 包管理 | ||
| 服务管理 | services.mscsc.exe | systemctl |
| 计划任务 | schtasks) | systemd timercron |
| 权限模型 | ||
| 网络配置 | netsh | netplannmcli / ip / systemd-networkd |
| 事件日志 | journalctl/var/log/ | |
| 文件系统 | ||
| 远程管理 |