如何从 Linux 命令行启动、关闭和暂停系统
让我们面对现实:我们使用计算机最基本的事情就是打开和关闭它们。其他一切都发生在这两个特定事件之间。有时,重新启动系统是故障排除或完成安装过程的关键组成部分。
通常,您需要 root 权限才能关闭或重新启动系统。确保您的帐户已配置/etc/sudoers
此权限。请记住,直接以 root 身份登录通常不是一个好主意。
启动系统
启动系统非常简单,只需按下电源按钮即可。具体操作会因硬件而异,但一般来说,流程如下:
- 固件(BIOS 或 UEFI)找到启动媒体。
- 引导加载程序启动,并加载主引导记录 (MBR) 或 GUID 分区表 (GPT)。
- 出现操作系统选择菜单。
- 引导加载程序第 2 阶段启动并加载选定的内核。
- 内核和驱动程序加载并挂载根文件系统。
- systemd 作为 PID 1 启动。
- default.target 文件已加载。
- 提示用户进行身份验证。
系统启动到default.target
(提供命令行界面或图形环境)。更改这些目标不在本文的讨论范围内,但我在如何在 Linux 服务器上的 CLI 和 GUI 之间切换中介绍了这些内容。
关闭系统
当 Red Hat Enterprise Linux (RHEL) 在 RHEL 7 中采用 systemd 时,这个古老的 Linux 命令发生了什么变化 shutdown
?它仍然存在 — 但现在它映射到 systemd 的关机功能。
该shutdown
命令有两个选项:--halt
和--poweroff
。--halt
选项停止操作系统,而--poweroff
选项关闭系统。
该命令的主要优点之一shutdown
是能够定义关机延迟,以便用户有时间保存他们的工作并注销系统。使用 hh:mm 格式(24 小时制)安排适合您需要的时间。
例如,要在晚上 10 点停止系统,请输入:
$ sudo shutdown --halt 22:00
您更有可能希望在几分钟后停止操作系统。在这种情况下,请指定从现在开始关闭过程的分钟数。
例如,要在五分钟延迟后停止系统,请输入:
$ sudo shutdown --halt +5
您可以在时间规范后输入一条消息并将其附加到所有用户,如下所示:
$ sudo shutdown --halt +5 “Attention. The system is going down in five minutes.”
使用以下-c
选项取消定时关机:
$ sudo shutdown -c
You can also use the systemctl
command to shut down the system. For example, type systemctl halt
or systemctl poweroff
to achieve similar results to the shutdown
command. The main disadvantage of using systemctl
is losing the ability to schedule or cancel the shutdown process.
Reboot the system
When someone approaches you for help with their computer, I suspect one of your first responses is, "Did you reboot it?" Restarting a computer helps with various problems and may even be necessary to finalize some configurations. However, restarting is considered downtime on a server and should be avoided if possible. Try to use systemctl restart {service-name}
to restart services rather than reboot the whole system.
However, sometimes a restart is inevitable. The option for restarting the system immediately with the shutdown
command is -r
, so it looks like this:
$ sudo shutdown -r now
You can still specify a delayed time using the hh:mm format explained above.
You can also use systemctl
to reboot the device by typing:
$ sudo systemctl reboot
Again, the disadvantage with systemctl
is the inability to delay the process.
Suspend the system
There are a couple of ways to place the system in a reduced or no-power mode. The first is suspend and the second is hibernate.
When you suspend the system:
- Contents of memory are moved to the swap location.
- The boot loader is configured to boot directly to the current kernel.
- The system shuts down (no-power mode).
- Upon power up, the system reloads itself from swap.
When you hibernate the system:
- Applications are stopped.
- System state is moved to RAM.
- The system remains powered on in a low-power state.
You can also suspend and hibernate the system by using the systemctl
command. The commands are exactly what you'd expect:
$ sudo systemctl suspend
$ sudo systemctl hibernate
$ sudo systemctl hybrid-sleep
The systemctl hybrid-sleep
command both suspends and hibernates the system.
Use the GUI
Depending on the graphical user interface (GUI) environment you have installed, you can initiate a reboot or shutdown from a menu. Many people rely on a graphical desktop like GNOME, so this is certainly a viable option.
Wrap up
The familiar shutdown
command now maps to systemd and executes timed shutdowns and reboots. You can use systemctl
if you wish, but being able to schedule the shutdown is helpful. In addition, systemctl
also offers suspend and hibernate options. Try to avoid bringing down the system when possible, but sometimes it's necessary.