Menu
Is free
registration
home  /  Advice/ Shutdown Linux from the command line. Restarting and shutting down the computer from the command line How to restart Linux from the console

Shutting down Linux from the command line. Restarting and shutting down the computer from the command line How to restart Linux from the console

The shutdown command in Linux allows you to shutdown, reboot, or schedule a shutdown of your system. This article explains the most common and useful examples shutdown commands 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, reboot, 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 Linux shutdown command examples

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 shutdown of the system.

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 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. It means that New user cannot log on for five minutes after a scheduled shutdown.

3. Reboot the system with the shutdown command

There is a separate reboot command, but you don't need to learn new team 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 timing 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 be notified of the scheduled outage and time. You can configure the broadcast message in the shutdown command itself:

Sudo shutdown 15:00 "The system will be shutdown 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 shutdown 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 passed a scheduled shutdown message like good System Administrator, 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): Very similar to stop, but also turns off the unit itself.

Historically, in the past, computers were used to shutdown the system and then printed 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. Hope you learned how to shutdown Linux system via.

If you have any questions or suggestions, feel free to let us know in the comments section.

Abstract: description of reboot types, story about sysrq, ipt_SYSRQ, ipmi, psu.

How do I restart the server?- This is a question that is usually asked by very novice users who get confused between halt, shutdown -r, reboot, init 6, etc.

An experienced administrator will clarify the question: "what is wrong with the server?" Different types of server failures require different types reboot - and an incorrectly chosen option will lead to dire consequences, of which a visit to the IPMI / DRAC / iLO web face in order to "reboot" will be the easiest. The hardest thing in my personal practice was a business trip of an enikeyschik to a neighboring town. In order to "push reboot" on a lonely server.

In this Article: What's Stopping Your Server from Restarting, and How to Help It.

Let's start with the rebut theory.

When you shutdown or reboot the server, the initialization manager (in most modern distributions - systemd, in eccentric Ubuntu 14.04 is still upstart, in archaic junk - sysv-init) in a certain order sends all daemons the command "shutdown". And most daemons (eg DBMS like mysql) know how to shutdown properly. For example, end all transactions, save all unsaved data to disk, etc. For an in-memory DBMS like redis, this can be critical at all: if you didn't save it, it lost it.

The old initialization systems waited indefinitely for each of the init scripts. For example, if the "joker" added the "sleep 3600" branch to your "stop", then your server will restart for an hour and a half. And if there is a bigger number, or just a program that does not want to end, then the reboot will never end.

New initialization systems (in fact, we are not shy - only systemd remains) give a certain timeout (usually 120 or 180 seconds) for saving data, after which they terminate the process by force. In addition to stopping daemons, file systems are unmounted (that is, all block caches are dropped), iscsi target "s are stopped (also with cache dropped), etc., etc. While the shutdown time is indefinitely long, it’s all Plus, there is at least some hope for the correct completion of all daemons, dropping file caches, etc.

Thus, on a healthy system, the correct answer to the question "how to reboot" is to execute the reboot command. In some cases, even the only correct one (amendment: if in graphical interface do "reboot", the desktop environment will think that this is an emergency reboot - to reboot from graphical mode, you must use "reboot" in the DE interface).

What can go wrong with a "regular reboot"? Well, first of all, some of the daemon processes can start to "blunt" - see above.

Second, there may be a problem with unmounting file systems... It is believed that it is enough to "kill" all processes, and it will be easy to unmount the disk - no one is using it. But, to put it mildly, this is not the case. Here are potential methods to “nail fs down without unmounting:

  • fallocate / fs / swap -l 1G; mkswap / fs / swap; swapon / fs / swap
  • dd if = / dev / sda of = / fs / image; kpartx / fs / image
  • losetup --find --show / fs / image
etc. In short: a file can be occupied not only by the file system, but also by the kernel. And a module in the kernel can be busy looking for answers to the meaning of life and have no intention of freeing the resource.

What is it fraught with? An unmounted file system. Systemd in this situation tries-tries, and even throws (unmounted file system). That is, the reboot in this situation will be VERY long, but it will still pass. But this is if umount returns an error.

