Tracking your /etc with git version control software – and make apt-get do it automagically

Linux computers (mostly) use /etc to store config files. From server software like Apache, PHP, ProFTPd and OpenVPN through to X11 and Mutt, they all keep their config files in /etc. User config files are mostly kept in /home/$username/ so this technique will not track these, but this will help you keep on top of changes by apt. You should still always make your own commits when you change something, but at least it won’t get lost in the chaos.

Why this isn’t tracked with version control by default is beyond the scope of this post, but the fact remains that it’s not. So lets change that. Most commands will need to be run as root with sudo when working in /etc

Install git first and change working directory to /etc with

apt-get install git
cd /etc

Initialise /etc as a git folder to track changes

git init
chmod og-rwx .git

Create a snapshot of the /etc files as they are right now

git add .

This adds the files which have been changed since last commit to the queue. As this is the first time, that will be all of them.

git commit -m "Initial commit"

“git commit” commits the files that were added to the queue, with the -m message. Being verbose with your commit messages is a good idea – It will make your life a lot easier if you have to roll back several steps.

And that’s all there is to it. Every time you make a change to a config file in /etc, just do the last two commands (git add, git commit) and you can revert your changes, inspect them and more.

To supercharge my version controlling of /etc, I’ve added a small script that runs every time apt-get does to automatically make a commit. Into the new file  /etc/apt/git-snapshot-script add

set -e
reason=$(ps axww | grep "^ *$$" -B3 | grep " apt-get " | head -n1 | sed 's/^.*\(apt-get .*\)/\1/' )
git add .
git commit -a --allow-empty -m "Snapshot because of: $reason"

and make it exectuable

chmod +x /etc/apt/git-snapshot-script

and configure apt to call it every time by adding to the (probably new) file /etc/apt/apt.conf

DPkg {Pre-Invoke {"cd /etc ; ./apt/git-snapshot-script";};
Post-Invoke {"cd /etc ; ./apt/git-snapshot-script";};}

Don’t forget to add the new changes you just made to /etc (adding/modifying files) by running “git add .” and “git commit” again.

git add .
git commit -m "Automatically trigger git add and git commit on /etc directory every time apt runs"

And that’s all there is to it.

Credit to Bart Trojanowski for a cleaner apt triggered commit message.

Codemonkey of Node.js, Django, PHP and all things HTML5 Solving tech challenges, one bit at a time.

Posted in bash, linux Tagged with: , , , , , ,
2 comments on “Tracking your /etc with git version control software – and make apt-get do it automagically
  1. Paul Tötterman says:

    Check out etckeeper

    • admin says:

      Cheers, etckeeper was the inspiration actually. In the end but I found this had lower administrant overhead. On a new server I’ll git clone my ~/code_and_dotfiles which I then symlink .bashrc/.vimrc and such to anyway. This way I could then track /etc by just symlinking two files and my aliased “git init && git add . && git commit -m ‘initial commit’”

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>