The time might come when you need to be able to load or unload a specific module into and/or out of the Linux kernel. Why? Let’s say you’re working on a machine that has no networking connection and pulls the mother lode of number-crunching (read database). For this machine, you want as much raw processing as you can possibly get.

To gain that extra processing power, you might want to think about unloading unnecessary kernel modules. In this Daily Feature, we’ll take a look at three commands you’ll find helpful: lsmod, insmod, and rmmod.

lsmod
The first of these commands, lsmod, is used to list currently loaded modules. The command structure is very simple because it has no switches or arguments to confuse the issue. The lsmod command must be run by root (if you’re not actually logged on as root, you can add the -l switch to the su command) and, when run, displays the name, size, use count, and list of referring modules.

A sample output would look like this:
[root@giles /root]# lsmod
Module Size Used by
ppp 20876 0 (unused)
slhc 4568 0 [ppp]
sb 33780 0 (autoclean)
vmmon 17056 0 (unused)
smbfs 24368 1 (autoclean)
vmnet 15712 3
vmppuser 5864 0 (unused)
parport_pc 7400 0 [vmppuser]
parport 7256 0 [vmppuser parport_pc]
autofs 9312 1 (autoclean)
lockd 31496 1 (autoclean)
sunrpc 53028 1 (autoclean) [lockd]
3c59x 19268 1 (autoclean)
opl3 11432 0
uart401 6160 0 [sb]
sound 58680 0 [sb opl3 uart401]
soundlow 420 0 [sound]
soundcore 2628 6 [sb sound]

Now that we have our output, let’s take a look at removing a module from the kernel with rmmod.

rmmod
The rmmod command, as you might guess, removes modules from the kernel. In our case, we’ll remove a module (or two) to gain back a few extra CPU cycles for our number-crunching monster.

The module(s) we’re going to remove concern dial-up networking and are called ppp and slhc. For our database machine, we won’t be needing a dial-up connection, so we’ll disable it altogether. Granted, we won’t receive a great deal of power back simply by removing the modules associated with ppp. Nevertheless, when it comes to finding as many extra umphs as we can, we’ll take ’em where we can get ’em.

We’ll add the module name (to be removed) to the rmmod command. So, if we wanted to remove the sound module, we’d run the command
rmmod slhc

If we did so, however, we’d be greeted with an error such as
[root@giles /root]# rmmod slhc
slhc: Device or resource busy

Naturally, you’d think that these modules could be removed easily, but if you notice in the listing from lismod, slhc is used by ppp. So it only makes sense that if we removed the ppp module first, we’d then be able to remove the slhc module as well.

So with the command
rmmod ppp

we are greeted with no errors, and we can move on to removing the slhc module with the command
rmmod slhc

Blam! It’s gone. We then double-check with lsmod and we see that, in fact, it is gone:
[root@giles /root]# lsmod
Module Size Used by
sb 33780 0 (autoclean)
vmmon 17056 0 (unused)
smbfs 24368 1 (autoclean)
vmnet 15712 3
vmppuser 5864 0 (unused)
parport_pc 7400 0 [vmppuser]
parport 7256 0 [vmppuser parport_pc]
autofs 9312 1 (autoclean)
lockd 31496 1 (autoclean)
sunrpc 53028 1 (autoclean) [lockd]
3c59x 19268 1 (autoclean)
opl3 11432 0
uart401 6160 0 [sb]
sound 58680 0 [sb opl3 uart401]
soundlow 420 0 [sound]
soundcore 2628 6 [sb sound]

insmod
Let’s say that, for some odd reason, you decide you do, in fact, need dial-up connectivity with the database box. Now we have to insert those modules back into the kernel. This is accomplished with the insmod command.

When we insert modules, we have to insert them in reverse order from the way we removed them. We successfully removed the modules by taking out ppp followed by slhc. Inserting the modules in reverse order will bring us the same success.

If we try to add these modules in reverse order, we’ll get something like the following:
[root@giles /root]# insmod ppp
Using /lib/modules/2.2.16-3/net/ppp.o
/lib/modules/2.2.16-3/net/ppp.o: unresolved symbol slhc_compress_Rcfd3a418
/lib/modules/2.2.16-3/net/ppp.o: unresolved symbol slhc_remember_R07972313
/lib/modules/2.2.16-3/net/ppp.o: unresolved symbol slhc_init_R1ca65fca
/lib/modules/2.2.16-3/net/ppp.o: unresolved symbol slhc_toss_Ra152cec0
/lib/modules/2.2.16-3/net/ppp.o: unresolved symbol slhc_free_Rb99033d9
/lib/modules/2.2.16-3/net/ppp.o: unresolved symbol slhc_uncompress_R3bb36b01
[root@giles /root]#

The clue here is the beginning of the line after unresolved symbol. When you see this, you can almost rest assured that slhc is the module association causing the problem. Load this module and you’ll see no warnings returned. So, by entering
insmod slhc

followed by
insmod ppp

you’ll have successfully loaded the modules necessary for dial-up networking.
This does not mean that dial-up networking is fully functional. You still must go through the standard configuration routines to complete the dial-up networking setup.
Conclusion
The ability to load and unload modules makes Linux a very slick predator in the OS food chain. Such flexibility is hard to come by, and when coupled with simplicity, it’s an administrator’s dream come true. Need to keep people from playing sounds on a certain machine? Remove the sound module. Need to keep employees from checking e-mail at a certain time? Remove the ppp modules.

By using this model, you don’t lose the PC’s capabilities—you allow capabilities depending on needs and/or resources.

Jack Wallen, Jr., editor in chief of Linux content, was thrown out of the “Window” back in 1995, when he grew tired of the “blue screen of death” and realized that “computing does not equal rebooting.” Prior to Jack’s headfirst dive into the computer industry, he was a professional actor, with film, TV, and Broadway credits (anyone see “The Great Gilly Hopkins”?). Now, Jack is determined to use his skills as a communicator to spread the word—Linux. Ladies and gentlemen, the poster boy for the Linux Generation!

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.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays