1
00:00:00,720 --> 00:00:01,740
In this lesson,

2
00:00:01,740 --> 00:00:03,719
we're going to dive deeper into the concepts

3
00:00:03,719 --> 00:00:04,980
of the Linux kernel

4
00:00:04,980 --> 00:00:07,440
by learning how to perform complex tasks

5
00:00:07,440 --> 00:00:09,660
by configuring kernel modules.

6
00:00:09,660 --> 00:00:11,880
Now, the Linux kernel is loaded into memory

7
00:00:11,880 --> 00:00:13,320
by the boot loader.

8
00:00:13,320 --> 00:00:14,310
The kernel modules

9
00:00:14,310 --> 00:00:17,130
are dynamically loaded and unloaded on demand,

10
00:00:17,130 --> 00:00:18,660
and they provide device drivers

11
00:00:18,660 --> 00:00:21,900
the ability to allow the kernel to access new hardware,

12
00:00:21,900 --> 00:00:23,850
support different file system types

13
00:00:23,850 --> 00:00:26,640
and generally extend the functionality of the kernel

14
00:00:26,640 --> 00:00:28,620
without the need to reboot the system,

15
00:00:28,620 --> 00:00:29,453
and this can lead to

16
00:00:29,453 --> 00:00:32,759
a lot of great flexibility during operations.

17
00:00:32,759 --> 00:00:36,480
Now, the /usr/lib/modules directory

18
00:00:36,480 --> 00:00:38,940
contains the modules of different kernel versions,

19
00:00:38,940 --> 00:00:41,040
that are going to be installed on the system.

20
00:00:41,040 --> 00:00:42,060
This holds a directory

21
00:00:42,060 --> 00:00:44,400
named after the kernel's version number.

22
00:00:44,400 --> 00:00:45,810
Inside this directory,

23
00:00:45,810 --> 00:00:48,630
modules are stored across various sub-directories

24
00:00:48,630 --> 00:00:51,000
based on the categories they belong to.

25
00:00:51,000 --> 00:00:53,220
For example, a Bluetooth driver

26
00:00:53,220 --> 00:00:57,140
might be stored in /usr/lib/modules

27
00:00:57,140 --> 00:01:02,140
/version number/kernel/drivers/bluetooth.

28
00:01:02,970 --> 00:01:07,970
Inside the /usr/lib/modules version number/kernel directory,

29
00:01:08,880 --> 00:01:10,590
there's going to be several subdirectories

30
00:01:10,590 --> 00:01:12,330
for each of the different devices,

31
00:01:12,330 --> 00:01:14,760
as well as some for the different architectures

32
00:01:14,760 --> 00:01:16,380
inside of these modules

33
00:01:16,380 --> 00:01:18,900
for different specific architecture types

34
00:01:18,900 --> 00:01:22,020
or crypto, which contains modules for encryption

35
00:01:22,020 --> 00:01:23,730
and other cryptographic functions.

36
00:01:23,730 --> 00:01:25,080
There's one called drivers,

37
00:01:25,080 --> 00:01:27,960
which contains modules for various types of hardware.

38
00:01:27,960 --> 00:01:31,410
fs contains modules for various types of file systems

39
00:01:31,410 --> 00:01:33,810
and net, which is a directory that contains modules

40
00:01:33,810 --> 00:01:35,070
for networking components,

41
00:01:35,070 --> 00:01:37,830
such as your firewalls and your protocols.

42
00:01:37,830 --> 00:01:39,360
This Linux system comes with

43
00:01:39,360 --> 00:01:41,130
a number of different commands as well

44
00:01:41,130 --> 00:01:43,230
to handle these kernel modules.

45
00:01:43,230 --> 00:01:44,730
This includes listing the modules

46
00:01:44,730 --> 00:01:46,650
currently loaded into the Linux kernel,

47
00:01:46,650 --> 00:01:48,420
displaying module information,

48
00:01:48,420 --> 00:01:49,770
as well as loading and unloading

49
00:01:49,770 --> 00:01:51,630
these different kernel modules.

50
00:01:51,630 --> 00:01:53,490
We're going to talk a little bit about these commands

51
00:01:53,490 --> 00:01:56,280
in a little bit more depth as we go through this lesson.

