How to turn off a computer on Linux. Linux: Reboot and shutdown the system

Turning off the computer is an action that ordinary users encounter almost every day. If you have to turn off the server quite rarely, then ordinary desktop computers are subject to this operation very often. Most users turn off Linux in the system's GUI. In the KDE desktop environment this is done through the main menu, and in Gnome and Unity there is even a special toolbar button for this task.

But it is not always possible to turn off a Linux computer this way. For example, if the desktop environment is not working, then there is nothing left to do except shut down the computer from the command line. In addition, shutting down a Linux computer through the terminal is the only possible way for servers that do not have a graphical interface. As you'll see, this method is also much more flexible than a regular shutdown, since you can set the shutdown time you want, or shut down the computer while waiting for a specific program to complete.

To shut down a Linux computer, use the shutdown command. There are also poweroff and halt commands, but they are for non-standard cases. Usually, just shutdown is enough. You can also turn off Linux from the console using the SysRq keyboard shortcuts. But first, let's look at the syntax of the shutdown command.

The command syntax is very simple:

$ shutdown [options] [time] [message]

Options set the shutdown parameters, we will look at them below. Time can be set in hh:mm format in 24 hour format. You can also use the +minutes entry, indicating how many minutes from the current moment you need to turn off your Linux computer. The now constant is also available, indicating that you need to turn it off right now.

The message parameter allows you to specify a message that will be displayed in the terminal before shutdown.

Options

  • --help- display help about the program
  • -H, --halt- turn off power without killing processes or remounting file systems
  • -P, --poweroff- normal shutdown
  • -k- do not perform real actions, but only display a message
  • --no-wall- turn off the computer, but do not display a message
  • -c- cancel scheduled shutdown of linux from command line

That’s basically all the options of the utility; there are more than enough of them for such a simple action. Now we can move on to the examples, but first let's look at what happens when Linux is turned on.

Linux shutdown process

Naturally, the shutdown linux command or any other does not automatically shut down the system and turn off the computer. It simply passes the shutdown request to the init system and then to the kernel. And they already perform a number of complex operations to prepare and turn off the computer. In short, here they are:

  • Ending user processes
  • Signal SIGTERM to all processes
  • SIGKILL signal to all processes
  • Mounting file systems in read-only mode
  • Preparing external devices for shutdown
  • User space locking to ensure that no user code is ever run again
  • Shutdown and power off most peripheral devices
  • Turning off the computer's power

Now you understand the essence of the shutdown process and why you can’t just unplug the power cord from the outlet. If you do this, the computer will turn off instantly and will not perform all necessary operations, and this may result in the loss of unsaved or even recently saved data, damage to the file system, etc. Therefore, it is advisable to turn off the computer correctly using shutdown or other special utilities.

Now let's move on to examples.

Shutting down your computer in Linux

1. shutdown

The simplest and most commonly used linux computer shutdown command will shut down the computer immediately:

sudo shutdown -h now

As you can see, the usual shutdown is used, and the time constant now, that is, now.

Now let's try to schedule the Linux computer to shut down after five minutes:

sudo shutdown -h +5 "The computer will shut down in 5 minutes"

We scheduled a message to appear before shutting down.

Now let's cancel the shutdown of the Linux computer:

sudo shutdown -c

In the same way, we can specify the exact shutdown time, for example at nine in the evening:

sudo shutdown -h 21:00

As I already said, not only the shutdown linux command can shut down the computer, there are several other utilities that can do this. Let's consider them too.

2. reboot

The reboot command is usually used to reboot the system, but it can also shut down the computer. We will not consider it in detail, because it is even simpler than the shutdown command. To turn it off, you need to specify the -p option:

3.halt

This command also turns off the computer. She just does it in her own unique way. It does not perform any preparatory steps before shutting down, but simply turns off the power:

Using halt may damage the system

4.poweroff

This is analogous to halt, doing exactly the same thing:

5. SysRq

Remember at the beginning of the article, I talked about the keyboard shortcut to turn on the computer? Let's consider this issue in more detail.

SysRq is a subsystem implemented at the kernel level. The kernel processes all keystrokes, and with the help of this subsystem, it can accept commands from the user even when the system is completely frozen. The main purpose of this subsystem is to work with the computer in problematic situations, for example, if you think that your computer has been infected by a virus, or the computer has frozen and needs to be turned off. Keyboard shortcuts are used to access SysRq Alt+PrtScr+number

The most interesting thing is that we can safely shutdown a Linux computer. To do this, hold down the Alt + PrtScr keys and press alternately:

  • R- unlock keyboard
  • E- send a SIGTERM signal to all processes
  • I- send a SIGKILL signal to all processes
  • S- transfer all data from the file system cache to disk
  • U- remount read-only file systems
  • B- reboot

