Being Proactive, Part 2 Labs

1. Managing Configuration Files with diff and patch

Goals
  • Use diff and patch to identify and replicate changes to configuration files

  • Apply changes from one configuration file to a second one

    1. Issue the following command as student on server1:

      [student@server1 ~]$ for i in 1 2; do echo "ServerAdmin root@host$i.example.com" > ~/host$i.cfg; done
    2. Log in as student with a password of student on server1.

      The set-up script copied two files in student’s home directory: host1.cfg and host2.cfg.

      [student@server1 ~]$ ls host*
      host1.cfg  host2.cfg
    3. Use diff to note there is only a single line that differs between them.

      That line contains an email address that refers to each host’s hostname.

      [student@server1 ~]$ diff host1.cfg host2.cfg
      262c262
      < ServerAdmin root@host1.example.com
      ---
      > ServerAdmin root@host2.example.com
    4. Back up host1.cfg using a file name with $(date +%Y%m%d-%H%M) appended.

      [student@server1 ~]$ cp host1.cfg host1.cfg-$(date +%Y%m%d-%H%M)
      [student@server1 ~]$ ls host*
      host1.cfg-20110830-1050  host1.cfg  host2.cfg
    5. Make a few changes to host1.cfg. Do not modify the line that contains a reference to its hostname.

      [student@server1 ~]$ vim host1.cfg
    6. Use diff and note the changes between the original and new versions of host1.cfg.

      This displays a condensed view of what has changed and excludes identical content that doesn’t interest us. Make sure you use the correct file name going forward (from the previous ls command output).

      [student@server1 ~]$ diff host1.cfg-20110830-1050 host1.cfg
      ... output omitted ...
    7. Use diff -u to create a patch file with the differences between the original version of host1.cfg and the newer version with your changes.

      [student@server1 ~]$ diff -u host1.cfg-20110830-1050 host1.cfg > changes.patch
    8. Use patch to update host2.cfg (making a backup as the patch is applied).

      [student@server1 ~]$ patch -b host2.cfg changes.patch
      patching file host2.cfg
    9. Use diff to note the changes between host2.cfg and its backup.

      [student@server1 ~]$ diff host2.cfg.orig host2.cfg
      ... output omitted ...
    10. Use diff to note any changes between the newest versions of host1.cfg and host2.cfg.

      The only difference between them should be the lines that reference their hostname.

      [student@server1 ~]$ diff host1.cfg host2.cfg
      262c262
      < ServerAdmin root@host1.example.com
      ---
      > ServerAdmin root@host2.example.com