1
00:00:00,360 --> 00:00:01,410
In this lesson,

2
00:00:01,410 --> 00:00:03,469
we're going to cover the concept of services

3
00:00:03,469 --> 00:00:05,520
in the Linux operating system.

4
00:00:05,520 --> 00:00:07,920
Now, services are programs or processes

5
00:00:07,920 --> 00:00:10,170
that run in your servers at all times,

6
00:00:10,170 --> 00:00:12,150
usually, from the time you boot up the server

7
00:00:12,150 --> 00:00:13,740
until you shut it down.

8
00:00:13,740 --> 00:00:16,560
These services are used to provide persistent support

9
00:00:16,560 --> 00:00:17,880
for requests and monitoring

10
00:00:17,880 --> 00:00:21,030
from other processes or from external clients.

11
00:00:21,030 --> 00:00:23,760
Some examples of well-known services are Apache

12
00:00:23,760 --> 00:00:24,960
for running a web server

13
00:00:24,960 --> 00:00:27,540
and Postfix for running an email server.

14
00:00:27,540 --> 00:00:29,730
Just like any other components of Linux though,

15
00:00:29,730 --> 00:00:31,770
services can be managed as well.

16
00:00:31,770 --> 00:00:33,360
If you're a system administrator,

17
00:00:33,360 --> 00:00:35,790
you're going to be dealing with services regularly.

18
00:00:35,790 --> 00:00:37,710
If you're a normal desktop user though,

19
00:00:37,710 --> 00:00:39,360
you may come across the need to restart

20
00:00:39,360 --> 00:00:43,380
or perform operations to run services as things go wrong.

21
00:00:43,380 --> 00:00:46,200
In this lesson, you're going to learn how to start, stop,

22
00:00:46,200 --> 00:00:49,500
and restart services in any Linux distribution.

23
00:00:49,500 --> 00:00:52,590
Now, most services are implemented in the form of daemons,

24
00:00:52,590 --> 00:00:55,350
which are running programs that operate in the background

25
00:00:55,350 --> 00:00:57,270
without the need for human intervention,

26
00:00:57,270 --> 00:00:59,430
which means that daemons are going to lie dormant

27
00:00:59,430 --> 00:01:00,810
until an event triggers them

28
00:01:00,810 --> 00:01:03,450
to take some kind of function or activity.

29
00:01:03,450 --> 00:01:05,850
Now, the init system is the first process

30
00:01:05,850 --> 00:01:07,680
that starts outside of the kernel,

31
00:01:07,680 --> 00:01:09,810
and it's generally going to be the backend service

32
00:01:09,810 --> 00:01:11,940
that controls when and how other services

33
00:01:11,940 --> 00:01:13,470
are going to be started.

34
00:01:13,470 --> 00:01:16,290
The init daemon refers to a configuration file

35
00:01:16,290 --> 00:01:19,530
and initiates the processes that are listed in that file.

36
00:01:19,530 --> 00:01:20,880
This prepares your system

37
00:01:20,880 --> 00:01:23,100
to run the required software for you.

38
00:01:23,100 --> 00:01:25,230
The init daemon is going to run continuously

39
00:01:25,230 --> 00:01:27,150
until the system is powered off.

40
00:01:27,150 --> 00:01:30,090
And the programs on your system are not able to run

41
00:01:30,090 --> 00:01:31,413
without the init daemon.

42
00:01:32,280 --> 00:01:34,650
In Linux, you can initialize the system

43
00:01:34,650 --> 00:01:37,530
through SysVinit and systemd.

44
00:01:37,530 --> 00:01:38,760
The method that's going to be active

45
00:01:38,760 --> 00:01:40,740
on your Linux system is going to affect

46
00:01:40,740 --> 00:01:43,650
how you manage services on that system.

47
00:01:43,650 --> 00:01:46,980
For example, if you use the systemd software suite,

48
00:01:46,980 --> 00:01:48,690
it's going to provide for an init method

49
00:01:48,690 --> 00:01:50,340
for initializing the system.

50
00:01:50,340 --> 00:01:53,100
It's also going to provide for tools for managing services