You can also use the keyboard shortcut instead of all this Alt+PrtScr+O, in this case, the entire procedure will be performed automatically; this command is not supported by all kernels.

conclusions

Now you know everything you need to know about how to shut down Linux through the terminal. If you are caught off guard by an error or your computer freezes, you know what to do. If you have any questions, write in the comments!

No operating system is perfect. Even if this is the case, there may be problems with drivers and applications. Linux is no exception. Even though it is more stable than Windows, there will likely come a time when you need to reboot your Linux computer.

But how could you do this? Several commands are available to shut down or restart your Linux computer through the terminal.

Let's look at them one by one and see how and when to use these commands.

1. Disconnection

If you are done with Linux, the solution is simply to disable it, you can use the command syntax for this:

shutdown

For example, to shut down your computer immediately, you would use:

Where -h means stop and now- now, which means the instruction must be executed immediately. Various delays can be used. For example, you could use instead +5 , which will tell the computer to perform the shutdown procedure in five minutes.

If you want to use message(message), it will flash to all registered users:

shutdown -h +5 "The server is shutting down, save your work and log out."

Remember that you can find a complete list of switches for these commands by typing:

Restart with -r

An alternative is to use the command -r to restart the computer. It is used instead -h, therefore, to reboot your computer or server you can use:

shutdown -r +5 "The server is rebooting in five minutes, save your work and log out."

Any scheduled shutdown or restart can be canceled by issuing the command -c:

2. Reboot

Since the shutdown command has a restart option, it is not surprising that the reboot command has a shutdown option.

Standard reboot command:

This will prompt your computer to shut down and turn on again. However, if you want to turn off the device, then the key -p will work:

Another option is a forced reboot. This can be useful if an application or service is frozen and you need to quickly reboot:

This command will force reboot your Linux PC.

3. Stop

We've already seen the switch -h above, but stop can be used as a command in itself. This will shut down your computer immediately with a simple four letter word:

Switch -f can also be used with stop, but results are inconsistent and may lead to system stability problems.

4.Power off

You may prefer the command terminology poweroff. This does the same thing as halt, except that typing takes twice as long.

However, in addition to using -f to force power off, you can also use the key -w to log the system reboot call in /var/log/wtmp. This is a potentially useful debugging tool like -verbose, which can help with shutdown problems.

poweroff --verbose

5. Emergency option: REISUB

All of the above commands can be used in situations where the system is running without problems. But what if the computer or server freezes and cannot be restarted in an acceptable way?

The answer then is a keyboard combination. If you switched from Windows, you probably know that Ctrl + Alt + Del displays a menu with turning off as an option. If you hold it longer, the machine will turn off automatically. On a Mac, meanwhile, you simply have to hold down the power button (an option that also works on Windows hardware).

In Linux, the keyboard shortcut to reboot Alt + Print Screen + B. However, if this doesn't work or there is a more complex problem, you can change the combination using up to six keys.

This is known as REISUB, due to the following forced abbreviation:

  • un R aw - Returns keyboard control back from the X display server.
  • t E rminate - Sends a SIGTERM termination signal to all processes to terminate gracefully.
  • k I ll - as above, but with a SIGKILL signal that causes processes to terminate immediately.
  • S ync - resets data to disk.
  • U nmount - Remounts all file systems to a read-only state.
  • re B oot - as you would expect.

For this to work you must hold Alt + Print Screen, and then press the keys one by one: R E I S U B, in the same order. The delay between each press should be 1-2 seconds. This is necessary in order to give some time to complete all the above processes. Please note that this method does not typically work on ARM-based computers.

Help, I accidentally turned off my Linux computer or server!

We have seen how to cancel a shutdown or restart command. However, there are situations that you run the shutdown command when a vital process is running, especially on a remote server. You can get around this by setting molly-guard, which can override the shutdown by checking certain parameters.

For example, there is a script that checks for the presence of SSH sessions. If you issue a reboot, stop, or shutdown command, molly-guard will require the name of the host you are about to shut down.

To do this, install molly-guard in the terminal:

sudo apt-get install molly-guard

Since molly-guard runs in the background it will detect a command like poweroff and will report that an SSH session has been detected. You can then enter the server hostname to confirm the shutdown or click Ctrl+C to cancel. Healthy!

These five ways to shut down a Linux computer from the command line are especially useful because they can be used on the computer itself or via remote SSH. Because these commands are very concise, they are suitable for quick use - which can lead to accidental reboots from time to time! Fortunately, the molly-guard utility is enough to avoid this.

To learn more about , check out our reference table.

The "shutdown" command safely shuts down the system. All logged-in users receive a notification that the system is being shut down and login operations are blocked. You can turn off the system immediately or after a specified delay.

Shutting down Linux from the command line is very convenient and many system users control their PC through the command line in such things. All processes are first notified that the system is preparing to sleep or reboot via SIGTERM. This gives programs such as vi time to save the file being edited, mail and news programs, the ability to log out, etc.

"Shutdown" does its job by signaling the init process, asking it to change its runlevel. Runlevel 0 is used to stop the system, runlevel 6 is used to reboot the system, and runlevel 1 is used to put the system into a state in which administrative tasks can be performed (single-user mode). Runlevel is 1 by default unless -h or -r options are specified.

Your system may have a different set of parameters for the shutdown command; Check the device documentation.

Shutdown Linux from the command line

shutdown [-akrhPHfFnc] [-t sec] time [message]

  1. -a Control access to the shutdown command using the /etc/shutdown.allow control access file. For more information, see the Access Control section below.
  2. -k Don't shutdown, but send warning messages as if the shutdown were real.
  3. -h Tells the system to shut down and then stop.
  4. -P Instructs the system to shut down and then shut down.
  5. -H If the -h option is specified, this option specifies that the system should boot to the boot monitor on systems that support it.
  6. -f Skip fsck after reboot.
  7. -F Force fsck after reboot.
  8. -n Do not call init to terminate processes; Instruct shutdown to do this itself.
  • This option is not recommended and its results are not always predictable.
  1. -c Cancel pending shutdown. (This does not apply to "shutdown now", which does not wait until shutdown.) You cannot specify a time argument with this option, but you can specify an explanatory message that will be sent to all users.
  2. t sec. Tell the initializer to wait seconds between the processes of sending the warning and the kill signal before moving to another runlevel.
  3. time The time argument specifies when the shutdown operation should be performed.

Time can be formatted in different ways:

First, it can be an absolute time in the format hh:mm, where hh is the hour (1 or 2 digits, from 0 to 23) and mm is the minute of the hour (in two digits).

Secondly, it can be in the format +m, where m is the number of minutes to wait.

Additionally, the word is now the same as indicating +0; He immediately shuts down the system.

  1. Message. A message sent to all users, along with the standard shutdown notification.

Details

If the shutdown is planned for the future, it will create an advisory file /etc/nologin, which forces programs like login to not allow new users. This file is created five minutes before the shutdown sequence starts. Shutdown deletes this file if it is stopped before it can signal initialization (that is, it is canceled or something went wrong). It also removes it before calling init to change the runlevel.

The -f flag means "reboot quickly". This only creates an advisory /fastboot file that can be checked by the system when it appears again. The system's boot rc file ("rc" stands for "runcom", which is short for "run commands") may check for the presence of this file and decide not to run fsck because the system was shut down properly. After this, the boot process should remove /fastboot.

The -F flag means "force fsck". This only creates an advisory /forcefsck file that can be checked by the system when it appears again. The boot rc file can check to see if this file is present and will decide to run fsck with a special "force" flag to check if the file systems are properly unmounted. After this, the boot process should remove /forcefsck.

The -n flag causes shutdown not to call init, but to kill all running processes. Shutdown will disable quota, accounting and exchanges and unmount all file systems.

Access control

Shutdown can be called from init when the CTRL-ALT-DEL key combination is pressed if a corresponding entry exists in /etc/inittab. This means that anyone with physical access to the console keyboard can shut down the system.

To prevent this, shutdown can check to see if an authorized user is logged in. If shutdown is called with the -a argument (add this to the shutdown call in /etc/inittab), it checks to see if the /etc/shutdown.allow file is present. It then compares the logins in this file with the list of people who are logged in to the virtual console (from /var/run/utmp). Only if one of the authorized users or root is logged in will it take effect. Otherwise it will write a message

Shutdown: no authorized users logged in

To the physical system console. The format of /etc/shutdown.allow is one username per line. Blank lines and comment lines (prefixed with #) are allowed. This file currently has a limit of 32 users.

Note that if /etc/shutdown.allow is not present, the -a argument is ignored.

Turning off Linux and computer power

The -H option simply sets the initialization environment variable INIT_HALT to HALT, and the -P option simply sets this variable to POWEROFF. A shutdown script that calls halt as the last thing in the shutdown sequence must check these environment variables and call halt with the correct options for those options to actually have any effect.

Files

  • /fastboot
    /etc/inittab
    /etc/init.d/halt
    /etc/init.d/reboot
    /etc/shutdown.allow

Shutting down Linux from the command line: examples

Schedule the system to turn off at 8 a.m.

Schedule the system to turn off at 8 p.m.

shutdown +15 "Upgrading hardware, downtime should be minimal"

Schedule the system to shut down after fifteen minutes. Along with the usual notification to users that the system has shut down, they will be given a descriptive message about the hardware update.

Shut down Linux immediately

Immediately shut down the system and automatically reboot it.

Immediately turn off and automatically shut down the system.

Related commands

  • Halt - Stop the computer.
  • Poweroff - Stop the computer.
  • Reboot - Stop the computer.
  • Wall - Send a message to all logged in users.

Translation from a wonderful English resource computerhope.

If you still have any questions on the topic “Shutting down Linux from the command line,” you can write them to us in the comment form on the site.

If you find an error, please highlight a piece of text and click Ctrl+Enter.

Let's look at how to restart or shut down your computer from the command line. I use this when, when installing some driver or under heavy load on the processor, the interface stops responding to my actions (although this happens extremely rarely). It is possible to kill a "gluttonous" process through the command line, but it is not always known which process it is, so a quick solution is to restart the computer.

Reboot the computer

I do this. I go to the console by pressing the keyboard shortcut Ctrl+Alt+F1. In the console, you must first enter your username and password (no characters are displayed when entering a password). And I run the command:

Sudo reboot

You will again need to enter the administrator password and press Enter. The computer will begin to restart.

You can also use the command to restart your computer shutdown, for this you need to use the key -r:

Sudo shutdown -r now

Turn off the computer

To shut down the computer from the command line, you need to run the command:

Sudo shutdown -h now

You can also shut down your computer by running the command:

Sudo halt

And also can be used.

This article is aimed at those new to Linux and those who are interested in how to reboot Linux from the console. For me, restarting the computer in the terminal or using the graphical interface is elementary, but this is the base, not all beginners know it, and we need to write about it.

Today we will look at issues such as the Linux reboot command from the console, rebooting remotely, and directly rebooting in the GUI. Let's start with a normal system reboot.

Here, as they say, it could be simpler. Let's look at rebooting in Ubuntu Unity first. Just click on the gear button in the upper right corner of the screen and select Shutdown:

Then in the window that opens, click on the item:

In the Gnome desktop environment, everything is very similar to Unity, but in KDE you need to open the main menu, go to the tab exit, and select the item reboot:

Then confirm reboot.

Rebooting Linux in the terminal

But here the scope is much wider; there are about a dozen commands that can be used to reboot Linux. Some need root privileges, others don't, some look simple and easy to remember, while others are long and complex. Next we will look at them all.

The first Linux reboot command, the most common and the simplest:

As you can see, the utility requires superuser rights. After pressing Enter, the computer will immediately reboot.

The shutdown utility, which is used to shutdown, also allows you to restart the computer; for this you need to pass the -r parameter to it. Plus, you can specify the reboot time. Now - 0 or now, in one minute +1 in two - +2, etc:

sudo shutdown -r +1

In initialization systems compatible with Init Scripts, there were system load levels - 0,1,2,3,4,5,6, level 0 meant shutdown, 6 reboot, other modes of system operation are not of interest to us now. You can switch between levels using the init command. But again, you need superuser rights. Thus:

The dbus system message service can also reboot the computer:

/usr/bin/dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart

Superuser rights are no longer needed here. These were the usual ways to reboot Linux, but there is another, non-standard one or two. These are the magical SysRq keys. The Linux kernel monitors the pressing of certain key combinations and performs the necessary actions in response to them. First we enable sysrq support:

echo 1 > /proc/sys/kernel/sysrq

It’s better to do this in advance, since this method is useful when the system is frozen and does not respond to anything:

nano /etc/sysctl.conf

kernel.sysrq = 1

To activate SysRq combinations, hold down Alt + SysRq and press the key code. For a normal reboot, it is recommended to use the following sequence: R E I S U B, press the keys in the same sequence with an interval of approximately a second.

  • R- returns keyboard control if the X server was terminated incorrectly;
  • E- the kernel sends a SIGTERM signal to all processes except init;
  • I- sends a SIGKILL signal to all processes except init;
  • S- the kernel synchronizes file systems, all data from the cache is transferred to the hard drive;
  • U- remounts all file systems to read-only mode;
  • B- immediate reboot, without synchronization, and additional preparations.

Before rebooting, the system waits for all processes to complete, stops all services, dismounts and mounts file systems in read-only mode. This is what we do by pressing these key combinations in sequence. But if you need to reboot the system now without waiting for all processes to shut down, for example, a server, you can immediately send signal B. Like this: Alt + SysRq + B.

SysRq You can use it without keyboard shortcuts by writing the desired operation code to the /proc/sysrq-trigger file:

echo b > /proc/sysrq-trigger

The system will reboot as is, without stopping services or preparing file systems, so unsaved data may be lost and the file system damaged.

Remote Linux reboot

If you have access to the server via ssh, then you can very easily reboot Linux remotely using one of the above commands, for example:

ssh [email protected]/sbin/reboot

But again, for this operation you need to have root rights on the remote server.

conclusions

Now you know how to reboot Linux, you even know how to reboot the server via ssh. If you have any questions, ask in the comments!

Related posts: