Shutting down Linux from the command line. Linux: reboot and shutdown the system Reboot ubuntu server from console

The shutdown command safely shuts down the system. All logged in users are notified that the system is shutting 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 this sort of thing. All processes are first notified that the system is preparing to sleep or reboot via SIGTERM. This gives programs like vi time to save the file being edited, mail and news handlers, the ability to log out, etc.

Shutdown does its job, signaling init to change the 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 in a state where administrative tasks can be performed (single user mode) The default runlevel is 1 unless the -h or -r options are specified.

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

Shutting down Linux from the command line

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

  1. -a Control access to the shutdown command using the control access file /etc/shutdown.allow. For more information, see the Access Control section below.
  2. -k Do not shutdown, but send warning messages as if shutdown was real.
  3. -h Tells the system to shutdown and then stop it.
  4. -P Instructs the system to shutdown and then shutdown.
  5. -H If the -h option is specified, this option specifies that the system should boot into the boot monitor on systems that support it.
  6. -f Skip fsck after reboot.
  7. -F Force fsck after reboot.
  8. -n Do not invoke init to terminate processes; Instruct shutdown to do this yourself.
  • 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 initialist to wait seconds between sending a warning and a kill signal before going to another runlevel.
  3. time The time argument specifies when to perform the shutdown operation.

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).

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

Also, 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 a 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 generated five minutes before the shutdown sequence starts. Shutdown removes this file if it is stopped before it can signal initialization (that is, canceled or something went wrong). It also removes it before calling init to change the runlevel.

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

The -F flag stands for "force fsck". This only creates an advisory / forcefsck file that can be checked by the system when it reappears. The boot rc file can check if the file is present and decides to run fsck with the special "force" flag to check that the filesystems are unmounted correctly. After that, the boot process should remove / forcefsck.

The -n flag makes shutdown not call init, but kill all running processes. Shutdown will disable quota, accounting and swaps and unmounting of all file systems.

Access control

Shutdown can be invoked from init when CTRL-ALT-DEL 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 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 that file against the list of people who are logged in on the virtual console (from / var / run / utmp). Only if one of the authorized users or root logs in, it will take effect. Otherwise, it will write a message

Shutdown: no authorized users logged in

To the physical console of the system. The format /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 missing, the -a argument is ignored.

Shutting down Linux and powering the computer

The -H option simply sets the initialization environment variable INIT_HALT to HALT, and the -P option simply sets this variable to POWEROFF. The shutdown script that calls halt as the last thing in the shutdown sequence must check these environment variables and call halt with the correct parameters for those parameters 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 a system shutdown at 8 am.

Schedule a system shutdown at 8 pm.

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

Schedule a system shutdown in fifteen minutes. Along with the usual system shutdown notification, users will be given a descriptive hardware upgrade message.

Shutdown Linux immediately

Power down the system immediately and reboot automatically.

Shut down and automatically shut down the system immediately.

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 comments form on the site.

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

No operating system is perfect. Even so, there might be driver and application issues. Linux is no exception. While it is more stable than Windows, there will probably come a time when you need to restart your Linux computer.

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

Let's take a look at them in turn and see how and when to use these commands.

1. Disconnection

If you're done with Linux, the solution is to simply disable it, you can use the command syntax to do 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 can use instead +5 to tell the computer to shutdown after 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 off."

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

Restart with -r

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

shutdown -r +5 "Server reboots in five minutes, save your work and log off."

Any scheduled shutdown or restart can be canceled by entering 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 turn off and on again. However, if you want to turn off the device, 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 reboot quickly:

This command will force restart your Linux PC.

3. Stop

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

Switch -f can also be used intermittently, but the results are inconsistent and may lead to system stability issues.

4. Poweroff

You may prefer the command terminology poweroff... This does the same as halt, except that it takes twice as long to enter text.

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

poweroff --verbose

5. Emergency option: REISUB

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

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

On Linux, the keyboard shortcut for reboot Alt + Print Screen + B... However, if that 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 coerced abbreviation:

  • un Raw - Returns keyboard control back from the X display server.
  • t Erminate - Send the SIGTERM terminate signal to all processes to terminate gracefully.
  • k Ill - As above, but with a SIGKILL signal that causes processes to terminate immediately.
  • Sync - Flushes data to disk.
  • Unmount - Remounts all filesystems read-only.
  • re Boot - which is to be expected.

For this to work, you must hold Alt + Print Screen, and then, alternately press the keys: 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 a little time to complete all of the above processes. Please note that this method usually does not work on ARM computers.

Help, I accidentally shut down my Linux computer or server!

