In this lesson,
we're going to dive deeper into the concepts
of the Linux kernel
by learning how to perform complex tasks
by configuring kernel modules.
Now, the Linux kernel is loaded into memory
by the boot loader.
The kernel modules
are dynamically loaded and unloaded on demand,
and they provide device drivers
the ability to allow the kernel to access new hardware,
support different file system types
and generally extend the functionality of the kernel
without the need to reboot the system,
and this can lead to
a lot of great flexibility during operations.
Now, the /usr/lib/modules directory
contains the modules of different kernel versions,
that are going to be installed on the system.
This holds a directory
named after the kernel's version number.
Inside this directory,
modules are stored across various sub-directories
based on the categories they belong to.
For example, a Bluetooth driver
might be stored in /usr/lib/modules
/version number/kernel/drivers/bluetooth.
Inside the /usr/lib/modules version number/kernel directory,
there's going to be several subdirectories
for each of the different devices,
as well as some for the different architectures
inside of these modules
for different specific architecture types
or crypto, which contains modules for encryption
and other cryptographic functions.
There's one called drivers,
which contains modules for various types of hardware.
fs contains modules for various types of file systems
and net, which is a directory that contains modules
for networking components,
such as your firewalls and your protocols.
This Linux system comes with
a number of different commands as well
to handle these kernel modules.
This includes listing the modules
currently loaded into the Linux kernel,
displaying module information,
as well as loading and unloading
these different kernel modules.
We're going to talk a little bit about these commands
in a little bit more depth as we go through this lesson.
Now, the first one I want to talk about is lsmod.
The lsmod command is used to display
the currently loaded kernel modules, their sizes,
their usage details and their dependent modules.
The modinfo command is going to be used to display information
about a particular kernel module,
such as the file name of the module, the license,
the description, the author's name,
the module version number, the dependent modules
and other parameters or attributes.
To run this command simply type in
modinfo, options and the module name.
Another command that's important to know
is insmod, which is used to install a module
into the currently running kernel.
This command inserts only the specified module
and does not insert any dependent modules though.
To use this type in insmod and the module name.
The rmmod command is going to be used to remove a module
from the currently running kernel.
To run this command type rmmod and the module name.
Another command we want to talk about is modprobe.
modprobe is a command that's used to add or remove modules
from the kernel.
This command is capable of loading all the dependent modules
before inserting the specified module though,
making it easier to use.
Therefore, we prefer to use this instead of insmod or rmmod.
To add modules using the modprobe,
simply type in the -a option and specify the modules
you want to add.
If you want to unload a module
you're going to use the -r option
to remove the specified modules.
To run this, type in modprobe,
the options and the module name.
In addition to options for adding and removing modules,
the modprobe command also has more options that you can use.
You can use -f if you want to force the module
to be inserted or removed.
You can use -n if you need to conduct a dry run
by making sure you look at the output results
without actually executing operations.
You can also use -s if you want to print errors
to the system log rather than to the standard error.
And you can use -v
if you want to enable verbose mode for troubleshooting.
In order for modprobe
to accurately install dependent modules
it reads the module's .dep file
to identify how modules are linked to one another.
The depmod command is going to be used
to update the database of dependencies,
so the modprobe can actually function properly.
The depmod or dependent modules command
will search the contents of /lib/modules for each module.
Any module can export a symbol
indicating they can provide a service to other modules.
Then the depmod tool can build the module's .dep file
by aggregating all the instances of symbols
that are being exported and used.
To run the command, simply type in depmod with the options.
The modprobe command can also add or remove modules.
And because the modules have dependencies,
we need a way of specifying what options
are to be used with these modules.
All the .conf files or config files
are going to be located inside the /etc/modprobe.d directory.
This is a directory extension that specifies
the options required for the various modules.
You can also use the alias command,
which is alias the alternative name and the module name
and it will specify an alternative name
for a module with a long name.
For example, if I had something that had a really long name
and I just wanted to call it, Jason, I could do that.
We can also use the blacklist command
which is used by typing blacklist and the module name
to ignore any internal aliases,
which occurs when a module defines its own aliases.
And if we want to use the install
we can do install module name and command
to run the specified command
without inserting the module into the kernel.
In addition to loading modules into the kernel at runtime
you can also change some of the kernel's parameters
while it's running.
You can use these parameters to improve system performance
hardening your security, configure networking limitations,
change virtual memory settings and much more.
To do this you're going to go into the /proc/sys directory.
This directory lists all the parameters
that you can configure on your system.
Like the directories containing kernel modules,
this /proc/sys directory is divided into several categories,
including crypto which includes parameters
related to encryption and other cryptographic services.
debug, which includes parameters related to
debugging the kernel,
dev, which includes parameters related to
specific hardware devices,
fs, which includes parameters related to file system data,
kernel, which includes parameters related to
miscellaneous kernel functions,
net, which includes parameters
related to networking functions,
user, which includes parameters related to
user space limitations,
and vm, which includes parameters related to
virtual memory management.
Another command that's really powerful in Linux
is the sysctl command.
This is spelled S-Y-S-C-T-L.
This is a powerful Linux command,
which acts as an interface to dynamically change
the kernel parameters.
With the help of this command
you can modify the kernel parameters
without recompiling the kernel or rebooting your machine.
The parameters available for modification
can all be found under the /proc/sys directory.
You can also view a parameter value
by displaying the content of the appropriate file.
It has various options, including things like -a
which is used to display all the parameters
and their current values,
w parameter equals value,
which is going to be used to set a parameter value
inside of the kernel.
You also can use option -p and a file name
to load the sysctl settings from a specified file
or the /etc/sysctl.conf file,
if no file name is being provided.
If you use -e, you can use this to ignore errors
about unknown keys.
If you use the option -r with a pattern
you can use this to apply a command to a parameter
that matches a given pattern,
if you're using an extended regular expression.
To run the sysctl command,
you're going to type S-Y-S-C-T-L and the options
on the command line.
The /etc/sysctl.conf file
is going to enable configuration changes
to your running Linux kernel.
These changes can include all sorts of improvements
to your networking, security configurations
or logging of information.
So as you can see,
it's really important to understand these modules
and how to modify them,
because they bring a lot of capability to your Linux system.