09.10.2021

Reboot remote Linux server sudo. Reboot and turn off the computer from the command line. reboot command: fast restart


Then SysRq can help out in the most seemingly hopeless situations, unless, of course, the kernel is in a "panic", which is usually indicated by randomly blinking keyboard LEDs. Interesting? Then we read on.

The SysRq key was around long before Windows took screenshots. Initially, IBM intended the SysRq key to switch between applications without stopping them. But that's history. Linuxoids adapted SysRq to give the user emergency access to the kernel. But even here everything is not simple. The fact is that for compatibility with Windows in Linux graphical environments, a single SysRq key works like PrintScreen, and the Alt + SysRq combination recommended in console tutorials, just like in Windows, stupidly puts the picture of the active window into the buffer. Therefore, in windowed Linuxes, there is no SysRq key ... either! Instead of this key in Linux graphical environments, the magic combination Alt + Ctrl + SysRq + Latin letter / number is used, which significantly increases your power over the machine.

M - displays the amount of used memory. On Ubuntu it works if you set the output verbosity to high first.

N - displays a list of real-time tasks. Also works if you have previously set the output level to high.

E - Aborts all processes except init.

I - kills all processes, including init.

T - prints a list of tasks to the console.

S - synchronizes all file systems, writing all buffered data to the hard disk.

R - forcibly returns the keyboard to a working state. In this case, the kernel starts working with the keyboard directly, bypassing the X server, and only in ASCII codes.

T - displays a list of processes. Again, only works at a high level of output verbosity.

P - processor register dump. The request may be of interest to those involved in software debugging.

Q - Displays hard timer events. Works when output verbosity is set to high.

O - immediately turns off the computer.

B - restarts the computer. True, they report that with the kernel 3.8.0-25, it is not a reboot, but a shutdown. But I didn't check it myself.

U - Remounts all filesystems to read-only mode.

V - Restores the console's framebuffer. Let's say you are watching some video in the virtual console (yes, Linux, unlike Windows, allows this too), and you urgently need to remember what you were doing in the console before the video was launched. The command will restore the console's framebuffer. In general, it would be necessary to tell in more detail about this amusing thing - the framebuffer, but not in this article.

W - Shows all hung tasks, if any.

Z - displays the contents of the kernel trace buffer.

So, SUDDENLY your Linux freezes in such a way that even rebooting the Xes with the Alt + Del + BS keys does not help. Calm, just calm. In no case do not rush to press the reset button on the computer case. With a very high probability, you will not lose data by pressing the R-E-I-S-U-B keys in sequence (hold Alt + Ctrl + SysRq !). And you know what? You can always instantly turn off a normally working computer with Alt + Ctrl + SysRq + O. Unless, of course, you closed your programs. :)

However, I think it's dangerous to make the magic SysRq available if you allow other users to work remotely on your machine, or you yourself work with it remotely. The point is that the break signal sent from the remote console can be interpreted as Alt+SysRq, with all the ensuing consequences. Therefore, if you decide to give remote access to your machine, then just in case, first reset the kernel.sysrq variable in the system config. You can also write a simple script on the bash for this and even attach a button on the desktop to it, so that each time you do not bother editing the system config control file. Good luck!

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 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 such as vi time to save the file being edited, mail and news handlers, 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 where administrative tasks can be performed (single user mode). Runlevel 1 is the default unless the -h or -r options are specified.

Your system may have a different set of options 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. See the Access Control section below for more information.
  2. -k Don't shutdown, but send warning messages as if the shutdown was 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 into the boot monitor on systems that support it.
  6. -f Skip fsck after reboot.
  7. -F Force fsck after reboot.
  8. -n Don't call init to terminate processes; Instruct shutdown to do it yourself.
  • The use of this option is not recommended and its results are not always predictable.
  1. -c Cancel a 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 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, 0 to 23) and mm is the minute of the hour (in two digits).

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

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

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

Details

