Compiling (or recompiling) your Linux kernel

Although the general consensus is that it's difficult to compile a Linux kernel, it's really not that hard, as you will see. After reading this brief tip, I hope you and I will share the title of Kernel Compiler!

What I am going to do is simply run through the steps of the actual compilation of my existing kernel. I won't make any changes (since changes and needs are so unique to each system) but I will walk through all the steps. I will also be approaching this with a path-of-least-resistance approach (read: we're going to do this the easy way).

Just to be safe...

Before I get into the compilation of my kernel I'm going to make a boot floppy just to be on the safe side. To do this, slap a floppy in your drive, su to root, and run the command:

dd if=/boot/bzImage of=/dev/fd0 bs=8192
where bzImage is the kernel image that you need to insert. For example, in my stock Red Hat 7.0 system the kernel image is /boot/vmlinuz-2.2.16-22. For this image, the command would be:
dd if=/boot/vmlinuz-2.2.16-22 of=/dev/fd0 bs=8192

Back to compiling the kernel

The first step is to locate the source code for your particular distribution. Typically, these files are located in /usr/source/linux. (This holds true for most Linux distributions).

Once you've located the source, you are ready to actually start the compilation process.


Author's note
Because I am doing a simple recompile of my current kernel I am leaving out some major steps necessary when compiling a new kernel.

Make xconfig

Since I am going to take the easy route, let's stick with the simplest (and sanest) kernel configuration tool. The xconfig tool is a graphical tool offering simple buttons that, when clicked, present the many kernel configuration choices. The main xconfig window looks like the one shown in Figure A.

Figure A
View
Above, you see the main xconfig window.

Each time you click on a button within the xconfig window, you will be offered a list of choices. In Figure B, you can see where I've chosen the Character Devices button. Each choice will have three selections: yes (y), no (n), and modular (m). The first two choices are obvious: yes means add the module; no means do not add the module. The third choice isn't so obvious. When modularly adding a kernel module, you are, in effect, saying you want the modules available and will load or unload them as you see fit.

Go ahead and run through the other buttons to see what's there. It's quite a hefty assortment of modules (giving you an idea of how complete the Linux kernel really is). Once you've run the gamut, you can click Save And Exit and move on to the next step of the compilation!

Figure B
View
Above you see a small sample of possible options to choose while compiling your kernel. .

Make and its options


You will now journey into the sometimes-maddening world of make. There are many ways to go about this. I am, however, going to try to outline the most universal process as well as the simplest.

While in the /usr/src/linux directory, you are going to run the following commands (as root):

make mrproper
make dep
make clean
make bzImage
make modules
make modules_install

Each of the above commands performs a very different and very critical role in the compiling of the Linux kernel. Once you've run the above set of commands without error, your kernel is compiled!

Postcompilation process

Now, you are ready for the final steps of your kernel compilation. These steps are very important, and it's crucial that you follow them closely.

BzImage to boot image

First, open up a second console window and cd to the /boot directory. Once you're in this directory, run the ls command to see what's there. On my Red Hat 7.0 system, I see the following ls output:

System.map map
System.map-2.2.16-22 message
System.map-2.2.16-22enterprise module-info
boot.2100 module-info-2.2.16-22
boot.b os2_d.b
chain.b vmlinux-2.2.16-22
kernel.h vmlinux-2.2.16-22enterprise
kernel.h-2.2.16 vmlinuz
kernel.h-2.4.0 vmlinuz-2.2.16-22
lost+found vmlinuz-2.2.16-22enterprise

What you're actually looking for is your current kernel image and the System.map file. In my case, the image is vmlinuz-2.2.16-22, and the System.map is a symlink to System.map-2.2.16-22.

Sticking with the above example, rename the file vmlinuz-2.2.16-22 to vmlinuz-2.2.16-22-old, and the System.map-2.2.16-22 to System.map-2.2.16-22-old. To do this, run these commands:

cd /boot
mv vmlinuz-2.2.16-22 vmlinuz-2.2.16-22-old
mv System.map-2.2.16-22 System.map-2.2.16-22-old

With these files renamed, you're ready to move the new files into /boot.

Now, change to the directory holding the new kernel image with:

cd /usr/src/linux/arch/i386/boot/bzImage /boot/

and then copy the bzImage file into /boot with the command:

cp bzImage /boot/vmlinuz-2.2.16-22

Then, it's time to do the same with the System.map file. Change to the /usr/src/linux directory and run the command:

cp System.map /boot/System.map-2.2.16-22

You are nearly done!

LILO, reboot and special circumstances

LILO

It's time to edit your /etc/lilo.conf file. In reality, you are going to simply add a new entry into this file to reflect the new kernel image. The section you want to edit looks like:

image=/boot/vmlinuz-2.2.16-22
label=linux
read-only
root=/dev/hde

and will finally (in our example) look like:

image=/boot/vmlinuz-2.2.16-22
label=test
read-only
root=/dev/hde
image=/boot/vmlinuz-2.2.16-22-old
label=linux
read-only
root=/dev/hde

Save this file and rerun lilo with the command:

/sbin/lilo

You should receive a return with something like:

Added linux *
Added test

and you're ready to rock and roll!

Reboot

With your boot floppy in hand (just in case), it's time to reboot. At the lilo: prompt, you can hit [Tab] and see both the linux and the test entry.

Special circumstances

Of course, not every situation is going to be the same. You may be compiling a new kernel. The biggest difference here will be the location of the source files. If you have downloaded a new kernel, and plan to compile it, you will want to move the source of your current kernel so as not to overwrite the source of the working kernel.

Before you unpack the new kernel, check to see if the /usr/src/linux directory is not merely a symbolic link to /usr/src/linux-2.*.* (where 2.*.* is the name of your current, running kernel). To check this, run the command:

ls -l /usr/src/linux

If you receive something like:

lrwxrwxrwx 1 root root 12 Nov 4 12:08 linux -> linux-2.2.16

you know it's a symbolic link. With this in mind, you will want to remove the symbolic link with the command:

rm /usr/src/linux

Now, you can unpack your new kernel, which will create a new directory. Let's say you are planning on compiling a 2.2.17 kernel. When you unpack the source, you will have a new /usr/src/linux directory. You'll want to rename that directory with the command: mv /usr/src/linux /usr/src/linux-2.2.17

and then create a symbolic link to /usr/src/linux with the command:

ln -s /usr/src/linux-2.2.17 /usr/src/linux

You can now move into the /usr/src/linux directory and start the compilation process!

Editorial disclaimer: The authors and editors have taken care in preparation of the content contained herein but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.

TechRepublic is the online community and information resource for all IT professionals, from support staff to executives. We offer in-depth technical articles written for IT professionals by IT professionals. In addition to articles on everything from Windows to e-mail to fire walls, we offer IT industry analysis, downloads, management tips, discussion forums, and e-newsletters.

©2001 TechRepublic, Inc.

Talkback

Add your opinion

In order to post a comment, you need to be registered. (Sign In or register below)

Post your comment

Terms of Service - As a ZDNet registrant, and by using this service, you indicate that you agree to our Terms and Conditions and have read and understand our Privacy Policy.

ZDNet Australia Live

by http://t.co/vmlLt4bh: SA Health's journey to e-health: Implementing e-health services for an entire state is a... http://t.co/NVrBd9c5

Facebook investor to sue Nasdaq over alleged bungled orders: http://t.co/XGRsNzA4 ^LH

Combining @Ariba's network & @SAP's applications - "SAP eyes cloud super network with Ariba buy" http://t.co/jeMWEKpB

SA Health's journey to e-health: Implementing e-health services for an entire state is a daunting task, but, as ... http://t.co/Vwchau6N

