mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
Merge pull request #9513 from SunWave/master
20180705 How to use dd in Linux without destroying your disk.md 翻译完毕
This commit is contained in:
commit
37dc1c02f1
@ -1,98 +0,0 @@
|
|||||||
Translating by SunWave...
|
|
||||||
|
|
||||||
How to use dd in Linux without destroying your disk
|
|
||||||
======
|
|
||||||
|
|
||||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_happy_sad_developer_programming.png?itok=72nkfSQ_)
|
|
||||||
|
|
||||||
This article is excerpted from chapter 4 of [Linux in Action][1], published by Manning.
|
|
||||||
|
|
||||||
Whether you're trying to rescue data from a dying storage drive, backing up archives to remote storage, or making a perfect copy of an active partition somewhere else, you'll need to know how to safely and reliably copy drives and filesystems. Fortunately, `dd` is a simple and powerful image-copying tool that's been around, well, pretty much forever. And in all that time, nothing's come along that does the job better.
|
|
||||||
|
|
||||||
### Making perfect copies of drives and partitions
|
|
||||||
|
|
||||||
`dd` if you research hard enough, but where it shines is in the ways it lets you play with partitions. You can, of course, use `tar` or even `scp` to replicate entire filesystems by copying the files from one computer and then pasting them as-is on top of a fresh Linux install on another computer. But, because those filesystem archives aren't complete images, they'll require a running host OS at both ends to serve as a base.
|
|
||||||
|
|
||||||
There's all kinds of stuff you can do withif you research hard enough, but where it shines is in the ways it lets you play with partitions. You can, of course, useor evento replicate entire filesystems by copying the files from one computer and then pasting them as-is on top of a fresh Linux install on another computer. But, because those filesystem archives aren't complete images, they'll require a running host OS at both ends to serve as a base.
|
|
||||||
|
|
||||||
Using `dd`, on the other hand, can make perfect byte-for-byte images of, well, just about anything digital. But before you start flinging partitions from one end of the earth to the other, I should mention that there's some truth to that old Unix admin joke: "dd stands for disk destroyer." If you type even one wrong character in a `dd` command, you can instantly and permanently wipe out an entire drive of valuable data. And yes, spelling counts.
|
|
||||||
|
|
||||||
**Remember:** Before pressing that Enter key to invoke `dd`, pause and think very carefully!
|
|
||||||
|
|
||||||
### Basic dd operations
|
|
||||||
|
|
||||||
Now that you've been suitably warned, we'll start with something straightforward. Suppose you want to create an exact image of an entire disk of data that's been designated as `/dev/``sda`. You've plugged in an empty drive (ideally having the same capacity as your `/dev/``sda` system). The syntax is simple: `if=` defines the source drive and `of=` defines the file or location where you want your data saved:
|
|
||||||
```
|
|
||||||
# dd if=/dev/sda of=/dev/sdb
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
The next example will create an .img archive of the `/dev/``sda` drive and save it to the home directory of your user account:
|
|
||||||
```
|
|
||||||
# dd if=/dev/sda of=/home/username/sdadisk.img
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
Those commands created images of entire drives. You could also focus on a single partition from a drive. The next example does that and also uses `bs` to set the number of bytes to copy at a single time (4,096, in this case). Playing with the `bs` value can have an impact on the overall speed of a `dd` operation, although the ideal setting will depend on your hardware profile and other considerations.
|
|
||||||
```
|
|
||||||
# dd if=/dev/sda2 of=/home/username/partition2.img bs=4096
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
Restoring is simple: Effectively, you reverse the values of `if` and `of`. In this case, `if=` takes the image you want to restore, and `of=` takes the target drive to which you want to write the image:
|
|
||||||
```
|
|
||||||
# dd if=sdadisk.img of=/dev/sdb
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also perform both the create and copy operations in one command. This example, for instance, will create a compressed image of a remote drive using SSH and save the resulting archive to your local machine:
|
|
||||||
```
|
|
||||||
# ssh username@54.98.132.10 "dd if=/dev/sda | gzip -1 -" | dd of=backup.gz
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
You should always test your archives to confirm they're working. If it's a boot drive you've created, stick it into a computer and see if it launches as expected. If it's a normal data partition, mount it to make sure the files both exist and are appropriately accessible.
|
|
||||||
|
|
||||||
### Wiping disks with dd
|
|
||||||
|
|
||||||
Years ago, I had a friend who was responsible for security at his government's overseas embassies. He once told me that each embassy under his watch was provided with an official government-issue hammer. Why? In case the facility was ever at risk of being overrun by unfriendlies, the hammer was to be used to destroy all their hard drives.
|
|
||||||
|
|
||||||
What's that? Why not just delete the data? You're kidding, right? Everyone knows that deleting files containing sensitive data from storage devices doesn't actually remove the data. Given enough time and motivation, nearly anything can be retrieved from virtually any digital media, with the possible exception of the ones that have been well and properly hammered.
|
|
||||||
|
|
||||||
You can, however, use `dd` to make it a whole lot more difficult for the bad guys to get at your old data. This command will spend some time writing millions and millions of zeros over every nook and cranny of the `/dev/sda1` partition:
|
|
||||||
```
|
|
||||||
# dd if=/dev/zero of=/dev/sda1
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
But it gets better. Using `/dev/``urandom` file as your source, you can write over a disk with random characters:
|
|
||||||
```
|
|
||||||
# dd if=/dev/urandom of=/dev/sda1
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### Monitoring dd operations
|
|
||||||
|
|
||||||
Since disk or partition archiving can take a very long time, you might want to add a progress monitor to your command. Install Pipe Viewer (`sudo apt install pv` on Ubuntu) and insert it into `dd`. With `pv`, that last command might look something like this:
|
|
||||||
```
|
|
||||||
# dd if=/dev/urandom | pv | dd of=/dev/sda1
|
|
||||||
|
|
||||||
4,14MB 0:00:05 [ 98kB/s] [ <=> ]
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
Putting off backups and disk management? With dd, you aren't left with too many excuses. It's really not difficult, but be careful. Good luck!
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://opensource.com/article/18/7/how-use-dd-linux
|
|
||||||
|
|
||||||
作者:[David Clinton][a]
|
|
||||||
选题:[lujun9972](https://github.com/lujun9972)
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]:https://opensource.com/users/remyd
|
|
||||||
[1]:https://www.manning.com/books/linux-in-action?a_aid=bootstrap-it&a_bid=4ca15fc9&chan=opensource
|
|
@ -0,0 +1,80 @@
|
|||||||
|
## 如何在linux系统中使用dd命令而不会损毁你的磁盘
|
||||||
|
|
||||||
|
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_happy_sad_developer_programming.png?itok=72nkfSQ_)
|
||||||
|
|
||||||
|
这篇文章节选自 Manning 出版社出版的图书[Linux in Action](https://www.manning.com/books/linux-in-action?a_aid=bootstrap-it&a_bid=4ca15fc9&chan=opensource)的第4章.
|
||||||
|
|
||||||
|
你是否正在从一个即将损坏的存储驱动器挽救数据,把本地文件进行远程备份,或者把一个活动分区的数据完整的复制到别处,那么你需要懂得如何安全而可靠的复制驱动器和文件系统数据.幸运的是,dd 是一个可以使用的简单而又功能强大的镜像复制命令,从现在到未来很长的时间内,也许直到永远都不会出现比 dd 更好的工具了。
|
||||||
|
|
||||||
|
### 完整的复制驱动器和分区中的数据
|
||||||
|
|
||||||
|
仔细研究后,你会发现你可以使用 dd 做各种任务,但是它最重要的功能是处理磁盘分区。当然,你可以使用 tar 命令或者 scp 命令从一台计算机复制整个文件系统的文件, 然后把这些文件原样粘贴在另一台刚刚安装好 linux 操作系统的计算机中。但是,因为文件系统的文件不是映像文件,所以在复制文件的过程中需要计算机操作系统的运行作为基础。
|
||||||
|
|
||||||
|
另一方面,使用 dd 可以对任何数字信息完美的进行逐个字节的镜像。但是不论何时何地,当你要对分区进行操作时,我要告诉你早期的Unix管理员曾开过这样的玩笑: " dd 象征着磁盘毁灭者"。 在使用 dd 命令的时候,如果你输入了哪怕是一个字母,也可能立即永久性的擦除掉整个磁盘驱动器里的所有重要的数据。因此,一定要注意命令的拼写格式规范。
|
||||||
|
|
||||||
|
**记住:** 在按下 Enter 键执行 dd 命令之前,暂时停下来仔细的认真思考一下。
|
||||||
|
|
||||||
|
### dd 命令的基本操作
|
||||||
|
|
||||||
|
现在你已经得到了适当的提醒,我们将从简单的事情开始。假设你要对代号为 /dev/sda 的整个磁盘数据创建精确的映像,你已经插入了一张空的磁盘驱动器 (理想情况下具有与代号为 /dev/sda 的磁盘驱动器相同的容量)。语法很简单: if = 定义源驱动器 `of =` 定义你要将数据保存到的文件或位置:
|
||||||
|
```
|
||||||
|
# dd if=/dev/sda of=/dev/sdb
|
||||||
|
```
|
||||||
|
接下来的例子将要对 /dev/sda 驱动器创建一个 .img 的映像文件,然后把该文件保存的你的用户帐号家目录:
|
||||||
|
```
|
||||||
|
# dd if=/dev/sda of=/home/username/sdadisk.img
|
||||||
|
```
|
||||||
|
上面的命令针对整个驱动器创建映像文件,你也可以针对驱动器上的单个分区进行操作。 下面的例子针对驱动器的单个分区进行操作,同时使用了一个 bs 参数用于设置单次拷贝的字节数量 (此例中是 4096)。设定 bs 参数值可能会影响 dd 命令的整体操作速度,但理想的设置取决于你的硬件配置和其它考虑。
|
||||||
|
```
|
||||||
|
# dd if=/dev/sda2 of=/home/username/partition2.img bs=4096
|
||||||
|
```
|
||||||
|
数据的恢复非常简单:通过颠倒 if 和 of 参数可以有效的完成任务。在此例中,if = 使用你要恢复的映像,of = 使用你想要写入映像的目标驱动器:
|
||||||
|
```
|
||||||
|
# dd if=sdadisk.img of=/dev/sdb
|
||||||
|
```
|
||||||
|
你也可以在一条命令中同时完成创建和拷贝任务。下面的例子中将从支持 SSH 的远程驱动器创建一个压缩的映像文件并把该文件保存到你的本地计算机中:
|
||||||
|
```
|
||||||
|
# ssh username@54.98.132.10 "dd if=/dev/sda | gzip -1 -" | dd of=backup.gz
|
||||||
|
```
|
||||||
|
你应该经常测试你的档案,确保它们可正常使用。如果它是你创建的启动驱动器,将它粘贴到计算机中,看看它是否能够按预期启动。如果它是普通分区的数据,挂载该分区,确保文件都存在而且可以正常的访问。
|
||||||
|
|
||||||
|
### 使用 dd 擦除磁盘数据
|
||||||
|
|
||||||
|
多年以前,我的一个负责政府海外大使馆安全的朋友曾经告诉我,在他当时在任的时候, 政府会给每一个大使馆提供一个官方版的锤子。为什么呢? 一旦大使馆设施可能被不友善的人员侵占,就会使用这个锤子毁坏所有的硬盘.
|
||||||
|
|
||||||
|
为什么要那样做?为什么不仅仅删除数据?你在开玩笑,对吧?所有人都知道从存储设备中删除包含敏感信息的文件实际上并没有真正移除这些数据。除非使用锤子彻底的毁坏这些存储介质,否则,只要有足够的时间和动机, 几乎所有的内容都可以从几乎任何数字存储介质重新获取。
|
||||||
|
|
||||||
|
但是,你可以使用 dd 命令让坏人非常难以获得你的旧数据。这个命令需要花费一些时间在 `/dev/sda1` 分区的每个扇区写入数百万个 zero:
|
||||||
|
```
|
||||||
|
# dd if=/dev/zero of=/dev/sda1
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
还有更好的方法。通过使用 /dev/urandom 作为源文件,你可以在磁盘上写入随机字符:
|
||||||
|
```
|
||||||
|
# dd if=/dev/urandom of=/dev/sda1
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### 监控 dd 的操作
|
||||||
|
|
||||||
|
由于磁盘或磁盘分区的存档可能需要很长的时间,因此你可能需要在命令中添加进度查看器。安装 Pipe Viewer(在Ubuntu系统上安装命令为 sudo apt install pv),然后把 pv 命令和 dd 命令结合在一起。使用 pv,最终的命令是这样的:
|
||||||
|
```
|
||||||
|
# dd if=/dev/urandom | pv | dd of=/dev/sda1
|
||||||
|
|
||||||
|
4,14MB 0:00:05 [ 98kB/s] [ <=> ]
|
||||||
|
```
|
||||||
|
想要推迟备份和磁盘管理工作?有了 dd 工具,你不会有太多的借口。它真的非常简单,但是要小心。祝你好运!
|
||||||
|
|
||||||
|
----------------
|
||||||
|
|
||||||
|
编译自:https://opensource.com/article/18/7/how-use-dd-linux
|
||||||
|
|
||||||
|
作者:[David Clinton](https://opensource.com/users/remyd)
|
||||||
|
选题:[lujun9972](https://github.com/lujun9972)
|
||||||
|
译者:[SunWave](https://github.com/SunWave)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user