亲爱的 nixCraft,
我几乎随身携带我的 Linux 驱动的笔记本电脑。 如何保护存储在分区或可移动存储介质上的私人数据免受裸机攻击,因为任何人都可以在旅行时拿到我的笔记本电脑或 USB 笔式驱动器?
-真挚地,
担心我的数据。
Linux 硬盘加密
这实际上是一个很好的问题。 许多企业、小型企业和政府用户需要加密他们的笔记本电脑以保护机密信息,例如客户详细信息、文件、联系信息等等。 Linux 支持以下加密技术来保护硬盘、目录和分区。 使用以下任何一种技术写入的所有数据都将在运行中自动加密和解密。
Linux 加密方法
有两种方法可以加密您的数据:
文件系统堆叠级加密
- eCryptfs – 它是一个加密堆叠的 Linux 文件系统。 eCryptfs 将加密元数据存储在每个写入文件的标头中,以便可以在主机之间复制加密文件; 该文件将使用 Linux 内核密钥环中的正确密钥进行解密。 该解决方案被广泛使用,作为 Ubuntu 的加密主目录的基础,在 Google 的 ChromeOS 中本地化,并透明地嵌入到多个网络附加存储 (NAS) 设备中。
- 编码文件系统 – 它在用户空间中提供了一个加密的文件系统。 它无需任何特殊权限即可运行,并使用 FUSE 库和 Linux 内核模块提供文件系统接口。 您可以在下面找到指向源代码和二进制版本的链接。 EncFS 是开源软件,在 GPL 下获得许可。
块设备级加密
- 循环AES – 快速透明的文件系统和用于 linux 的交换加密包。 没有对 linux 内核进行源代码更改。 适用于 3.x、2.6、2.4、2.2 和 2.0 内核。
- VeraCrypt – 它是基于 TrueCrypt 代码库的免费开源磁盘加密软件,适用于 Windows 7/Vista/XP、Mac OS X 和 Linux。
- dm-crypt+LUKS – dm-crypt 是 Linux 内核 v2.6+ 及更高版本和 DragonFly BSD 中的透明磁盘加密子系统。 它可以加密整个磁盘、可移动媒体、分区、软件 RAID 卷、逻辑卷和文件。
在本教程中,我将解释如何在基于 Linux 的计算机或笔记本电脑上使用 Linux Unified Key Setup-on-disk-format (LUKS) 加密分区。
第 1 步:在 Linux 上安装 cryptsetup 实用程序
您需要安装以下软件包。 它包含 cryptsetup,这是一个使用 Device Mapper 和 dm-crypt 目标设置加密文件系统的实用程序。 Debian / Ubuntu Linux 用户键入以下 apt-get 命令或 apt 命令:# apt-get install cryptsetup
或者$ sudo apt install cryptsetup
示例输出:
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: console-setup console-setup-linux cryptsetup-bin kbd keyboard-configuration xkb-data Suggested packages: dosfstools keyutils The following NEW packages will be installed: console-setup console-setup-linux cryptsetup cryptsetup-bin kbd keyboard-configuration xkb-data 0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded. Need to get 3,130 kB of archives. After this operation, 13.2 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 https://deb.debian.org/debian stretch/main amd64 kbd amd64 2.0.3-2+b1 [343 kB] Get:2 https://deb.debian.org/debian stretch/main amd64 keyboard-configuration all 1.164 [644 kB] Get:3 https://deb.debian.org/debian stretch/main amd64 console-setup-linux all 1.164 [983 kB] Get:4 https://deb.debian.org/debian stretch/main amd64 xkb-data all 2.19-1 [648 kB] Get:5 https://deb.debian.org/debian stretch/main amd64 console-setup all 1.164 [117 kB] Get:6 https://deb.debian.org/debian stretch/main amd64 cryptsetup-bin amd64 2:1.7.3-4 [221 kB] Get:7 https://deb.debian.org/debian stretch/main amd64 cryptsetup amd64 2:1.7.3-4 [174 kB] Fetched 3,130 kB in 0s (7,803 kB/s) Preconfiguring packages ... Selecting previously unselected package kbd. (Reading database ... 22194 files and directories currently installed.) Preparing to unpack .../0-kbd_2.0.3-2+b1_amd64.deb ... Unpacking kbd (2.0.3-2+b1) ... Selecting previously unselected package keyboard-configuration. Preparing to unpack .../1-keyboard-configuration_1.164_all.deb ... Unpacking keyboard-configuration (1.164) ... Selecting previously unselected package console-setup-linux. Preparing to unpack .../2-console-setup-linux_1.164_all.deb ... Unpacking console-setup-linux (1.164) ... Selecting previously unselected package xkb-data. Preparing to unpack .../3-xkb-data_2.19-1_all.deb ... Unpacking xkb-data (2.19-1) ... Selecting previously unselected package console-setup. Preparing to unpack .../4-console-setup_1.164_all.deb ... Unpacking console-setup (1.164) ... Selecting previously unselected package cryptsetup-bin. Preparing to unpack .../5-cryptsetup-bin_2%3a1.7.3-4_amd64.deb ... Unpacking cryptsetup-bin (2:1.7.3-4) ... Selecting previously unselected package cryptsetup. Preparing to unpack .../6-cryptsetup_2%3a1.7.3-4_amd64.deb ... Unpacking cryptsetup (2:1.7.3-4) ... Setting up keyboard-configuration (1.164) ... Setting up xkb-data (2.19-1) ... Setting up kbd (2.0.3-2+b1) ... Processing triggers for systemd (232-25+deb9u1) ... Setting up cryptsetup-bin (2:1.7.3-4) ... Processing triggers for man-db (2.7.6.1-2) ... Setting up console-setup-linux (1.164) ... Created symlink /etc/systemd/system/sysinit.target.wants/keyboard-setup.service → /lib/systemd/system/keyboard-setup.service. Created symlink /etc/systemd/system/multi-user.target.wants/console-setup.service → /lib/systemd/system/console-setup.service. Setting up console-setup (1.164) ... Setting up cryptsetup (2:1.7.3-4) ... update-initramfs: deferring update (trigger activated) Processing triggers for systemd (232-25+deb9u1) ... Processing triggers for initramfs-tools (0.130) ... update-initramfs: Generating /boot/initrd.img-4.9.0-3-amd64
在 RHEL/CentOS/ 中加密硬盘驱动器所需的工具Fedora Linux
RHEL / CentOS / Oracle / Scientific Linux 用户输入以下 yum 命令:# yum install cryptsetup-luks
Fedora Linux 用户使用 dnf 命令:# dnf install cryptsetup-luks
第二步:配置LUKS分区
警告! 以下命令将删除您正在加密的分区上的所有数据。 您将丢失所有信息! 因此,请确保在键入以下任何命令之前将数据备份到外部源,例如 NAS 或硬盘。
打开终端列出所有 Linux 分区/磁盘,然后使用 cryptsetup 命令:# fdisk -l
语法是:cryptsetup luksFormat --type luks1 /dev/DEVICE
cryptsetup luksFormat --type luks2 /dev/DEVICE
在此示例中,我将加密 /dev/xvdc。 键入以下命令:# cryptsetup -y -v luksFormat /dev/xvdc
例如,使用 luks2 格式在 /dev/sdc 上设置 cryptsetup,运行:
示例输出:
WARNING! ======== This will overwrite data on /dev/xvdc irrevocably. Are you sure? (Type uppercase yes): YES Enter LUKS passphrase: Verify passphrase: Command successful.
# cryptsetup -y -v --type luks2 luksFormat /dev/sdc
此命令初始化卷,并设置初始密钥或密码。 请注意,密码是不可恢复的,所以不要忘记它。键入以下命令创建一个映射:# cryptsetup luksOpen /dev/xvdc backup2
示例输出:
Enter passphrase for /dev/xvdc:
在成功验证使用 luksFormat 命令扩展创建的提供的密钥材料后,您可以看到映射名称 /dev/mapper/backup2:# ls -l /dev/mapper/backup2
示例输出:
lrwxrwxrwx 1 root root 7 Oct 19 19:37 /dev/mapper/backup2 -> ../dm-0
您可以使用以下命令查看映射的状态:# cryptsetup -v status backup2
示例输出:
/dev/mapper/backup2 is active. type: LUKS1 cipher: aes-cbc-essiv:sha256 keysize: 256 bits device: /dev/xvdc offset: 4096 sectors size: 419426304 sectors mode: read/write Command successful.
您可以使用以下命令转储 LUKS 标头:# cryptsetup luksDump /dev/xvdc
示例输出:
LUKS header information for /dev/xvdc Version: 1 Cipher name: aes Cipher mode: xts-plain64 Hash spec: sha256 Payload offset: 4096 MK bits: 256 MK digest: 21 07 68 54 77 96 11 34 f2 ec 17 e9 85 8a 12 c3 1f 3e cf 5f MK salt: 8c a6 3d 8c e9 de 16 fb 07 fd 8e d3 72 d7 db 94 7e e0 75 f9 e0 23 24 df 50 26 fb 92 f8 b5 dd 70 MK iterations: 222000 UUID: 4dd563a9-5bff-4fea-b51d-b4124f7185d1 Key Slot 0: ENABLED Iterations: 2245613 Salt: 05 a8 b4 a2 54 f7 c6 ee 52 db 60 b6 12 7f 2f 53 3f 5d 2d 62 fb 5a b1 c3 52 da d5 5f 7b 2d 38 32 Key material offset: 8 AF stripes: 4000 Key Slot 1: DISABLED Key Slot 2: DISABLED Key Slot 3: DISABLED Key Slot 4: DISABLED Key Slot 5: DISABLED Key Slot 6: DISABLED Key Slot 7: DISABLED
第三步:格式化 Linux LUKS 分区
首先,您需要将零写入 /dev/mapper/backup2 加密设备。 这将用零分配块数据。 这可以确保外部世界将其视为随机数据,即它可以防止泄露使用模式:# dd if=/dev/zero of=/dev/mapper/backup2
dd 命令可能需要几个小时才能完成。 我建议你使用 pv 命令来监控进度:# pv -tpreb /dev/zero | dd of=/dev/mapper/backup2 bs=128M
示例输出:
dd: error writing '/dev/mapper/backup2': No space left on device ] 200GiB 0:16:47 [ 203MiB/s] [ <=> ] 1600+1 records in 1599+1 records out 214746267648 bytes (215 GB, 200 GiB) copied, 1008.19 s, 213 MB/s
您还可以将 status=progress 选项传递给 dd 命令:# dd if=/dev/zero of=/dev/mapper/backup2 status=progress
示例输出:
2133934592 bytes (2.1 GB, 2.0 GiB) copied, 142 s, 15.0 MB/s
接下来,创建一个文件系统即格式文件系统,输入:# mkfs.ext4 /dev/mapper/backup2
示例输出:
mke2fs 1.42.13 (17-May-2015) Creating filesystem with 52428288 4k blocks and 13107200 inodes Filesystem UUID: 1c71b0f4-f95d-46d6-93e0-cbd19cb95edb Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
要将新文件系统挂载到 /backup2,请输入:# mkdir /backup2
# mount /dev/mapper/backup2 /backup2
# df -H
# cd /backup2
# ls -l
如何卸载和保护数据?
键入以下命令:# umount /backup2
# cryptsetup luksClose backup2
如何挂载或重新挂载加密分区?
键入以下命令:# cryptsetup luksOpen /dev/xvdc backup2
# mount /dev/mapper/backup2 /backup2
# df -H
# mount
示例输出:
看
打开 LUKS 分区的 shell 脚本包装器
并为 nas 设备设置映射。
我可以在基于 LUKS 的分区/LVM 卷上运行 fsck 吗?
是的,您可以在基于 LUKS 的系统上使用 fsck 命令:# umount /backup2
# fsck -vy /dev/mapper/backup2
# mount /dev/mapper/backup2 /backu2
有关更多详细信息,请参阅如何在基于 LUKS (dm-crypt) 的 LVM 物理卷上运行 fsck。
如何更改加密分区的 LUKS 密码(密码)?
键入以下命令### see key slots, max -8 i.e. max 8 passwords can be setup for each device ####
# cryptsetup luksDump /dev/xvdc
# cryptsetup luksAddKey /dev/xvdc
Enter any passphrase: Enter new passphrase for key slot: Verify passphrase:
删除或删除旧密码:# cryptsetup luksRemoveKey /dev/xvdc
请注意,您需要输入旧密码/密码。
接下来是什么?
您可以使用以下软件存储文件或存储备份:
- Debian / Ubuntu Linux 使用 rsnapshot 增量备份实用程序安装和配置远程文件系统快照
- 如何设置 Red hat / CentOS Linux 远程备份 / 快照服务器
查看相关媒体
本教程还提供视频格式:
(视频01: cryptsetup 命令演示)
结论
您现在拥有所有数据的加密分区。
优点:
- LUKS 对整个块设备进行加密,因此非常适合保护移动设备的内容,例如可移动存储介质(usb pen)或笔记本电脑磁盘驱动器。
- 您还可以与您的 nas 服务器一起使用来保护备份。
- 带有 AES-NI(高级加密标准指令集)的 Intel 和 AMD cpus 可以为 Linux 内核 v2.6.32+ 加速基于 dm-crypt 的加密。 这将加速硬盘加密。
- 也适用于交换分区,以便您的笔记本电脑可以使用休眠功能(挂起到磁盘),在关闭机器之前将 RAM 的内容写入交换分区。
缺点:
- LUKS 最多只支持 8 个密码,即只有 8 个用户可以对同一设备拥有不同的访问密钥。
- LUKS 也不推荐用于需要文件级加密的应用程序。
结论
在本教程中,我们了解了 Linux 上的硬盘加密。 有关更多信息,请参阅 cryptsetup 手册页并阅读 RHEL 6.x 文档:man cryptsetup
这个条目是 1 的 5 在里面 Linux Unified Key Setup (LUKS) 是一个磁盘加密教程 系列。 继续阅读本系列的其余部分:
- 使用 LUKS 的 Linux 硬盘加密
- 在 Linux 上备份和恢复 LUKS 标头
- 在 Linux 上更改 LUKS 磁盘加密密码
- 在 Linux 中使用 Dropbear SSH 密钥远程解锁 LUKS
- 在 Linux 上使用密钥文件添加/启用 LUKS 磁盘加密