51
00:01:53,100 --> 00:01:56,310
on that system that derive from the init daemon.

52
00:01:56,310 --> 00:01:58,860
The systemd suite was designed as a replacement

53
00:01:58,860 --> 00:02:01,380
for other methods like SysVinit

54
00:02:01,380 --> 00:02:03,900
and is now the dominant init method that's used

55
00:02:03,900 --> 00:02:05,970
in modern Linux distributions.

56
00:02:05,970 --> 00:02:08,729
The systemd suite is going to offer several improvements

57
00:02:08,729 --> 00:02:10,590
over older methods as well.

58
00:02:10,590 --> 00:02:13,110
For example, it supports parallelization,

59
00:02:13,110 --> 00:02:16,380
like starting programs at the same time for quicker boot up

60
00:02:16,380 --> 00:02:18,360
and reducing shell overhead.

61
00:02:18,360 --> 00:02:22,200
In systemd, control groups are also used to track processes

62
00:02:22,200 --> 00:02:24,990
instead of process IDs, or PIDs,

63
00:02:24,990 --> 00:02:26,610
which provides a better isolation

64
00:02:26,610 --> 00:02:29,580
and categorization for all of your processes.

65
00:02:29,580 --> 00:02:32,010
Now, systemd is going to use unit files

66
00:02:32,010 --> 00:02:34,110
to determine how it's going to handle units,

67
00:02:34,110 --> 00:02:37,710
which are system resources that systemd can actually manage.

68
00:02:37,710 --> 00:02:40,260
Resources can include network services,

69
00:02:40,260 --> 00:02:42,990
block storage devices, peripheral devices,

70
00:02:42,990 --> 00:02:45,210
file systems, and many others.

71
00:02:45,210 --> 00:02:46,530
Daemons are going to access

72
00:02:46,530 --> 00:02:48,990
and manage all of these different resources for you,

73
00:02:48,990 --> 00:02:50,850
and unit files can also be used

74
00:02:50,850 --> 00:02:53,730
to set system environment variables and parameters

75
00:02:53,730 --> 00:02:55,500
as well as which values are going to be passed

76
00:02:55,500 --> 00:02:58,980
from a parent process to a child process that it creates.

77
00:02:58,980 --> 00:03:01,380
By adding directives for an environment variable,

78
00:03:01,380 --> 00:03:03,420
you're going to be able to make it easier for a service

79
00:03:03,420 --> 00:03:06,930
or other unit to work with custom values as well.

80
00:03:06,930 --> 00:03:09,210
In systemd, targets are a method

81
00:03:09,210 --> 00:03:12,600
of grouping unit configuration files together, typically,

82
00:03:12,600 --> 00:03:15,690
in order to represent specific modes of operations.

83
00:03:15,690 --> 00:03:18,210
Each dot target file is going to be used

84
00:03:18,210 --> 00:03:20,070
to determine one of several ways

85
00:03:20,070 --> 00:03:21,660
in which the system can operate,

86
00:03:21,660 --> 00:03:24,060
such as running with just a command line environment,

87
00:03:24,060 --> 00:03:26,220
running with a graphical desktop environment,

88
00:03:26,220 --> 00:03:29,250
initiating a system shutdown, and many others.

89
00:03:29,250 --> 00:03:32,130
Therefore, you can activate a target in order to boot

90
00:03:32,130 --> 00:03:35,040
into a desired environment if you want to.

91
00:03:35,040 --> 00:03:36,810
Now, another command we need to talk about

92
00:03:36,810 --> 00:03:39,810
is systemctl, or system control.

93
00:03:39,810 --> 00:03:41,910
The system control command enables you

94
00:03:41,910 --> 00:03:44,370
to control the systemd init daemon,

95
00:03:44,370 --> 00:03:46,380
and you can view running services,

96
00:03:46,380 --> 00:03:48,930
enable and disable services to run at boot

97
00:03:48,930 --> 00:03:50,580
or in the current session,

98
00:03:50,580 --> 00:03:53,280
and you can determine the status of these services

99
00:03:53,280 --> 00:03:55,590
as well as managing the system target.

100
00:03:55,590 --> 00:03:57,930
You can do all of this through various subcommands