And it so happens that umount cannot complete the operation due to the fact that something is not available. For example, a file on an nfs server. If some process accesses such a file, then it cannot be terminated (even with kill -9). And in this situation, "reboot" will just hang up the server. Again, the most typical places in systemd are "covered", but the probability of stumbling upon TASK_UNINTERRUPTIBLE ("D" in ps aux) is still possible.
What to do? It is possible to reboot without syncing filesystems and completing anything reboot -f. But he can also hang. About the reasons below, but for now about the consequences: all processes are not stopped and die instantly, tcp sessions are not closed, disk caches are not flushed. However, the kernel still performs some movements in the reboot area (and, possibly, some of the caches will be reset). The main thing is that most of the kernel will be involved in the reboot process. And this means that if the core gets sick, then we may not come back.

The second, extremely unpleasant situation: problems with the file system on / (at the root). Any attempt to do ls, grep, and even "reboot" either causes the console to hang or an error. Problems with libc (including removing it) fall into the same category when, upon attempting to "reboot", they talk about a linking problem and refuse to do anything. Or, we have reached the limit on the number of pids and they are all in the "D" state. Or some other nasty thing of the same caliber, going under the "server bad" category.

It so happens that only one console is left open on the server (and the second one does not open anymore). Why? Because someone cheated something with the disk driver. Or a raid controller. Or something else, after which only the memories in the disk cache are left from the "/". This means that we only have bash commands (built-in) that run without starting new processes.

There is a reboot method that does not require executing any executable files (i.e. reading from the missing disk). This is (from root): echo b> / proc / sysrq-trigger. The sysrq-trigger file allows you to "press" any of the SysRq key combinations (kernel emergency buttons). Including SysRq-b, that is, an emergency "reboot". It often happens that after pressing enter, a line feed does not even appear in time - the server is already in reboot before syscall returns. This is the strongest of the software that there is for a rebut.
Note: what seems to be correct in this situation is "sync, reboot", i.e. SysRq-s, SysRq-B is an error because after SysRq-S, the kernel may try to start talking to an empty set, and potentially panic or break off the last available console for you. If an emergency reboot is done, it must be emergency

ipt_sysrq

This all works if you have a console on the server. And if the login hangs and there is no open console? There is a module ipt_SYSRQ that allows you to execute sysrq requests to receive a specific network packet (more precisely, according to the iptables rule). Works entirely in the kernel, i.e. does not depend on FS. The send_sysrq command is also attached to it.

watchman for watchman

One would think that this is "all", but there are even more unpleasant freezes. For example, hung Network Card... And the usual reboot (including through sysrq) does not help. The second example of such a bad situation is enclosure freezing, which is stuck on a bad disk and ignores all bus resets. Reboot seems to reset everything, and the disks are unavailable.

In this case, we need a power cycle (enable / disable). Physically running to the server is not interesting, so you can look at the capabilities of modern servers: IPMI. It is an embedded microcomputer that allows you to control a “big” computer. It is usually called IPMI, DRAC, iLO, etc.

The team we are interested in: ipmitool chassis power cycle. It is more demanding on the system's performance (kernel modules must be loaded, ipmitool itself must start successfully, ipmi must be working, etc.). But on the other hand, it allows you to distort everyone's nutrition. More precisely, almost everyone - if the server has jbods, then this command does not reach them. But, nevertheless, this is a very solid and good reboot.

If the kernel is completely sick, then the command can be executed remotely (ipmitool -H ipmi.server.local chassis power cycle)

Another difficult situation is when ipmi is frozen. If the system is still more or less alive, then you can "reboot ipmi": ipmitool mc reboot hard. After that, you can make a power cycle for the chassis. It sounds strange, but several times in my life I "pulled" the server into a normal reboot with just such a sequence. ( After mc reboot hard you need to give a couple of minutes to load BMC).

The next point of "pain" is hanging power supplies. Yes, it happens. Bugs in the firmware of power supplies are fixed, they need to be flashed. Of course, any soft reboots (such as the ipmi power cycle) do not work in this situation. You need to either physically poke the cable, or distort the power supply remotely. An IP socket helps in this situation.