RT @JamesVickery: Google warns users of DNSChanger malware http://t.co/DsHUnC5r

Upskill. RT @zdnetaustralia Job vacancies are down 22 per cent on a year ago. So what are IT professionals to do? http://t.co/PrFEBfqS ^ST

Google warns users of DNSChanger malware http://t.co/DsHUnC5r

National Botnet Network coming: Earthwave http://t.co/t49r3IV0

Surely IT is more than just a game? http://t.co/WvSk0C0N

RT @JLLLOW: Revolution. RT @zdnetaustralia: Job vacancies are down 22 per cent on a year ago. So what are IT professionals to do? http://t.co/rdjqdACC

Revolution. RT @zdnetaustralia: Job vacancies are down 22 per cent on a year ago. So what are IT professionals to do? http://t.co/rdjqdACC

Google has joined in on the chorus of organisations warning users about DNSChanger infections http://t.co/ysaIHiuG ^ML

Akku Asus A32-K72 Original,Kompatibler Ersatz akku für Li-ion Asus A32-K72 Original Laptop Akkus Asus A32-K72 Original,A32-K72 Original...

5 hours ago by akkuakku on HP Compaq 6730b

It is great to see the NSW government taking this step, however there's plenty of home-grown talent loeaving or being rediverted due to l...

5 hours ago by Aceyducey on NSW Govt appoints Silicon Valley champion

Job vacancies are down 22 per cent on a year ago. So what are IT professionals to do? http://t.co/EpY9YiFg ^ST

by http://t.co/vmlLt4bh: JobWatch: where the jobs are: The latest analysis on online job ads from the Department ... http://t.co/nh1wg7Y6

@chieftech @zdnetaustralia that's a fair call. Still an area that requires consideration work. BYOD = BYOViruses & Malware :)

JobWatch: where the jobs are http://t.co/Lqo8BNVT

EMC hones focus on hybrid cloud big data Hardware News ZDNet Australia: EMC has launched 42 prod... http://t.co/uR56HXDz #bigdata #blogs

Are specific gaming development degrees bollocks? http://t.co/z2zbaWvT ^ST

#NSW Govt announces shopfront in Silicon Valley + 7 consortia to dev #mobile for public sector http://t.co/GPrIXH4F via @johnW3LLS #govcamp

JobWatch: where the jobs are: The latest analysis on online job ads from the Department of Education, Employment... http://t.co/qJce42h2

RT @johnW3LLS: #NSW Govt announces shopfront in Silicon Valley + 7 consortia to dev #mobile for public sector http://t.co/JDSdSxWu #gov2au

RT @zdnetaustralia: Android fragmentation threw a spanner into Victorian Health's app strategy: http://t.co/4pkmnkMB ^LH

What Microsoft won't tell you about Windows 7 licensing http://t.co/Y2e6sXdI #Win7

#Android fragmentation steers Vic Health - @ZDNet Australia : http://t.co/chrmWl7B

RT @zdnetaustralia: Android fragmentation threw a spanner into Victorian Health's app strategy: http://t.co/4pkmnkMB ^LH

Android fragmentation steers Vic Health - ZDNet Australia: Android fragmentation steers Vic Healt... http://t.co/VTbMBy5A #android #news

by http://t.co/vmlLt4bh: Android fragmentation steers Vic Health: Fragmentation issues in Android were a key conc... http://t.co/wOmHdAav

Android fragmentation steers Vic Health http://t.co/CqTImM5l

Android fragmentation steers Vic Health - ZDNet Australia: Android fragmentation steers Vic... http://t.co/3ssDp1SW http://t.co/KpTZdvuO

Android fragmentation steers Vic Health: Fragmentation issues in Android were a key concern for the Victorian De... http://t.co/NnjPEqSu

Android fragmentation steers Vic Health http://t.co/jcB7UGer