101
00:03:57,930 --> 00:04:00,930
that are issued along with the systemctl command,

102
00:04:00,930 --> 00:04:03,750
including status service, which is going to be used

103
00:04:03,750 --> 00:04:06,270
to retrieve the current status of a given service,

104
00:04:06,270 --> 00:04:09,090
enable service, which is used to enable a service

105
00:04:09,090 --> 00:04:12,120
to be started on boot, disable service,

106
00:04:12,120 --> 00:04:14,100
which is going to be used to disable a service

107
00:04:14,100 --> 00:04:17,850
so it no longer is started up on boot, start service,

108
00:04:17,850 --> 00:04:20,310
which is used to activate a service immediately,

109
00:04:20,310 --> 00:04:22,200
stop service, which is used

110
00:04:22,200 --> 00:04:25,770
to deactivate a service immediately, restart service,

111
00:04:25,770 --> 00:04:28,170
which is used to restart a service immediately,

112
00:04:28,170 --> 00:04:31,560
set-default target, which is used to set the default target

113
00:04:31,560 --> 00:04:33,780
for the system to be used on boot,

114
00:04:33,780 --> 00:04:36,720
isolate target, which is used to force the system

115
00:04:36,720 --> 00:04:40,680
to immediately change the provided target, mask unit file,

116
00:04:40,680 --> 00:04:42,870
which is used to prevent the provided unit file

117
00:04:42,870 --> 00:04:44,940
from being enabled or activated

118
00:04:44,940 --> 00:04:48,240
even when it's done manually, and daemon-reload,

119
00:04:48,240 --> 00:04:51,030
which is used to reload the systemd init daemon,

120
00:04:51,030 --> 00:04:53,220
including all of the unit files.

121
00:04:53,220 --> 00:04:55,140
To run the system control command,

122
00:04:55,140 --> 00:04:59,700
type in systemctl, space, the options, space,

123
00:04:59,700 --> 00:05:02,850
the subcommand, space, and the arguments.

124
00:05:02,850 --> 00:05:04,140
The system control command

125
00:05:04,140 --> 00:05:06,240
has many different options you can use,

126
00:05:06,240 --> 00:05:10,110
including -t unit file type, which is an option that's used

127
00:05:10,110 --> 00:05:13,800
to specify the unit file type to perform the operation on.

128
00:05:13,800 --> 00:05:16,560
The -a option is used to list all the unit files

129
00:05:16,560 --> 00:05:18,930
or properties regardless of their state.

130
00:05:18,930 --> 00:05:21,360
The dash no-reload option is used

131
00:05:21,360 --> 00:05:23,670
to prevent the reloading of configuration changes

132
00:05:23,670 --> 00:05:25,980
when enabling or disabling a service.

133
00:05:25,980 --> 00:05:29,280
The --no-ask-password option is used

134
00:05:29,280 --> 00:05:31,620
to prevent users from being asked to authenticate

135
00:05:31,620 --> 00:05:34,110
when they're performing privileged operations.

136
00:05:34,110 --> 00:05:36,810
The --runtime option is going to be used

137
00:05:36,810 --> 00:05:38,310
to make changes temporary,

138
00:05:38,310 --> 00:05:41,790
this way they won't be present after you reboot the system.

139
00:05:41,790 --> 00:05:44,790
The -H remote host option is going to be used

140
00:05:44,790 --> 00:05:47,220
to execute the operation on a remote host

141
00:05:47,220 --> 00:05:50,220
that's specified by its host name or IP address.

142
00:05:50,220 --> 00:05:53,880
And the --no-pager option is used to prevent the output

143
00:05:53,880 --> 00:05:56,460
from being piped into a paging utility.

144
00:05:56,460 --> 00:05:58,290
There are lots of different instances

145
00:05:58,290 --> 00:06:00,810
when a system administrator needs to work in a GUI

146
00:06:00,810 --> 00:06:02,400
for a short period of time,

147
00:06:02,400 --> 00:06:05,400
and you can do this by using the systemctl command

148
00:06:05,400 --> 00:06:07,020
to change your target.

149
00:06:07,020 --> 00:06:09,210
Now, it's common to configure a Linux server