It looks something like this (a fragment of the control panel for servers.com/servers.ru):

Obviously, under these conditions, the reboot will follow a very tough scenario, but it will definitely pass.

This article is aimed at the most Linux newbies 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 basis, not all beginners know it and I need to write about it.

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

Here that being said, what could be easier. Consider rebooting into Ubuntu Unity first. It is enough to click on the button with the gear in the upper right corner of the screen, and select the item 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 output, and select the item reload:

Then confirm the reboot.

And here the scope is much wider, there are about a dozen commands that can be used to restart Linux. Some need root privileges, others do not, some look simple and easy to remember, while others are long and complex. We'll look at all of them below.

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

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

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

sudo shutdown -r +1

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

The dbus system message service can also restart the computer:

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

You no longer need superuser rights. These were conventional ways Linux reboots, but there is one more, non-standard or even two. These are magic SysRq keys. The Linux kernel monitors when certain keyboard shortcuts are pressed and responds to them. First, enable sysrq support:

echo 1> / proc / sys / kernel / sysrq

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

nano /etc/sysctl.conf

kernel.sysrq = 1

To activate SysRq shortcuts hold Alt + SysRq and press the key code. For a normal reboot, the following sequence is recommended: R E I S U B, press the keys in the same sequence at intervals of about a second.

  • R- returns control of the keyboard if the X server was terminated incorrectly;
  • E- the kernel sends the 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 disk;
  • U- remounts all file systems in read-only mode;
  • B- immediate reboot, no synchronization, and additional preparations.

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

SysRq can be used without keyboard shortcuts by writing required code operations to file / proc / sysrq-trigger:

echo b> / proc / sysrq-trigger

The system will be rebooted as is, without stopping services and preparing file systems, so not saved data may be lost, and the file system is damaged.

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 a 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 entries:


The shutdown command safely shuts down the system. All logged on users receive a notification 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 command line is very convenient, and many users of the system 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 like vi time to save the file being edited, mail and news handlers, the ability to log off, and so on.

Shutdown does its job by 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. To receive additional 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 to 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.

The 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 scheduled 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, it was canceled or something went wrong). It also removes it before calling init to change the runlevel.

The -f flag means "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 to see 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 everything running processes... Shutdown will disable quota, accounting and swaps, and unmounting all file systems.

Access control

Shutdown can be called 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 if the / etc /shutdown.allow file is present. It then compares the logins in that file to the list of people who are logged in at 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 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 should check these Environment Variables and call halt with correct parameters for these parameters to actually have some 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.

Let's see how to restart or shutdown your computer from the command line. I use this when, when installing a driver or with a heavy load on the processor, the interface stops responding to my actions (although this happens very rarely). It is possible to kill a "gluttonous" process through the command line, but it is not always known which process it is, therefore fast decision it is a restart of the computer.

From time to time you need to remotely reboot or shutdown operating system running Linux from the command line. You can do it different ways, then we will consider them.

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

The options set the shutdown parameters, we will discuss 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 the linux computer. In addition, the now constant is available, indicating that you need to turn off right now. Shutdown command, source -h.

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 "PC will shutdown in 5 minutes"

The halt and poweroff commands are used to shutdown the system. The initial command shuts down the system, but does not power down the system. You will see the message “System is halted” indicating that the power can be turned off. This command is for older computers that do not support incremental 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 with halt

The command is identical to the reboot command in its actions, the difference is that the halt command shuts down the system. Also, the computer can be turned off by running the brigade:

Using halt can damage the system!

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.

Shutting down Linux with poweroff

The poweroff command is similar to the halt command, except that after the system stops, a special request is sent to the feed management system to turn off the power, which allows the systems to be shut down remotely. And you can also use:

Shutting down Linux using telinit 0

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

Shutdown Linux from command line without sudo

Possibly via dbus and ConsoleKit. It can be controlled via dbus. Only now they seem to be sawing him out everywhere.

ConsoleKit is such a daemon serving user sessions.

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

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

That's it, discussion of the main ways to shutdown and reboot Linux systems from the command line finished. Now you know everything you need to know about how to shutdown Linux through the terminal. If you are caught off guard by a typo or computer freeze, you know what to do.