Chrome beats Internet Explorer in global Web browser race | ZDNet http://t.co/7G7xMfJj

Android fragmentation steers Vic Health: Fragmentation issues in Android were a key concern for the Victorian De... http://t.co/HLdurfS5

Mining the social data stream for deeper customer insight | via @ZDNet http://t.co/x4xouPQh)

Android fragmentation steers Vic Health http://t.co/A6SJkfJw

But this is the thing. There are still plenty of good-quality graduates whose skills can raise seasoned professional eyebrows... if they ...

7 hours ago by techkid on Skills shortage: companies being too picky?

I wouldn't have called Vista cheesy. Its GUI was pretty slick (and indeed handed on to Windows 7). It was, however, poorly implemented, h...

7 hours ago by techkid on Microsoft admits Vista was 'cheesy'

Thanks Nelson, it should be right now.

-Michael.

7 hours ago by Mukimu on Ausgrid network to talk back to operators

I guess the mouse was a necessary evil at the time. I mean, yes, keyboard shortcuts in the right hands are faster than any mouse action (...

7 hours ago by techkid on Microsoft admits Vista was 'cheesy'

fyi google may always lie

7 hours ago by rt luvs youh on Google shows we're killing our language

they probaly always lie about in4mation bout people

7 hours ago by rt luvs youh on Google shows we're killing our language

$6.7million, now we know the price to the tax payer of a government IT project clean up. You've got to ask the question don't you: why o...

8 hours ago by Takenforgranted on Vic scraps HealthSMART system

why some mp4 files with higher frame width can not be played in my 3m mp180??

8 hours ago by cyrusmann_ymail.com on 3M MP180 Pocket Projector

Unfortunately there is NO such place as Nelson's Bay. It's Nelson Bay!! Probably not your fault for the error, as your Media Release prob...

9 hours ago by Nelson on Ausgrid network to talk back to operators

@Wow - thats one of the benefits of the iPad (and tablets in general). They are one of the most generation neutral products ever made. ...

11 hours ago by Gav on Westpac board goes paperless with iPads

and why is this such a super idea? http://www.itnews.com.au/News/301778,thousands-affected-in-billing-cloud-breach.aspx oh, yeah, right...

11 hours ago by btone on Fed Govt steps up on shared cloud plan

Wow, seems like a fantastic initiative that helps to save the environment. It must have taken a lot of convincing to get the Board to mov...

12 hours ago by Wow on Westpac board goes paperless with iPads

I'm a payed up lib member who has voted Labor in the last 2 federal elections. I had the previlege of speaking to Mr Turnball 3 months ag...

12 hours ago by spazmanaught on NBN contracts may be left alone: Turnbull

Good to see Westpac's concentrating on the real IT issues !

12 hours ago by jeff_syd on Westpac board goes paperless with iPads

I am not sure how this issue becomes an attack on Mr Turnbull. But I guess he is fair game. In any event I would have thought a Ddos woul...

23 hours ago by Doubt on National Botnet Network coming: Earthwave

I still use 98SE. Windows ME was an abortion in a bucket and Vista was ME without the bucket. My screen may look boring, but I jumped str...

23 hours ago by Treknology on Microsoft admits Vista was 'cheesy'

This story has been voted 10 times in the last 24 hours!

23 hours ago, CeBIT 2012 opens: photos

This story has been voted 15 times in the last 24 hours!

1 day ago, Lenovo ThinkPad 3G tablet (32GB)

Well I don't know what they have done with their EFTPOS machines, local one in WA Coles Express I used this morning and I normally do "ch...

1 day ago by harryinthesoup on Coles ditches PINs in payment pilot

6.7 M last ditch attempt - interesting - The Auckland region (population 1.4 mil) has estimated to have spent less than this in total ...

1 day ago by debsteele on Vic scraps HealthSMART system

Facebook Activity

Keep up with ZDNet Australia

ZDNet Events Calendar

ZDNet Events Calendar