Cryptsetup
在本書中,我將解釋如何在基於 Linux 的計算機或筆記型電腦上使用 Linux 統一金鑰設定磁碟格式 (LUKS) 對您的分割槽進行加密。
有兩種方法可以加密您的資料
- eCryptfs - 它是一個加密的堆疊式 Linux 檔案系統。eCryptfs 將加密元資料儲存在每個寫入檔案的標頭中,因此加密檔案可以在主機之間複製;檔案將在 Linux 核心金鑰環中使用正確的金鑰進行解密。此解決方案被廣泛使用,作為 Ubuntu 加密主目錄的基礎,原生整合在 Google 的 ChromeOS 中,並透明地嵌入在多個網路附加儲存 (NAS) 裝置中。
- EncFS - 它在使用者空間提供一個加密檔案系統。它在沒有特殊許可權的情況下執行,並使用 FUSE 庫和 Linux 核心模組來提供檔案系統介面。您可以在下面找到指向原始碼和二進位制釋出版本的連結。EncFS 是開源軟體,根據 GPL 許可證授權。
- Loop-AES - 適用於 Linux 的快速透明檔案系統和交換加密包。無需更改 Linux 核心的原始碼。適用於 3.x、2.6、2.4、2.2 和 2.0 核心。
- TrueCrypt - 它是適用於 Windows 7/Vista/XP、Mac OS X 和 Linux 的免費開源磁碟加密軟體。(已棄用)
- dm-crypt+LUKS - dm-crypt 是 Linux 核心 v2.6+ 及更高版本和 DragonFly BSD 中的透明磁碟加密子系統。它可以加密整個磁碟、可移動介質、分割槽、軟體 RAID 卷、邏輯卷和檔案。
您需要安裝以下軟體包。它包含 cryptsetup,一個使用裝置對映器和 dm-crypt 目標設定加密檔案系統的工具。Debian / Ubuntu Linux 使用者鍵入以下 apt-get 命令
# apt-get install cryptsetup
示例輸出
Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: cryptsetup-bin libcryptsetup4 Suggested packages: busybox The following NEW packages will be installed: cryptsetup cryptsetup-bin libcryptsetup4 0 upgraded, 3 newly installed, 0 to remove and 7 not upgraded. Need to get 168 kB of archives. After this operation, 669 kB of additional disk space will be used. Do you want to continue [Y/n]? y Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ precise/main libcryptsetup4 amd64 2:1.4.1-2ubuntu4 [55.8 kB] Get:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ precise/main cryptsetup-bin amd64 2:1.4.1-2ubuntu4 [32.2 kB] Get:3 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ precise/main cryptsetup amd64 2:1.4.1-2ubuntu4 [80.0 kB] Fetched 168 kB in 0s (268 kB/s) Preconfiguring packages ... Selecting previously unselected package libcryptsetup4. (Reading database ... 25374 files and directories currently installed.) Unpacking libcryptsetup4 (from .../libcryptsetup4_2%3a1.4.1-2ubuntu4_amd64.deb) ... Selecting previously unselected package cryptsetup-bin. Unpacking cryptsetup-bin (from .../cryptsetup-bin_2%3a1.4.1-2ubuntu4_amd64.deb) ... Selecting previously unselected package cryptsetup. Unpacking cryptsetup (from .../cryptsetup_2%3a1.4.1-2ubuntu4_amd64.deb) ... Processing triggers for man-db ... Processing triggers for ureadahead ... Setting up libcryptsetup4 (2:1.4.1-2ubuntu4) ... Setting up cryptsetup-bin (2:1.4.1-2ubuntu4) ... Setting up cryptsetup (2:1.4.1-2ubuntu4) ... update-initramfs: deferring update (trigger activated) Processing triggers for libc-bin ... ldconfig deferred processing now taking place Processing triggers for initramfs-tools ... update-initramfs: Generating /boot/initrd.img-3.2.0-31-virtual
RHEL / CentOS / Fedora Linux 使用者鍵入以下 yum 命令
# yum install cryptsetup-luks
警告! 以下命令將刪除您要加密的分割槽上的所有資料。您將丟失所有資訊!因此,在鍵入以下任何命令之前,請確保將您的資料備份到外部來源,例如 NAS 或硬碟。 |
在本例中,我將使用名為 /dev/xvdc 的分割槽,我們的第一個任務是使用隨機資料將該分割槽覆蓋 3 次,這足以保護您免受法醫調查。我花了將近 30 分鐘才將 20 GB 的分割槽覆蓋 3 次。
# shred --verbose --random-source=/dev/urandom --iterations=3 /dev/xvdc
接下來,我將加密 /dev/xvdc。鍵入以下命令
# cryptsetup --verbose --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 5000 --use-urandom luksFormat /dev/xvdc
示例輸出
WARNING! ======== This will overwrite data on /dev/xvdc irrevocably. Are you sure? (Type uppercase yes): YES Enter LUKS passphrase: Verify passphrase: Command successful.
此命令初始化卷,並設定初始金鑰或密碼。請注意,密碼不可恢復,因此請不要忘記它。鍵入以下命令建立對映
# 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
首先,您需要將零寫入 /dev/mapper/backup2 加密裝置。這將用零分配塊資料。這確保了外部世界將看到它為隨機資料,即它防止披露使用模式
# dd if=/dev/zero of=/dev/mapper/backup2
dd 命令可能需要幾個小時才能完成。我建議您使用 pv 命令來監控進度
# pv -tpreb /dev/zero | dd of=/dev/mapper/backup2 bs=128M
要建立檔案系統,即格式化檔案系統,請輸入
# mkfs.ext4 /dev/mapper/backup2
要將新檔案系統掛載到 /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 分割槽併為 NAS 裝置設定對映的 shell 指令碼包裝器。
是的,您可以在基於 LUKS 的系統上使用 fsck 命令
# umount /backup2 # fsck -vy /dev/mapper/backup2 # mount /dev/mapper/backup2 /backup2
檢視如何在基於 LUKS (dm-crypt) 的 LVM 物理捲上執行 fsck 的更多詳細資訊。
鍵入以下命令
### 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
請注意,您需要輸入舊密碼。