52
00:01:56,280 --> 00:01:59,580
Now, the first one I want to talk about is lsmod.

53
00:01:59,580 --> 00:02:01,890
The lsmod command is used to display

54
00:02:01,890 --> 00:02:04,950
the currently loaded kernel modules, their sizes,

55
00:02:04,950 --> 00:02:07,980
their usage details and their dependent modules.

56
00:02:07,980 --> 00:02:11,430
The modinfo command is going to be used to display information

57
00:02:11,430 --> 00:02:13,410
about a particular kernel module,

58
00:02:13,410 --> 00:02:16,140
such as the file name of the module, the license,

59
00:02:16,140 --> 00:02:18,330
the description, the author's name,

60
00:02:18,330 --> 00:02:21,060
the module version number, the dependent modules

61
00:02:21,060 --> 00:02:23,160
and other parameters or attributes.

62
00:02:23,160 --> 00:02:25,140
To run this command simply type in

63
00:02:25,140 --> 00:02:28,590
modinfo, options and the module name.

64
00:02:28,590 --> 00:02:30,390
Another command that's important to know

65
00:02:30,390 --> 00:02:33,780
is insmod, which is used to install a module

66
00:02:33,780 --> 00:02:35,850
into the currently running kernel.

67
00:02:35,850 --> 00:02:38,460
This command inserts only the specified module

68
00:02:38,460 --> 00:02:41,490
and does not insert any dependent modules though.

69
00:02:41,490 --> 00:02:45,750
To use this type in insmod and the module name.

70
00:02:45,750 --> 00:02:48,630
The rmmod command is going to be used to remove a module

71
00:02:48,630 --> 00:02:50,490
from the currently running kernel.

72
00:02:50,490 --> 00:02:54,900
To run this command type rmmod and the module name.

73
00:02:54,900 --> 00:02:58,080
Another command we want to talk about is modprobe.

74
00:02:58,080 --> 00:03:01,140
modprobe is a command that's used to add or remove modules

75
00:03:01,140 --> 00:03:02,430
from the kernel.

76
00:03:02,430 --> 00:03:05,640
This command is capable of loading all the dependent modules

77
00:03:05,640 --> 00:03:08,100
before inserting the specified module though,

78
00:03:08,100 --> 00:03:09,990
making it easier to use.

79
00:03:09,990 --> 00:03:14,990
Therefore, we prefer to use this instead of insmod or rmmod.

80
00:03:15,210 --> 00:03:17,400
To add modules using the modprobe,

81
00:03:17,400 --> 00:03:20,790
simply type in the -a option and specify the modules

82
00:03:20,790 --> 00:03:21,900
you want to add.

83
00:03:21,900 --> 00:03:23,670
If you want to unload a module

84
00:03:23,670 --> 00:03:25,440
you're going to use the -r option

85
00:03:25,440 --> 00:03:27,750
to remove the specified modules.

86
00:03:27,750 --> 00:03:30,090
To run this, type in modprobe,

87
00:03:30,090 --> 00:03:32,610
the options and the module name.

88
00:03:32,610 --> 00:03:35,520
In addition to options for adding and removing modules,

89
00:03:35,520 --> 00:03:39,390
the modprobe command also has more options that you can use.

90
00:03:39,390 --> 00:03:42,180
You can use -f if you want to force the module

91
00:03:42,180 --> 00:03:44,100
to be inserted or removed.

92
00:03:44,100 --> 00:03:47,340
You can use -n if you need to conduct a dry run

93
00:03:47,340 --> 00:03:49,230
by making sure you look at the output results

94
00:03:49,230 --> 00:03:51,840
without actually executing operations.

95
00:03:51,840 --> 00:03:54,540
You can also use -s if you want to print errors

96
00:03:54,540 --> 00:03:57,600
to the system log rather than to the standard error.

97
00:03:57,600 --> 00:03:58,950
And you can use -v

98
00:03:58,950 --> 00:04:02,130
if you want to enable verbose mode for troubleshooting.

99
00:04:02,130 --> 00:04:03,270
In order for modprobe

100
00:04:03,270 --> 00:04:05,550
to accurately install dependent modules

101
00:04:05,550 --> 00:04:08,040
it reads the module's .dep file

102
00:04:08,040 --> 00:04:11,190
to identify how modules are linked to one another.

