一、环境
linux:                       CentOS 5.8
内核版本                   2.6.18                         (用命令uname --a 查看)
计划新内核版本        inux-3.2.35.tar.gz             (文件保存在 /root中)
操作用户:               root

二、内核裁剪计划:
拷贝/boot/config-2.6.18-308.el5 到源代码目录并改名为.config,由于内核选项多达上千条,  所以用当前内核版本配置文件为蓝本,在它的基础上配置。内核的具体裁减可参考网络上的其他文章  (一定要加入RAM disk support 和initial RAM disk (initrd) support 的支持,要不然等下你的新系统肯定启动不了)

三、编译准备 yum groupinstall “Development Tools” #这样做会确定你拥有编译时所需的一切工具。 yum install ncurses-devel #这样才能让 make *config 这个指令正确地执行。 yum install qt-devel #如果你用 make xconfig 而不是 make gconfig 或 make menuconfig,才需要这样做。
1、解决红帽的源更新问题,替代方案:使用centos的源
cd /usr/src
rpm -qa|grep yum                                               查看yum包
rpm -qa|grep yum|xargs rpm -e --nodeps           卸载rhel的默认安装的yum包
wget http://mirrors.ustc.edu.cn/centos/5/os/x86_64/CentOS/yum-3.2.22-39.el5.centos.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/5/os/x86_64/CentOS/yum-fastestmirror-1.1.16-21.el5.centos.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/5/os/x86_64/CentOS/yum-metadata-parser-1.1.2-3.el5.centos.x86_64.rpm
rpm  ivh  yum*                                                 同时安装新的yum
cd  /etc/yum.repos.d/
wget http://docs.linuxtone.org/soft/lemp/CentOS-Base.repo              下载yum的配置源
yum  makecache                                                生成缓存
2、准备相关软件包
yum install rpmdevtools yum-utils ncurses-devel make rpm-build rpm-devel
yum install ncurses ncurses-devel
3、安装gcc编译包
yum -y install gcc

四、源码编译安装 cd /usr/src wget http://www.kernel.org/pub/linux/kernel/v3.x/linux-3.2.9.tar.gz
1、解压源码
tar -zxvf linux-3.2.9.tar.gz
2、清除环境变量,即清除配置文件,如果是第一次编译,可省略该步骤
make mrproper
cd linux-3.2.9
3、定制内核配置:(以下几种方法任选一种) cp  /boot/config-2.6.18-194.el5xen .config  (保持和原系统的一致性)
make menuconfig  | make config | make menuconfig | make xconfig | make gconfig
进入后load旧配置文件,然后再根据自己的需要修改(见(五)),完后则保存
五、*******************menuconfig配置**************************
找到以下选中选项并选中networking support → networking options → network packet filtering framework(netfilter)
(1).Core netfilter configuration
勾中”Netfilter connection tracking support” -m state相关模块是依赖它的,不选则没有。
将netbios name service protocal support(new) 编译成模块,不然后面升级iptables后启动时会出错
勾中“Netfilter Xtables support (required for ip_tables)”
(2).IP: Netfilter Configuration
将 “IPv4 connection tracking support (require for NAT)” 编译成模块。
勾中IP tables support (required for filtering/masq/NAT) 。
将 “Full NAT” 下的 “MASQUERADE target support” 和 “REDIRECT target support” 编译成模块

(3)使用Broadcom Corporation NetLink BCM57788 Gigabit Ethernet PCIe网卡的需要注意增加以下选项:
Device Drivers ->  Network device support  >PHY Device support and infrastructure  -->
<*> Drivers for Broadcom PHYs
<*> Drivers for Broadcom 63xx SOCs internal PHY(可能没有,不影响)
//以上两项编译到内核
Device Drivers->  Ethernet device support  > Ethernet (1000 Mbit)  --> <*>   Broadcom Tigon3 support
//该项编译到内核
General setup -->[*] enable deprecated sysfs features to support old userspace tools
//注意这项已经选中
完成后保存配置文件.
(4)修改.config 1、vi .config 修改“CONFIG_SYSFS_DEPRECATED_V2”和“CONFIG_SYSFS_DEPRECATED”,默认该选项为not set,被注释掉的,将其改为=y CONFIG_SYSFS_DEPRECATED=y CONFIG_SYSFS_DEPRECATED_V2=y 然后:qw保存.config
六、开始编译内核

1、替换老的头文件

mv /usr/include/linux /usr/include/linux.old

ln -s /root/linux-3.2.35/include/linux/ /usr/include/linux
2、编译内核
make -j4 bzImage

3、编译模块 make modules
4、安装内核模块
make modules_install
5、安装内核(自动方式)
make install

6、修改grub.conf vi /boot/grub/grub.conf  把default=1改为0
七、收尾工作:
1、清除多余的文件(可略)
make  clean
2、重启系统
reboot     或          shutdown -r now
3、出现启动画面时,按  e  方出现启动菜单

4、uname -a看一下内核版本吧
其他一:手动安装内核方式
make  bzImage
cp  bzImage  /boot/vmlinuz-3.2.9
cp  System.map  /boot/System.map-3.2.9
mkinitrd   /boot/initrd_2.6.18.img   2.6.18
update-grub
其他二:重启报错 insmod: error inserting ‘/lib/dm-region-hash.ko’: --1 File exits 解决方法:
1、解压initrd文件
mkdir  /tmp/mylinux
cp  /boot/initrd-3.2.9.img  /tmp/mylinux/initrd-3.2.9.img.gz
cd  /tmp/mylinux
gunzip  initrd-3.2.9.img.gz
cpio  -id  <  initrd-3.2.9.img
2、下边就是编辑init,删掉其中重复的四行中的两行
echo "Loading dm-region-hash.ko module"
insmod /lib/dm-region-hash.ko
echo "Loading dm-region-hash.ko module"
insmod /lib/dm-region-hash.ko
3、重新打包initrd
find  .  |  cpio  H  newc  -o  -quiet  |  gzip  -9  >  /boot/initrd-3.2.9.img
ps:其实将init文件的第二行setquiet去掉,就知道initrd文件到底在做什么了