etckeeper: Git for /etc, So You Can Always Answer "What Changed?"

Put /etc under version control with etckeeper: automatic commits around package upgrades, saved permissions and owners, and an answer to 'what changed?'

5 min read
AI-assisted

Author's AI disclosure

The author reports using AI to help with this article.

The author confirms reviewing this article.

etckeeper: Git for /etc, So You Can Always Answer "What Changed?"

Something that worked yesterday is broken today. Nobody deployed anything. But last month you "just tweaked" an nginx setting, an upgrade overwrote a config, and somebody (you) edited sshd_config at midnight. Now you're comparing a broken server against your memory.

On Linux, almost all system configuration lives in /etc, which makes it the one directory you most want a history for. etckeeper puts /etc under version control (git by default) and takes care of the parts plain git gets wrong.

flowchart LR
    A["apt install or upgrade"] -->|"pre and post hooks"| C["etckeeper commit"]
    B["Hand edits"] -->|"daily autocommit"| C
    C --> D["/etc/.git, with permissions saved in /etc/.etckeeper"]
    D -->|"optional PUSH_REMOTE"| E["Private bare repo, mode 700"]

What it does

  • Keeps /etc in a git repository at /etc/.git.

  • Hooks into your package manager (apt, and pacman, dnf and others), so config changes made by installing or upgrading packages are committed automatically.

  • Commits hand edits with a daily automatic commit (a cron job or systemd timer, depending on your distro).

  • Stores file permissions and owners, which git doesn't track, in a metadata file, /etc/.etckeeper.

  • Keeps the repository readable by root only.

  • Can push each commit to a remote copy.

Why not just run git init in /etc?

Git tracks file contents and the executable bit. It does not track other permissions, owners or empty directories, and /etc depends on all three. Here's a demo in a throwaway directory:

# shadow is mode 600; nginx.conf is mode 640, group nogroup; empty_dir is empty
git init . && git add -A && git commit -m initial
git clone . ../copy && ls -l ../copy
# shadow            -rw-r--r--   (was -rw-------: now readable by everyone)
# nginx/nginx.conf  -rw-r--r-- root:root   (was -rw-r----- root:nogroup)
# empty_dir         (missing)

That's what I got when I ran it. The first line is the dangerous one: a restored /etc/shadow that anyone can read. etckeeper's pre-commit hook records permissions and owners in /etc/.etckeeper so they can be reapplied later (with etckeeper init), and it sets the repository up so only root can read it.

Install and make the first commit

sudo apt install etckeeper          # Debian and Ubuntu
sudo git -C /etc log --oneline -3   # the package usually initializes and commits for you

If there's no history yet, run sudo etckeeper init and then sudo etckeeper commit "Initial commit".

The main settings live in /etc/etckeeper/etckeeper.conf. These are the ones worth knowing:

VCS="git"
HIGHLEVEL_PACKAGE_MANAGER=apt
LOWLEVEL_PACKAGE_MANAGER=dpkg
#AVOID_DAILY_AUTOCOMMITS=1       # uncomment to turn the daily autocommit off
#AVOID_COMMIT_BEFORE_INSTALL=1   # uncomment to cancel installs if /etc has uncommitted changes
PUSH_REMOTE=""                    # name of a git remote to push every commit to

By default, etckeeper commits any uncommitted changes before a package install, so a package's changes show up as their own commit. With AVOID_COMMIT_BEFORE_INSTALL=1, it cancels the install instead and leaves the commit to you.

Everyday use

Commit before you change something, with a message that says why:

sudo etckeeper commit "before tightening nginx TLS settings"
sudoedit /etc/nginx/nginx.conf
sudo git -C /etc diff                       # review what you changed
sudo etckeeper commit "nginx: TLS 1.2 minimum"

When you run etckeeper commit through sudo, it records your original username in the commit, so on a shared server the history shows who made each change.

Answer "what changed?"

sudo git -C /etc log --oneline --since="3 days ago"
sudo git -C /etc log -p --since="3 days ago" -- nginx/
sudo git -C /etc show --stat HEAD           # which files did the last commit touch?

After an apt upgrade, git show --stat HEAD tells you exactly which config files the packages changed. Changes made by hand show up in the daily autocommit, typically titled "daily autocommit".

Restore a file without breaking its permissions. The obvious command is git checkout, but in my test it replaced a file that was mode 640 and group nogroup with a fresh 644 root:root one. Redirecting git show into the existing file keeps its mode and owner:

sudo sh -c 'git -C /etc show HEAD~1:nginx/nginx.conf > /etc/nginx/nginx.conf'
sudo nginx -t && sudo systemctl reload nginx      # check before you reload

The restore is itself a new uncommitted change, so commit it when you're happy.

If you ever do check out a whole branch or tree in /etc, run sudo etckeeper init afterwards to reapply the saved permissions. Arch's documentation warns that checkout, rebase and merge can break permissions badly enough to lock you out of SSH or sudo. For anything risky, work in a clone of /etc in a directory created with mode 700, not in /etc itself.

Push a copy somewhere else

/etc/.git lives on the same disk as /etc. If the disk dies, so does the history. Push to a second machine:

# On the backup server: a private bare repo, readable only by its owner
ssh backup-server 'mkdir /srv/etc-backup && cd /srv/etc-backup && chmod 700 . && git init --bare'

# On the machine being tracked
sudo git -C /etc remote add backup ssh://backup-server/srv/etc-backup
sudo git -C /etc push backup --all

Then set PUSH_REMOTE="backup" in etckeeper.conf so each commit is pushed automatically. The push runs as root, so root needs an SSH key the backup server accepts.

Secrets: the part to take seriously

/etc holds password hashes, SSH host keys, VPN private keys and database passwords inside application configs. Version control remembers all of it, including secrets you've since rotated.

  • Keep the remote private, with mode 700, reached only over SSH, on a server you trust. The etckeeper README is explicit about this. Never push /etc to a public repository.

  • Check what's tracked: sudo git -C /etc ls-files | grep -iE 'key|secret|shadow|passwd'.

  • To keep a file out of history, add it to /etc/.gitignore outside the block marked as managed by etckeeper, and remove it from the index with sudo git -C /etc rm --cached <file>. The file stays on disk, but old commits still contain it, so rotate anything that already got committed.

Limits

  • It's history, not a backup of the machine. It covers /etc only, not /var/lib, /srv, your app directories or /usr/local/etc.

  • It records changes; it doesn't prevent them. Nothing stops someone from editing a file. You just get to see afterwards.

  • It doesn't capture running state, such as firewall rules loaded in memory but not saved to a file.

  • Daily commits are a snapshot, not an audit trail. If a file changes three times in one day, you'll see one commit with the net result.

  • Check how your distro schedules the daily commit (cron or a systemd timer): systemctl list-timers | grep etckeeper.

Wrap-up

Installing etckeeper is one command, and the history starts building immediately. You'll rarely think about it until the day something breaks, and then git log -p over /etc will answer in seconds what would otherwise take an afternoon of guessing. Set up the private remote on day one, and check what's tracked before you push anything.

Comments0

Log in to join the discussion.

No comments yet.

Anshu Pathak

Read articles by this author.

Subscribe to Anshu Pathak's newsletter

Get the articles this author chooses to send by email.