103
00:04:11,190 --> 00:04:13,560
The depmod command is going to be used

104
00:04:13,560 --> 00:04:15,600
to update the database of dependencies,

105
00:04:15,600 --> 00:04:18,750
so the modprobe can actually function properly.

106
00:04:18,750 --> 00:04:21,720
The depmod or dependent modules command

107
00:04:21,720 --> 00:04:26,610
will search the contents of /lib/modules for each module.

108
00:04:26,610 --> 00:04:28,860
Any module can export a symbol

109
00:04:28,860 --> 00:04:31,800
indicating they can provide a service to other modules.

110
00:04:31,800 --> 00:04:36,090
Then the depmod tool can build the module's .dep file

111
00:04:36,090 --> 00:04:38,550
by aggregating all the instances of symbols

112
00:04:38,550 --> 00:04:40,740
that are being exported and used.

113
00:04:40,740 --> 00:04:45,540
To run the command, simply type in depmod with the options.

114
00:04:45,540 --> 00:04:48,720
The modprobe command can also add or remove modules.

115
00:04:48,720 --> 00:04:50,790
And because the modules have dependencies,

116
00:04:50,790 --> 00:04:52,830
we need a way of specifying what options

117
00:04:52,830 --> 00:04:54,930
are to be used with these modules.

118
00:04:54,930 --> 00:04:58,020
All the .conf files or config files

119
00:04:58,020 --> 00:05:03,020
are going to be located inside the /etc/modprobe.d directory.

120
00:05:03,930 --> 00:05:06,120
This is a directory extension that specifies

121
00:05:06,120 --> 00:05:09,210
the options required for the various modules.

122
00:05:09,210 --> 00:05:11,160
You can also use the alias command,

123
00:05:11,160 --> 00:05:14,640
which is alias the alternative name and the module name

124
00:05:14,640 --> 00:05:16,590
and it will specify an alternative name

125
00:05:16,590 --> 00:05:18,660
for a module with a long name.

126
00:05:18,660 --> 00:05:20,908
For example, if I had something that had a really long name

127
00:05:20,908 --> 00:05:23,640
and I just wanted to call it, Jason, I could do that.

128
00:05:23,640 --> 00:05:25,620
We can also use the blacklist command

129
00:05:25,620 --> 00:05:28,500
which is used by typing blacklist and the module name

130
00:05:28,500 --> 00:05:30,690
to ignore any internal aliases,

131
00:05:30,690 --> 00:05:33,660
which occurs when a module defines its own aliases.

132
00:05:33,660 --> 00:05:35,280
And if we want to use the install

133
00:05:35,280 --> 00:05:37,710
we can do install module name and command

134
00:05:37,710 --> 00:05:39,480
to run the specified command

135
00:05:39,480 --> 00:05:42,390
without inserting the module into the kernel.

136
00:05:42,390 --> 00:05:45,600
In addition to loading modules into the kernel at runtime

137
00:05:45,600 --> 00:05:47,850
you can also change some of the kernel's parameters

138
00:05:47,850 --> 00:05:49,260
while it's running.

139
00:05:49,260 --> 00:05:52,110
You can use these parameters to improve system performance

140
00:05:52,110 --> 00:05:55,260
hardening your security, configure networking limitations,

141
00:05:55,260 --> 00:05:58,080
change virtual memory settings and much more.

142
00:05:58,080 --> 00:06:02,400
To do this you're going to go into the /proc/sys directory.

143
00:06:02,400 --> 00:06:04,500
This directory lists all the parameters

144
00:06:04,500 --> 00:06:06,840
that you can configure on your system.

145
00:06:06,840 --> 00:06:09,180
Like the directories containing kernel modules,

146
00:06:09,180 --> 00:06:13,350
this /proc/sys directory is divided into several categories,

147
00:06:13,350 --> 00:06:15,450
including crypto which includes parameters

148
00:06:15,450 --> 00:06:18,510
related to encryption and other cryptographic services.

149
00:06:18,510 --> 00:06:20,640
debug, which includes parameters related to

150
00:06:20,640 --> 00:06:22,080
debugging the kernel,

151
00:06:22,080 --> 00:06:24,090
dev, which includes parameters related to

