< Return to Video

CompTIA Linux+: Managing Services #11

  • 0:00 - 0:03
    [Music].
  • 0:03 - 0:06
    You're watching ITPRO.TV.
  • 0:06 - 0:08
    Hello. I think you're watching ITPRO.TV,
  • 0:08 - 0:10
    helping you level up with IT learning
  • 0:10 - 0:12
    everywhere you go. I'm Zach Memos, your
  • 0:12 - 0:14
    host for this episode of CompTIA Linux
  • 0:14 - 0:19
    Plus. And in this episode, we're managing services with Systemd.
  • 0:19 - 0:21
    And we've asked our expert, Don Pezet,
  • 0:21 - 0:22
    to tell us more. Don, good to see you.
  • 0:22 - 0:24
    Thank you for being here. Glad to be here,
  • 0:24 - 0:28
    Zach. I'm ready to dive into the world of Systemd, which is a really powerful
  • 0:28 - 0:30
    new technology. I'm going to say "new."
  • 0:30 - 0:32
    It's actually been around since, I think, 2008,
  • 0:32 - 0:33
    but really gained prominence in the last
  • 0:33 - 0:36
    five to six years. It is responsible
  • 0:36 - 0:38
    for powering the majority of Linux
  • 0:38 - 0:40
    distros that are out there. So it's really
  • 0:40 - 0:41
    important that we learn how it
  • 0:41 - 0:43
    works, what it does, and why--because
  • 0:43 - 0:44
    everything it's doing is behind the
  • 0:44 - 0:46
    scenes. So in this episode, what we're
  • 0:46 - 0:47
    going to do is kind of pull back the
  • 0:47 - 0:49
    curtain and get a chance to see exactly
  • 0:49 - 0:52
    what Systemd is like, how it functions, and
  • 0:52 - 0:53
    how we can interact with it to keep our
  • 0:53 - 0:55
    system up and running and make sure our
  • 0:55 - 0:56
    services are operating properly. Well, how
  • 0:56 - 0:59
    are services controlled under Linux?
  • 0:59 - 1:01
    So, you know, there are a few different ways
  • 1:01 - 1:03
    this stuff can be controlled. And we're
  • 1:03 - 1:05
    going to focus on Systemd, but it's not
  • 1:05 - 1:06
    the only way. There are many
  • 1:06 - 1:09
    systems that use the original Unix init
  • 1:09 - 1:11
    system. So, you know, when Linux boots up,
  • 1:11 - 1:12
    there's a kernel. Right? And the Linux
  • 1:12 - 1:13
    kernel gets called. That's really
  • 1:13 - 1:15
    important. That's the part that Linus
  • 1:15 - 1:18
    Torvalds wrote. But the kernel itself
  • 1:18 - 1:20
    doesn't interact with the user much.
  • 1:20 - 1:22
    It's kind of limited in what it can do.
  • 1:22 - 1:24
    So it's actually--like, I don't wanna say
  • 1:24 - 1:26
    it's not impressive--but from a
  • 1:26 - 1:27
    standpoint of computer technology, it's
  • 1:27 - 1:30
    very impressive. But from a standpoint of human interaction, it's not very
  • 1:30 - 1:32
    impressive at all. So what the kernel
  • 1:32 - 1:35
    does is, as soon as it fires up, the next
  • 1:35 - 1:37
    thing that it does is it launches what's
  • 1:37 - 1:39
    called an init process. Right?
  • 1:39 - 1:42
    And the init process is what actually starts all
  • 1:42 - 1:43
    the various services and makes your
  • 1:43 - 1:46
    operating system work. It seems super
  • 1:46 - 1:48
    impressive to us, but, technologically,
  • 1:48 - 1:50
    it's really basic. In fact, in the original
  • 1:50 - 1:52
    versions of Unix, they used what was just
  • 1:52 - 1:55
    called the SysV init. The SysV--System V--
  • 1:55 - 1:57
    was, you know, for tracking one of their releases.
  • 1:57 - 2:00
    And the SysV init--what it would
  • 2:00 - 2:02
    do is, it just had a folder with a bunch
  • 2:02 - 2:03
    of scripts in it, and it would run the
  • 2:03 - 2:05
    scripts one after the other, and that
  • 2:05 - 2:07
    would start your system up. And then, when
  • 2:07 - 2:08
    you got to a login prompt, it was because,
  • 2:08 - 2:10
    at some stage, a script was run that
  • 2:10 - 2:13
    launched a shell or a console and then
  • 2:13 - 2:14
    made that available to you.
  • 2:14 - 2:16
    That's all it was. Right?
  • 2:16 - 2:18
    Well, that sounds really simple--because it was
  • 2:18 - 2:20
    really simple--and it had certain
  • 2:20 - 2:22
    drawbacks, certain problems, certain
  • 2:22 - 2:23
    things that made it not efficient.
  • 2:23 - 2:25
    The number one problem
  • 2:25 - 2:28
    was that SysV init ran everything in
  • 2:28 - 2:31
    a serial order, one after the other.
  • 2:31 - 2:33
    So it would run script one, then script two,
  • 2:33 - 2:35
    then script three. And if script three
  • 2:35 - 2:37
    took a long time or if it hung, the
  • 2:37 - 2:39
    system just waited right there. Right?
  • 2:39 - 2:41
    So it wasn't terribly fast. So that was one
  • 2:41 - 2:43
    problem that it had. The other problem
  • 2:43 - 2:45
    was that the scripts were just run when
  • 2:45 - 2:47
    the system booted up. Right?
  • 2:47 - 2:50
    And after that, if something changed on the
  • 2:50 - 2:51
    system, the scripts wouldn't detect it
  • 2:51 - 2:53
    unless you recalled them. That wasn't a
  • 2:53 - 2:58
    problem back in the 1970s and in the 1980s. But fast forward to
  • 2:58 - 3:02
    2019, where we are constantly adding and removing
  • 3:02 - 3:08
    hardware from our systems. You might have USB thumb drives or webcams or whatever that plug in and go.
  • 3:08 - 3:11
    Imagine if you plugged in a USB key
  • 3:11 - 3:12
    and the system didn't even know that
  • 3:12 - 3:14
    happened. Right? And we had to run these
  • 3:14 - 3:21
    scripts and tell it to look for that missing hardware. So that is a problem with SysV init.
  • 3:21 - 3:24
    Systemd was created as the next
  • 3:24 - 3:27
    generation of that--to try and replace SysV init--and do it in a different
  • 3:27 - 3:30
    way. So what Systemd does is, it's not
  • 3:30 - 3:32
    just a series of scripts. Systemd is an
  • 3:32 - 3:34
    actual program. It's an actual daemon.
  • 3:34 - 3:36
    It actually stands for "system daemon."
  • 3:36 - 3:41
    And if you've never heard that term, it's D-A-E-M-O-N--daemon. And in the Unix and
  • 3:41 - 3:44
    Linux world, any program that runs in the
  • 3:44 - 3:46
    background as part of the operating
  • 3:46 - 3:48
    system--that runs unattended from the
  • 3:48 - 3:50
    user--is called a daemon. It's just, you
  • 3:50 - 3:53
    know, doing its whole job in the background. It actually comes from a
  • 3:53 - 3:57
    physics book that described some weird physical motion.
  • 3:57 - 3:59
    It's a crazy history. Some other book.
  • 3:59 - 4:02
    Yeah. But, basically, you know, these
  • 4:02 - 4:04
    daemons are what the system is running in
  • 4:04 - 4:05
    the background, and it used to be the
  • 4:05 - 4:07
    init scripts that would call them.
  • 4:07 - 4:10
    Now it's the Systemd executable. So this
  • 4:10 - 4:12
    program runs, and it doesn't just run
  • 4:12 - 4:14
    when the system boots up. It stays
  • 4:14 - 4:15
    running. So Systemd stays running the
  • 4:15 - 4:18
    whole time your system is online, and
  • 4:18 - 4:20
    it's able to do things like--well, one, it
  • 4:20 - 4:23
    can detect hardware changes because it's always running.
  • 4:23 - 4:26
    Two, it can do things in parallel. It can say,
  • 4:26 - 4:28
    "Alright, I'm gonna run these five
  • 4:28 - 4:30
    routines all at the same time." And if one
  • 4:30 - 4:31
    hangs, it's no big deal because it's still
  • 4:31 - 4:33
    doing the other four--and so it's faster.
  • 4:33 - 4:36
    Right? Systemd is faster, more stable.
  • 4:36 - 4:37
    There are some people out there that
  • 4:37 - 4:39
    don't like Systemd. You'll hear some
  • 4:39 - 4:40
    people complain about it from time to
  • 4:40 - 4:43
    time because it moves away from
  • 4:43 - 4:45
    tradition. Right? Traditionally, you could
  • 4:45 - 4:48
    control your entire Unix-like operating
  • 4:48 - 4:50
    system by modifying some text files.
  • 4:50 - 4:52
    With Systemd, it's not like that. It does have
  • 4:52 - 4:54
    configuration files, but it's actually
  • 4:54 - 4:55
    doing a lot of stuff in a binary.
  • 4:55 - 4:58
    And some people--if you're a Unix purist--
  • 4:58 - 4:59
    that's not something you're happy
  • 4:59 - 5:02
    about. But I'll tell you, as a Linux
  • 5:02 - 5:04
    administrator myself, I love it. I think
  • 5:04 - 5:06
    it's faster. It works really well, and
  • 5:06 - 5:09
    it's a great way to deal with things. So Systemd
  • 5:09 - 5:12
    is pretty prominent, and it's typically how we're gonna be
  • 5:12 - 5:13
    manipulating services. So going back to
  • 5:13 - 5:16
    Zach's original question--hey. How are these services controlled?
  • 5:16 - 5:22
    You power on your system. Right? Your BIOS kicks in. It reaches out to the hard drive.
  • 5:22 - 5:24
    It finds the boot files, loads up
  • 5:24 - 5:26
    that Linux kernel, then the Linux kernel
  • 5:26 - 5:28
    launches init. And in this case, it would
  • 5:28 - 5:30
    be the Systemd that it fires up. And then
  • 5:30 - 5:32
    Systemd goes through the routine of starting
  • 5:32 - 5:33
    everything else that you need to
  • 5:33 - 5:35
    interact with your system and bring it
  • 5:35 - 5:37
    online if it's supposed to. Right?
  • 5:37 - 5:38
    If there are things that aren't supposed to
  • 5:38 - 5:40
    come online, it leaves them off. Systemd
  • 5:40 - 5:41
    is what's actually controlling that.
  • 5:41 - 5:44
    So how do we know if we are actually using
  • 5:44 - 5:46
    Systemd? Alright. So, you know, good
  • 5:46 - 5:49
    question. I will say that all of the
  • 5:49 - 5:51
    major Linux distros these days use
  • 5:51 - 5:54
    Systemd. So if you get Ubuntu, you get Red
  • 5:54 - 5:57
    Hat Enterprise Linux, CentOS, Fedora, Mint--
  • 5:58 - 6:01
    it just--I mean, it goes on and on. All of the major
  • 6:01 - 6:06
    distros use Systemd by default. So the odds are, you are using Systemd.
  • 6:06 - 6:08
    But it's not 100%. There are some distros
  • 6:08 - 6:10
    out there that have chosen not to go
  • 6:10 - 6:12
    with Systemd, that are following the old
  • 6:12 - 6:14
    model--SysV init. Or there was
  • 6:14 - 6:16
    actually another init system called
  • 6:16 - 6:18
    Upstart. Upstart was really just a slight
  • 6:18 - 6:24
    modification to SysV init. It wasn't a full-on change. Mhmm.
  • 6:24 - 6:26
    So you may find some systems that do that.
  • 6:26 - 6:30
    Typically, it's if you're running older versions. Like, if you're running CentOS 7,
  • 6:30 - 6:31
    you're using Systemd. But if you're
  • 6:31 - 6:35
    running CentOS 6, you're using Upstart.
  • 6:35 - 6:37
    And if you're running CentOS 5, you're
  • 6:37 - 6:38
    running SysV init. Right? So, you know,
  • 6:38 - 6:40
    it's kind of changed over time.
  • 6:40 - 6:41
    If you're running Ubuntu, they've been
  • 6:41 - 6:43
    running Systemd for the last three or
  • 6:43 - 6:44
    four versions, I think. And then before
  • 6:44 - 6:47
    that, they were on SysV init.
  • 6:47 - 6:49
    But there are current distros like
  • 6:49 - 6:50
    Slackware. Slackware was actually the
  • 6:50 - 6:53
    very first Linux distro that I ran back
  • 6:53 - 6:57
    in 1993, 1992, 3, I think 1993. Either
  • 6:57 - 7:00
    way, it ran SysV init back then, and
  • 7:00 - 7:02
    it runs SysV init today. They have
  • 7:02 - 7:04
    not moved over to Systemd because they
  • 7:04 - 7:06
    follow that model of a pure Unix-type
  • 7:06 - 7:08
    deployment. There's other ones.
  • 7:08 - 7:10
    There's variants of Debian. I think it's one
  • 7:10 - 7:13
    that's called Devlon that actually uses
  • 7:13 - 7:16
    sysv init instead of systemd, but none of
  • 7:16 - 7:18
    the major distros. So if you're deploying
  • 7:18 - 7:21
    something in a business, if you're deploying something where you need
  • 7:21 - 7:23
    support, all of those companies are using
  • 7:23 - 7:26
    systemd. But we can verify, right? Let's be
  • 7:26 - 7:28
    sure, right? So I've got a CentOS box here
  • 7:28 - 7:30
    in front of me, and I'm running CentOS 7.
  • 7:30 - 7:33
    And I know that it defaults to systemd.
  • 7:33 - 7:35
    I shouldn't say default--like, it just runs
  • 7:35 - 7:39
    systemd. But if we wanna verify that, there's a few different ways.
  • 7:39 - 7:41
    I mentioned the boot order earlier.
  • 7:41 - 7:44
    And when I turn the computer on, the kernel
  • 7:44 - 7:46
    fires up, and the thing it does is
  • 7:46 - 7:49
    it starts init, right? So, technically, your
  • 7:49 - 7:52
    init process is the very first process
  • 7:52 - 7:54
    that runs. It's number one. So if we use a
  • 7:54 - 7:58
    utility like ps, right--this is the process show command--it shows me all the
  • 7:58 - 8:01
    processes running in the background. If I do a ps
  • 8:01 - 8:07
    aux, right, that says show all of the processes with the user that's attached to them,
  • 8:07 - 8:10
    and x--I can't remember what x does.
  • 8:10 - 8:13
    It's like services or something. I can't remember
  • 8:13 - 8:16
    what x is. But if I do ps aux and I run
  • 8:16 - 8:17
    that, it will show me all the processes
  • 8:17 - 8:19
    that are running. And if you look at
  • 8:19 - 8:21
    these process numbers, right--this
  • 8:21 - 8:28
    second column--I’ve got Process 13,999, Process 14,416. When a process runs,
  • 8:28 - 8:32
    it gets a number, and when it closes, the number goes away. And then it just kind of
  • 8:32 - 8:34
    keeps incrementing and growing. So over
  • 8:34 - 8:37
    14,000 processes have run since I booted
  • 8:37 - 8:38
    this machine up. Yeah. It's a couple.
  • 8:38 - 8:40
    Which hasn't been up that long, right?
  • 8:40 - 8:42
    I've been up for an hour and a half.
  • 8:42 - 8:45
    And so that's a big number. What if I
  • 8:45 - 8:47
    scroll up on that list? Let's just scroll,
  • 8:47 - 8:49
    scroll, scroll. The numbers get smaller
  • 8:49 - 8:51
    and smaller. These are processes that ran
  • 8:51 - 8:53
    early on and are still running. And I can
  • 8:53 - 8:57
    find process number one right there, PID
  • 8:57 - 8:59
    number one. And if I look to the side
  • 8:59 - 9:05
    over here, it's /usr/lib/systemd/systemd.
  • 9:05 - 9:07
    That tells me that I'm running
  • 9:07 - 9:09
    systemd. I'm on CentOS. Let me jump over
  • 9:09 - 9:12
    to Ubuntu. So this is an Ubuntu machine,
  • 9:12 - 9:15
    and I can do the exact same thing here.
  • 9:15 - 9:17
    If I jump into my terminal,
  • 9:17 - 9:20
    and I'll just do ps aux, and I'm just
  • 9:20 - 9:21
    gonna feed it into the head command to
  • 9:21 - 9:23
    save a little time. And there's my
  • 9:23 - 9:28
    process ID 1, and I can see--oh, this one's actually a little interesting.
  • 9:28 - 9:31
    It's telling me /sbin/init.
  • 9:31 - 9:35
    Alright. Now wait a minute. I said this defaulted to systemd.
  • 9:35 - 9:40
    That's not systemd. If you see /sbin/init, that's sysv init. Okay?
  • 9:40 - 9:43
    However, if I relied on this, I might get
  • 9:43 - 9:44
    tricked. And let me show you what I mean
  • 9:44 - 9:47
    because I'm, like, 99.9% positive this
  • 9:47 - 9:48
    should be systemd. I didn't check before
  • 9:48 - 9:50
    the show, so that's my fault.
  • 9:50 - 9:53
    But if you pull up a listing--I'm gonna
  • 9:53 - 9:56
    do an ls -l /sbin--I wanna see
  • 9:56 - 9:58
    the binaries that are in that file, or in
  • 9:58 - 10:00
    that folder. Okay? And I go in here and
  • 10:00 - 10:05
    find init--actually, let me just do /sbin/init. There we go.
  • 10:05 - 10:08
    They're tricking us, right? It's a symlink.
  • 10:08 - 10:16
    They have symlinked /sbin/init does exist and it's being pointed to /lib/systemd/systemd.
  • 10:16 - 10:18
    So there we go. So it looked like in the process
  • 10:18 - 10:20
    output that we weren't. Now that means that
  • 10:20 - 10:22
    we can't really trust the process output,
  • 10:22 - 10:26
    can we? The ps aux--it works if we're
  • 10:26 - 10:27
    full-on running systemd, but it could
  • 10:27 - 10:29
    trick us with older stuff. But if we
  • 10:29 - 10:34
    actually look at /sbin/init, it's in the official standard
  • 10:34 - 10:37
    that any operating system that's using
  • 10:37 - 10:42
    the Linux kernel has to have a file in /sbin/init. Even if it's not
  • 10:42 - 10:44
    what's called, it has to be there. And so
  • 10:44 - 10:46
    that's a more reliable way to look and
  • 10:46 - 10:50
    see what we're running. If I jump back over to
  • 10:50 - 10:53
    my CentOS box and do the same thing
  • 10:53 - 10:55
    in /sbin, I'm gonna look for--so
  • 10:55 - 11:00
    I'm running ls -l to get a long output--/sbin/init,
  • 11:00 - 11:02
    and I can see they've linked it there as
  • 11:02 - 11:04
    well. The difference is in the process
  • 11:04 - 11:07
    command--that ps aux. On CentOS, it's
  • 11:07 - 11:10
    actually showing me the true binary.
  • 11:10 - 11:12
    On Ubuntu, they're showing me the link
  • 11:12 - 11:13
    instead of the binary, so they've changed
  • 11:13 - 11:15
    the way that command works. But I can see
  • 11:15 - 11:19
    here they're both clearly running sbin, now, I mean systemd.
  • 11:19 - 11:23
    Now, if I looked in here and it actually said /sbin/init, and it was green and that was
  • 11:23 - 11:26
    that, then I'm running sysv init or
  • 11:26 - 11:27
    upstart. Upstart, I mentioned, was like an
  • 11:27 - 11:32
    upgrade to sysv init--not a true change. So it would be
  • 11:32 - 11:35
    /sbin/init as well. So I wouldn't be able to tell the difference
  • 11:35 - 11:37
    between upstart and sysv init, but that
  • 11:37 - 11:41
    doesn't matter for us. I know we're running systemd.
  • 11:41 - 11:43
    So, systemd starts right after the
  • 11:43 - 11:45
    kernel, right? Yeah. Then what happens?
  • 11:45 - 11:47
    So it's a big deal, right? It's the first
  • 11:47 - 11:52
    thing. And if systemd crashes, your system crashes, right? It's powering everything. Mhmm.
  • 11:52 - 11:55
    So once it fires up, then it's gotta get
  • 11:55 - 11:56
    to work. And it says, "Okay. Now it's time
  • 11:56 - 11:59
    for me to do my job. I need to start some services.
  • 11:59 - 12:02
    I need to open up some ports on the system for
  • 12:02 - 12:05
    network communication. I need to figure out whether we're gonna load a
  • 12:05 - 12:07
    GUI or go straight to the command line."
  • 12:07 - 12:09
    It has to do all these different things,
  • 12:09 - 12:11
    and it does that through the use of unit
  • 12:11 - 12:14
    files. Alright? Unit files are stored in--
  • 12:14 - 12:21
    if I change directories here--I'm gonna go into /lib/systemd/system.
  • 12:21 - 12:24
    Alright? If you go into that folder right there and pull up a
  • 12:24 - 12:30
    listing, what you're gonna find is a ton of files. And each file will be a
  • 12:30 - 12:38
    name, and then it'll end in .service, or .timer, .target, .slice. Each one is kind of
  • 12:38 - 12:41
    something different. Most of them are .service, right?
  • 12:41 - 12:44
    Those .service files are identifying daemons. Daemons are gonna
  • 12:44 - 12:45
    run in the background. Those are services
  • 12:45 - 12:51
    or applications that run and just keep running and do the thing. Alright?
  • 12:51 - 12:58
    Other ones, though, like .target--that's a user environment, right? So there's a .target file for if I want to have a
  • 12:58 - 13:04
    GUI, right? It's actually, I think, called "graphical-something-or-another." It's in here.
  • 13:04 - 13:06
    Remembering the names of these is pretty hard because there's so many.
  • 13:06 - 13:12
    Yeah--graphical.target, right there. And then there's a command-line one, which is actually called multi-user.target.
  • 13:12 - 13:14
    There are several others that are like that.
  • 13:14 - 13:19
    You'll also find .socket, which is identifying a network port that's
  • 13:19 - 13:21
    going to be open--or possibly a named
  • 13:21 - 13:24
    pipe or something that'll allow an application to talk to another
  • 13:24 - 13:26
    application via network service.
  • 13:26 - 13:29
    Let's see--that was targets, services, and
  • 13:29 - 13:31
    sockets. Those are the most common.
  • 13:31 - 13:33
    You also see .mount. So those are where
  • 13:33 - 13:35
    it'll mount a hard drive partition.
  • 13:35 - 13:40
    You still have the file system table--the /etc/fstab file--but you don't need
  • 13:40 - 13:41
    it anymore if you don't want to, because
  • 13:41 - 13:44
    systemd can use .mount unit files to
  • 13:44 - 13:46
    be able to specify what to reach out to.
  • 13:46 - 13:50
    So when systemd starts, it's actually parsing all of the stuff in this
  • 13:50 - 13:52
    directory and looking at it and saying,
  • 13:52 - 13:58
    "Alright. What do I need to do?" And if we pick—let me pick something easy--
  • 13:58 - 14:03
    SSH. Alright. So SSH--that's the Secure Shell daemon, and it's
  • 14:03 - 14:06
    running by default on most Linux systems
  • 14:06 - 14:08
    so that we can create a secure remote
  • 14:08 - 14:13
    shell, connect in, and get a command prompt, right? So it should be starting when the system starts.
  • 14:13 - 14:20
    If I look inside of that file--so let me just cat sshd.service.
  • 14:20 - 14:24
    There we go. There's not a whole heck of a lot in here, right? So it starts with this
  • 14:24 - 14:27
    block quote right here where it says [Unit].
  • 14:27 - 14:30
    And from there, it goes on to specify
  • 14:30 - 14:32
    information about that daemon. So I can
  • 14:32 - 14:35
    see that this is the OpenSSH server daemon,
  • 14:35 - 14:37
    and it has documentation, right? There's a
  • 14:37 - 14:39
    man file that's available for it, so it's
  • 14:39 - 14:41
    got that registered here. And then it's
  • 14:41 - 14:44
    saying, we wanna start it--but don't
  • 14:44 - 14:46
    start it right away. You need to make
  • 14:46 - 14:48
    sure that the network.target environment
  • 14:48 - 14:49
    has started, right? We have to have networking
  • 14:49 - 14:53
    working before we can start SSH, and we
  • 14:53 - 14:56
    need the sshd-keygen service running.
  • 14:56 - 14:58
    So this is setting up a hierarchy, right--
  • 14:58 - 15:00
    like an order and dependencies that
  • 15:00 - 15:02
    systemd is able to use. So it says, okay.
  • 15:02 - 15:04
    I can't start this. I need to
  • 15:04 - 15:09
    start network.target, and I need to start sshd-keygen.service.
  • 15:09 - 15:10
    So once those are started, then I can
  • 15:10 - 15:11
    come back and start this one.
  • 15:11 - 15:13
    And remember, it's doing all this in parallel,
  • 15:13 - 15:16
    so it can reach out and try and do multiple things at once.
  • 15:16 - 15:18
    And then after that, it says once. Right?
  • 15:18 - 15:20
    After it says, "I can't start until these are
  • 15:20 - 15:24
    started," but once means, "Once I start, I
  • 15:24 - 15:26
    want this other thing to start as well."
  • 15:26 - 15:31
    Now sshd-keygen.service is listed twice.
  • 15:31 - 15:35
    And what that's saying is it needs to be started when I start.
  • 15:35 - 15:37
    But if something goes wrong, I need that
  • 15:37 - 15:39
    one started again because I depend on it.
  • 15:39 - 15:42
    Right? That's establishing that dependency too. So we've got those
  • 15:42 - 15:44
    entries. And then after that, we see the
  • 15:44 - 15:46
    actual service. Right? And so it's telling
  • 15:46 - 15:51
    me I can find my configuration in /etc/sysconfig/sshd. That's where the
  • 15:51 - 15:53
    configuration is gonna be. And then
  • 15:53 - 15:55
    when we execute, when we start, here's the
  • 15:55 - 16:00
    command that it runs: /usr/sbin/sshd. That's an executable.
  • 16:00 - 16:02
    -d tells it to run as a daemon, run in the
  • 16:02 - 16:04
    background. Otherwise, when that shell
  • 16:04 - 16:06
    closes, the application would close.
  • 16:06 - 16:07
    We can see what to do when we close it,
  • 16:07 - 16:09
    and it's gonna run the kill command,
  • 16:09 - 16:10
    and it's gonna shut that one down.
  • 16:10 - 16:12
    You can see how to kill it--you just
  • 16:12 - 16:16
    use it as a process execute. Sounds really violent, but it's just exiting.
  • 16:16 - 16:18
    And then restart--if there's a failure, if
  • 16:18 - 16:20
    it crashes, it's gonna try and restart
  • 16:20 - 16:22
    it. And if it does a restart, it'll wait
  • 16:22 - 16:24
    42 seconds. Right? This is all defined
  • 16:24 - 16:26
    right here. When we install this service,
  • 16:26 - 16:28
    if I want it running every time, it's
  • 16:28 - 16:33
    gonna install it and attach it to multi-user.target. So when that starts,
  • 16:33 - 16:35
    which is our command line, when the
  • 16:35 - 16:37
    command line starts, it's going to make
  • 16:37 - 16:39
    sure that SSH starts as well. These are
  • 16:39 - 16:41
    unit files, and the unit files are just
  • 16:41 - 16:43
    text. You can actually go in and
  • 16:43 - 16:45
    edit them if you want. It's not a great
  • 16:45 - 16:47
    idea to edit these. Most of the time, when
  • 16:47 - 16:49
    you install it, when you install the SSHD
  • 16:49 - 16:52
    service, it creates these files for you.
  • 16:52 - 16:53
    Right? So I didn't create these. They were
  • 16:53 - 16:56
    made for me. If I want to modify them, I
  • 16:56 - 16:58
    usually will not modify it here because
  • 16:58 - 17:01
    if we get an update, right, so SSHD gets
  • 17:01 - 17:03
    an update and they update this unit file,
  • 17:03 - 17:04
    it's gonna overwrite my changes.
  • 17:04 - 17:06
    I'm gonna lose them. So if you're gonna
  • 17:06 - 17:08
    make changes, don't make them in this
  • 17:08 - 17:09
    folder. So, let me get back in here.
  • 17:09 - 17:12
    I actually need to zoom back out. There we go.
  • 17:12 - 17:15
    So I'm in /lib/systemd/system.
  • 17:15 - 17:17
    You really should never edit anything in
  • 17:17 - 17:25
    /lib. It should just be left alone. But if you go into /etc/systemd/system, alright?
  • 17:25 - 17:26
    And that folder might not exist.
  • 17:26 - 17:28
    You might have to make it. On CentOS, it's
  • 17:28 - 17:35
    there by default. So /etc/systemd/system as opposed to /lib/systemd/system.
  • 17:35 - 17:39
    When you look in here, you'll find service files and target
  • 17:39 - 17:43
    files and wants and so on. These are where
  • 17:43 - 17:46
    we're overriding what's done inside of
  • 17:46 - 17:48
    systemd's folder. So if I want to make a
  • 17:48 - 17:50
    change, I shouldn't go and change the
  • 17:50 - 17:52
    files in /lib. I can come right in
  • 17:52 - 17:53
    here and create a file with the same
  • 17:53 - 17:55
    name. And when I do that, this file will
  • 17:55 - 17:56
    override the other one. And when an
  • 17:56 - 17:59
    update comes along, it'll leave my file
  • 17:59 - 18:00
    alone. Right? Now, I haven't overridden
  • 18:00 - 18:02
    anything, and so everything in here was
  • 18:02 - 18:04
    done by the system. Looks like DBUS is
  • 18:04 - 18:07
    doing some stuff. We even have sysinit,
  • 18:07 - 18:08
    which is for backwards compatibility
  • 18:08 - 18:10
    with SysVinit. So some of these entries
  • 18:10 - 18:13
    are already in here. Most of these are just links.
  • 18:13 - 18:17
    In fact, I think all of them are just links.
  • 18:17 - 18:18
    Well, no. Some of them just have weird
  • 18:18 - 18:20
    permissions on them. But many of them are
  • 18:20 - 18:22
    links to other locations, and you'll see
  • 18:22 - 18:26
    them pointing to /lib or somewhere to override an activity
  • 18:26 - 18:28
    from somewhere else. So that's how that
  • 18:28 - 18:30
    kind of functions, and that's really the
  • 18:30 - 18:32
    heart and soul of systemd. Systemd is a
  • 18:32 - 18:34
    binary. It's an executable that runs.
  • 18:34 - 18:36
    In fact, I think I browsed right past it.
  • 18:36 - 18:45
    If I go into /lib/systemd and we look in here somewhere... right here.
  • 18:45 - 18:50
    There we go. There's the systemd executable. It's actually not very flashy at all.
  • 18:50 - 18:54
    It's--yeah. Let me do a human-readable size on this one.
  • 18:54 - 19:00
    It's a blazing 1.6 megabytes, and yet that 1.6 megabytes is powering
  • 19:00 - 19:03
    my entire user environment right now.
  • 19:03 - 19:04
    The kernel is handling my hardware
  • 19:04 - 19:07
    interaction, and then systemd is making
  • 19:07 - 19:09
    all of my software work. I mean, this is
  • 19:09 - 19:11
    super important software, and it's really,
  • 19:11 - 19:13
    really small. And then it relies on all
  • 19:13 - 19:15
    those configuration files to do its job.
  • 19:15 - 19:19
    Well, do we have to create unit files for new services, Don?
  • 19:19 - 19:21
    You can. So if I'm installing a new
  • 19:21 - 19:22
    service, you can go in and create and
  • 19:22 - 19:24
    define them yourself. I've had to do that
  • 19:24 - 19:26
    on occasion, but normally, the services
  • 19:26 - 19:28
    will create them automatically. Right?
  • 19:28 - 19:30
    So let's say--let me give you an example.
  • 19:30 - 19:32
    I want to be a web server. Right?
  • 19:32 - 19:37
    So I want to install Apache. Well, if I take a look inside of
  • 19:37 - 19:42
    my systemd library folder--so I'm back in /lib/systemd/system.
  • 19:42 - 19:44
    If I look for any file that starts with
  • 19:44 - 19:48
    httpd, oh, shoot. I actually see it right there: httpd.service. So it looks like
  • 19:48 - 19:50
    I'm already a web server.
  • 19:50 - 19:54
    That file is defining the Apache service and getting it set up to do its job.
  • 19:54 - 19:56
    Right? Well, I didn't create that file, or at
  • 19:56 - 20:00
    least I don't remember doing it. So when I installed httpd,
  • 20:00 - 20:02
    it must have created that. Right?
  • 20:02 - 20:13
    If I were to remove httpd--so if I do a sudo yum remove or we'll just remove httpd.
  • 20:13 - 20:15
    There we go. So I'm gonna uninstall it.
  • 20:15 - 20:21
    So I'm uninstalling httpd, and now I'll just take a look again,
  • 20:21 - 20:24
    and I see there's no unit file defined
  • 20:24 - 20:25
    for it anymore. So when it uninstalled
  • 20:25 - 20:27
    httpd, it knew to take away that unit
  • 20:27 - 20:31
    file. And if I install it back again--
  • 20:31 - 20:33
    so I'm just gonna come in here and do an install,
  • 20:33 - 20:35
    and when I do that, it's gonna go
  • 20:35 - 20:38
    through. It's gonna do the install.
  • 20:38 - 20:44
    There it goes. And if I take a look, I can see it created that file again.
  • 20:44 - 20:48
    Right? So most services, if you're installing them from a package file,
  • 20:48 - 20:50
    whoever the developer is that created
  • 20:50 - 20:51
    that software will know, "Hey, this is the
  • 20:51 - 20:52
    unit file I need," and it'll go ahead and
  • 20:52 - 20:54
    create it for you. So you really don't
  • 20:54 - 20:55
    have to worry about it. You don't have to
  • 20:55 - 20:57
    go in and edit these things. It just does it.
  • 20:57 - 20:59
    Now, I will tell you, sometimes you
  • 20:59 - 21:03
    install software, and it doesn't create the unit file for you. Right?
  • 21:03 - 21:08
    And that might be software that's designed to be run on demand. Okay?
  • 21:08 - 21:10
    You need unit files for software that
  • 21:10 - 21:11
    runs in the background, that runs
  • 21:11 - 21:13
    automatically without interaction. I want
  • 21:13 - 21:14
    this web server running all the time.
  • 21:14 - 21:16
    It needs a unit file. But if I have something like
  • 21:16 - 21:19
    OpenOffice, right--OpenOffice, I need
  • 21:19 - 21:22
    it to run when I'm interacting with it. I don't want it running in the background.
  • 21:22 - 21:24
    So there's not going to be a unit file
  • 21:24 - 21:26
    for OpenOffice. Those are just user applications.
  • 21:26 - 21:30
    Alright? But sometimes when you install a service, it still
  • 21:30 - 21:33
    doesn't create the unit file because most services don't start automatically.
  • 21:33 - 21:37
    You've got to configure them. And we do that with systemctl.
  • 21:37 - 21:42
    systemctl is a utility that is packaged alongside systemd.
  • 21:42 - 21:45
    It's the system control utility, and it lets us control
  • 21:45 - 21:47
    systemd and do things that otherwise
  • 21:47 - 21:49
    might require us to edit some files.
  • 21:49 - 21:55
    So, for example, if I want httpd to start right now,
  • 21:55 - 22:03
    I could do sudo systemctl start httpd. Right? And it's gonna read the
  • 22:03 - 22:10
    unit file. It's actually looking for that unit file, and I should have typed this the whole way and said httpd.service
  • 22:10 - 22:12
    because that's the unit file name, and
  • 22:12 - 22:14
    it's gonna look for it, find it, and
  • 22:14 - 22:16
    start it. Okay. If you leave the extension
  • 22:16 - 22:21
    off, it looks for services first because I'm telling it to start something.
  • 22:21 - 22:25
    And that way, it doesn't look for .socket, .target, and those guys. Right?
  • 22:25 - 22:27
    But if you want, the full name would actually
  • 22:27 - 22:29
    have .service on the end. So it's gonna
  • 22:29 - 22:31
    find it. It's gonna start it. Alright?
  • 22:31 - 22:33
    Now, we can start a service even if it
  • 22:33 - 22:36
    doesn't have a unit file. If it's got dependencies, things will break.
  • 22:36 - 22:38
    But even if it didn't have one, you could do that.
  • 22:38 - 22:41
    But when the system reboots,
  • 22:41 - 22:42
    that service is not gonna be started
  • 22:42 - 22:44
    by default unless we tell it. And we
  • 22:44 - 22:48
    could go in and modify the actual
  • 22:48 - 22:52
    service file and/or the unit file and actually tell it,
  • 22:52 - 22:55
    "Yes, the multi-user or graphical target depends on
  • 22:55 - 22:58
    this file. We need HTTPD to start." Right?
  • 22:58 - 23:05
    Or you can just do it right here with systemctl. You can say systemctl enable httpd. Right?
  • 23:05 - 23:17
    And when I run that, look what it did. Created a symlink from /etc/systemd/system/multi-user.target.wants/httpd.service.
  • 23:17 - 23:21
    So it put a symlink in /etc. Right?
  • 23:21 - 23:27
    And it's pointing to /usr/lib/systemd/system/httpd.service.
  • 23:27 - 23:34
    It didn't edit anything inside of /lib or /usr/lib. It edited instead inside of /etc.
  • 23:34 - 23:36
    And even then, it wasn't really an edit. It was just a link that
  • 23:36 - 23:43
    says, "Now whenever I start the multi-user target, I will also start httpd."
  • 23:43 - 23:47
    It did that for me. I didn't have to create unit files. I didn't have to go and modify /etc.
  • 23:47 - 23:50
    It was just done automatically. And if I say, "You know
  • 23:50 - 23:52
    what? I want the web server, but I don't want
  • 23:52 - 23:56
    it to start automatically," I can just come in and say disable.
  • 23:56 - 23:59
    And when you do that, it removes the link.
  • 23:59 - 24:06
    It didn't disable Apache. I can still run Apache if I do sudo systemctl start
  • 24:06 - 24:09
    httpd like that, and then I can do status.
  • 24:09 - 24:14
    So, sudo systemctl status httpd, and it will
  • 24:14 - 24:20
    actually show me that it's active and running right now. It's up and operational. Okay?
  • 24:20 - 24:22
    But looking up here, it says that it's
  • 24:22 - 24:25
    loaded, but it also tells me that it's
  • 24:25 - 24:27
    disabled, which means if I reboot, Apache
  • 24:27 - 24:29
    is not gonna be running anymore. Right?
  • 24:29 - 24:33
    If I go back and enable it again, let me do that.
  • 24:33 - 24:35
    And then we pull that status. What I'm
  • 24:35 - 24:38
    gonna see now is that the service is
  • 24:38 - 24:40
    enabled. So if I reboot, Apache is going
  • 24:40 - 24:42
    to come back up, and we'll be in business.
  • 24:42 - 24:45
    So the important part here is that we do all of this
  • 24:45 - 24:50
    without ever actually editing the file. systemctl does it all behind the scenes
  • 24:50 - 24:52
    for us. So this is another area where we kind
  • 24:52 - 24:54
    of deviate away from the way that Unix
  • 24:54 - 24:55
    used to be, where we'd edit all these
  • 24:55 - 24:57
    script files. Now it's all automated
  • 24:57 - 24:59
    through systemctl. How does systemd keep
  • 24:59 - 25:01
    track of which services to run?
  • 25:01 - 25:03
    So we've kind of seen that a little bit
  • 25:03 - 25:05
    with our dependencies, but it really
  • 25:05 - 25:07
    boils down to the target files. Right?
  • 25:07 - 25:08
    The target files are what define what's
  • 25:08 - 25:10
    gonna get fired up and so on.
  • 25:10 - 25:11
    So let's take a look at one of those
  • 25:11 - 25:14
    target files. If I get... what folder am
  • 25:14 - 25:18
    I in? I'm in the right folder: /lib/systemd/system.
  • 25:18 - 25:23
    If I pull up a listing of *.target, alright, you'll see a lot of targets.
  • 25:23 - 25:26
    Right? Targets are like collections of
  • 25:26 - 25:28
    unit files. It says, "Hey, if I want the
  • 25:28 - 25:30
    network to function, I've got a network
  • 25:30 - 25:32
    target, and here's all the stuff I need
  • 25:32 - 25:33
    for the network to function." And you'll
  • 25:33 - 25:35
    see other things in here that are like
  • 25:35 - 25:40
    that. You know, if I want remote encryption to work or reboot to work or
  • 25:40 - 25:42
    swap files to work, they've all got
  • 25:42 - 25:44
    different targets, and those targets are
  • 25:44 - 25:46
    defining all the services needed to make
  • 25:46 - 25:52
    that work. So if I want to boot to a command line, just to a command line, then
  • 25:52 - 26:01
    I need what's in multi-user.target. If I take a look at that file--let's use less on multi-user.target,
  • 26:01 - 26:06
    I can look in there, and it's actually not a very big file at all.
  • 26:06 - 26:08
    Right? But that's because it's including
  • 26:08 - 26:11
    some things that it needs. It says, "Alright, multi-user can start whenever it
  • 26:11 - 26:14
    wants." Right? So I don't see a... well, actually, I do
  • 26:14 - 26:17
    have a require, don't I? Here's a require that says basic.target.
  • 26:17 - 26:19
    So before multi-user can start, anything in
  • 26:19 - 26:22
    basic.target has to start. Then this one
  • 26:22 - 26:25
    can start. And once it starts, afterwards,
  • 26:25 - 26:26
    we'll fire up basic.target if it isn't
  • 26:26 - 26:28
    already, but we've got rescue.service,
  • 26:28 - 26:29
    rescue.target. We've got a few other
  • 26:29 - 26:32
    things that can be triggered as well
  • 26:32 - 26:33
    to, you know, get our system up and
  • 26:33 - 26:36
    running. Well, let's look at basic.target.
  • 26:36 - 26:39
    So if I just... I'll cat basic.target, and I
  • 26:39 - 26:46
    look in here, it's also not very big at all. Right? But it's got that it requires sysinit.target,
  • 26:46 - 26:50
    and it's calling several other things too. And if I look at
  • 26:50 - 26:51
    sysinit.target, we're kind of following
  • 26:51 - 26:56
    this back. Right? Going back, back, back, and we see how it's calling more and more things.
  • 26:56 - 26:59
    All of this is coming together to build
  • 26:59 - 27:01
    the environment that is required
  • 27:01 - 27:03
    for our system to boot. If I want a
  • 27:03 - 27:08
    graphical user interface, then I would need graphical.target,
  • 27:08 - 27:10
    and you'll see where it depends on
  • 27:10 - 27:12
    multi-user. The command line has to work
  • 27:12 - 27:16
    before I can launch stuff to get the GUI. Right? They all are interdependent.
  • 27:16 - 27:19
    As a human, it would take me forever to sift through all these
  • 27:19 - 27:23
    files and build out the tree of dependencies, but systemd does it every
  • 27:23 - 27:25
    time you boot. It parses through these
  • 27:25 - 27:28
    files, figures out the dependencies.
  • 27:28 - 27:31
    It actually doesn't monitor the files on an ongoing basis. If I change a file
  • 27:31 - 27:36
    right now, it won't recognize it unless I tell it to reload. Right? So you can go in
  • 27:36 - 27:37
    and... well, you can reboot your system.
  • 27:37 - 27:39
    It's one way to do it. Or, like with a service,
  • 27:39 - 27:41
    you use the restart command, and then it'll
  • 27:41 - 27:42
    reload the unit file for that service.
  • 27:42 - 27:44
    But, otherwise, it's figuring out all
  • 27:44 - 27:46
    those dependencies, kind of mapping it
  • 27:46 - 27:47
    together. It gets pretty complex because of
  • 27:47 - 27:49
    the sheer amount of files, but at the end
  • 27:49 - 27:51
    of the day, they are just simple text
  • 27:51 - 27:52
    files. You can view it all. Well, can we
  • 27:52 - 27:54
    change between the targets? You can.
  • 27:54 - 27:57
    Yeah. You know, my system right now, if I were to reboot... Mhmm.
  • 27:57 - 28:00
    It's set to default to the graphical
  • 28:00 - 28:02
    user interface. So it's basically set
  • 28:02 - 28:07
    to boot to graphical.target. Right? But if this is a server, I don't need a GUI.
  • 28:07 - 28:09
    I might want to have the GUI installed
  • 28:09 - 28:10
    so when I'm sitting at the server, I can
  • 28:10 - 28:12
    use it, but the rest of the time, I don't
  • 28:12 - 28:14
    need a GUI running. So I can actually
  • 28:14 - 28:18
    switch targets and tell it, "Hey, don't run graphical anymore. Just run
  • 28:18 - 28:20
    multi-user." And we can do that pretty
  • 28:20 - 28:24
    easily. So here I am in my command prompt, and I could be in any
  • 28:24 - 28:26
    prompt. It really wouldn't... or any folder.
  • 28:26 - 28:31
    It wouldn't matter which one. I'm gonna do a sudo systemctl
  • 28:31 - 28:36
    isolate. And so I'm telling it I want to target a specific
  • 28:36 - 28:38
    target. That's a little redundant.
  • 28:38 - 28:42
    And then I'll just say multi-user.target.
  • 28:42 - 28:44
    Alright? Multi-user means, yes, it's
  • 28:44 - 28:45
    command line, but it's not just one
  • 28:45 - 28:49
    console just for you. It still allows network services and other things to run.
  • 28:49 - 28:53
    And so when I run that, see how my system freaked out? Yeah. Right?
  • 28:53 - 28:55
    It recognized immediately. "Okay, we're not
  • 28:55 - 28:57
    gonna do the GUI target anymore.
  • 28:57 - 29:00
    We're gonna just do a command line." And so
  • 29:00 - 29:01
    now I can come in and I can log in,
  • 29:01 - 29:04
    assuming I can remember my password,
  • 29:04 - 29:07
    and there we go. I'm not in a GUI.
  • 29:07 - 29:11
    I'm just in the command line. And so now if I reboot, I'm gonna boot just to
  • 29:11 - 29:13
    this interface right here. You know, this
  • 29:13 - 29:16
    is my... well, actually, I need to go and modify my GRUB
  • 29:16 - 29:18
    loader to tell it exactly what I want.
  • 29:18 - 29:20
    But at this point, I'm now in just
  • 29:20 - 29:24
    command line. X Windows and all the GUI stuff is not running at all.
  • 29:24 - 29:26
    I can switch back with that same command...
  • 29:26 - 29:35
    well, a little bit different. So if I do sudo systemctl isolate, and then I'll do graphical.target,
  • 29:35 - 29:36
    and that's gonna switch me back.
  • 29:36 - 29:37
    And now it's loading X Windows. See how
  • 29:37 - 29:39
    it's taking a moment? It's not like X Windows
  • 29:39 - 29:40
    was running in the background. It's got
  • 29:40 - 29:43
    to relaunch all that stuff and get me in
  • 29:43 - 29:45
    here and, and get me put back in place.
  • 29:45 - 29:48
    Now, because this is happening after my
  • 29:48 - 29:51
    boot operation and all that, you see how it's kind of, like, screwed up my
  • 29:51 - 29:53
    resolution? Yeah. And all that?
  • 29:53 - 29:56
    That's one of the trickier parts about moving from one to another.
  • 29:56 - 29:59
    If something's not defined in the unit
  • 29:59 - 30:01
    files or some other configuration file,
  • 30:01 - 30:03
    then stuff gets lost. I... oh, it cleaned
  • 30:03 - 30:08
    up. Well, sort of. I got large icons now. So, you know, there's a lot of
  • 30:08 - 30:10
    people like me. If you do this stuff a
  • 30:10 - 30:13
    lot, you'll end up writing scripts to kind of automate some of the changes.
  • 30:13 - 30:17
    And so, for example, I have a little script in my home directory.
  • 30:17 - 30:18
    This will show you how often I do this
  • 30:18 - 30:22
    to manually set my resolution so that I can just run it real quick,
  • 30:22 - 30:24
    and then I'm right back in.
  • 30:24 - 30:26
    Now my icon’s the right size and all.
  • 30:26 - 30:28
    So, you can switch between targets at the
  • 30:28 - 30:30
    drop of a hat, but just know that there
  • 30:30 - 30:31
    are certain penalties to it.
  • 30:31 - 30:32
    There are certain things that happen on your
  • 30:32 - 30:33
    system you might not expect.
  • 30:33 - 30:35
    So, definitely test it out, you know, run
  • 30:35 - 30:36
    through the routine a few times.
  • 30:36 - 30:38
    But that’s basically the nuts and bolts of
  • 30:38 - 30:40
    systemd right there. There’s a lot more to
  • 30:40 - 30:42
    it, but the basic operation is that there are
  • 30:42 - 30:44
    a ton of text files. It reads them and
  • 30:44 - 30:47
    performs as they dictate. Great episode on
  • 30:47 - 30:49
    managing services with systemd.
  • 30:49 - 30:51
    Wonderfully done, Don. And before we move
  • 30:51 - 30:52
    on, what else would you like to say?
  • 30:52 - 30:53
    You know, if you want to learn more about
  • 30:53 - 30:57
    systemd, definitely check out the systemd website.
  • 30:57 - 30:59
    That’ll be like your Google response, I’m sure.
  • 30:59 - 31:00
    But there’s also a whole suite of other
  • 31:00 - 31:02
    utilities. I use systemctl.
  • 31:02 - 31:04
    There’s journald for the special logging system
  • 31:04 - 31:08
    that systemd uses. There’s hostnamectl for changing your computer’s hostname.
  • 31:08 - 31:10
    There are a number of other CTL commands
  • 31:10 - 31:12
    that are all part of what systemd
  • 31:12 - 31:14
    creates. So, there’s a lot more than what we
  • 31:14 - 31:15
    went over in this episode. I was just
  • 31:15 - 31:17
    focusing on really what’s involved in
  • 31:17 - 31:20
    getting your system up and operational and getting your services running.
  • 31:20 - 31:22
    Great advice as usual. Here’s some more great
  • 31:22 - 31:24
    advice: Watch every episode of CompTIA
  • 31:24 - 31:26
    Linux Plus. You’re going to be glad you
  • 31:26 - 31:28
    did. It’s going to help you now and in
  • 31:28 - 31:30
    the future, and keep studying.
  • 31:30 - 31:32
    Take advantage of the supplementary information in our course library.
  • 31:32 - 31:35
    It’s there to do one thing: help you be even
  • 31:35 - 31:36
    more successful, so check that out as
  • 31:36 - 31:38
    well. And tell everybody you know about
  • 31:38 - 31:42
    ITPRO.TV. You know, ITPRO.TV is binge-worthy. Thanks for watching.
  • 31:42 - 31:47
    I’m Zach Memos. And I’m Don Pezet. And we will see you again soon.
  • 31:47 - 31:52
    [Music].
  • 31:52 - 31:56
    Thank you for watching ITPRO.TV.
  • 31:59 - 32:03
    Welcome to ITPRO.TV. I'm your host Don Pezet.
  • 32:03 - 32:06
    Live from San Francisco.
  • 32:07 - 32:11
    You're watching ITPRO.TV.
  • 32:11 - 32:13
    Hello, hello, and thank you for watching ITPRO.TV,
  • 32:13 - 32:15
    helping you level up with IT learning
  • 32:15 - 32:20
    everywhere you go. I’m your host, Zach Memos, for this episode of CompTIA Linux Plus.
  • 32:20 - 32:23
    Managing services with SysV init is
  • 32:23 - 32:26
    the name of this episode, and Don
  • 32:26 - 32:27
    Pezet is going to be showing us the way.
  • 32:27 - 32:29
    Don, good to see you. Thanks for joining
  • 32:29 - 32:31
    us. Well, glad to be back, Zach. And like I
  • 32:31 - 32:33
    said, we’re going to be tackling SysV init.
  • 32:33 - 32:35
    You know, in the last episode, we got a chance to see systemd, which is the most
  • 32:35 - 32:38
    popular initialization system in Linux
  • 32:38 - 32:40
    right now. However, if you’re working out
  • 32:40 - 32:42
    there in the field, there are still many,
  • 32:42 - 32:44
    many systems deployed that use the older
  • 32:44 - 32:46
    SysV init. In fact, on servers, it’s
  • 32:46 - 32:48
    actually pretty common. So, definitely
  • 32:48 - 32:49
    something we want to learn about, and
  • 32:49 - 32:50
    that’s what we’re going to do right here
  • 32:50 - 32:55
    in this episode. We’re going to learn a little bit about how SysV init works.
  • 32:55 - 32:58
    It’s for you and SysV init. So, it can happen to anybody, to anyone.
  • 32:58 - 33:02
    We’ll see how it works, we’ll see how to manage it, manipulate it, and just kind
  • 33:02 - 33:04
    of see how to operate in an environment
  • 33:04 - 33:06
    that is running SysV init. Fantastic. Don,
  • 33:06 - 33:08
    how are services controlled under Linux?
  • 33:08 - 33:13
    Alright, so basically, all of our services inside of Linux depend on an
  • 33:13 - 33:15
    initialization of some sort, right?
  • 33:15 - 33:17
    And systemd, we’ve already seen this one way,
  • 33:17 - 33:19
    right? Your system boots up, and systemd
  • 33:19 - 33:21
    is, well, kind of follows a boot process,
  • 33:21 - 33:24
    right? You know, your kernel kicks in, and then it
  • 33:24 - 33:26
    launches your init. And so, systemd would
  • 33:26 - 33:27
    launch, and then it would control
  • 33:27 - 33:29
    services from there. But with SysV init,
  • 33:29 - 33:31
    it’s a little bit different. Your system
  • 33:31 - 33:33
    boots up, your kernel fires up, and then
  • 33:33 - 33:39
    the kernel calls a series of scripts. SysV init is not a binary executable.
  • 33:39 - 33:40
    Well, I guess it sort of is. It’s a little bit
  • 33:40 - 33:42
    of a binary. But the majority of what it
  • 33:42 - 33:46
    does is all being driven by script files.
  • 33:46 - 33:47
    We saw with systemd, it was driven by
  • 33:47 - 33:49
    unit files, which aren’t really scripts;
  • 33:49 - 33:50
    they’re kind of like descriptions, and
  • 33:50 - 33:51
    then systemd knows what to do with it
  • 33:51 - 33:54
    and executes. SysV init just runs scripts.
  • 33:54 - 33:55
    Whatever happens to be in the script, it
  • 33:55 - 33:58
    runs. And you can put literally anything
  • 33:58 - 34:02
    in there, like starting a service, passing parameters to a service,
  • 34:02 - 34:03
    checking to make sure the service is
  • 34:03 - 34:05
    running, or the permissions are set right.
  • 34:05 - 34:07
    All of that is different things that
  • 34:07 - 34:10
    SysV init can do, and it does it in a
  • 34:10 - 34:16
    significantly different way than systemd does. Now, many people feel that systemd is the
  • 34:16 - 34:18
    better of the two options for modern
  • 34:18 - 34:21
    systems. But if you’ve got a server that
  • 34:21 - 34:24
    millions of users rely upon and it needs
  • 34:24 - 34:29
    to be rock solid, SysV init’s been around since the late 1970s, early 1980s.
  • 34:29 - 34:32
    It’s a very stable, tried-and-true system.
  • 34:32 - 34:36
    And that’s why when you go out and look at servers, you’ll find many still use it.
  • 34:36 - 34:37
    Let me give you an example:
  • 34:37 - 34:42
    If you’re deploying Linux servers in Amazon Web Services (AWS), they have what’s called
  • 34:42 - 34:55
    Amazon Linux. And Amazon Linux is based on CentOS, which is based on Upstart,
  • 34:55 - 34:58
    which is based on SysV init. So, it’s kind of a long
  • 34:58 - 35:03
    chain. So, when you go into an Amazon Linux AMI, it’s going to be using
  • 35:03 - 35:07
    SysV init initialization strings and all of that, so you’ve got to know it.
  • 35:07 - 35:11
    Amazon Linux 2, which only recently reached production, now uses
  • 35:11 - 35:13
    systemd, so they’re moving in that direction.
  • 35:13 - 35:16
    But most people haven’t ported their workload over to it yet, so it is very,
  • 35:16 - 35:19
    very common to encounter SysV init systems.
  • 35:19 - 35:21
    So, how do we know if we’re using SysV init?
  • 35:21 - 35:24
    Yeah. So, you know, if you’ve just been hired on somewhere, you’re taking
  • 35:24 - 35:28
    over a server, you might not necessarily know whether it’s running systemd or not.
  • 35:28 - 35:31
    Maybe you’ve got all your distros and version numbers memorized and you just
  • 35:31 - 35:34
    know offhand. But if not, we can double-check. You know, I mentioned that the
  • 35:34 - 35:38
    kernel is going to fire off the initialization system, and that might be systemd
  • 35:38 - 35:41
    or it might be SysV init. Well, that
  • 35:41 - 35:43
    process of firing off initialization is
  • 35:43 - 35:45
    the very thing that it does. So, we
  • 35:45 - 35:47
    can just look at process ID number one,
  • 35:47 - 35:52
    and that will usually tell us whether we’re running systemd or SysV init.
  • 35:52 - 35:54
    Right? So, for example, here, let me take
  • 35:54 - 35:58
    this--I’ve got two boxes here. This one is a CentOS 6 box.
  • 35:58 - 36:03
    So, it’s running CentOS 6.7, which is currently supported. So, this isn’t like
  • 36:03 - 36:05
    this is old and crusty. Like, you can run
  • 36:05 - 36:11
    this in a production environment. And if I come in and do a ps aux, right,
  • 36:11 - 36:14
    and I’m going to just pipe that into the
  • 36:14 - 36:20
    head command. So, I want to see the 10 lines of my process list. When I look
  • 36:20 - 36:22
    at the top processes here, process number
  • 36:22 - 36:25
    one right here fired off by root, if I
  • 36:25 - 36:26
    come along to the side here, I can see
  • 36:26 - 36:31
    that that’s /sbin/init. If it just says that, /sbin/init,
  • 36:31 - 36:34
    then odds are you’re running SysV init.
  • 36:34 - 36:38
    Now, it’s not a guarantee, and that’s because
  • 36:38 - 36:44
    some of the OS distributions, like Ubuntu, will actually show /sbin/init also.
  • 36:44 - 36:46
    But when you go and look at that binary,
  • 36:46 - 36:55
    if you do an ls -l /sbin/init, when you look at the binary on an Ubuntu system, on a newer Ubuntu system,
  • 36:55 - 36:59
    it will actually be a symlink pointing to systemd. Right?
  • 36:59 - 37:05
    So, be careful with this one. In fact, if I go to, like, my--here’s a CentOS 7 box.
  • 37:05 - 37:10
    Let me get into a terminal here, and I’ll do the same kind of thing with ps aux,
  • 37:10 - 37:12
    and I’ll pipe that into head.
  • 37:12 - 37:15
    And that very process I can see
  • 37:15 - 37:19
    right here is pointing to /usr/lib/systemd/systemd.
  • 37:19 - 37:25
    So, this is pretty obvious. Right? Ubuntu, at least in the newest ones in the 18 releases,
  • 37:25 - 37:27
    they’re a little bit trickier. So always be on the lookout for that. But that’s
  • 37:27 - 37:30
    probably the easiest way to tell whether
  • 37:30 - 37:32
    you’re running SysV init or not.
  • 37:32 - 37:36
    So what you’re looking for is that /sbin/init; that tells you what
  • 37:36 - 37:39
    you’re running. And once you’ve got that figured out, now you know that you
  • 37:39 - 37:40
    need to administer this system a
  • 37:40 - 37:43
    slightly different way than you would a systemd system.
  • 37:43 - 37:46
    So, SysV init starts right after the
  • 37:46 - 37:50
    kernel. Right? So, then what happens? Alright. So, SysV init does have a binary,
  • 37:50 - 37:55
    right? So, it did /sbin/init. Right? So, our BIOS fires up on the
  • 37:55 - 37:56
    system, and it does all the hardware
  • 37:56 - 38:00
    detection. Then it reaches out. It finds a hard drive partition.
  • 38:00 - 38:02
    It finds the /boot partition, finds the kernel, and launches
  • 38:02 - 38:05
    the kernel. The kernel then turns around
  • 38:05 - 38:11
    and says, "Okay, I’m a SysV init system," and it launches /sbin/init.
  • 38:11 - 38:15
    And when it does, it then proceeds to start launching a
  • 38:15 - 38:19
    series of scripts. That init executable is then just going to run a
  • 38:19 - 38:21
    series of scripts, and it runs them in
  • 38:21 - 38:25
    serial, not in parallel. Systemd does stuff in parallel. It’ll do more than one
  • 38:25 - 38:30
    thing at the same time. But with SysV init, it runs one script at a time.
  • 38:30 - 38:31
    And when it finishes, then it runs the next
  • 38:31 - 38:33
    one. When that’s done, then it runs the
  • 38:33 - 38:35
    next one, and so on. And it all kicks off
  • 38:35 - 38:41
    with one script. Let me show you here on this system. If you go into /etc,
  • 38:41 - 38:44
    you’ll find a folder in there called
  • 38:44 - 38:48
    rc.d, this folder right here. Now this
  • 38:48 - 38:51
    folder has changed a lot over the years. Originally, there were
  • 38:51 - 38:53
    different folders. In fact, see all these
  • 38:53 - 38:59
    light blue folders up here? Yeah. rc, rc0.d, rc.local, rc.assistant.
  • 38:59 - 39:07
    All these are, like, previous iterations of what SysV init or, like, System V init or System 4 init.
  • 39:07 - 39:10
    Like, each of them kind of relied on different folders. They’re there for backward
  • 39:10 - 39:15
    compatibility. But now pretty much everything is tucked away inside rc.d.
  • 39:15 - 39:22
    And if we go into that folder and look, you’ll see a number of subfolders based on all sorts of
  • 39:22 - 39:24
    different stuff. And each one of these
  • 39:24 - 39:26
    kind of influences what SysV init does
  • 39:26 - 39:28
    when it launches. And the main one that
  • 39:28 - 39:30
    we want to pay attention to is this guy
  • 39:30 - 39:36
    right here, rc.sysinit. When SysV init starts, this is the
  • 39:36 - 39:38
    first thing that it calls. It reaches out
  • 39:38 - 39:40
    to this script and it executes it.
  • 39:40 - 39:44
    And then from there, it will proceed to
  • 39:44 - 39:47
    spawn other things. And there’s a lot of things that influence what it’s going to
  • 39:47 - 39:52
    spawn, which are all kind of tied to parts of what it does. But this one’s
  • 39:52 - 39:57
    really the heart and soul of it. So, /sbin/init runs, and it
  • 39:57 - 39:59
    calls this script to then initialize the
  • 39:59 - 40:01
    system. Does that one script then call
  • 40:01 - 40:04
    other scripts? Absolutely. Yep. So, it
  • 40:04 - 40:07
    runs, and as it’s finishing, it’s then
  • 40:07 - 40:08
    launching other things based on a
  • 40:08 - 40:10
    certain criteria. Right? And that criteria
  • 40:10 - 40:16
    is what’s called a run level. So notice all these other folders over here, rc0.d, rc1.d, rc2.d.
  • 40:16 - 40:18
    Those are tied to run levels. Right?
  • 40:18 - 40:20
    So, in Linux, you have seven run levels,
  • 40:20 - 40:27
    and each one indicates a different status of the machine. So, for example, 0 actually means the system is shut down.
  • 40:27 - 40:31
    Now, you might wonder to yourself, like,
  • 40:31 - 40:33
    why would I have a run level for shutdown? Well, you’ve got to tell the system
  • 40:33 - 40:35
    to shut down. And you might actually have
  • 40:35 - 40:36
    scripts that are part of that.
  • 40:36 - 40:40
    When your system is shutting down, it needs to, you know, basically
  • 40:40 - 40:45
    perform a few tasks, maybe stop writes to the hard disk or so on and
  • 40:45 - 40:49
    close out what it’s doing. And those will be part of the shutdown script. You also
  • 40:49 - 40:53
    have rc6 here, which is reboot. And so that’s
  • 40:53 - 40:55
    when your system is rebooting. It might
  • 40:55 - 40:56
    have certain tasks. They might be the
  • 40:56 - 40:57
    same. They might be totally empty.
  • 40:57 - 41:00
    There might be no tasks tied to reboot and shutdown, but that’s what those run
  • 41:00 - 41:03
    levels are. The other run levels
  • 41:03 - 41:05
    are kind of up to the distro, and they
  • 41:05 - 41:08
    can operate in different modes, and
  • 41:08 - 41:12
    they’ve kind of standardized on it. So, most of the distros follow the
  • 41:12 - 41:14
    same pattern, but it’s not a guarantee, so
  • 41:14 - 41:16
    always be careful. If you’re not sure,
  • 41:16 - 41:19
    check your distro’s documentation. A lot of them
  • 41:19 - 41:22
    will document what their run levels are
  • 41:22 - 41:31
    in their initialization table. So there’s a file. If I’m just going to cat /etc/inittab, that’s the initialization table.
  • 41:31 - 41:33
    And when you look in that file, most
  • 41:33 - 41:35
    distros do take the time to document the
  • 41:35 - 41:36
    run levels. And so I can see right here
  • 41:36 - 41:40
    for CentOS, and this applies to CentOS
  • 41:40 - 41:42
    and Red Hat Enterprise Linux. I can see
  • 41:42 - 41:46
    the seven run levels. So see how 0 is halt? Halt is when the computer’s
  • 41:46 - 41:52
    turned off. And then 6 is reboot. 1 is single-user mode. That means that only
  • 41:52 - 41:54
    one user can be logged into the system,
  • 41:54 - 41:56
    and nobody else can be logged in. Right?
  • 41:56 - 41:58
    That’s actually pretty handy if you’re
  • 41:58 - 42:01
    an administrator. Sometimes you want to modify a file, but it’s always in use.
  • 42:01 - 42:03
    Somebody’s accessing it, or there’s a
  • 42:03 - 42:04
    file share, and people are accessing it over
  • 42:04 - 42:06
    the network. You need to make sure you
  • 42:06 - 42:07
    have exclusive access to it. So, you can
  • 42:07 - 42:12
    boot in single-user mode, and you know you’ll have that. There’s multi-user
  • 42:12 - 42:16
    without networking, which is actually multi-user without networking.
  • 42:16 - 42:20
    So, it boots up in the full mode, but with no networking services at all. And that
  • 42:20 - 42:21
    way, you can ensure nobody over the
  • 42:21 - 42:23
    network is getting into the system.
  • 42:23 - 42:26
    If you think you’ve been hacked, here’s a great way to boot and shut off your
  • 42:26 - 42:28
    network, and now you can get in and
  • 42:28 - 42:31
    examine the system, and you know you’re not communicating out.
  • 42:31 - 42:35
    You have full multi-user mode, which is run level 3, and that’s where you’re
  • 42:35 - 42:40
    saying, look. I want to boot up to a command-line interface,
  • 42:40 - 42:41
    but I’m allowing multiple users.
  • 42:41 - 42:45
    People can SSH in, or I have more than one console, so a lot of people can be
  • 42:45 - 42:49
    working on the system. That’s the run level that most servers run at.
  • 42:49 - 42:53
    Run level 4, it’s actually not used. I don’t think I’ve seen a distro yet
  • 42:53 - 42:55
    that makes use of it, but it’s available.
  • 42:55 - 42:56
    So, if you want to create a custom run level, that’s usually what run
  • 42:56 - 43:02
    level 4 is reserved for. Run level 5, it’s labeled here as x11, but
  • 43:02 - 43:04
    that means we want to boot a GUI. We want
  • 43:04 - 43:06
    to boot a graphical user interface, which
  • 43:06 - 43:07
    is typically the X environment, but, you
  • 43:07 - 43:10
    know, you might have some alternate user interface.
  • 43:10 - 43:14
    And that one is multi-user with networking and a GUI. So, it’s kind of
  • 43:14 - 43:17
    like the full shebang. Right? It boots
  • 43:17 - 43:19
    everything, and then you get access to
  • 43:19 - 43:21
    the system. So those are the run levels.
  • 43:21 - 43:27
    And looking at this /etc/rc.d folder, it’s kind of hard to say
  • 43:27 - 43:31
    all these subfolders: rc1.d, rc2.d.
  • 43:31 - 43:34
    These contain scripts. So when I boot into
  • 43:34 - 43:38
    multi-user mode, command line only,
  • 43:38 - 43:43
    rc.sysinit is going to run, and then it’s going to look in rc3.d,
  • 43:43 - 43:44
    and it’s going to look at what scripts
  • 43:44 - 43:46
    are in there. And look at all these
  • 43:46 - 43:49
    scripts that I’ve got. I’ve got where it’s starting the quota daemon and
  • 43:49 - 43:54
    IP tables for the firewall, the LVM monitor for my logical volume manager,
  • 43:54 - 43:56
    network manager is starting, postfix.
  • 43:56 - 44:00
    All these different things will start when my system starts. These are tied to
  • 44:00 - 44:04
    various services. Right? Postfix. That’s an email service.
  • 44:04 - 44:07
    SSHD, that’s my secure shell daemon.
  • 44:07 - 44:09
    These are all being started. That’s how
  • 44:09 - 44:11
    it knows what services to fire up, and
  • 44:11 - 44:13
    it’s all controlled by the run level.
  • 44:13 - 44:21
    If I look in, let’s go into rc5.d. Alright. So this is if I boot with a full GUI,
  • 44:21 - 44:22
    and I’ll see things are a little bit
  • 44:22 - 44:24
    different. There’s a lot of overlap. Right?
  • 44:24 - 44:26
    So I still see SSHD right there. I still
  • 44:26 - 44:29
    see postfix. Right? But I’ll also see
  • 44:29 - 44:31
    some things in here that are tied to
  • 44:31 - 44:33
    X Windows. Let’s see. I should be seeing
  • 44:33 - 44:37
    GDM in here somewhere. I must be skimming over it.
  • 44:37 - 44:38
    It's hard when you're under pressure on
  • 44:38 - 44:40
    the show, but somewhere in here is GDM,
  • 44:40 - 44:43
    which starts the graphical device manager to let you get
  • 44:43 - 44:47
    logged in and all of that. So, it's additional things that will get loaded.
  • 44:47 - 44:48
    You may see variations depending on
  • 44:48 - 44:53
    which run level you boot into. That's what all happens right here.
  • 44:53 - 44:58
    Then there's one more thing that may not get loaded, but typically doesn't.
  • 44:58 - 45:02
    Back in rc.d, there's one more file,
  • 45:02 - 45:08
    rc.local. Okay, on really old systems, I'm talking Unix, right?
  • 45:08 - 45:13
    It would run rc.sysinit, then it would run all the scripts for
  • 45:13 - 45:18
    the run level, and then last, it would run rc.local. And that was your way to
  • 45:18 - 45:22
    override anything else that was run before we kind of undo or redo stuff.
  • 45:22 - 45:25
    Most distros have the file for backwards
  • 45:25 - 45:27
    compatibility, but don't do anything with
  • 45:27 - 45:29
    it. So it's usually empty, or it may not
  • 45:29 - 45:30
    even be called at all. You'd have to
  • 45:30 - 45:32
    check your run level scripts to see if
  • 45:32 - 45:34
    they return to it or check rc.sysnet.
  • 45:34 - 45:40
    So, like, if I take a look at my rc.local to see what's in it, yeah, it's
  • 45:40 - 45:44
    pretty much empty. In fact, it just touches a file to indicate that it's locked, and that's it.
  • 45:44 - 45:47
    So, it's not used in this case, but it is
  • 45:47 - 45:49
    there. So if you want to make use of it,
  • 45:49 - 45:54
    you can. Versus if I take a look at rc.sysnet
  • 45:54 - 45:55
    and take a look at that one, there's all
  • 45:55 - 45:56
    sorts of stuff in here as it goes
  • 45:56 - 46:01
    through mounting your disks or turning on SELinux right there, you know,
  • 46:01 - 46:03
    doing all sorts of other stuff. This is a
  • 46:03 - 46:08
    big deal, right? It's got a lot of work to do. rc.local is empty.
  • 46:08 - 46:09
    Do we have to modify the script files
  • 46:09 - 46:13
    for each run level when we install a service?
  • 46:13 - 46:14
    Alright, so, yeah, good question. Right? So I
  • 46:14 - 46:16
    showed where sshd was in there. Right?
  • 46:16 - 46:18
    So my SSH daemon is starting automatically,
  • 46:18 - 46:19
    and it's more in more than one run level.
  • 46:19 - 46:21
    Right? So if I install a service, like,
  • 46:21 - 46:24
    let's say I want to install a web server.
  • 46:24 - 46:26
    Right? So I'm gonna install Apache.
  • 46:26 - 46:30
    And when I install Apache, I want it to run when my system boots. Okay?
  • 46:30 - 46:33
    Well, if you notice with SSH, it was set to
  • 46:33 - 46:36
    start at run level three, and it was set to start at run level five.
  • 46:36 - 46:38
    They skipped run level four because it's not
  • 46:38 - 46:41
    used, and they skipped run level two because run level two boots with no
  • 46:41 - 46:45
    networking. So why bother starting a web server if there's no networking,
  • 46:45 - 46:48
    right, or SSH in that case?
  • 46:48 - 46:49
    I might want a web server to do the same
  • 46:49 - 46:50
    thing. So I want the web server to start
  • 46:50 - 46:53
    with run level three and run level five.
  • 46:53 - 46:56
    So, to do that, I mean, we could manually
  • 46:56 - 46:57
    create scripts and put them in there, but
  • 46:57 - 46:59
    what you'll find is that most of this is
  • 46:59 - 47:01
    handled for us through a handful of
  • 47:01 - 47:05
    commands. Alright? Let me just show you kind of how this works. So, on my
  • 47:05 - 47:10
    system, I'm gonna do a sudo yum install httpd. Alright? So, I'm going to
  • 47:10 - 47:13
    install Apache. Right?
  • 47:13 - 47:16
    And, apparently, it's already installed. Great.
  • 47:16 - 47:18
    So, I've already got it installed.
  • 47:18 - 47:20
    Now, once it's installed, you can go through
  • 47:20 - 47:27
    and create scripts. So, if I go back to, let's see, rc3.d
  • 47:27 - 47:29
    and take a look in here. I don't recall
  • 47:29 - 47:32
    seeing HTTP in the list. Oh, it is. Right there.
  • 47:32 - 47:36
    K 15 httpd. So, I've already got it installed, and it's already set to start.
  • 47:36 - 47:38
    So I picked a bad example. Alright.
  • 47:38 - 47:40
    Let's do, maybe we'll do a database
  • 47:40 - 47:42
    instead. Is MySQL on here? Let me just
  • 47:42 - 47:43
    skim the list to make sure I don't make
  • 47:43 - 47:47
    the same mistake twice. Alright. We'll do MySQL. So, I'm gonna do sudo yum
  • 47:47 - 47:53
    install mysql and I'll do mysql-server as well, and
  • 47:53 - 47:54
    we'll do -y, so it just does it.
  • 47:54 - 47:56
    Alright. So, it's gonna install the
  • 47:56 - 47:58
    database server. Now, when the database
  • 47:58 - 48:01
    server is installed, it's not set to run
  • 48:01 - 48:02
    automatically, or at least it shouldn't
  • 48:02 - 48:06
    be. So, I'm gonna pull a list back up here and look,
  • 48:06 - 48:08
    and I do see it actually created a link
  • 48:08 - 48:11
    right there. So, it's smart enough to
  • 48:11 - 48:13
    create the links with what the default
  • 48:13 - 48:16
    run level should be. If we want it on
  • 48:16 - 48:19
    three and we want it on five, it shows up right there.
  • 48:19 - 48:22
    Now, what if that's not what we want, though?
  • 48:22 - 48:25
    I don't want MySQL to run. Maybe I only
  • 48:25 - 48:27
    want it to run when my GUI is running,
  • 48:27 - 48:29
    but I don't want it to run when I'm
  • 48:29 - 48:31
    booting to command line only, or, you know,
  • 48:31 - 48:33
    something crazy like that. Right? Well, you
  • 48:33 - 48:34
    can actually manipulate these pretty
  • 48:34 - 48:36
    easily without having to write or even
  • 48:36 - 48:39
    edit the scripts at all. Right? So, you
  • 48:39 - 48:40
    certainly can come in here. Most of these
  • 48:40 - 48:41
    are just symlinks. See how they're light
  • 48:41 - 48:44
    blue? If you pull up a long listing,
  • 48:44 - 48:48
    you'll see they're almost all symlinked to the init.d folder,
  • 48:48 - 48:50
    and in there is where these text files
  • 48:50 - 48:52
    are kind of being created. But we can
  • 48:52 - 48:55
    manipulate all of this a few different ways.
  • 48:55 - 49:02
    The easiest way is to use the chkconfig command, chkconfig.
  • 49:02 - 49:05
    What chkconfig does is it lets you turn services
  • 49:05 - 49:08
    on or off for different run levels.
  • 49:08 - 49:11
    It defaults to run level three and five.
  • 49:11 - 49:18
    So if I do a chkconfig, and then I say httpd on,
  • 49:18 - 49:20
    that's gonna turn--oh, I need to sudo that. Let me sudo that command there.
  • 49:20 - 49:23
    There we go. That's gonna turn on
  • 49:23 - 49:27
    httpd for run levels three and five. If I boot
  • 49:27 - 49:29
    at run level one or two or four, it's not going
  • 49:29 - 49:32
    to start, but three and five, it will.
  • 49:32 - 49:34
    And that's kind of its default operation.
  • 49:34 - 49:38
    If I say off, then HTTP is not going to start in run
  • 49:38 - 49:40
    level anything. Right? It's gonna turn
  • 49:40 - 49:42
    them all off. Right? But I can go a step
  • 49:42 - 49:43
    further with this command, and I can be
  • 49:43 - 49:50
    really specific. I can say, for example, chkconfig httpd --level
  • 49:50 - 49:52
    and then specify the levels I want. The
  • 49:52 - 49:54
    default would be three and five, like that.
  • 49:54 - 49:56
    So run levels three and five. That's where
  • 49:56 - 49:57
    it's gonna start. Maybe I want it to
  • 49:57 - 49:59
    run--well, I'll just leave it at three and five.
  • 49:59 - 50:03
    Let's take MySQL. Oops.
  • 50:03 - 50:08
    Sorry. I have to do one more option here and say on. There.
  • 50:08 - 50:11
    So, I left off a little on tag, so it got unhappy with me.
  • 50:11 - 50:13
    Let's say that I want MySQL to run in
  • 50:13 - 50:17
    run level three and run level five and
  • 50:17 - 50:21
    run level two. Right? MySQL actually doesn't need the network in a
  • 50:21 - 50:23
    traditional sense. You can use named pipes
  • 50:23 - 50:24
    to access it. So maybe I want that
  • 50:24 - 50:26
    database up and running even when the
  • 50:26 - 50:29
    network stack isn't up and going. So, for
  • 50:29 - 50:37
    that one, I could do a sudo chkconfig --level 2 3 5 mysqld on,
  • 50:37 - 50:39
    and now it's set to go with multiple run
  • 50:39 - 50:41
    levels. So, it's kind of a cool one.
  • 50:41 - 50:43
    It's easy to lose track of this stuff,
  • 50:43 - 50:44
    though, and we could go and sift through
  • 50:44 - 50:46
    the scripts and figure out where it's
  • 50:46 - 50:47
    set to run and where it's set not to.
  • 50:47 - 50:56
    So, if I go into /etc/rc.d/rc2.d and take a look in there, I can
  • 50:56 - 50:59
    find--wait a minute. HTTP is in the list.
  • 50:59 - 51:01
    Right? It's actually symlinking to all
  • 51:01 - 51:03
    of these, but the script is telling it
  • 51:03 - 51:05
    only to run at a certain run level, and
  • 51:05 - 51:06
    that run level is left off. So if we try
  • 51:06 - 51:09
    and track it right here through these symlinks,
  • 51:09 - 51:11
    it really doesn't help. We don't actually
  • 51:11 - 51:13
    know if it's gonna start or not.
  • 51:13 - 51:14
    So, a better way to figure out what's gonna
  • 51:14 - 51:19
    start and what isn't is to use chkconfig --list.
  • 51:19 - 51:21
    And that's gonna list your services
  • 51:21 - 51:23
    and tell you what run levels they're
  • 51:23 - 51:25
    gonna execute at. So, if I run that, I
  • 51:25 - 51:27
    get this nice big list right here,
  • 51:27 - 51:31
    and it's got all seven run levels with a
  • 51:31 - 51:34
    simple on or off next to each one. So, I
  • 51:34 - 51:38
    can find HTTP--right, whoops.
  • 51:38 - 51:42
    If I can scroll back up to it. I can find it right here,
  • 51:42 - 51:45
    and the mouse is my biggest challenge with this stuff.
  • 51:45 - 51:50
    And I can see run levels one, zero, one, and two, it's off. Run level three is
  • 51:50 - 51:53
    on. Four is off. Five is on. Six is off. So, just three
  • 51:53 - 52:05
    and five are on, even though I saw an httpd link in /etc/rc.d/rc2.d. And if I look at MySQL right here,
  • 52:05 - 52:07
    I can see that it's a little different
  • 52:07 - 52:09
    in that it's two, three, and five that are
  • 52:09 - 52:12
    all turned on. Okay? This is a better way
  • 52:12 - 52:14
    to see what's being run at each run
  • 52:14 - 52:15
    level. If you just look at the links, you
  • 52:15 - 52:17
    can't tell. You'd have to follow the link,
  • 52:17 - 52:19
    and then you'd have to edit the script that it--or
  • 52:19 - 52:20
    at least view the script that it links
  • 52:20 - 52:22
    to. And in that script, it'll have an if
  • 52:22 - 52:24
    statement at the beginning that says,
  • 52:24 - 52:29
    like, if runlevel ==, and it'll list the run levels where it's gonna start. That's a pain.
  • 52:29 - 52:33
    chkconfig --list. You'll see it all right there. And if you have a huge list,
  • 52:33 - 52:35
    you can always pipe that into the grep
  • 52:35 - 52:39
    command. And once it's in the grep command,
  • 52:39 - 52:41
    you can just say, like, I'm looking for
  • 52:41 - 52:43
    MySQL, and then you just get that one
  • 52:43 - 52:44
    line. That makes life a little bit easier
  • 52:44 - 52:46
    there. So, pretty easy to verify.
  • 52:46 - 52:48
    We could also just do it manually.
  • 52:48 - 52:50
    Like, maybe, maybe I'm not so concerned about
  • 52:50 - 52:51
    when the system starts. I just need to
  • 52:51 - 52:53
    use MySQL for a little while right now.
  • 52:53 - 52:55
    Okay? So, for that, you'll use the service
  • 52:55 - 53:01
    command, and we can just come in and say sudo service
  • 53:01 - 53:03
    and then start, and whatever the service is
  • 53:03 - 53:07
    we wanna start. If I wanna start httpd, there we go. Oops. I got it
  • 53:07 - 53:13
    backwards. service httpd start. There we go.
  • 53:13 - 53:16
    Sorry. You’ve got to say the service name and start. I got it
  • 53:16 - 53:20
    backwards. So, sudo service start httpd,
  • 53:20 - 53:22
    and now it starts up. And I can do the
  • 53:22 - 53:24
    same thing with, like, mysqld, which it'll
  • 53:24 - 53:25
    probably be unhappy that I haven't
  • 53:25 - 53:27
    secured it yet, but we'll do it anyway.
  • 53:27 - 53:29
    We can start that one up. Yeah.
  • 53:29 - 53:30
    It's warning me that I need to run the
  • 53:30 - 53:32
    security startup script. But, but there
  • 53:32 - 53:35
    it goes. So, now those two services are running.
  • 53:35 - 53:38
    But let's say I'm at runlevel five right
  • 53:38 - 53:40
    now, and those are up and running, and I
  • 53:40 - 53:42
    reboot my system. If those aren't set to
  • 53:42 - 53:43
    start with runlevel five, they won't be
  • 53:43 - 53:45
    started when I reboot. So, using the
  • 53:45 - 53:46
    service command like this is not
  • 53:46 - 53:48
    permanent. It's really just kind of a
  • 53:48 - 53:53
    temporary thing just to get the service up and running and so on. And you
  • 53:53 - 53:58
    can do a service status for any service
  • 53:58 - 54:02
    and take a look at, like, httpd and--oops.
  • 54:02 - 54:07
    Backwards again. Darn it. service httpd status. There we go.
  • 54:07 - 54:09
    And if we still do that one, I'll get it right
  • 54:09 - 54:10
    eventually. We can see if the service is
  • 54:10 - 54:12
    actively running right now.
  • 54:12 - 54:14
    You may find where it started with the
  • 54:14 - 54:16
    script, and then something went wrong and
  • 54:16 - 54:18
    it closed. And so, even though rc.d is
  • 54:18 - 54:20
    telling it, yeah, you need to start this
  • 54:20 - 54:22
    at boot, it may not be booted. So, this is
  • 54:22 - 54:24
    a good way to quick-check that
  • 54:24 - 54:26
    and see. And I'll do the same thing with
  • 54:26 - 54:28
    mysqld, and I can verify right there.
  • 54:28 - 54:31
    They're both up and running and happy.
  • 54:31 - 54:34
    Don, how does this V unit know which runlevel to boot to?
  • 54:34 - 54:36
    You know, I glanced over it earlier.
  • 54:36 - 54:38
    So, we kind of have some defaults, right,
  • 54:38 - 54:39
    where we're saying boot to the graphical
  • 54:39 - 54:41
    user interface or boot to the command
  • 54:41 - 54:44
    line, and your OS distribution will typically default to
  • 54:44 - 54:47
    one or the other. That comes from the
  • 54:47 - 54:49
    /etc/inittab folder--or file--
  • 54:49 - 54:54
    that I opened earlier. If I cat that one, /etc/inittab.
  • 54:54 - 54:56
    Alright. So, this is the file I
  • 54:56 - 54:58
    brought up on screen earlier to show the
  • 54:58 - 55:00
    runlevels. Well, there's one tiny little
  • 55:00 - 55:01
    line at the very end that I didn't
  • 55:01 - 55:08
    mention. It's this guy right here that says id:5:initdefault:. Alright.
  • 55:08 - 55:13
    So, this is telling the system what runlevel to boot to by default. So, mine is
  • 55:13 - 55:16
    at the default to runlevel five, which is a
  • 55:16 - 55:18
    full graphical user interface. If I'm
  • 55:18 - 55:20
    setting up a server, I probably don't
  • 55:20 - 55:22
    want the GUI. So, I'll tell it to boot to
  • 55:22 - 55:24
    the command line--to boot to multi-user
  • 55:24 - 55:25
    mode, which is runlevel three. So, to do
  • 55:25 - 55:27
    that, I would just edit this file and
  • 55:27 - 55:29
    change that five to a three, and then
  • 55:29 - 55:31
    when you reboot, it's gonna boot to the
  • 55:31 - 55:32
    command line. And when you're ready, you
  • 55:32 - 55:33
    can always change it back to a five
  • 55:33 - 55:36
    again, and it'll boot up, and that'll
  • 55:36 - 55:38
    be kind of the runlevel that it goes to.
  • 55:38 - 55:41
    Now, that's if I wanna make a permanent change.
  • 55:41 - 55:42
    Let's say it's just a temporary thing,
  • 55:42 - 55:44
    right? Maybe I'm having some kind of
  • 55:44 - 55:49
    crazy issues with X Windows, and I just want
  • 55:49 - 55:50
    to dump out and get rid of all of X
  • 55:50 - 55:53
    Windows and then come back into X Windows, but not
  • 55:53 - 55:57
    kind of, not have to restart all my services or whatever. So, we can actually
  • 55:57 - 55:59
    change the runlevel whenever we want
  • 55:59 - 56:01
    while the system is running. Now, it can
  • 56:01 - 56:02
    have disastrous effects, so we need to be
  • 56:02 - 56:05
    a little bit careful with it. You know, all these scripts are gonna execute
  • 56:05 - 56:10
    and do stuff. But if you ever wanna change your runlevel just temporarily, there's a command
  • 56:10 - 56:12
    you can run, which is init.
  • 56:12 - 56:15
    If you run init followed by a number--let me pull up the
  • 56:15 - 56:18
    man file for that. So, if you just run
  • 56:18 - 56:19
    init followed by a number, it will
  • 56:19 - 56:24
    immediately change your system into whatever that other
  • 56:24 - 56:26
    init level happens to be. Now, if you're
  • 56:26 - 56:28
    gonna do this, be really careful of
  • 56:28 - 56:31
    other users on their systems. You can really mess with them, especially if
  • 56:31 - 56:33
    you're about to drop to single-user mode.
  • 56:33 - 56:35
    So, you might wanna use the who command
  • 56:35 - 56:37
    to see who's logged in and how they're
  • 56:37 - 56:38
    accessing the system, to say, alright,
  • 56:38 - 56:40
    here are people I need to contact before I
  • 56:40 - 56:42
    change this runlevel. But assuming
  • 56:42 - 56:44
    you're the only one in here, let me take
  • 56:44 - 56:46
    this system. I'm gonna go to
  • 56:46 - 56:47
    multi-user mode. Right? So, I'm gonna
  • 56:47 - 56:50
    say sudo init 3. I wanna drop to
  • 56:50 - 56:53
    runlevel three. And when I run that,
  • 56:53 - 56:56
    my system drops to the eensy-weensy tiny little console,
  • 56:56 - 56:59
    and so I get to see that. And we get out
  • 56:59 - 57:01
    the magnifying glass. Yeah. Yeah. We'd have
  • 57:01 - 57:04
    to use it here. So, we lose all the, you know, the cool
  • 57:06 - 57:08
    screen enhancements and so on. So now
  • 57:08 - 57:11
    we're just in straight command-line mode,
  • 57:11 - 57:14
    and there we are. Right? So, now I'm kind of operating in this
  • 57:14 - 57:17
    different world. X Windows has been
  • 57:17 - 57:19
    completely unloaded, and it's executed
  • 57:19 - 57:22
    the scripts found in that rc3.d folder
  • 57:22 - 57:25
    to move me into this kind of mode. And I
  • 57:25 - 57:28
    can switch back if I want by saying init--
  • 57:28 - 57:30
    well, I gotta sudo that--sudo init,
  • 57:30 - 57:32
    and then move back into runlevel five.
  • 57:32 - 57:36
    And when I run that, there we go. I see that it takes me back
  • 57:36 - 57:41
    to my CentOS login screen, and I can get logged back in.
  • 57:41 - 57:43
    And now I'm back into the GUI. So, we
  • 57:43 - 57:45
    quickly and easily change our runlevel
  • 57:45 - 57:47
    like that. Now,
  • 57:47 - 57:49
    most of the time, we're gonna do it
  • 57:49 - 57:52
    just like I did right there. It's gonna happen fast.
  • 57:52 - 57:53
    There may be times where you want to
  • 57:53 - 57:58
    schedule changing the runlevel, and the official recommended way to do that
  • 57:58 - 58:00
    is to use cron. You can set up a cron job to
  • 58:00 - 58:02
    change the runlevel at a certain time if you want.
  • 58:02 - 58:04
    That's one technique you can use.
  • 58:04 - 58:10
    There used to be a command called telinit, and what telinit would do
  • 58:10 - 58:12
    is it would let you set a time delay for
  • 58:12 - 58:14
    when the runlevel would change. And many
  • 58:14 - 58:17
    systems don't have the command anymore.
  • 58:17 - 58:20
    And--let's see if I do. I have to spell it right if you wanna find it.
  • 58:20 - 58:24
    I do have it. Okay. So, there it is. telinit lets you change the system
  • 58:24 - 58:25
    runlevel, so just like the init command,
  • 58:25 - 58:27
    but the difference is it takes some
  • 58:27 - 58:29
    options. The init command really didn't
  • 58:29 - 58:33
    take any options. This one does. And one of the options is -t, which is in
  • 58:33 - 58:38
    here somewhere. Well, I didn't see it, but it does exist, where you can come in and you can say
  • 58:38 - 58:40
    telinit, and then what number you wanna go to.
  • 58:40 - 58:45
    So, I'll say I'm gonna jump to runlevel three, and you can say -t
  • 58:45 - 58:47
    followed by a time. And, you know, maybe I
  • 58:47 - 58:49
    wanna wait thirty seconds before I do it,
  • 58:49 - 58:51
    and so I can specify that. I would need
  • 58:51 - 58:55
    to sudo that command. And then I'll do that -t 30 and run it.
  • 58:55 - 58:57
    Okay. So, that's gonna start that
  • 58:57 - 58:59
    timer. Well, notice how mine just totally
  • 58:59 - 59:04
    ignored the timer. Mhmm. That's because the telinit command--it's an old command.
  • 59:04 - 59:06
    It's not supported by most. Now, a lot of users
  • 59:06 - 59:07
    don't even have it, and the ones that do
  • 59:07 - 59:09
    don't fully support it. So, it's kind of
  • 59:09 - 59:11
    the old way. So instead, if you wanna
  • 59:11 - 59:13
    schedule it, you're a little better off
  • 59:13 - 59:17
    by jumping in and doing a cron job to
  • 59:17 - 59:19
    run init at a certain time. Fantastic
  • 59:19 - 59:24
    information. Managing services with SysV init. Great episode as usual. Thank you,
  • 59:24 - 59:25
    Don. And before we move on, I think you
  • 59:25 - 59:26
    have something else you'd like to say.
  • 59:26 - 59:28
    Yeah. You know, I've mentioned before that
  • 59:28 - 59:30
    SysV init is really old, and that systemd is
  • 59:30 - 59:33
    really new and shiny. And I personally like systemd,
  • 59:33 - 59:38
    but I think that SysV init is still really, really important. You are gonna
  • 59:38 - 59:40
    find many, many servers that are out
  • 59:40 - 59:42
    there that rely upon it. And if you start
  • 59:42 - 59:44
    jumping into other operating systems
  • 59:44 - 59:47
    like FreeBSD and OpenBSD and those guys,
  • 59:47 - 59:51
    they still use SysV init too. systemd hasn't made the leap over to those sides yet.
  • 59:51 - 59:54
    So, learning how this works is a
  • 59:54 - 59:55
    skill that you'll be able to apply in many
  • 59:55 - 59:57
    different places. It's definitely, definitely a
  • 59:57 - 59:58
    valuable one to have. Great advice.
  • 59:58 - 60:01
    Thank you, Don. Here's some more great stuff to come.
Title:
CompTIA Linux+: Managing Services #11
Video Language:
English
Duration:
01:00:01

English subtitles

Revisions Compare revisions