親愛的 nixCraft
我大部分時間都隨身攜帶一台 Linux 筆記本電腦。如何保護存儲在分區和可移動存儲介質上的私人數據免受裸機攻擊?
– 真摯地,
請擔心我的數據。
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
第 2 步:配置 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
第 3 步:格式化 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 筆)和筆記本電腦磁盤驅動器。
- 它還可以用於 NAS 服務器上以保護備份。
- 帶有 AES-NI(高級加密標準指令集)的 Intel 和 AMD CPU 可以在 Linux 內核 v2.6.32+ 中加速基於 dm-crypt 的加密。這加速了硬盤加密。
- 它也適用於交換,因此筆記本電腦可以在關閉機器之前使用休眠(掛起到磁盤)將 RAM 的內容寫入交換。
壞處:
- LUKS 最多只支持 8 個密碼。換句話說,只有 8 個用戶可以對同一設備擁有不同的訪問密鑰。
- LUKS 也不推薦用於需要文件級加密的應用程序。
結論是
在本教程中,您了解了 Linux 中的硬盤加密。有關更多信息,請參閱 cryptsetup 手冊頁。 RHEL 6.x 文檔:man cryptsetup
這個條目是 1 的 五 裡面 Linux Unified Key Setup (LUKS) 是一個磁盤加密教程 系列。閱讀本系列的其餘部分。
- 使用 LUKS 進行 Linux 硬盤加密
- 在 Linux 上備份和恢復 LUKS 標頭
- 在 Linux 上更改 LUKS 磁盤加密密碼
- 在 Linux 上使用 Dropbear SSH 密鑰遠程解鎖 LUKS
- 在 Linux 上使用 Keyfile 添加/啟用 LUKS 磁盤加密