I want to apply a patch file to my current Ubuntu install (patch file example). What is the process to properly apply a patch file to my Linux kernel?

Do I need to go through the whole process of applying the patch to a disk image file, flashing that to a USB and installing a new copy of Linux? Can I just patch the current install? Or can I patch a disk image and then use that to upgrade my current install?

What I've tried:

  1. Figure out which kernel version I'm running:

    $ uname -srm
    Linux 5.11.0-25-generic x86_64
  2. Go to my kernel directory (I think):

    cd /lib/modules/5.11.0-25-generic/kernel
  3. Apply the patch file

    patch -p1 < ~/my-patch-file.patch

But no patch is applied. The output is:

can't find file to patch at input line 23
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
File to patch:

What is the right way to do this?

Note: this question is different than how to patch any random file. I know how to do that. The question is specifically about how to patch the currently installed kernel.

1 Answer 1


Patches get applied to source files, not binary modules.

You'll have to install the appropriate kernel source, apply the patch, then rebuild. If it works, you can copy rebuild module to wherever under /lib/modules and restart.

The kernel source is usually stored under /usr/src.

  • Thank you for your answer. I'm not an experienced Linux user, so I'm not sure what you mean by "rebuild" or "copy rebuild module to wherever". If you update your answer to explain this process or link to documentation/instructions for it, I will mark this as the accepted answer.
    – Jo Sprague
    Jul 25, 2021 at 14:33
  • After you install the source, inside its directory, do: 'make oldconfig; make modules', then apply the patch. Then another "make modules" and you will see which modules have been rebuild. A search under /lib/modules will show you where the previous module was install, and copy over then reboot. This is of course, if the patch gets applied to a modules source code rather that the kernel core image. I guarantee you that you will get it wrong initially, but you will eventually get it working.
    – Bib
    Jul 25, 2021 at 14:36
  • 1
    @JoSprague There must be better guides out there, but here are Gentoo wikis that may help you get the feel of it: Configuring the Linux kernel and Kernel rebuild (there's some overlapping).
    – Quasímodo
    Jul 25, 2021 at 15:25
  • I think the op will have to look at the distros docs. Trying to configure the latest kernel from scratch is going to take you many hours, and you are going to get it wrong initially. Oh for the days when the source came on a single floppy!
    – Bib
    Jul 25, 2021 at 15:29
  • @Bib It really won’t take many hours if you’re using a sane distro, because all the sane ones include some way to get the config they used to build (be it stored in a file in /boot, found in /proc/confi.gz if a specific module is loaded, or just bundled in their sources for the kernel). If someone has no experience, those are what they should be using to build their kernels, as they will work (provided the user does not change anything). Jul 26, 2021 at 1:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .