如何在Ubuntu中关闭硬盘?

我正在使用外置硬盘运行Kubuntu。我的内置硬盘上安装了Windows。在Ubuntu上,我不想使用它,并且希望关闭它以减少发热并节省电池电量。我认为关闭硬盘旋转对我来说不是一个选项,因为这会损坏硬盘,而且我也不打算花钱购买新的硬盘 :)

已经有类似的问题被提出并得到回答:http://askubuntu.com/questions/39760/how-can-i-control-hdd-spin-down-time - Guilhem Soulas
1man hdparm sudo hdparm -Y /dev/sdX - earthmeLon
@GuilhemSoulas 我的问题不是关于硬盘休眠,而是如何关闭硬盘。 - AhmedBilal
3个回答

sudo hdparm -Y /dev/sdX

其中/dev/sdX是您想关闭的设备。您还可以运行sudo blkid来确定设备的“指纹”(UUID),这将使您能够更可靠地控制要关闭的设备。

在这种情况下,您将运行:

sudo hdparm -Y /dev/disk/by-uuid/DEVICE-IDENT-HERE

man hdparm

   -Y     Force  an  IDE  drive  to  immediately  enter  the  lowest power
          consumption sleep mode, causing it to shut down  completely.   A
          hard  or soft reset is required before the drive can be accessed
          again (the Linux IDE driver will automatically handle issuing  a
          reset  if/when  needed).   The  current power mode status can be
          checked using the -C option.

什么是“硬重置”或“软重置”,也就是如何恢复驱动器? - Asalle
1这个命令确实关闭了硬盘驱动器,但是运行sudo hdparm -C /dev/sdX来查询状态会再次打开驱动器然后进入待机模式(我猜是软重置)。这个命令适用于节能,但不能模拟没有硬盘驱动器的安装。 - user37165

你可能已经安装了udisks2软件包;你可以使用它。
udisksctl power-off -b /dev/sdX

在这里,/dev/sdX 是您想要关闭的设备。

来自 udisksctl 手册页面(版本2.7.6):

power-off
    Arranges for the drive to be safely removed and powered off. On the OS
    side this includes ensuring that no process is using the drive, then
    requesting that in-flight buffers and caches are committed to stable
    storage. The exact steps for powering off the drive depends on the
    drive itself and the interconnect used. For drives connected through
    USB, the effect is that the USB device will be deconfigured followed
    by disabling the upstream hub port it is connected to.

    Note that as some physical devices contain multiple drives (for
    example 4-in-1 flash card reader USB devices) powering off one drive
    may affect other drives. As such there are not a lot of guarantees
    associated with performing this action. Usually the effect is that the
    drive disappears as if it was unplugged.

那在Ubuntu 20.04上可以运行。之后有没有办法开机? - Sven
1@Sven 我相信poweroff命令字面上告诉硬件关机,因此在那之后它不会接受任何命令。在那之后,你可能需要手动开启设备;这很可能取决于设备(断开/重新连接电源等)。 - Taylor R
你是否对低级命令的本质了解得很深?抱歉,只是为了学习目的。 - stackunderflow
我其实不是! - Taylor R
似乎这个命令只适用于USB驱动器?我收到了“关闭驱动器错误:没有USB设备(udisks-error-quark,0)”的提示。 - Kenji Noguchi

您可以使用以下内容(这里的sdc是感兴趣的相应块设备的名称):
sync
echo 1 > /sys/block/sdc/device/delete

或者(从非root用户):
sync
echo 1 | sudo tee /sys/block/sdc/device/delete

3+1 这个方法按预期工作,可以完全阻止安装程序检测硬盘驱动器。必须以 root 身份运行命令(不是 sudo)。 - user37165
4我认为使用sudo也是可行的:sudo bash -c 'echo 1 > /sys/block/sdc/device/delete' - Tomilov Anatoliy
1它在使用USB闪存进行Linux Mint 19.1安装时完美运行。 - Matt Mello
1这应该是被接受的答案,因为它适用于任何内部(例如SATA)和外部(例如USB)硬盘(HD),固态硬盘(SSD),闪存驱动器(也称为pendrive,也称为thumb drive),SD卡等。 - Yuri Sucupira
这并没有真正地关闭我的USB硬盘,只是让它从系统中消失了。使用hdparm的被接受的答案起作用了。 - blade