Linux系统差异对比工具:如何快速找出Ubuntu自定义安装与默认配置的差异


阅读 2 次

当两台Ubuntu表现不一致时

接手他人配置的Linux服务器时,最头疼的就是发现同样的命令在不同机器上产生不同结果。上周我在adduser命令上就栽了跟头——在A服务器创建用户自动分配home目录,在B服务器却报错。

核心工具链推荐

这里分享几个我常用的配置对比方案:


# 1. 包管理差异检测
diff <(apt-mark showmanual | sort) <(ssh user@host "apt-mark showmanual | sort")

# 2. 关键配置文件对比
rsync -n -avrc --delete /etc/ root@remote:/etc/ | grep -v "building file list"

深度配置审计方案

对于生产环境,我通常会建立完整的审计脚本:


#!/bin/bash
# 系统基础信息快照
{
  uname -a
  lsb_release -a
  dpkg -l
  crontab -l
  systemctl list-units --type=service
} > system_snapshot_$(date +%Y%m%d).log

实战案例:Apache配置差异

最近排查的典型问题:


# 对比加载的模块
diff <(apache2ctl -M | sort) <(ssh user@host "apache2ctl -M | sort")

# 对比虚拟主机配置
diff -qr /etc/apache2/sites-enabled/ user@host:/etc/apache2/sites-enabled/

进阶方案:自动化差异报告

使用Python实现自动化对比:


import difflib
from pathlib import Path

def compare_files(local, remote):
    local_lines = Path(local).read_text().splitlines()
    remote_lines = Path(remote).read_text().splitlines()
    return difflib.unified_diff(local_lines, remote_lines)

# 示例:对比SSH配置
for line in compare_files('/etc/ssh/sshd_config', '/mnt/backup/sshd_config'):
    print(line)

避坑指南

  • 注意/etc/alternatives/下的软链接差异
  • 检查update-alternatives --config的输出
  • 特别关注/etc/profile.d/下的自定义脚本