If a shutdown is scheduled for the future, it will create an advisory /etc/nologin file that forces programs like login to prevent new users from being allowed. This file is created five minutes before the start of the shutdown sequence. Shutdown removes this file if it is stopped before it can signal initialization (i.e. 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 a /fastboot advisory file that can be checked by the system when it reappears. The system's 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 properly shut down. After that, 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 comes up again. The boot rc file can check if this file is present and decides to run fsck with a special "force" flag to check if the unmounted filesystems are correct. The boot process should then remove /forcefsck.

The -n flag tells shutdown not to call init, but to kill all running processes. Shutdown will disable quota, accounting and swaps, and unmount all filesystems.

Access control

Shutdown can be called from init when the key combination CTRL-ALT-DEL is pressed, if there is a corresponding entry 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 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

On the physical console of the system. The /etc/shutdown.allow format 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.

Turning off Linux and powering the computer

The -H option simply sets the INIT_HALT initialization environment variable 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 needs to check those 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 a system shutdown at 8:00 am.

Schedule a system shutdown at 8 pm.

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

Plan to shut down the system in fifteen minutes. Along with the usual notification of users about the shutdown of the system, they will be given a descriptive message about the hardware upgrade.

Shut down Linux immediately

Lower the system immediately and reboot automatically.

Turn off the system immediately and automatically turn off.

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 computer hope.

If you still have any questions on the topic “Shutting down Linux from the command line”, then 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.

This article is aimed at the most 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 I need to write about it.

Today we'll cover issues such as the command to reboot Linux from the console, reboot remotely, and directly reboot in the graphical interface. Let's start with the usual reboot of the system.

Here, as they say, what could be easier. Consider first rebooting in Ubuntu Unity. Just click on the gear button 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 reboot.

And 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 all of them.

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 go into reboot.

The shutdown utility, which is used to shut down, 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 initialization systems compatible with Init Scripts, there were system boot levels - 0,1,2,3,4,5,6, level 0 - meant shutdown, 6 reboot, other modes of the system are not of interest to us now. You can switch between levels with the init command. But again, you need superuser rights. In this way:

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 the usual ways to reboot Linux, but there is one more, non-standard or even two. These are the magic SysRq keys. The Linux kernel listens for certain key combinations being pressed, and performs the desired action in response 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 combinations hold 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 about 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 disk;
  • U- remounts all file systems in read-only mode;
  • B- immediate reboot, without synchronization, and additional preparations.

Before rebooting, the system waits for all processes to terminate, stops all services, unmounts and mounts read-only file systems. 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, the server, you can immediately send a signal B. Like this: Alt+SysRq+B.

SysRq can be used without keyboard shortcuts by writing the desired operation code to the /proc/sysrq-trigger file:

echo b > /proc/sysrq-trigger

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

If you have access to the server via ssh, then it is very easy to 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:


Turning off the 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 GUI. In the KDE desktop environment, this is done through the main menu, and Gnome and Unity even have a dedicated 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 running, then there is nothing left but to perform a shutdown of the computer from the command line. In addition, shutting down the linux computer through the terminal is the only possible way for servers where there is no graphical interface. As you will see, this method is also much more flexible than a regular shutdown, since you can set the desired shutdown time, or you can turn off the computer until a certain program is finished.

The shutdown command is used to shut down a Linux computer. There are also poweroff, halt commands, but they are for non-standard cases. Usually only one 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]

The options set the disable 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 to indicate 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 it off right now.

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

Options

  • --help- show program help
  • -H, --halt- power off without terminating processes or remounting filesystems
  • -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's basically all the options of the utility, they are more than enough for such a simple action. Now we can move on to the examples, but first, let's look at what happens during Linux startup.

Linux shutdown process

Naturally, the shutdown linux command or any other command does not automatically shut down 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 turn off the computer. In short, here they are:

  • End user processes
  • SIGTERM signal to all processes
  • Signal SIGKILL to all processes
  • Mounting filesystems in read-only mode
  • Preparing external devices for shutdown
  • User space lock to ensure no more user code is run
  • Shut down and power off most peripheral devices
  • Power off the computer

Now you understand the essence of the shutdown process and why you can’t just unplug the power cord from the outlet. If this is done, the computer will shut down instantly and will not perform all the necessary operations, and this may threaten the loss of unsaved or even recently saved data, file system damage, 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.shut down

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 the shutdown of the linux computer in five minutes:

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

We scheduled a message to be displayed before the shutdown.

Now let's undo shutting down 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 said, not only the shutdown linux command can turn off the computer, there are several other utilities capable of 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 shuts down the computer. She just 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. power off

This is an analogue of halt, does 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 question 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 problem situations, for example, if you think that your computer has been infected with 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 perform a safe shutdown of a linux computer. To do this, hold down the Alt + PrtScr keys and press in turn:

  • 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 cache of file systems to disk
  • U- remount read-only filesystems
  • B- reload

Also, instead of all this, you can use the keyboard shortcut 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 shut down Linux through the terminal. If you are caught off guard by an error or computer freeze, you know what to do. If you have any questions - write in the comments!

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

How to restart the server?- This is a question commonly asked by very beginner users who are confused between halt, shutdown -r, reboot, init 6, etc.

An experienced administrator will clarify the question: "what's wrong with the server?" Different types of server failures require different types of reboot - and the wrong 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 the business trip of an enikey worker to a neighboring city. In order to "press reboot" on a lonely server.

In this article: what prevents the server from rebooting and how to help it.

Let's start with the reboot theory.

When shutting down or rebooting the server, the initialization manager (in most modern distributions - systemd, in the eccentric Ubuntu 14.04 is still upstart, in archaic trash - sysv-init) in a certain order sends the “shut down” command to all daemons. And most daemons (like DBMS like mysql) know how to shut down 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 don’t save it, you lose it.

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

New initialization systems (actually, we are not shy - only systemd remains) give a certain timeout (usually 120 or 180 seconds) to save data, after which they force the process to end. In addition to stopping daemons, file systems are unmounted (that is, all block caches are dropped), iscsi targets are stopped (also with cache dropping), etc. etc. Despite the fact that the shutdown time turns out to be indefinitely long, it is all Plus, there is at least some hope for the correct termination of all daemons, dumping 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 (correction: if you do “reboot” in the graphical interface, then the desktop environment will think that this is an emergency reboot - to reboot from the graphical mode, you must use “reboot” in the DE interface).

What can go wrong with a "normal reboot"? Well, firstly, one of the daemon processes can start to "stupid" - see above.

Secondly, 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 uses it. But, to put it mildly, this is not so. Here are potential methods to "nail fs so it doesn't unmount:

  • 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 the risk? An unmounted file system. Systemd in this situation tries, tries, and throws (an unmounted file system). That is, reboot in this situation will be VERY long, but still it will pass. But this is if umount returns an error.

And it 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 a process accesses such a file, then it cannot be terminated (even with kill -9). And in this situation "reboot" will simply hang the server. Again, systemd's most typical places are "hidden", but it's still possible to stumble upon TASK_UNINTERRUPTIBLE ("D" in ps aux).
What to do? It is possible to reboot without synchronizing the filesystems and completing anything with 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 reset. However, the kernel still performs some movements in the reboot area (and, perhaps, 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 go back.

The second, extremely unpleasant situation: problems with the file system on / (at the root). Any attempt to ls, grep, and even "reboot" causes either a console hang or an error. Problems with libc (including its removal) fall into the same category, when a "reboot" attempt is told about a linking problem and refuses 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 muck of the same caliber, going in the "server bad" category.

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

There is a reboot method that does not require any executables to execute (i.e. read from the missing drive). This is (from root): echo b >/proc/sysrq-trigger . The sysrq-trigger file allows you to "press" any button from the SysRq combinations (kernel emergency buttons). Including SysRq-b, that is, an emergency "reboot". It often happens that after pressing enter, the line feed does not even have time to appear - the server is already in reboot before the syscall returns. This is the strongest of the software that is for reboot.
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 talk to the empty set, and potentially panic or crash your last available console. If an emergency reboot is done, it must be emergency

ipt_sysrq

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

watchman for watchman

One would think that this is “everything”, but there are even more unpleasant freezes. For example, the network card is frozen. And the usual reboot (including through sysrq) does not help. The second example of such a bad situation is an enclosure hanging, which is stuck on a bad disk and ignores all bus resets. Reboot seems to reset everything, but the disks are inaccessible.

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. This is a built-in microcomputer that allows you to control a "large" computer. It is commonly referred to as IPMI, DRAC, iLO, etc.

The team we are interested in: ipmitool chassis power cycle. It is more demanding on system 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 the nutrition of everyone. 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 hangs. If the system is more or less alive, then you can “reboot ipmi”: ipmitool mc reboot hard . After that, it will be possible to make a power cycle for the chassis. It sounds strange, but several times in my life I “pulled out” 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 the BMC).

The next point of "pain" is freezing 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 ipmi power cycle) do not work in this situation. You need to either physically poke the cable, or juggle the power 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 take place according to a very tough scenario, but it will definitely pass.


2022
maccase.ru - Android. Brands. Iron. news