150
00:06:09,210 --> 00:06:12,000
to boot up by default to the command line environment.

151
00:06:12,000 --> 00:06:15,750
And you do this by using the multi-user dot target file.

152
00:06:15,750 --> 00:06:17,670
An administrator though might want to switch

153
00:06:17,670 --> 00:06:19,380
to the graphical user environment,

154
00:06:19,380 --> 00:06:22,200
which is known as the graphical dot target file.

155
00:06:22,200 --> 00:06:24,390
And we can do this for a particular task.

156
00:06:24,390 --> 00:06:26,340
And then we can switch the server back

157
00:06:26,340 --> 00:06:30,600
to the multi-user dot target file when the task is complete.

158
00:06:30,600 --> 00:06:32,790
The system administrator would then leave the server

159
00:06:32,790 --> 00:06:35,190
in a more efficient command line configuration

160
00:06:35,190 --> 00:06:36,720
after the task is completed,

161
00:06:36,720 --> 00:06:39,780
which lowers the resources required to run that server

162
00:06:39,780 --> 00:06:42,060
instead of leaving that graphical user interface

163
00:06:42,060 --> 00:06:43,350
up and running.

164
00:06:43,350 --> 00:06:46,110
In most cases, the systemctl command

165
00:06:46,110 --> 00:06:48,090
is going to be used to control services,

166
00:06:48,090 --> 00:06:50,730
but there are some additional systemd commands

167
00:06:50,730 --> 00:06:52,530
that you can also use too.

168
00:06:52,530 --> 00:06:55,620
For example, hostnamectl will enable you

169
00:06:55,620 --> 00:06:57,600
to view the system's network host name

170
00:06:57,600 --> 00:07:00,270
and other information about the system's hardware

171
00:07:00,270 --> 00:07:02,100
and the Linux kernel it's running.

172
00:07:02,100 --> 00:07:03,300
You can also use the command

173
00:07:03,300 --> 00:07:05,190
to change the system's host name.

174
00:07:05,190 --> 00:07:09,000
To run this command, type hostnamectl, the options,

175
00:07:09,000 --> 00:07:11,100
the subcommand, and the arguments.

176
00:07:11,100 --> 00:07:15,060
For example, if you want to set your host name to server01,

177
00:07:15,060 --> 00:07:20,060
you need to enter hostnamectl, space, set-hostname, space,

178
00:07:20,790 --> 00:07:24,870
server01 at the command line, and then hit enter.

179
00:07:24,870 --> 00:07:27,210
Now, earlier I mentioned System V init,

180
00:07:27,210 --> 00:07:28,920
which is an older init method

181
00:07:28,920 --> 00:07:31,620
that has largely been replaced by systemd.

182
00:07:31,620 --> 00:07:33,420
However, some distributions

183
00:07:33,420 --> 00:07:35,940
will still support System V init.

184
00:07:35,940 --> 00:07:37,410
Just like systemd,

185
00:07:37,410 --> 00:07:40,020
SysVinit is going to provide you with various tools

186
00:07:40,020 --> 00:07:42,720
to manage services and the state of the system.

187
00:07:42,720 --> 00:07:45,360
Now, one of the major differences between systemd

188
00:07:45,360 --> 00:07:49,590
and System V init is that System V init uses runlevels.

189
00:07:49,590 --> 00:07:51,720
They control the state of the operating system

190
00:07:51,720 --> 00:07:55,140
in much the same way that systemd targets are going to be used.

191
00:07:55,140 --> 00:07:56,160
And they're going to determine

192
00:07:56,160 --> 00:07:58,260
what type of daemons should be running

193
00:07:58,260 --> 00:08:01,560
in order to create a specific type of environment for you.

194
00:08:01,560 --> 00:08:04,350
In fact, the systemd targets were actually created

195
00:08:04,350 --> 00:08:06,480
to map the existing runlevels.

196
00:08:06,480 --> 00:08:08,820
When we use something like runlevel zero,

197
00:08:08,820 --> 00:08:10,530
this shuts down the system.

198
00:08:10,530 --> 00:08:13,800
Runlevel one starts up a single-user mode.

