Friday 27 January 2017

Creating and applying patch files in CentOS 7

Patch files are often are often included when compiling applications from source and often provide fixes for security and stability in cases where maintained versions in the repositories are not kept up to date / patched quickly.

For this tutorial we will start of by creating a simple patch file that will make the relevant changes for version of a simple python program:

vi /tmp/


for i in list:
    print('The number is: ' + str(i))

vi /tmp/


for i in list:
    print('The word is: ' + i)

Now create the diff file:

diff -u > main.patch

and inspect the contents with:

cat test.patch

--- 2017-01-27 14:34:49.077788860 +0000
+++ 2017-01-27 14:34:36.044762764 +0000
@@ -1,4 +1,5 @@
+list=['this', 'is', 'a', 'test']
 for i in list:
-    print('The number is: ' + str(i))
+    print('The word is: ' + i)

Now we can patch the original file with the 'patch' command:

patch < main.patch

Although in the real world you are more likely to encounter much larger patches than single files - in some cases the patch might span several files over an entire source tree.

To demonstrate we will download two different versions of freeradius:

cd /usr/source


tar xvf freeradius-server-3.0.12.tar.bz2 && tar xvf freeradius-server-3.0.8.tar.bz2

Create the patch file:

diff -Naur /tmp/freeradius-server-3.0.8 /tmp/freeradius-server-3.0.12 > freerad.patch

and then apply the patch with:

patch -p3 freerad.patch


Post a Comment