如何使用命令行重新启动或关闭Linux
Fatmawati Achmad Zaenuri/Shutterstock.comFatmawati Achmad Zaenuri / Shutterstock.com Feel like starting over? This tutorial will show you how to cleanly and safely reboot or shut down your Linux or ma
Feel like starting over? This tutorial will show you how to cleanly and safely reboot or shut down your Linux or macOS computer from the command line.
想要重新开始? 本教程将向您展示如何从命令行干净安全地重新引导或关闭Linux或macOS计算机。
我们要下去 (We’re Going Down)
Sometimes you just have to go for the reboot or the total shutdown. If you’re working on a GUI-less server or you’re on an SSH session to a remote computer, the command line is your only option. Linux and Unix-like systems such as macOS provide several commands to shutdown or reboot your system right from the command line.
有时,您只需要重新启动或完全关闭即可。 如果您正在使用不带GUI的服务器,或者正在与远程计算机进行SSH会话,那么命令行是您唯一的选择。 类似macOS之类Linux和Unix之类的系统提供了几个命令,可以从命令行直接关闭或重新引导系统。
The commands you can use are:
您可以使用的命令是:
- shutdown 关掉
- reboot 重启
- halt 停止
- poweroff 断电
Looking through the man pages for these commands can be confusing. Depending on which command line options you choose, all of these commands can perform shutdowns, reboots, and system halts. In fact, the man pages for reboot
, halt
, and poweroff
contain exactly the same information.
浏览手册页中的这些命令可能会造成混淆。 根据您选择的命令行选项,所有这些命令都可以执行关机 ,重新引导和系统暂停。 实际上,用于reboot
, halt
和poweroff
的手册页包含完全相同的信息 。
背后是什么? (What’s Behind This?)
The answer lies in the systemd bootstrap system which replaced the venerable System V init
system. In the Linux world, Fedora started to use systemd
in 2011. Since then it has been adopted by a great many distributions. Debian and Ubuntu swapped to systemd
in 2015.
答案在于systemd 引导系统,该系统取代了古老的System V init
系统。 在Linux世界中,Fedora从2011年开始使用systemd
。从那时起,它被许多发行版所采用。 Debian和Ubuntu在2015年换成了systemd
。
On systemd
-based distributions the shutdown
, reboot
, halt
, and poweroff
commands are effectively shortcuts that point to the systemctl
command. Retaining these commands provides a degree of compatibility with System V init
-based distributions. It means shell scripts (and hard-core System V system administrators) don’t keel over if they are moved to a computer with a systemd
distribution running on it.
在基于systemd
的发行版上, shutdown
, reboot
, halt
和poweroff
命令实际上是指向systemctl
命令的快捷方式。 保留这些命令可以与基于System V init
的发行版提供一定程度的兼容性。 这意味着如果将外壳程序脚本(以及系统V核心系统管理员)转移到运行了systemd
发行systemd
的计算机上,它们将不会systemd
。
使用关机 (Using Shutdown)
Shutting down or rebooting a multi-user system means you have to plan ahead. You need to decide when you’re going to go for the shutdown or reboot, and warn the other system users the shutdown is coming, and when. If it is your own computer and you’re the only one who uses it, life is much simpler.
关闭或重新引导多用户系统意味着您必须提前计划。 您需要确定何时要进行关闭或重新引导,并警告其他系统用户即将关闭以及何时关闭。 如果它是您自己的计算机,并且您是唯一使用它的计算机,那么生活会简单得多。
To run any of these commands you have to be in the sudo
group. That is, you must have superuser permissions and be able to use the sudo
command. If the command you have issued is going to take effect immediately and will not affect other logged in users, you will not need to use sudo
. If you try to use one of these commands and the command is refused, re-try with sudo
.
要运行任何这些命令,您必须位于sudo
组中。 也就是说,您必须具有超级用户权限,并且能够使用sudo
命令。 如果您发出的命令将立即生效并且不会影响其他登录用户,则无需使用sudo
。 如果尝试使用这些命令之一,但命令被拒绝,请使用sudo
重试。
By default the shutdown
command ensures that all processes are stopped cleanly, all filesystems are synced, and all CPU activity has ceased. This is the ‘halt’ state. It then sends a message to the hardware to cut off power. This, of course, is the shutdown or “poweroff” state.
默认情况下, shutdown
命令可确保所有进程都干净地停止,所有文件系统都已同步,并且所有CPU活动都已停止。 这是“暂停”状态。 然后,它将消息发送到硬件以切断电源。 当然,这是关机或“关机”状态。
It is common to pass shutdown
some parameters, such as a time string and a message that will be sent to the logged in users to warn them off the shutdown. Let’s schedule a shutdown for 15 minutes from now. Type shutdown
, a space, +15
, a space, and then the message to send to the users.
通常会传递shutdown
某些参数,例如时间字符串和一条消息,该参数将发送给已登录的用户以警告他们关闭。 让我们安排从现在开始关闭15分钟。 键入shutdown
,一个空格, +15
,一个空格,然后键入要发送给用户的消息。
shutdown +15 Shutting down in 15 minutes!
The time string we used was +15
, representing 15 minutes from now. The +
is optional. We could have typed 15
.
我们使用的时间字符串是+15
,表示从现在开始15分钟。 +
是可选的。 我们可以输入15
。
We get a response that confirms a shutdown is scheduled and when it will occur. Logged in users will receive the message that we provided.
我们得到一个确认已计划关闭以及何时关闭的响应。 登录的用户将收到我们提供的消息。
To cancel a shutdown, use the -c
(cancel) option.
要取消关闭,请使用-c
(取消)选项。
shutdown -c
Although you don’t get any notification that your shutdown has been canceled, your logged in users do get notified.
尽管您没有收到任何有关取消关机的通知,但已登录的用户确实会收到通知。
If you don’t provide a time string a shutdown will be scheduled for one minute from now. Note that you can’t provide a message to your logged in users if you don’t specify a time string.
如果您不提供时间字符串,则将从现在起一分钟内关闭。 请注意,如果未指定时间字符串,则无法向登录的用户提供消息。
shutdown
If you can’t even wait a minute, you can use now
as the time string and the shutdown takes immediate effect. Using now
is like using +0
.
如果您什至不能等待一分钟,您now
可以将其用作时间字符串,关闭会立即生效。 now
使用就像+0
。
The time string can be a set time, such as 23:00. It must follow the format of HH:MM
and must be in the 24-hour clock. Five minutes before the system goes down new logins are prevented.
时间字符串可以是设置的时间,例如23:00。 它必须遵循HH:MM
的格式,并且必须是24小时制。 在系统关闭前五分钟,将阻止新的登录。
We know the default action of shutdown
makes the computer go down to the halt state and then into the powered off state. We can override this behavior by passing other command line options to it.
我们知道shutdown
的默认操作会使计算机进入暂停状态,然后进入关机状态。 我们可以通过传递其他命令行选项来覆盖此行为。
The
-H
(halt) option will take your computer down to the halt state but will not ask the hardware to power down.-H
(暂停)选项将使您的计算机进入暂停状态,但不会要求硬件关闭电源。The
-P
(poweroff) is the default action . The computer is brought down to the halt state and is then powered off.-P
(关闭电源)是默认操作。 使计算机进入暂停状态,然后关闭电源。The
-r
(reboot) option will take your computer down to the halt state and then restart it.-r
(重新引导)选项将使您的计算机进入暂停状态,然后重新启动它。The
-h
(halt and poweroff) option is the same as-P
. If you use-h
and-H
together, the-H
option takes priority.-h
(暂停和关闭电源)选项与-P
相同。 如果同时使用-h
和-H
,则-H
选项优先。The
-c
(cancel) option will cancel any scheduled shutdown, halt or reboot.-c
(取消)选项将取消所有计划的关闭,停止或重新启动。
Here’s an example where we have scheduled a reboot.
这是我们计划重新启动的示例。
shutdown -r 08:20 System rebooting at 08:20
重启,停止和关机命令 (The reboot, halt and poweroff Commands)
These commands perform the action their name suggests. However, each of them will accept command line options to make any one of them perform a reboot, a halt, or a poweroff. But why confuse matters? These commands are best used at face value.
这些命令执行其名称建议的操作。 但是,它们中的每一个都将接受命令行选项,以使它们中的任何一个执行重新引导,暂停或关闭电源。 但是,为什么要混淆问题呢? 这些命令最适合以票面价值使用。
If you want to reboot now, use reboot
. If you want to poweroff now, use poweroff
, and if you want halt the system now, use halt
.
如果要立即重启,请使用reboot
。 如果要立即关闭电源,请使用poweroff
;如果要立即停止系统,请使用halt
。
reboot
halt
poweroff
These commands take immediate effect. If any of these commands are refused, precede them with sudo
. But be aware, a refusal is usually because there are other users logged into the system that you’re about to take offline.
这些命令立即生效。 如果这些命令中的任何一个被拒绝,请在它们前面加上sudo
。 但是请注意,拒绝通常是因为有其他用户登录到您要脱机的系统中。
哪个命令适合我? (Which Command is Right For Me?)
In multi-user environments using shutdown
to perform these actions gives you more control. The facility to schedule shutdowns and reboots, and to alert users with a broadcast message will be invaluable in these cases. For a single-user computer, reboot
and poweroff
will probably meet your needs.
在多用户环境中,使用shutdown
执行这些操作可以为您提供更多控制。 在这些情况下,安排关机和重新启动以及用广播消息警告用户的功能将非常宝贵。 对于单用户计算机, reboot
和poweroff
可能会满足您的需求。
翻译自: https://www.howtogeek.com/411925/how-to-reboot-or-shut-down-linux-using-the-command-line/
更多推荐
所有评论(0)