Let's look at how to restart or shut down your computer from 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 via the command line, but it is not always known which process it is, so fast decision This is a computer reboot.

From time to time it is necessary to remotely reboot or shut down the operating system under Linux control from the command line. This can be done different ways, then we will consider them.

All the commands listed below must be executed as the root user.

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. In addition, the now constant is available, indicating that you need to turn it off right now. shutdown command, with source -h.

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 in five minutes:

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

To shut down the system, use the halt and poweroff commands. The initial command shuts down the system but does not turn off the system's power. You will see a "System is halted" message indicating that you can turn off the power. This command Designed for older computers that do not support advanced power management. The second (poweroff) shuts down the system ( a set of elements that are in relationships and connections with each other, which forms a certain integrity, unity) and turns off its power.

Shutting down Linux using halt

The command is identical to the reboot command in its actions, the difference is that the halt command turns off the system. You can also turn off the computer by running the command:

Using halt may damage the system!

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

Shutdown Linux using poweroff

The poweroff command is similar to the halt command, except that after stopping the system, a special request is sent to the feeding control system to turn off power, which allows you to turn off systems remotely. You can also use:

Shutdown Linux using telinit 0

Using this command, you can tell the init daemon to go to a certain execution level, namely the number 0 indicates that you need to go to the 0th level (system stop). The telinit crew does not support setting pauses and warning messages. Typically used when testing changes made to the inittab file.

Shut down Linux from the command line without sudo

Possible via dbus and ConsoleKit. It can be controlled via dbus. Only now it seems to be cut out everywhere.

ConsoleKit is a daemon that serves user sessions.

If you have systemd, then hocus pocus won't work. Maybe experts will drop by and tell you how to do this via logind.

dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop

That's all, the discussion of the basic ways to shutdown and reboot Linux systems from the command line is completed. Now you know everything you need to know about how to shut down Linux through the terminal. If you're caught off guard by a typo or your computer freezes, you know what to do.

None operating system not ideal. 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 issues.

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 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 works in 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 random reboots from time to time! Fortunately, the molly-guard utility is enough to avoid this.

To learn more about , check out our reference table.

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.

Problem
Just look at how many ways there are to shutdown and reboot: shutdown, halt, init 0, poweroff, Ctrl+Alt+Delete... Which one is better to use?
Solution
The choice is not that significant; use the method that suits you best. The following shutdown commands can only be used by the 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 shutting down your computer, run the following command
With root rights:
# shutdown -с
The reboot is performed with the command
# shutdown -r now
or
# reboot
or by pressing Ctrl+Alt+Delete. Any user can restart the computer, unless you disable this opportunity in /etc/inittab (Section 7.11 explains how to disable reboots or grant permission to specific users).
A comment
Remember that the shutdown process must always be controlled. Everyone
computers, including machines with Linux system, we have to do
a considerable amount of work so that the power can be turned off safely. The system must shut down services, unmount file systems, and flush buffers to disk.
The shutdown, poweroff and halt commands can only be executed by privileged people
user root. The restriction looks pretty stupid, because anyone
The window manager and desktop environment have their own shutdown menu, and anyone in the vicinity of the computer can press the power button. But such is life, and you have to come to terms with it.
A possible solution is to grant limited privileges to execute shutdown commands with sudo. Another solution is based on creating a special group of users who are allowed to shutdown.
see also
shutdown(8), poweroff(8); section 8.20; Section 8.21.

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 getting additional information see 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 it.
  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.
  • Using 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 decide to run fsck with a special "force" flag to check if it is properly unmounted file systems. 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 everyone who has physical access to the console keyboard, can close 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 should 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 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.