Linux Essentials - systemd: Using the systemctl command

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] it's time to talk about systemd the technology that some people in the linux community just seem to love to hate now i'm going to spare you from the political side of systemd in this video this is the linux essentials series after all but what i am going to do is show you guys how to use the system ctl command to start stop and restart units and also some other examples as well i think by the end of this video you will know all the things that you need to know to manage the running units on your linux server or workstation but before we get into that i want to mention today's sponsor lenode lynode has been doing cloud computing since 2003 which is actually before amazon web services was even a thing on the nodes platform you can get your server up and running in minutes and they include all of the popular distributions such as centos debian ubuntu fedora and get this also arch linux and let's be honest what could be better than a linux cloud server provider that allows you to tell all of your friends i run arch the note has multiple server plans available to make any app scalable and flexible you can use it to host a blog set up a vpn server a minecraft server or you could do what i did and set up a website for your youtube channel because the official website for learn linux tv runs on lenode and lenode offers 24x7 365 support regardless of plan size so you can get live help from a real person when you need it new users can get started right now with one hundred dollars in credit towards a new account and i highly recommend you check them out because lenote is awesome and thanks to lenode for their continued support of learnlinux tv i really appreciate it and now let's get into the main topic at hand and check out the systemctl command all right so let's see some examples of systemd on my end i'm currently running fedora on my laptop but it really doesn't matter which distribution you are using on your end and the reason why that doesn't matter so much is because system d is used in quite a few linux distributions in addition to fedora it's also used in debian ubuntu arch linux centos alma linux rocky linux linux mint open sousa susa enterprise now all of those were just off the top of my head i could go on but i think you get the idea this is one of those situations where you're about to learn a set of commands that'll serve you quite well across various distributions but before we get started though we have to have something to manage with systemd we need an example service so what i'm going to do on my end is i'm going to install apache and if you are on a fedora or centos system then you can go ahead and run the same command that i'm about to run right now on the distributions that i've just mentioned this is the command right here don't worry if you are running debian or ubuntu i'll give you that command as well here in a moment i'm going to go ahead and get this installed and apparently i must have already installed it as you can see here the package httpd is already installed it's the latest version so i should be good to go on that if you are running ubuntu or debian then you should be able to run this variation of the command the package manager on debian and ubuntu is different those distros use apt as you see here linux mint that's the same thing but the package name is also different on those platforms they have it as apache 2. so i'm not going to press enter on my end obviously because i'm running fedora this command will not work on fedora but you should be able to use either the first command or the second command depending on your distro and if you're using something completely different than anything i've mentioned then all you should have to do is consult the documentation for your chosen distribution now at this point depending on your distribution apache might already be working so what i'll do is switch over here to a browser and i'm going to type localhost in the address bar just like that it's not going to work on my end because apache is not actually running but on debian and ubuntu and some other distributions as well those will often start the process for you automatically so it's possible that when you enter a local host in the browser of your computer you might actually see the result and if you are actually managing a linux server then of course localhost won't work you could just type in the ip address of that server and if apache is running you will see the default web page now as i mentioned in the intro the whole point of systemd among many other things is to help you manage what is running on your server laptop workstation or whatever you are running linux on it allows you to easily manage running services now systemd actually refers to services as units a lot of times i'll slip and i'll say services or daemons those are other terms that mean essentially the same thing but regardless of your favorite terminology system d names services as units just keep that in mind so right now we can type system ctl or system control that's the chosen command that is used to manage the services on your machine and if you press enter with no options or arguments there's going to be a lot of output i can keep scrolling and scrolling i'm just pressing enter and these are all the services that are running in the background but specific to our current use case we've installed apache and on my end it's not even running and that brings me to the first example that i would like to show you systemctl status this allows you to check the status of something that's running in the background or something that should or might be running in the background now on my end i know that the process name at least on fedora centos and similar distributions is httpd just like you see here if you are running on debian or ubuntu it's going to be apache 2 essentially the same name as the package that you installed but like i mentioned this variation of the system control command allows us to check the status of something the status of a unit and as you can see here the apache unit is not running and that makes sense because i wasn't able to access the web page either now i just made the font a little bit smaller there was something that was fairly important in the output and i want to make sure that i mention it so again systemctl status and then the name of the unit and what i wanted to show you is that it says disabled right here so from the output we can see that it's inactive and dead that explains why i can't access the default web page for apache so that means it's not running right now but also right here it says disabled a unit that is disabled will not automatically start when your server or your linux machine boots up so if i was to leave this as disabled and this was for example a web server then this particular web server would need me to log in and manually start apache every time i boot it up and that's not really something that i want to do so what i'm going to do is enable it because i wouldn't want to be in a situation where i restart a web server and then forget to actually start the web server process so let's go ahead and enable the service right now so in order to actually make changes we'll either need to be running as root or we can use sudo then systemctl we're going to be using that command quite a bit and then the keyword enable we want to enable a unit and then we type the name of the unit that we would like to enable in my case again that's httpd it could be apache 2 for you if you are running on debian or ubuntu but anyway i'll press enter and there we go so if i check the status we can see that it's actually enabled so if i was to restart my computer then apache would automatically start and that's great but maybe i don't want to restart my computer just to get apache running if this is a production server then i probably especially won't want to restart it so what should we do so for that i will type sudo systemctl yet again and the keyword is going to be start you want to start a unit and then you type the name of the unit easy enough so far and if i check the status we can see that it's active and it's running it's enabled because we did enable it something that's also pretty cool is down here we have some example log entries for the unit you may not always see this sometimes some units require sudo in order to see all of the detail here on the screen you don't actually need sudo to simply check the status of a unit whether or not it's running but sometimes some of the output might be hidden if you don't use sudo in this case it doesn't really matter we see everything here so nothing is hidden now the thing is apache still won't work in the case of fedora or centos because there's often a firewall that blocks it from being accessed even localhost believe it or not so i'm going to fix that real quick this isn't a tutorial on firewalls but this is just one of those things sometimes you do actually have to manage the firewall so i just entered the firewall command for allowing the http service and let's see if we have any different result now when we go to access apache on this instance and i had to remove the s in http because this is not a secure site i haven't actually added a certificate but anyway you can see that the default fedora test page has displayed in my browser so if i go back to my terminal we can do the exact opposite of starting the unit we can also stop the unit and this is going to play out exactly the way that you think this is going to play out i stopped the service and now the page is no longer accessible now speaking of opposites we enabled this unit before but we can also disable it as well so now the unit is disabled and also it's not running anymore and the reason why i did that and it's not just to show you stop and disable i wanted to show you another trick that i've picked up as well because as you know we can enable a unit by typing the keyword enable and then the name of the unit but what we can also do is add the dash dash now option to it as well which is actually not only going to just enable the unit it's also going to start it as well and as you can see i was able to start apache and enable it in one shot alright so you just saw an example of a server application a web server something that is going to run in the background and be accessible for whoever has access to the server but in some cases you might actually want a systemd unit to run only for a particular user and to better understand that let's see an example back in the terminal let's install yet another package and the package that we'll be installing is called sync thing and sync thing is an amazing utility that allows you to synchronize files between two or more computers it's actually my file synchronization utility of choice but i'm not going to go into too much detail about sync thing itself i have an entire tutorial that's all about sync thing if you want to learn more about it but i just figured it would be a good example of an application that you may want to run on a per user basis after all if you want to synchronize your files and not the files of other users on the system then you'll probably want the service to run under your user now whether or not the package for sync thing is available on your chosen distribution well that depends on the distribution i think by now it is available on most distributions but if you don't have sync thing available on your chosen distribution then don't worry about it it doesn't so much matter what we're running it's more about how we actually run a unit as a particular user so first of all what i'm going to do is run sudo dnf install and then sync thing and if you are on debian or ubuntu you should be able to run apt install sync thing pretty much the same difference i'll press enter and sync thing is available and it's installed now let's first check the status of sync thing itself and you might think that the command stops there but what i'm actually going to do is in addition to the name of the unit i'm going to type at and then the name of the user that i want to have it run as and of course i haven't started it or enabled it yet so it should show disabled and stopped and that's exactly the case it's disabled and it's also inactive now sync thing on fedora at least isn't going to really recognize trying to run it as a server-wide application i mean you can and there's different systemd units that you can install for this purpose but as you can see here checking the status of just sync thing isn't really something that you can do and this may differ from one distribution to another but the main takeaway is that you can target a specific user by typing at and then the username now i just showed you how to check the status of the systemd unit for sync thing in regards to a specific user now let's go ahead and start it as that user so i'm going to type sudo systemctl enable sync thing at then the name of the user that i want it to run as in this case my user and if we check the status we should see that it is now enabled which it is and i forgot to add the dash dash now option so i can manually start it up and we'll check the status again and we can see that it's running now as a spoiler the sync thing service runs under port 8384 so i already have localhost right here so i should be able to type colon8384 and there it is i am actually running sync thing on my laptop right now which is pretty cool now if you are running this on a server it's not actually going to allow you to hit the web console unless you configure it to allow you to hit the web console so if this step failed then don't worry about it the main thing is that we were able to run a systemd unit as a particular user which would be something that you may want to do from time to time but let's go ahead and take a look at the systemd unit file for sync thing so i'm going to go into user live systemd and then system and we have quite a few services here i'm going to scroll up a bit and then we have it right here sync thing at dot service so what i'm going to do is open that up in an editor and here it is now it's telling me at the bottom that this file is unwritable and that's just because i didn't use sudo if i'm going to show you guys something i don't want to accidentally make changes to it unless i really want to but we can see some information here about the actual unit itself and if you dive deeper into systemd then you'll be looking at files just like this or maybe even creating your own going over all of the possibilities that you can have in a systemd unit file is beyond the scope of this particular video but i'm going to give you some highlights right now on the second line here we have a description which is pretty self-explanatory and then we have a string for documentation which in this case is just asking us to check the man pages anyway we have this right here which is called after and this is pretty important for something like sync thing because we really wouldn't want sync thing to start before our network is even up and running if you think about it when you start your linux server laptop workstation or whatever you have it's going to start all of the units one after another as it comes online now if we left this to chance we could have sync things starting before the network which means it's probably going to fail because sync thing wants a network connection after all it's a tool you can use to sync files over the network so it's pretty much useless if we don't have a network so here we're telling systemd that we only want this particular unit to start after the network is already up and running so if you have a unit file that depends on something else this is actually a good practice we have user set to be a variable here essentially it's whatever user you run it against and the line here for exec start is basically just telling system d what command we want it to run when this unit starts up so we have user bin sync thing that's where sync thing is on the file system and then we have some options there and then we're telling it if for some reason sync thing stops working then we wanted to go ahead and restart so if it fails restart it and we have a bunch more options here and i'm not going to go over every single option here like i mentioned i'm not going to go into full detail into systemd unit files that could be a video oven by itself and here we can see that this is a multi-user target so by looking at this you might be under the impression that multi-user.target is where we define that we want to run this as a user but that's not actually the case essentially what this means is that we really don't need a gui for this sync thing is something that runs in the background and an alternative to this could be graphical.target which would mean that we want a graphical user interface started up but again sync thing doesn't require that so we're just simply going to require multi-user and not graphical now let's look at another command when it comes to systemd earlier in the video i showed you another command where i simply ran systemctl by itself and that gave me a very long list of service files here but they're not just service files there's actually much more to it than that i'm going to show you a more graceful way of listing the service files that are on the system as you can see here we have an entire option that's dedicated to listing unit files and specifically we want to list unit files that are of type service and you know what i think the output of this command looks a lot better than the command i gave you near the start of the video in fact you can even see the state of the service itself in the second column it tells you whether or not it's enabled and then in the third column it tells you what the vendor preset is and vendor preset what that means is whether or not the system d unit is desired to be started automatically as soon as you install the package for that utility if a vendor preset is disabled then that means as soon as you install the package for that utility then the systemd unit will not be enabled if the vendor preset is enabled then as soon as you install the package for that utility then that systemd unit is already going to be enabled right from the get-go and i can go ahead and just hold down enter and just keep scrolling through these as you can see we have quite a few services here on our system but you get the idea we were able to run this command right here to list the unit files that we have here on the system now there's a couple other examples i would like to give you as well i can't believe i almost forgot to talk to you about the restart option and it's probably self-explanatory but restart is going to do exactly what you think it's going to do it's going to restart the unit so this command right here apache in this case is going to go down and then it's going to come right back up and this is something that you would do if you changed the configuration file for a unit you would want to restart the unit to take advantage of the new settings now when it comes to apache and many other services as well we also have another option that does almost the same thing we have an option for reload as well what's the difference well basically if we restart apache like i mentioned it's going to go down and then it's going to come right back up but not all changes require a full restart of the unit a reload will actually keep the unit running it's not going to go down but it will actually take advantage of any new settings that you've added in the configuration now keep in mind that not all changes to settings are compatible with a reload sometimes you actually do need to restart the unit now reload is preferred anytime you can use it because if you were to restart apache and people were on your website then they would actually get disconnected from your website and they would need to reconnect to the website again sometimes you can't help it sometimes reload will not work but anytime you can use reload it's recommended and reload is very common when it comes to web servers so there you go that was my video on systemctl part of systemd i hope it was helpful and if you did find it helpful make sure you click that like button because that lets youtube know that you want to see more content just like this and also be sure to subscribe because i'll have some awesome content here very soon i can't wait to show you guys what i have in store for this channel anyway thanks for watching i really appreciate it [Music] you
Info
Channel: LearnLinuxTV
Views: 24,374
Rating: undefined out of 5
Keywords: Linux, Tutorial, Review, Howto, Guide, Distribution, Distro, Learn Linux, operating system, os, open-source, open source, gnu/linux, LearnLinuxTV, LearnLinux.tv, systemd, systemctl, services, linux services, init system, units, starting, stopping, daemon, linux tutorial for beginners, linux commands, linux tutorial, systemd basics, systemctl in linux, command line, systemd tutorial, service management, linux tutorial 2021, linux command line, linux commands to know, how to
Id: 5JVBpXiYMKo
Channel Id: undefined
Length: 26min 31sec (1591 seconds)
Published: Thu May 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.