199
00:08:13,800 --> 00:08:16,440
Runlevel two starts up a multi-user mode

200
00:08:16,440 --> 00:08:18,030
without remote networking.

201
00:08:18,030 --> 00:08:20,490
Runlevel three starts multi-user mode

202
00:08:20,490 --> 00:08:22,020
with remote networking.

203
00:08:22,020 --> 00:08:24,418
Runlevel four is not really used.

204
00:08:24,418 --> 00:08:27,030
Runlevel five though starts multi-user mode

205
00:08:27,030 --> 00:08:30,360
with networking and a graphical user interface capability,

206
00:08:30,360 --> 00:08:33,330
and runlevel six is going to reboot your system.

207
00:08:33,330 --> 00:08:36,270
Now, a single-user mode is going to boot the operating system

208
00:08:36,270 --> 00:08:39,900
into an environment where the superuser must log in.

209
00:08:39,900 --> 00:08:42,630
This means you're logging in as the root user.

210
00:08:42,630 --> 00:08:45,600
Networking here is disabled in single-user mode,

211
00:08:45,600 --> 00:08:48,180
and most partitions are not going to be mounted.

212
00:08:48,180 --> 00:08:50,730
Typically, this is going to be used to troubleshoot issues

213
00:08:50,730 --> 00:08:52,740
with networking or issues that prevent you

214
00:08:52,740 --> 00:08:55,740
from booting into your normal multi-user environment.

215
00:08:55,740 --> 00:08:58,200
The telinit command will enable you to switch

216
00:08:58,200 --> 00:09:00,570
to the current runlevel of the system.

217
00:09:00,570 --> 00:09:02,850
Now, on the systemd environments,

218
00:09:02,850 --> 00:09:05,190
the telinit command is going to be translated

219
00:09:05,190 --> 00:09:08,520
into the appropriate target request for you automatically.

220
00:09:08,520 --> 00:09:10,800
The runlevel command will print the previous

221
00:09:10,800 --> 00:09:12,960
and current runlevels of the system,

222
00:09:12,960 --> 00:09:15,540
each one being separated by a space.

223
00:09:15,540 --> 00:09:17,820
So to use the telinit command,

224
00:09:17,820 --> 00:09:22,820
simply type in T-E-L-I-N-I-T, the options, and the runlevel.

225
00:09:23,700 --> 00:09:26,130
Now, there is a special file on your system

226
00:09:26,130 --> 00:09:28,680
if you're using a SysVinit system.

227
00:09:28,680 --> 00:09:33,680
This is known as the slash E-T-C slash I-N-I-T-T-A-B file.

228
00:09:34,290 --> 00:09:35,850
This is the inittab file,

229
00:09:35,850 --> 00:09:38,430
and it's going to store details of various processes

230
00:09:38,430 --> 00:09:42,540
related to your system initialization on a SysVinit system.

231
00:09:42,540 --> 00:09:43,980
It's also going to store details

232
00:09:43,980 --> 00:09:46,290
of the different runlevels that are in use.

233
00:09:46,290 --> 00:09:48,720
The init daemon is going to read from this file

234
00:09:48,720 --> 00:09:51,030
to determine what runlevel to boot into,

235
00:09:51,030 --> 00:09:52,380
what daemons to start,

236
00:09:52,380 --> 00:09:55,020
and what to do if the runlevel changes.

237
00:09:55,020 --> 00:10:00,020
Every entry inside the /etc/inittab file takes the format

238
00:10:00,090 --> 00:10:04,990
of id:rstate:action:process.

239
00:10:06,210 --> 00:10:09,600
The id is just a unique identifier for that entry.

240
00:10:09,600 --> 00:10:13,260
The rstate defines what runlevels they're going to apply to.

241
00:10:13,260 --> 00:10:16,080
And the action specifies one of several tasks

242
00:10:16,080 --> 00:10:19,230
that determine how SysVinit is going to handle the command

243
00:10:19,230 --> 00:10:21,660
that's defined in the process field.

244
00:10:21,660 --> 00:10:25,260
The /etc/init.d directory is going to store

245
00:10:25,260 --> 00:10:28,560
all of your initialization scripts for all of your services.