152
00:06:24,090 --> 00:06:25,890
specific hardware devices,

153
00:06:25,890 --> 00:06:29,460
fs, which includes parameters related to file system data,

154
00:06:29,460 --> 00:06:31,380
kernel, which includes parameters related to

155
00:06:31,380 --> 00:06:33,210
miscellaneous kernel functions,

156
00:06:33,210 --> 00:06:34,770
net, which includes parameters

157
00:06:34,770 --> 00:06:36,660
related to networking functions,

158
00:06:36,660 --> 00:06:38,700
user, which includes parameters related to

159
00:06:38,700 --> 00:06:40,290
user space limitations,

160
00:06:40,290 --> 00:06:42,660
and vm, which includes parameters related to

161
00:06:42,660 --> 00:06:44,880
virtual memory management.

162
00:06:44,880 --> 00:06:47,340
Another command that's really powerful in Linux

163
00:06:47,340 --> 00:06:49,230
is the sysctl command.

164
00:06:49,230 --> 00:06:52,800
This is spelled S-Y-S-C-T-L.

165
00:06:52,800 --> 00:06:54,540
This is a powerful Linux command,

166
00:06:54,540 --> 00:06:57,030
which acts as an interface to dynamically change

167
00:06:57,030 --> 00:06:58,590
the kernel parameters.

168
00:06:58,590 --> 00:07:00,060
With the help of this command

169
00:07:00,060 --> 00:07:01,740
you can modify the kernel parameters

170
00:07:01,740 --> 00:07:05,640
without recompiling the kernel or rebooting your machine.

171
00:07:05,640 --> 00:07:07,680
The parameters available for modification

172
00:07:07,680 --> 00:07:11,760
can all be found under the /proc/sys directory.

173
00:07:11,760 --> 00:07:13,680
You can also view a parameter value

174
00:07:13,680 --> 00:07:16,500
by displaying the content of the appropriate file.

175
00:07:16,500 --> 00:07:19,680
It has various options, including things like -a

176
00:07:19,680 --> 00:07:21,570
which is used to display all the parameters

177
00:07:21,570 --> 00:07:23,140
and their current values,

178
00:07:23,140 --> 00:07:25,830
w parameter equals value,

179
00:07:25,830 --> 00:07:28,350
which is going to be used to set a parameter value

180
00:07:28,350 --> 00:07:30,000
inside of the kernel.

181
00:07:30,000 --> 00:07:33,120
You also can use option -p and a file name

182
00:07:33,120 --> 00:07:36,360
to load the sysctl settings from a specified file

183
00:07:36,360 --> 00:07:40,410
or the /etc/sysctl.conf file,

184
00:07:40,410 --> 00:07:42,720
if no file name is being provided.

185
00:07:42,720 --> 00:07:45,900
If you use -e, you can use this to ignore errors

186
00:07:45,900 --> 00:07:47,580
about unknown keys.

187
00:07:47,580 --> 00:07:50,280
If you use the option -r with a pattern

188
00:07:50,280 --> 00:07:52,680
you can use this to apply a command to a parameter

189
00:07:52,680 --> 00:07:54,300
that matches a given pattern,

190
00:07:54,300 --> 00:07:56,940
if you're using an extended regular expression.

191
00:07:56,940 --> 00:07:58,680
To run the sysctl command,

192
00:07:58,680 --> 00:08:02,100
you're going to type S-Y-S-C-T-L and the options

193
00:08:02,100 --> 00:08:03,660
on the command line.

194
00:08:03,660 --> 00:08:07,440
The /etc/sysctl.conf file

195
00:08:07,440 --> 00:08:09,240
is going to enable configuration changes

196
00:08:09,240 --> 00:08:11,130
to your running Linux kernel.

197
00:08:11,130 --> 00:08:13,560
These changes can include all sorts of improvements

198
00:08:13,560 --> 00:08:15,750
to your networking, security configurations

199
00:08:15,750 --> 00:08:17,640
or logging of information.

200
00:08:17,640 --> 00:08:18,660
So as you can see,

201
00:08:18,660 --> 00:08:21,000
it's really important to understand these modules

202
00:08:21,000 --> 00:08:22,260
and how to modify them,

203
00:08:22,260 --> 00:08:25,260
because they bring a lot of capability to your Linux system.

