Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to turn off the Linux computer from the command line

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to shut down the Linux computer from the command line, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

1. Turn off the computer

If you are already using a Linux machine, the solution to simply shutting it down is to use the shutdown instructions sent through SSH (if you are using Windows PC, you have several SSH tools to choose from). This can be used to shut down the computer (until you decide to restart it) or restart it. The syntax for this command is:

Shutdown [option] [time] [message]

For example, to turn off the computer immediately, you will use:

Shutdown-h now

Here,-h indicates a pause, and it is now clear that the instruction should be executed immediately. Different delays can be used. For example, you can use + 9 instead, which will tell the computer to run the shutdown program within 9 minutes.

If you want to include a message, it will be displayed to all logged-in users:

Linuxidc@linuxidc:~/linuxidc.com$ shutdown-h + 9 "server is shutting down, please save your work and log out." Shutdown scheduled for Sun 2019-04-28 10:37:30 CST, use 'shutdown-c' to cancel.

Remember, you can find a complete list of switches for these commands, enter:

[command]-- help

Restart with-r

Another option is to restart the computer using the-r command. This is used instead of-h, so to restart the computer or server, you can use:

The linuxidc@linuxidc:~/linuxidc.com$ shutdown-h + 3 "server will restart in 3 minutes. Please save your work and log out." Shutdown scheduled for Sun 2019-04-28 10:49:38 CST, use 'shutdown-c' to cancel.

You can cancel any scheduled shutdown or restart by entering the shutdown-c command:

Linuxidc@linuxidc:~/linuxidc.com$ shutdown-c

2. Restart

Since the shutdown command has a restart option, it's not surprising to know about the shutdown option of the reboot command.

The standard restart command is:

Reboot

This will prompt your computer to automatically turn off and then turn it back on. However, if you want to turn off the device, the-p switch will work:

Reboot-p

Another option is to force a restart. This is useful if the application or service is hanging and you need to restart quickly:

Reboot-f

This will force a restart of the Linux machine.

3 、 Halt

We have seen the-h switch above, but halt can be used as a command alone. This will cause the computer to shut down immediately, using a simple four-letter word:

Halt

The-f switch can also be used to pause, but the results are inconsistent and may cause system stability problems.

4. Turn off the computer at a fixed time

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

However, in addition to using the-f forced shutdown, you can also use the-w switch to log system restart calls to / var/log/wtmp. This is a potentially useful debugging tool, like-verbose, which can help solve shutdown problems.

Poweroff-verbose

5. Emergency option: REISUB

When the system is running normally, you can use all of the above commands. But what if it collapses? What if the PC or server is hanging and cannot be restarted in an appropriate manner?

The answer is the keyboard combination. If you have switched from Windows, you probably know that Ctrl + Alt + Del displays a menu with Shutdown as an option. If you keep it for a longer time, the machine will shut down automatically. At the same time, on Mac, you only need to press and hold the power button (the option also applies to Windows hardware).

In Linux, the keyboard combination is Alt + Print Screen + B to restart. However, if this does not work, or if there is a more complex problem, you can use up to six keys to change the combination.

This is called REISUB, which is an acronym for the following words:

UnRaw-controls the keyboard from the X display server.

TErminate-sends the termination signal SIGTERM to all processes for normal termination.

KIll-as mentioned above, but the SIGKILL signal forces the process to be terminated immediately.

Sync-flushes data to disk.

Unmount- this remounts all file systems into a read-only state.

ReBoot-just as you expected.

For it to work, you should press Alt + Print Screen, and then press the R E I S U B key sequentially. Leave a second or two between each button. Note that this method usually does not apply to computers with ARM architectures, such as Raspberry Pi.

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

We have learned how to cancel the shutdown or restart command. However, when an important process is running, you can easily start the shutdown command-- especially on a remote server. One way to solve this problem is to install molly-guard, which can override the shutdown by checking some parameters.

For example, there is a script that checks the SSH session (unlike FTP). If you send a restart, stop, shutdown, or shutdown command, molly-guard will ask for the name of the host you want to shut down.

To use it, install molly-guard in the terminal:

Linuxidc@linuxidc:~/linuxidc.com$ sudo apt-get install molly-guard

When molly-guard is running in the background, it detects commands such as poweroff and reports that a SSH session has been detected. You can then choose to enter the hostname of the server to confirm the shutdown, or press Ctrl + C to cancel. It works!

These five methods of shutting down a Linux computer from the command line are particularly useful because they can be used either on the computer itself or through a remote SSH. Because these commands are very concise, they can be used quickly-which can lead to an unexpected restart! Fortunately, the molly-guard utility is sufficient to avoid this situation.

Thank you for reading this article carefully. I hope the article "how to turn off the Linux computer from the command line" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report