在Linux中,重新启动计算机、注销用户帐户或完全关闭系统非常简单(就像在大多数操作系统中一样)。通常,它就像点击计算机上的电源按钮或单击工具栏上的按钮一样简单。在Ubuntu中,您会看到这样的窗口出现。
您可以选择立即关闭计算机(关闭会话后)、重新启动、注销等。
所有这些也可以通过终端(Terminal)完成,这对于那些始终保持终端(Terminal)会话打开的Ubuntu用户来说特别方便。此方法还包括一些选项,尤其是用于关闭系统。
从终端关闭 Ubuntu
首先,我们将打开一个终端。
在我们开始使用命令之前,请注意我们将执行的许多操作将立即结束您当前的会话,因此在浏览本教程时保存所有内容是个好主意,所以如果 - 偶然 - 你错误地关闭了你的电脑,你不会失去任何东西。
也就是说,我们开始吧。
我们要讨论的第一件事是关闭您的计算机。我们将使用shutdown 命令执行此操作。在最基本的情况下,我们可以通过在终端(Terminal)中输入以下内容来关闭我们的计算机:
sudo shutdown -h now
除非您最近以管理员身份执行过操作,否则系统会要求您输入密码。成功输入后,您的计算机将立即启动关机程序。
使用此命令基本上可以让您的计算机选择停止或关闭电源,然后立即执行此操作。停止只是停止所有正在运行的进程,从而可以安全地关闭计算机,而 poweroff 命令可以同时执行这两种操作。
注意:如果要暂停,也可以在终端中输入 sudo halt。(Note: if you want to halt, you can also type sudo halt into the Terminal.)
如果您想确保您的系统默认停止或关机,以下命令就是这样做的。
sudo shutdown -P HH:MM
或者
sudo shutdown -H HH:MM
在任何一种情况下,最后的时间都是必要的。在没有时间的情况下键入sudo shutdown -P或sudo shutdown -H将显示一条消息,通知您需要时间。
如果你想要更多的控制权怎么办?关闭命令有很多选项。例如,如果您想在 30 分钟内关闭计算机,则sudo shutdown +30将是命令。
如果您想在下午 5:30 关闭计算机(例如),那么sudo shutdown 17:30将是要使用的命令。
注意:所有需要时间的命令都使用 24 小时制,即使您的时钟设置为 12 小时制,所以 sudo shutdown 5:30 将是 5:30 AM。(Note: all commands which demand a time use 24-hour time, even if your clock is set to 12-hour time, so sudo shutdown 5:30 would be 5:30 AM.)
最后,如果您使用了这些示例中的一个并附上了时间,并且意识到您想继续使用您的计算机,那么在不同的终端会话中键入(Terminal)sudo shutdown -c将取消预定的关机。
如果要重新启动计算机,有两种选择。第一个仍然使用关机命令,但在末尾添加了一个选项,告诉您的系统在关机成功完成后重新启动。
当然,输入sudo shutdown -r不一定直观,所以幸运的是我们可以简单地在终端中输入(Terminal)sudo reboot来完成同样的事情。
最后,一些系统在其标准注销屏幕上有休眠或挂起选项。挂起选项保存您当前的状态,然后“几乎”关闭,以便您可以快速返回当前状态。暂停时,您的计算机仍会使用电源(如果运行笔记本电脑则很重要)。 休眠(Hibernate)与挂起相同,不同之处在于您的当前状态保存到磁盘并且计算机已关闭。您需要重新启动系统,但重新启动后可以恢复到当前状态。要从命令行执行上述任一操作,需要使用电源管理器命令。
键入sudo pm-suspend将暂停您的计算机。
同样, sudo pm-hibernate是将其置于休眠模式的命令。
就是这样。基本的关闭和重新启动命令可能非常基本,但是通过添加上述修饰符(除了休眠和挂起命令),您将拥有一套非常完整的会话管理命令,所有这些命令都来自一个基本的终端(Terminal)会话。
Shutdown and Restart Your Computer From The Ubuntu Terminal
In Linux, it’s рretty simple (as it is in most opеratіng systems), to restart your computer, logout of your user account, or completely shutdown your system. Oftеn, it’s as simple as hitting the power button on your computer, or clicking a button on the toolbar. In Ubuntu, you’ll see a window like this appeаr.
You’ll have the option to immediately turn off your computer (after shutting down your session), rebooting, logging out, and more.
All of this can be done via the Terminal as well, which is especially handy for those Ubuntu users who always keep a Terminal session open. This method also includes a few options, especially for shutting down your system.
Shutdown Ubuntu from Terminal
First, we’ll open a Terminal.
Before we get to the commands, take note that many of the things we’ll do will immediately end your current session, so it’s a good idea to have everything saved while browsing this tutorial, so if – by chance – you mistakenly turn off your computer, you won’t lose anything.
That said, here we go.
The first thing we’ll talk about is shutting down your computer. We’ll do this using the shutdown command. At its most basic, we can shutdown our computer by typing the following into Terminal:
sudo shutdown -h now
Unless you’ve recently performed an action as the administrator, you’ll be asked for your password. Once you successfully enter it, your computer will immediately start the shutdown procedure.
Using this command basically gives your computer the option to either halt or poweroff, then does that immediately. Halting simply stops all running processes, making it safe to turn off your computer, while the poweroff command does both.
Note: if you want to halt, you can also type sudo halt into the Terminal.
If you would like to ensure that your system halts or poweroffs by default, the following commands do just that.
sudo shutdown -P HH:MM
or
sudo shutdown -H HH:MM
In either instance, the time at the end is necessary. Typing either sudo shutdown -P or sudo shutdown -H without the time will bring up a message informing you that the time is necessary.
What if you want a bit more control? The shutdown command has many options. For instance, if you want to shutdown your computer in 30 minutes, then sudo shutdown +30 would be the command.
If you want to shutdown your computer at 5:30 PM (as an example), then sudo shutdown 17:30 would be the command to use.
Note: all commands which demand a time use 24-hour time, even if your clock is set to 12-hour time, so sudo shutdown 5:30 would be 5:30 AM.
Finally, if you’ve used one of these examples with a time attached, and realize you want to keep using your computer, typing sudo shutdown -c in a different Terminal session will cancel the scheduled shutdown.
If you want to restart your computer, you have two options. The first still uses the shutdown command, but adds an option at the end that tells your system to restart once the shutdown has completed successfully.
Of course, typing sudo shutdown -r isn’t necessarily intuitive, so thankfully we can simply type sudo reboot into the Terminal to accomplish the same thing.
Finally, some systems have hibernate or suspend options on their standard logout screen. The suspend option saves your current state, then “almost” shuts down so that you can quickly return to your current state. While suspended, your computer still uses power (important if running a laptop). Hibernate is the same as suspend, except your current state is saved to disk and the computer turned off. You’ll need to restart your system, but can return to your current state once restarted. To do either of these from the command line, a power manager command is used.
Typing sudo pm-suspend will suspend your computer.
Similarly, sudo pm-hibernate is the command to place it in hibernate mode.
And that’s it. The basic shutdown and reboot commands can be very basic, but with the addition of the modifiers mentioned (in addition to the hibernate and suspend commands), you have a very complete set of commands for session management, all from a basic Terminal session.