We have seen how to undo a shutdown or restart command. However, there are times when you run a shutdown command when a vital process is running, especially on a remote server. You can work around this by setting molly-guardwhich can undo the shutdown by checking certain parameters.

For example, there is a script that checks for SSH sessions. If you send a reboot, stop or shutdown command, molly-guard will ask for 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 is running in the background, it will detect a command like poweroff, and will report that an SSH session has been detected. Then you can enter the hostname of the server to confirm shutdown or press Ctrl + C to cancel. Helpful!

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

To learn more about, check out our reference table.

The shutdown command in Linux allows you to shutdown, reboot, or schedule your system to shutdown. This article explains the most common and useful examples of shutdown command in Linux.

Shutdown command syntax

Before seeing the use of the shutdown command, let's first look at its syntax. shutdown
  • options: you can specify if you want to stop, power off, restart, etc.
  • time: you can specify when to shutdown
  • message: you can send a custom message to all registered users

Note

The shutdown command needs superuser privileges. Hence, you must either be root or run the command with sudo.

5 practical examples of shutdown command in Linux

Now that you know the syntax for the shutdown command, let's see how to use it.

If you just use the shutdown command, it will start the shutdown process after one minute. Therefore, remember that the default time interval for the shutdown command is one minute.

Sudo shutdown Shutdown scheduled for Mon 2018-11-19 23:46:21 UTC, use "shutdown -c" to cancel.

Unsuspecting Linux users expect the shutdown command to shutdown the system immediately, but when they see a message such as UTC timestamp, they are often confused.

1. Immediate system shutdown.

You don't always have to wait a minute for the system to shut down. You can shutdown your system immediately by specifying the scheduled time +0 or now .

Sudo shutdown now

2. Schedule a system shutdown

You can schedule a stop in the future by specifying the time argument in either + t format or hh: mm format.

For example, if you want to shutdown the system after 20 minutes, you can use this command:

Sudo shutdown +20

If you want to shutdown the system at 3pm, you can use it like this:

Sudo shutdown 15:00

Needless to say, the key time and time zone is the system time.

Note

Five minutes before the scheduled shutdown time, the system will not allow any login activity. This means that a new user cannot log in for five minutes after the scheduled shutdown.

3. Reboot the system with the shutdown command

There is a separate reboot command, but you don't need to learn the new command just to reboot the system. You can use shutdown command in Linux to reboot.

To reboot the system using the shutdown command, use the -r parameter.

Sudo shutdown -r

The behavior is the same as the normal shutdown command. It's just that instead of shutting down the system, it will restart.

So, if you used shutdown -r without any time arguments, it will restart in one minute.

You can schedule a reboot in the same way as when shutting down.

Sudo shutdown -r +45

You can also reboot the system immediately with the shutdown command:

Sudo shutdown -r now

4. Sending a custom message

If you are in a multi-user environment and multiple users are logged in, you can send them your own broadcast message with the shutdown command.

By default, all registered users will receive a scheduled outage notification and time. You can configure the broadcast message in the shutdown command itself:

Sudo shutdown 15:00 "The system will be shut down to update hardware, please save your work."

You can use the shutdown command with the -k option to initiate a "fake shutdown". It will not shut down the system, but a broadcast message will be sent to all registered users.

5. Cancel scheduled shutdown

If you've scheduled a shutdown, you don't have to live with it. You can always cancel the shutdown with the -c option.

Sudo shutdown -c

And if you've passed the scheduled shutdown message like a good sysadmin, you can also notify other users to cancel the scheduled shutdown.

Sudo shutdown -c "scheduled shutdown was canceled"

Halt vs Power off

Halt (option -H): terminates all processes and shuts down the CPU,
Power off (option -P): Much like stopping, but also turns off the unit itself.

Historically, computers were used to shutdown the system, and then they would print a message like “shut down normally now,” and then the computers were turned off using physical switches.

These days, the termination should automatically shut off power to the system thanks to ACPI support.

These were the most common and most useful examples of the shutdown command in Linux. I hope you learned how to shutdown Linux system via.

If you have any questions or suggestions, do not hesitate to let us know in the comments section.

