How To Manage Linux Services with systemctl and journalctl | Sysadmin Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again everyone and welcome back to the channel it's really important to keep an eye on what's running on your lenode server there's going to be all kinds of services running in the background and these services provide value to you as the administrator as well as your users and some of them are things that run in the background as part of the distribution such as networking and things like that and then on top of that we add things like nginx maybe apache ssh there's all kinds of things that we can run on our lenode server so what we're going to do in this video is take a look at the services that are running in the background and how we interact with them in addition to that we'll also take a look at log files as well so let's go ahead and get started [Music] so let's go ahead and get started and explore the commands that are related to systemd including system control and journal control which are going to be two commands that you'll definitely want to master so the first command that i want to show you guys is system control which is abbreviated systemctl as you see on the screen right now and this command allows you to start stop and restart services that are running on your server in addition to that it also allows you to enable services so that they automatically start whenever your server itself starts and the best way to learn it is to see some examples now off camera i went ahead and installed apache on the server so basically i just ran sudo apt install apache 2 and this is the command for the debian and ubuntu package which is actually called httpd on other distributions but i just figured i would use it as an example in this video and with the systemctl command we can use it to check the status of a service in addition to controlling the service itself so we can actually see the health of the service that's running in the background and i'll use it against our example service of apache 2. and here we can see some very useful information first of all apache is active and running on this server so if i switch to a web browser and then paste the ip address of my server right here you can see the default apache website and the reason why this works is again because the service is active and it's running now in the linux world we use the term service to describe something that runs in the background and stays running but we can also refer to a service as a daemon spelled with an a d a e m o n and system d actually refers to services as units it's important to keep in mind because some of the options that we're going to explore later will assume the title of units but regardless of whether you call them services demons or units they all refer to the same thing so again we could see that the apache unit is active and running but if for some reason that wasn't the case and it wasn't running how would we actually start it so when you're viewing the status of a systemd unit you can press q on your keyboard to break out of this screen right here and we're back to the command prompt so if it wasn't running then what we could do is run sudo systemctl start and then the name of the unit in our case apache2 i'll press enter type in my super secret password and typically you won't see any output when you run the systemctl start command against a unit and if it wasn't running before it should be running now another thing that we want to keep our eye on as well is where it says enabled right here and it might not actually say enabled on your end basically some distributions will automatically enable a service when you install the package for that service and what enabled actually means is that this particular unit will start when the server starts so after we reboot the server we don't actually have to log in and manually start the service it's automatically going to come up generally speaking that's what you want however some distributions don't automatically default to starting a unit when you install a package for that unit so if it's disabled for example what we can do is actually enable it so to do that we just run sudo systemctl enable and then the name of the unit and that didn't really do us much good because well it's already enabled as we can see right here now just press the right arrow so we can see the lines of text that are further to the right because of how large i have the font size and here we can see the vendor preset is enabled and what vendor preset enabled means is that when this package was compiled it was set up so that when installed it would automatically enable the service contained within the package essentially it's the default so if this says vendor preset disabled that means that package will automatically be disabled when you install the package and if you want it to be enabled you'll have to do so manually now opposite to that you can simply disable the service by you guessed it changing the keyword to disable and if we check the status again you can actually see the word disable right here it's cut off so we see that it's disabled however it's still running disabling a unit does not turn it off it doesn't stop it it just makes sure that the next time you restart your server it doesn't automatically start up with the server but if you actually want to stop the unit then what we can do we can use the stop option and if we check the status again we can see that it's actually inactive it's not running and then on the browser i try to refresh the page here we can see that it's not responding and well that makes sense we stopped apache so it shouldn't be responding at all and it isn't let's go ahead and start it right back up again and just like you'd expect now it's responding again now in addition to the status command we can actually run a specific variation of system control that'll tell us if a unit is enabled or disabled as you recall can easily find that information out by running status as you see here which actually gives us the information right here but if we only want to see whether or not a unit is enabled or disabled here's what we can do you can run systemctl is hyphen enabled and then the name of the unit could be any unit you want or anything you have installed could be nginx doesn't matter and we can see here that it is disabled so what i'll do is just go ahead and enable it and run the other command again and now it's enabled so i used apache as the example in this video but that's not going to be the only unit that you'll have on your linux server even if you haven't even gotten to the point yet where you've installed any packages there's still going to be things running in the background and as an administrator you probably want to know well what's running and with systemctl we can easily find out what's running in the background by using the list units option then we can narrow down the output to show us only services in particular that are running and of course we're going to get a lot of output here and you can press the up and down arrow to go through the content here but if you wanted to know what's running in the background at least when it pertains to services well now you know now also included with systemd is a command called journalctl and this command in particular allows you to keep an eye on log files so basically systemd has its own command that you can use to go through log files when you are troubleshooting or trying to find information about something that's running now you may have noticed when we checked the status earlier we actually saw some of the log output down here at the bottom you may not see that if you don't have access to view it i didn't use sudo here so my user actually has access to all the log files i configured that off-camera so you may not actually see logging information on the bottom unless you use sudo or your user has access to the logging information but as you can see here systemctl is very helpful because it shows us the most recent log entries down here at the bottom so not only can we see the status of a service we can also view the log entries down here as well but we only see a few log entries though so anything beyond that we'll definitely want to use the journal ctl command to view even more information so let's actually run it journalctl i'll press enter and it's going to show us all kinds of logging information here from the various log files on the server it's going to be quite a bit of information here and just like systemctl status we can use q to quit out of the output and return back to the prompt what's probably more useful is to use the journalctl command to view output from a specific unit and we can do that by running journalctl dash u for unit and then the name of a unit that we want to take a look at for example maybe we're troubleshooting a website that we're hosting on our server that's made accessible via apache 2 and if that's the case we can actually use this variation of the journal ctl command right here to do exactly that so now we're seeing log files that are specific to apache we only have 14 lines here so it may not be the best example but just like before you can press the up and down arrow to go through the output and with the dash u option we're able to narrow down the output to a specific unit and i also have ssh running on the server as well so we can easily change the name of the unit to ssh we can also see some failed password attempts right here which i've done on purpose i wanted to show something useful in the log file so on another computer i tried to ssh into this server i typed the wrong password on purpose and we can see that right here if somebody was trying to get into our server that's not authorized to access our server and of course this would be of concern but considering that i did this on purpose we don't have to worry about it but as you can see we have some very useful information about the ssh server that we can see right here so let's see some more examples of journalctl so again i'm going to use ssh as the example and you can also press forward slash to activate the search mode and then you can type a keyword for example password or whatever keyword you might want to find in the file and not only did it bring me to the first occurrence of that word it also highlighted the matches as well which is very useful but one option i think is very important to learn when it comes to journalctl is the dash f option so for example i could do journalctl-u and then ssh in this case ssh is the name of the unit that i want to show log entries for and i can also add the dash f option which is similar to the tail dash f option which allows you to tail and follow the end of a log file and this is essentially the same thing when it comes to journalctl if i press enter notice that it brought me to the very end of the line but it didn't bring me back to the bash prompt so at this point anytime there's new information added to the logs for the name of the unit that i used with the dash u option it's going to immediately show it to me right here so essentially i'll see the log entries as they're generated for example on another computer i'll try to initiate ssh but first i'll press enter a few times here add some blank space there and on my other computer i essentially just tried to ssh into this server i failed the password on purpose and you actually saw that take place right here in the log file as i was doing it which is the value of the dash f option it allows you to follow the log entries for a particular unit and another option that i will leave you with is the sense option and it looks like this so let's say that i want to see logging information that occurred since a particular time this is very useful because if you have an issue and you're troubleshooting that you probably don't want to scroll through all the log entries to get to a particular point where this issue may have started to happen considering log files can get extremely large with tens of thousands of lines or more it's very useful to see log entries that start from a particular period of time which is what we're doing right here and we're going to see quite a bit of output here because quite a bit has happened since the time that i entered in the sense command but as you can see it's very easy to view log files that happened since a particular time and we can also target a particular unit as well and we get all the log entries that happened since the time that i've added in this case specific to ssh and of course there's a lot of other options when it comes to systemctl and journalctl so i highly recommend that you check out the documentation on the website as well as the man commands for both commands to see even more information but with what we've gone over in this particular video i think you have all the information that you need to get started with both of these commands so there you go you just saw some examples of the systemctl command as well as the journal ctl command and you can use those commands to manage the services that are running in the background as well as log files respectively i hope that was helpful as always make sure you click that subscribe button there's some awesome content coming very soon thanks for watching [Music] you
Info
Channel: Linode
Views: 3,064
Rating: undefined out of 5
Keywords: linode, linux, cloud computing, alternative cloud, linux server, open source, sysadmin, systemd basics, linux commands, linux tutorial, system administration, linux tutorial for beginners, service management, systemctl, journalctl, linux services, linux logs, linux tutorial ubuntu, learn linux, systemd tutorial, learn linux tv, service management in linux, linux logs tutorial, linux server commands, linux server tutorial, linux services tutorial, linux services restart
Id: 3kl62YSU9XA
Channel Id: undefined
Length: 14min 0sec (840 seconds)
Published: Wed May 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.