Being Proactive, Part 2 Labs
1. Managing Configuration Files with diff and patch
Use
diffandpatchto identify and replicate changes to configuration filesApply changes from one configuration file to a second one
Issue the following command as
studentonserver1:[student@server1 ~]$ for i in 1 2; do echo "ServerAdmin root@host$i.example.com" > ~/host$i.cfg; done
Log in as
studentwith a password ofstudentonserver1.The set-up script copied two files in student’s home directory:
host1.cfgandhost2.cfg.[student@server1 ~]$ ls host* host1.cfg host2.cfg
Use
diffto 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
Back up
host1.cfgusing 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
Make a few changes to
host1.cfg. Do not modify the line that contains a reference to its hostname.[student@server1 ~]$ vim host1.cfg
Use
diffand note the changes between the original and new versions ofhost1.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
lscommand output).[student@server1 ~]$ diff host1.cfg-20110830-1050 host1.cfg ... output omitted ...
Use
diff -uto create a patch file with the differences between the original version ofhost1.cfgand the newer version with your changes.[student@server1 ~]$ diff -u host1.cfg-20110830-1050 host1.cfg > changes.patch
Use
patchto updatehost2.cfg(making a backup as the patch is applied).[student@server1 ~]$ patch -b host2.cfg changes.patch patching file host2.cfg
Use
diffto note the changes betweenhost2.cfgand its backup.[student@server1 ~]$ diff host2.cfg.orig host2.cfg ... output omitted ...
Use
diffto note any changes between the newest versions ofhost1.cfgandhost2.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