Problem
Just look at how many shutdown and reboot methods there are: shutdown, halt, init 0, poweroff, Ctrl + Alt + Delete ... Which one is better to use?
Decision
The choice is not so significant; use the method that suits you best. The following shutdown commands can only be used by the privileged root user:
# shutdown -h now
or
# poweroff
or
#halt
Shutdown after six minutes:
# shutdown -h +6
The shutdown command sends alerts to all connected users.
You can specify your own text:
# shutdown -h +6 "Time to stop working and start partying."
Console users will see the following message:
Broadcast message from root (pts / 6) Wed Aug 14 13:51:24 2003
Time to stop working and start partying.
The system is going DOWN for system halt on 6 minutes!
To cancel the shutdown of the computer, run the following command
as root:
# shutdown -с
Reboot is performed by the command
# shutdown -r now
or
# reboot
or by pressing Ctrl + Alt + Delete. Any user can reboot the computer, unless this feature is disabled in / etc / inittab (Section 7.11 explains how to disable reboots or grant permission to specific users).
Comment
Remember that the shutdown process must always be controlled. To all
computers, including machines running Linux, have to do
no small amount of work so that the power can be safely turned off. The system should shut down services, unmount file systems, and flush buffers to disk.
Shutdown, poweroff and halt commands are only executed by privileged
user root. The limitation looks pretty silly, because everyone
The window manager and desktop environment have their own shutdown menu, and any user in the vicinity of the computer can press the power button. But this is life, and you have to put up with it.
A possible way out is to grant limited privileges to execute shutdown commands with the sudo command. Another solution is based on the creation of a special group of users who are allowed to shutdown.
see also
shutdown (8), poweroff (8); section 8.20; See section 8.21.

Turning off a computer is an action that ordinary users face 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 graphical interface. In the KDE desktop environment, this is done through the main menu, and Gnome and Unity even have a dedicated panel button for this task.

But it is not always possible to turn off your linux computer this way. For example, if the desktop environment does not work, then there is nothing else to do but to 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 will see, this method is also much more flexible than a regular shutdown, since you can adjust the shutdown time you want, or turn off the computer after waiting for a specific program to finish.

The shutdown command is used to shutdown a Linux computer. There are also commands poweroff, halt, but they are for non-standard cases. Usually shutdown alone is enough. You can also turn off linux from the console using the SysRq keyboard shortcuts. But first, let's look at the syntax for the shutdown command.

The command syntax is very simple:

$ shutdown [options] [time] [message]

The options set the parameters for disabling, we will discuss them below. The time can be specified in hh: mm format in 24 hour format. You can also use the + minutes entry, indicating in how many minutes from the current moment you need to turn off the linux computer. The now constant is also available, indicating that you need to turn off right now.

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

Options

  • --help - display help on the program
  • -H, --halt - turn off the 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 linux shutdown from command line

That is, in principle, all the utility options, there are more than enough for such a simple action. Now you can get down 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 command does not automatically shutdown the system and turn off the computer. It just passes the shutdown request to the init system and then to the kernel. And already they perform a number of complex operations to prepare and shutdown the computer. In short, here they are:

  • Terminating user processes
  • SIGTERM signal to all processes
  • SIGKILL signal to all processes
  • Mount filesystems read-only
  • Preparing external devices for disconnection
  • User space locking to ensure that no user code is ever run again
  • Shutdown and power off most peripheral devices
  • Power off the computer

Now you understand the essence of the shutdown process and why you cannot simply unplug the power cord from the outlet. If you do this, the computer will turn off immediately and will not perform all the necessary operations, and this may threaten the loss of unsaved or even recently saved data, damage to the file system, etc. Therefore, it is advisable to shut down the computer correctly using shutdown or other special utilities.

Now let's move on to examples.

Shutting down a 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 is now, that is, now.

Now let's try to schedule a shutdown of the linux computer in five minutes:

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

We have scheduled the message to be displayed before shutting down.

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

sudo shutdown -c

Likewise, we can specify the exact shutdown time, for example, at nine in the evening:

sudo shutdown -h 21:00

As I said, not only the shutdown linux command is able to shut down the computer, there are also several utilities that can do this. Consider them too.

2.reboot

The reboot command is usually used to reboot the system, but it can also shutdown the computer. We will not go into 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 shuts down the computer. Only she does it in her own way. It does not perform any preparatory actions before shutting down, but simply turns off the power:

Using halt can damage the system

4.poweroff

It is analogous to halt, does exactly the same thing:

5. SysRq

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

SysRq is a kernel-level subsystem. The kernel handles 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 a computer in problem situations, for example, if you think that your computer has been infected by a virus, or the computer is 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 perform a safe shutdown of the linux computer. To do this, hold down the Alt + PrtScr keys and alternately press:

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

Alternatively, you can use the keyboard shortcut instead Alt + PrtScr + O, in this case, the whole 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 shutdown Linux through the terminal. If you are caught off guard by a bug or a freeze, you know what to do. If you have any questions - write in the comments!

Did you like the article? To share with friends: