 Two of the most frequent commands that you're going to use in Linux, whether you're a Linux desktop user, or maybe you're a Linux sys-admin, is shutting down your machine and rebooting your machine. And this seems like, well, that's a rather simple process, especially for those of you using desktop Linux, because typically you're going to find a shutdown button or a restart button in your desktop environments panel or in the menu system somewhere. And that's great that you have a GUI method, but today I wanted to talk about some other methods, especially involving the command line for shutting down a Linux machine and rebooting a Linux machine. So let me switch over to my desktop here. And obviously if you're in a graphical environment, shutting down and rebooting is very easy. If you can actually log into a graphical environment in your desktop environment, check your menu system. You've probably got buttons or menu entries for shutdown and reboot and log out and things like that. If it's not in the menu system, sometimes on the panels themselves, you'll see a little circle, a half circle with an arrow for reboot, and then the circle with the little downward slash through it for power off. Now what happens, though, when you're on a system that doesn't have a graphical environment? So you have a graphical environment, but it's crashed, right? Your desktop environment has crashed, and all you can get into is a TTY. What do you do then? Or if you're a Linux sysadmin, you're working on machines that sometimes don't have Xorg or Wayland installed. You don't have a display server. All you have is the command prompt. Well, let me pull up a terminal. So the overwhelming majority of Linux distributions these days use system D and pretty much all the common Linux server distributions use system D. And you can reboot a machine with system D using sudo system CTL start reboot dot target. So reboot is a system D service and you can do sudo system CTL start name of service in this case reboot to actually run that service right now. In this case, it'll reboot the machine right now if I hit enter. Now you don't need to typically type all of this. You can actually shorten this instead of reboot dot target. Get rid of the dot target and you really don't need start either. You can actually shorten this command to sudo system CTL reboot and that works just fine. And actually you can shorten that even more. You can actually do a sudo reboot by itself and that's okay. And system D has made it so convenient these days because you're on a system D distro system D doesn't even require sudo privileges for reboot anymore. So you can actually just type one word reboot and that works as well. Now all of those various commands that I just showed you work with reboot. They also work with shutdown and with halt. So I could have done a sudo system seat. Well CTL I can type correctly shutdown but I don't need to do the system CTL. I could just do sudo shutdown or system D distributions. You could just do shutdown. You don't even need sudo privileges and the same with halt. You could just do the halt command. Now one interesting thing is the shutdown commands and the reboot commands do have flags. For example most people don't know this but you can actually do a sudo shutdown dash R for reboot. So you can actually use the shutdown command to do a reboot. Why you would need to do that I don't know. And then after that you can specify a delay. You know just give it a time for a delay or you can do the word now for sudo shutdown reboot the machine right now. You can also halt the machine using shutdown instead of dash R for reboot do dash capital H for halt. If you want to specify a specific time in the future for a shutdown maybe you want to schedule a shutdown later this evening you know I could do sudo shutdown and then I could actually just specify a specific time in this case I'll do 2230. And if I schedule this and later decide hey I want to cancel that all I need to do is come back and do a sudo shutdown space dash C for hey cancel that last command. Now I mentioned that reboot also has some flags you could do a sudo reboot and give it the dash P flag and that will do a power off so essentially shutting down the machine you could also do sudo reboot dash F and that is a force flag because if you're in a situation where you try to do a reboot and the machine is hung or frozen sometimes a sudo reboot dash F can actually force the reboot to take place. Now we should also talk about non-system D distributions typically on those you could also do a sudo init and then give it a number a run level number for example 6 sudo init 6 is a reboot command and if we change the run level here from 6 to 0 sudo init 0 is a shutdown command. Now what happens if you're trying to do something on a machine that is hung up or frozen or is unresponsive what do you do then? Well for those of us that have physical access to our machine so your typical Linux desktop user or laptop user you just hit the power button right just power off the machine physically those of you working on remote systems if you don't have a way to actually physically power off that machine then you know as a last resort I'm going to give you a nuclear option and that is using the sysrq key. So if you're not familiar with the sysrq key if you have a full keyboard like a 110 key keyboard chances are you have a key on it that says sysrq it may say print screen slash sysrq a double function key or it may just say print screen the print screen key is actually just the sysrq key on many keyboards and what you want to do is you want to hold alt plus sysrq and then you have to give it a sequence of keys after that to force either a reboot or a shutdown. Now the sysrq method of rebooting and shutting down will only work if you have privileges to do so and on most Linux distributions you may not have privileges to do so so let me actually get back over here to this terminal to check and see if you have privileges for sysrq do a cat and do slash proc slash sys slash kernel slash sysrq check that file go ahead and cat it out and see what the output is and in my case it's 176 that is not a good number for privileges so on many Linux systems you'll get something like 176 here on Linux Mint or I know on Ubuntu and I think on Arch you'll get 16 as the output what you want is actually one one gives you all privileges with the sysrq commands if it happens to read zero that means you have absolutely no privileges with the sysrq commands that are available so let's change this to one now many people are going to want to do something like this sudo echo one and then direct that into that file right that's typically how you do this on Linux we're just going to overwrite that file we're going to overwrite 176 with one but the problem here if I hit enter it's going to complain that we don't have permissions and it didn't even ask me for sudo password that's just the way this is written with sudo echo and then we're directing this into this protected file here the way you want to do this to get around this permission problem is a very similar command we're just going to do echo without the sudo one and then pipe that into sudo and then t and then the name of the file which was slash proc slash sys slash kernel slash sysrq and now it outputs one and actually wrote that to the file if I go back and cat that we also get one so now we have privileges to use sysrq so if on my keyboard I held alt plus sysrq right now and then gave it a specific sequence of commands you know I could reboot or shut down the machine now I'm in a virtual machine here so I can't actually do this on camera because when I hit alt plus sysrq it's not going to recognize I'm doing this in a virtual machine it's actually going to think I'm doing it on my physical system and it's actually going to shut down right so it's actually going to kill the recording so I can't demonstrate this on camera but I will give you the sequence of letters that you actually want to hit so let me clear the screen here so what you want to do is you want to hold alt plus sysrq or alt plus print screen and then you want to do the following sequence for a reboot you want to hit r on the keyboard and if you're in a command prompt a tty prompt after r you're probably going to get some output because that actually executes a command that what that does is it switches your keyboard from raw to excel a te mode and then after that particular command with r gets run you want to run the following command you want to run e what he does is it sends a sig term signal to all running processes except a knit and again if you're in a tty you'll get some output in the terminal and then what you want to do is run i and that sends a sig kill process a sig kill signal to all the processes running except a knit and then you want to hit s and s syncs all your mounted file systems then you want to hit u and that remounts all mounted file systems as read only and then finally the last command all this while holding alt plus sysrq you want to hit b for reboot now if you want to do a shutdown do the exact same sequence of letters except instead of b for reboot at the end do o for a shutdown now the sysrq method again it's kind of the nuclear option you could have some work that's lost using that command that's not typically how you want to reboot and shut down a machine right that's the method you use when the system is just hung up or it's completely unresponsive and nothing else works typically you just want to use your system ctl commands or in most Linux distributions you can just do a simple sudo shutdown or a sudo reboot now before i go i need to think a few special people i need to think the producers of this episode i'm talking about devon gave james matt michael mitchell paul scott west alan chuck commander angry dieokai dylan george lead linux ninja maxim like erion alexander peace arch and for door polytech red prophet steven and willy these guys they're my house tiered patrons over on patreon without these guys this episode you just watched would not have been possible the shows brought to you by each and every one of these ladies and gentlemen as well all these names are seeing on the screen right now these are all my supporters over on patreon because i don't have any corporate sponsors i'm just sponsored by you guys the community if you like my work want to see more videos about linux free and open source software and how to shut down your machine and things like that subscribe to distro tube over on patreon all right guys peace for a shutdown you could also kill the main breaker