Do you want to make your Linux PC run even better than it already does? You're probably not experiencing any performance or stability problems--Linux has earned its reputation as a rock-solid OS, after all--but there's at least one Linux power-user trick you should be aware of. Recompiling your Linux kernel may sound about as fun as a root canal, but it's a relatively simple process that leaves you with a leaner, faster Linux system. How? A smaller kernel--the very nucleus of the OS--takes less time to boot, takes up less space in memory, and rids you of system components you simply don't need.
Let's get more specific: At the heart of Linux is the kernel--the core code that controls every aspect of your system, divvying up resources to programs and coordinating the flow of data to and from your computer's hardware and peripherals. Unlike the Windows kernel, Linux's kernel is completely configurable. You can add and remove support for various types of hardware, optimize the kernel for your particular processor, and more.
Linux was designed to run as easily as possible "out of the box," so most versions ship already configured for every type of hardware and every networking protocol imaginable. By tailoring your Linux kernel to your particular machine and eliminating support for devices you don't own and protocols you'll never use, you can boost Linux's performance to new levels. This process is called recompiling the kernel, because after we tell Linux exactly what we want (and what we don't), we then let it compile a new, customized kernel, converting the source code into machine language your computer can understand.
For the sake of simplicity, we'll assume for this article that you're using Red Hat Linux (for instructions on installing it, see "Get Started With Linux" (/heres_how/article/0,1400,11379,00.html) ). But most of the kernel recompilation process is the same for all distributions (including Caldera, Debian, and Mandrake), so users running non-Red Hat flavors can benefit, too.
One warning: While recompiling the kernel isn't difficult, one wrong move can render the Linux side of your machine unbootable. Read through this entire article first, to be sure you're comfortable with the task ahead of time. If you're not, leave well enough alone; your Linux system will function fine with its default kernel. If you think you can handle it, before you do anything else, back up crucial data to removable media, and be sure you have a Linux boot disk ready in case disaster strikes (you should have created one when you installed the OS).
But if you carefully follow our instructions and pay attention to the information Linux provides--built-in help is available during the most difficult part of the process--you should pull through safely, and with a better setup to boot (pun intended).
Got Boot Disk?
To start, you'll need to log on as the root user on your computer. (You assigned yourself a root user password when you installed the operating system.) And you'll need to be in a command shell to follow the steps in this article. If your copy of Linux boots up to a text console, you're ready to roll. If you boot to an X Windows session, you'll need to open a terminal window (on the default Red Hat GNOME desktop, click the button on the taskbar that looks like a computer).
If you followed the instructions in "Get Started With Linux," (/heres_how/article/0,1400,11379,00.html) you created an emergency boot disk. Good for you! Should something go wrong, you'll need this floppy to resurrect your computer.
If you didn't create that disk, then make one now. To do so in Red Hat 5.x or 6.x, first type more /etc/lilo.conf on the command line and press Enter. The contents of the LInux LOader, or LILO, configuration file will scroll by, a page at a time. You're looking for a line that looks like this: image=/boot/vmlinuz-2.2.12-20
This is the path for your current Linux kernel, the one that loads whenever you start this operating system. Note the version number of the kernel (everything after "vmlinuz-")--you'll need this in a second. If you don't have a command line prompt back, hit q to stop viewing the lilo.conf file. Now type the following:
mkbootdisk --verbose --device /dev/fd0 2.212-20
Replace the version number in this command with the one you discovered in lilo.conf. Linux will ask you to insert the floppy disk and press Enter to continue. You'll have a new boot disk in a few seconds. (If any errors crop up during the creation of the disk, toss out the floppy you inserted and try again with another.) Now set this boot disk aside--you may need it later.
Getting the Packages Together
It's time to make sure you have all the required bits and bytes of code to recompile your kernel. The steps for taking inventory are different from distribution to distribution; again, our directions are specific to Red Hat. Once we get past this step, the instructions should work for most distributions.
Keeping in mind that the Linux command line is case-sensitive, start the discovery process by typing:
rpm -q kernel kernel-headers kernel-ibcs kernel-pcmcia-cs kernel-source make cpp egcs dev86 glibc-devel
Whew! This command tells Linux to search for all the packages you'll need to get the job done. If they're around, Linux will report back names and version numbers:
The version numbers you see here correspond to version 6.1 of the Red Hat distribution; if you have another version, then different numbers will appear. That's no problem.
What if you don't see anything like this list after entering that awful rpm command? It could mean that, when you installed Red Hat, you didn't install any of its kernel and development packages. If that's the case, you'll see output along the lines of "package kernel-source is not installed."
Still no problem--but you have a few extra commands to issue in order to prepare. Write down the names of the files that were reported missing and insert the Red Hat installation CD-ROM (not the "contributed binaries" disc). Now issue the commands below (steps 1, 5, and 6 are unnecessary in Red Hat 6.1).
After you've installed all the packages, try that long rpm command once more and make sure it reports all necessary packages present.
Only for the Very Brave: Kernel Upgrades
If you've been playing with Linux for some time, you might want to get more adventurous. In this case, try downloading the latest stable release of the kernel from the Linux Kernel Archives. (www.kernel.org/) Why? Newer kernels have new features, support new types of hardware, fix bugs, and so on.
After downloading a new kernel source, move your current kernel source out of harm's way: Change directories to /usr/src (cd /usr/src ), and shuffle the current kernel to the side by typing mv linux linux_backup. This renames the Linux directory as linux_backup. Place the kernel file you download in the /usr/src directory before continuing. To unpack that file, type tar filename.tar.gz, substituting the correct file name for your download. A massive amount of text will scroll across your screen--this means the files are placing themselves where they ought to go.
Once all the necessary files are in place, you get to the fun part: Configuring your kernel. (All right, it's not exactly fun as in Friday-night, ha-ha-ha, but if you're geeky enough to install Linux in the first place, you probably enjoy tinkering.) You'll have a choice between two interfaces at this juncture. Before you try either, make sure that your working directory is /usr/src/linux; entering cd /usr/src/linux will do the trick.
Make menuconfig works from any command line, letting you make changes using the arrow and Tab keys. Make xconfig works within X Windows, offering a more user-friendly, clickable menu. We'll walk you through the latter, but our advice works for the first option as well--you'll just be dealing with a different interface.
If you're a text-mode sort of person, you might prefer menuconfig, shown here, over its X Windows equivalent, xconfig.
Start by typing make xconfig and pressing Enter. Some gobbledygook will flow across the terminal window, and then a new window will pop up, full of buttons. Each button leads to a new dialog box that lets you configure a particular part of the kernel. The second button, for instance, labeled "Processor type and features," lets you specify which type of processor your machine has.
This is xconfig's main menu. It sports the same options as menuconfig, but it looks nicer and lets you point and click.
In each new dialog box, you can turn kernel features on (select the "y" option for "yes") or off ("n" for "no"), or you can elect to compile that particular feature as a module (the "m" option). Modules are components that can be grafted onto the kernel on the fly but aren't an integral part of it. You'll notice as you wander through the dialog boxes that many features are compiled as modules by default.
To begin, tell Linux what sort of processor you have. Click "Processor type and features"; to the left of "Processor family," click the drop-down menu and select the appropriate chip. Is your computer a Pentium or better? If yes, select "n" next to "Math emulation"--your chip has a built-in math coprocessor, so there's no need for Linux to emulate one. Peruse the other options in this dialog box before choosing "Main Menu" to close it.
From the main menu, you can explore other feature groups by clicking their buttons and perusing their listings. Unsure what a particular feature is all about? Hit the Help button for a full account of each feature's goal in life, often with a recommendation as to whether you need it. Explore these help dialog boxes. If you don't understand a particular feature even after reading about it, leave it be, no matter what its setting. And if at any point you feel you've screwed things up beyond repair, make your way back to the main menu and quit without saving your changes. Then start from scratch with another make xconfig.
Each kernel option has its own help section--that's what all those Help buttons are for.
Need a push in the right direction? You can probably live without some feature sets. To eliminate them from your system, click the appropriate category (you'll choose from those listed below) and select "n" for the first option listed in the resulting dialog box:
Hurry Up and Wait
Now that you've specified how you want to customize the kernel, you need to make sure it will compile properly. First, issue the make dep command to be sure all the source code files you need are in place. Once this command has run (a few minutes at the most), enter make clean to remove any extraneous files that you won't need to compile your new kernel.
You're finally ready to start compiling. Shut down all programs and, back at your command line, take a deep breath and enter make bzImage. Now go fix yourself a snack: This process will take anywhere from 10 to 30 minutes (more in extreme cases), depending on the speed of your machine.
When the command line returns, make sure the terminal isn't reporting an error of some sort: If it is, you don't have a new kernel waiting for you. Either a necessary package is not installed, or you made a mistake during configuration (like enabling a feature without enabling another feature it depends on--such relationships are always detailed in Help). Sorry to say it, but you have to start over at this point.
If there are no error messages, it's now time to compile and install the modules you requested during kernel configuration. Begin with make modules. This is another lengthy process, so go have yourself another snack. When the command line returns, enter make modules_install. This last command should finish in a matter of seconds.
The Moment of Truth
Pat yourself on the back: You're almost ready to boot with your new kernel for the very first time. First you need to copy your new kernel to the Linux boot directory. Enter the following command:
cp arch/i386/boot/bzImage /boot/vmlinuz-mykernel
Now, with your new kernel in place, you need to let LILO know it's there. To do so, you have to alter a configuration file with a text editor. The commands given here are for the pico text editor; if you're familiar with another editor, go ahead and use it.
Enter pico /etc/lilo.conf to launch pico with the correct file. You're once again looking for the "image=" line that we first mentioned in the boot disk creation section. It looks something like:
Jot down this line's current form before you make any changes. Now replace that file name with /boot/vmlinuz-mykernel. Press Ctrl-X, followed by Y and Enter to save your work. When you're back at the command line, type lilo and press Enter. Now enter reboot to restart the computer.
When the LILO: prompt appears, enter the text you normally do to boot Linux (usually linux or just plain Enter). Here's where your work pays off--or you return to the drawing board.
Scenario 1: Trouble
If the stars are aligned against you, your new kernel will fail, often with a "Kernel Panic" error message. Don't worry--your computer is fine, and you have a boot disk to get you out of this. Boot with that disk, get yourself back to a command shell, enter pico /etc/lilo.conf and change the "image=" line back to its original form. Save your changes, and then just as before, issue the lilo command, followed by reboot. Your old kernel will boot when you choose Linux at the LILO: prompt.
What went wrong? It's hard to say. Perhaps you failed to enable support for an essential piece of hardware, or you misconfigured a certain option. To fix the problem, you need to reconfigure and recompile the kernel again by following the steps we've gone through--not exactly a quick fix, but after you get used to the process, you probably won't make those sorts of mistakes. Try making your changes one at a time to isolate the problem. It may take a little longer, but it could save you frustration in the long run.
Scenario 2: Success!
If the stars are with you, this arduous troubleshooting won't be necessary, and your new kernel will boot. With some versions of Red Hat, you'll need to run the sound configuration program again to get sound up and running (see the "Let There Be Sound" (/heres_how/article/0,1400,12567+1+1,00.html) section of "Second Steps With Red Hat Linux" (/heres_how/article/0,1400,12567+1+0,00.html) ). If you find after booting that a certain piece of hardware isn't working, check to see if you accidentally disabled it in the kernel by issuing a make xconfig command and checking the configuration. If that's the problem, reenable the hardware and recompile the kernel.
Dan Berkes is a freelance writer based in Galena, Illinois, where he runs a small Web hosting business.