246
00:10:28,560 --> 00:10:31,010
These scripts control the initiation of services

247
00:10:31,010 --> 00:10:32,260
in a particular runlevel.

248
00:10:33,100 --> 00:10:37,110
The scripts are going to be invoked from the /etc/inittab file

249
00:10:37,110 --> 00:10:39,330
when the system initialization begins

250
00:10:39,330 --> 00:10:43,080
using different symbolic links that are found in that file.

251
00:10:43,080 --> 00:10:45,660
SysVinit scripts are highly flexible

252
00:10:45,660 --> 00:10:47,370
and can be configured according to the needs

253
00:10:47,370 --> 00:10:48,990
of any given user.

254
00:10:48,990 --> 00:10:53,580
The /etc/rc.local file is also going to be executed

255
00:10:53,580 --> 00:10:55,740
at the end of the init boot process.

256
00:10:55,740 --> 00:10:58,860
Typically, it's going to be used to start custom services.

257
00:10:58,860 --> 00:11:01,290
However, this file is rarely used

258
00:11:01,290 --> 00:11:03,960
and is not even supported in some distributions

259
00:11:03,960 --> 00:11:06,480
that still use SysVinit.

260
00:11:06,480 --> 00:11:08,130
Now, another command we need to talk about

261
00:11:08,130 --> 00:11:10,380
is the check configuration command.

262
00:11:10,380 --> 00:11:14,850
This is spelled C-H-K-C-O-N-F-I-G.

263
00:11:14,850 --> 00:11:16,890
The check configuration command can be used

264
00:11:16,890 --> 00:11:19,380
to control services in each runlevel.

265
00:11:19,380 --> 00:11:21,690
It's also used to start or stop services

266
00:11:21,690 --> 00:11:23,550
during system startup.

267
00:11:23,550 --> 00:11:25,710
Some subcommands and options that can be used

268
00:11:25,710 --> 00:11:27,360
with the check configuration command

269
00:11:27,360 --> 00:11:30,450
to control your services include service on.

270
00:11:30,450 --> 00:11:32,850
This is an option which is used to enable a service

271
00:11:32,850 --> 00:11:34,470
to be started on boot.

272
00:11:34,470 --> 00:11:36,510
Another one is service off.

273
00:11:36,510 --> 00:11:38,790
This option is used to disable a service

274
00:11:38,790 --> 00:11:40,830
so it's no longer started on boot.

275
00:11:40,830 --> 00:11:43,140
Service reset is an option that's used

276
00:11:43,140 --> 00:11:45,330
to reset the status of a service,

277
00:11:45,330 --> 00:11:48,030
and the --level runlevel option is used

278
00:11:48,030 --> 00:11:49,500
to specify the runlevel

279
00:11:49,500 --> 00:11:52,080
in which to enable or disable a service.

280
00:11:52,080 --> 00:11:57,080
To use this command, simply type in C-H-K-C-O-N-F-I-G,

281
00:11:57,240 --> 00:11:59,583
the options, the service, and the subcommand.

282
00:12:00,570 --> 00:12:03,210
Now, another command we have is the service command.

283
00:12:03,210 --> 00:12:06,180
This is another way to control the SysVinit services

284
00:12:06,180 --> 00:12:08,280
through the SysVinit scripts.

285
00:12:08,280 --> 00:12:10,650
It's going to support various common subcommands

286
00:12:10,650 --> 00:12:12,240
including service status,

287
00:12:12,240 --> 00:12:14,880
which is used to print the current state of a service,

288
00:12:14,880 --> 00:12:18,090
service start, which is used to start a service immediately,

289
00:12:18,090 --> 00:12:21,030
service stop to stop a service immediately,

290
00:12:21,030 --> 00:12:24,060
service restart to restart a service immediately,

291
00:12:24,060 --> 00:12:27,630
and service reload to reread a service's configuration files

292
00:12:27,630 --> 00:12:29,490
while the service remains running.

293
00:12:29,490 --> 00:12:32,430
To run the service command, simply type in service,

294
00:12:32,430 --> 00:12:34,743
the options, the service, and the subcommand.

