First, visit http://www.kernel.org/ to pick the kernel you need.

The current latest mainline is at: http://www.kernel.org/pub/linux/kernel/v3.0/testing/linux-3.6-rc7.tar.bz2

The latest stable is at: http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.5.4.tar.bz2

The official site offers both bz2 and xz packages for download.

You can download it with any download tool — for example, I use wget. Extract with tar -xjvf linux* (use -xJvf for the xz package).

It's customary to move the source code into the /usr/src/kernels/ directory.

Then enter the source tree.

If this isn't a fresh install (for instance, the tree was copied from somewhere else, or a previous install attempt failed), be sure to clean up any traces of the earlier build first.

Bash
make mrproper #清除环境变量,即清除配置文件
make clean #清除以前编译出的文件
make dep #清除依赖

To make kernel configuration easier, copy the config file from the boot directory into your own directory as the .config file.

Bash
#cp /boot/config-* /usr/src/kernels/linux-*/.config
Fill in the above according to your own setup.

Ncurses is a handy kernel configuration tool that can be installed directly via yum.

Bash
yum install ncurses-devel
It's essentially running a graphical interface on the console.

Then configure:

make menuconfig

A text-based GUI pops up — scroll all the way to the bottom and check "Load an Alternate Configuration". In the dialog that appears, the default .config file is naturally already selected. After confirming, the previous configuration is loaded into Ncurses.

You can then look through it to see if there's anything you want to tweak, and then save.

Now let's actually start compiling:

Bash
make bzImage #生成内核文件
make modules #编译模块
make modules_install #安装模块
make install #安装

If the compilation completes without errors, you'll see a new set of kernel files added under the /boot directory.

In most cases, Linux uses something like grub/grub2 to manage boot entries, so you can use grub directly to check whether things went through. The commands are:

Bash
grub-mkconfg
or
Bash
grub2-mkconfg
Then comes a bunch of output; scroll through it — usually you'll see the various kernel versions listed, along with windows and so on. If there are no errors, you can write the result to the grub configuration file:
Bash
grub-mkconfig -o /boot/grub/grub.conf
or similar commands to output it to the grub config file under boot. Since machines differ, for example, on my fedora it outputs to the /boot/grub2/grub.cfg file, and the tool is even called grub2-mkconfig. So when generating the grub config file, be sure to first check where the original config file is located. After that, you're ready to reboot.

When you reboot, the boot menu should show the new kernel as the first entry.

By the way,

rpm -qa | grep kernel
lets you check your current kernel.

References:

  1. Upgrading the kernel on CentOS: http://liaozy.blog.51cto.com/921527/553921
  2. Steps and methods for upgrading the kernel on CentOS 6.0: http://www.cnblogs.com/zhunian/archive/2012/04/04/2431883.html(This article has been copied all over the internet, so I'm not sure which one is the original; I'm only citing the version I referenced)