1.關於luks加密
LUKS(Linux Unified Key Setup)為Linux硬盤分區加密提供了一種標准,它不僅能通用於不同的
Linux發行版本,還支持多用戶/口令。因為它的加密密鑰獨立於口令,所以如果口令失密,我們可以
迅速改變口令而無需重新加密整個硬盤。通過提供一個標准的磁盤上的格式,它不僅方便之間分布的
兼容性,而且還提供了多個用戶密碼的安全管理。必須首先對加密的卷進行解密,才能掛載其中的文件
系統。
文件系統在加密層之上,當加密層被破壞掉之后,磁盤里的內容就看不到,因為沒有設備對
他解密
crypsetup工具加密的特點:
(1)加密后不能直接掛載
(2)加密后硬盤丟失也不用擔心數據被盜
(3)加密后必須做映射才能掛載
2.磁盤加密
cryptsetup luksFormat /dev/vdb1 # 將分區進行LUKS格式加密(變成LUKS分區)
cryptsetup open 設備名 # 打開,也就是解密,設備名任意,此處的設備為虛擬設備
cryspsetup close 設備名 # 關閉解密,也就是恢復加密狀態
[root@server ~]# cryptsetup luksFormat /dev/vdb5 # 將分區進行LUKS格式加密(變成LUKS分區) 
[root@server ~]# cryptsetup open /dev/vdb5 lala # 打開,也就是解密,lala為虛擬設備
Enter passphrase for /dev/vdb5:
[root@server ~]# ll /dev/mapper/ # 當解密成功時,會發現在這個目錄下生成虛擬設備
[root@server ~]# mkfs.xfs /dev/mapper/lala # 格式化虛擬設備
[root@server ~]# mount /dev/mapper/lala /mnt # 掛載虛擬設備就可以使用了
[root@server ~]# df
[root@server ~]# touch /mnt/file{1..10} # 因為設備是解密狀態,所以可以新建文件
[root@server ~]# ls /mnt # 也可以查看內容
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@server ~]# umount /mnt
[root@server ~]# cryptsetup close lala # 關掉解密狀態,也就是恢復加密狀態
[root@server ~]# ll /dev/mapper/ # 查看到虛擬設備消失
total 0
crw------- 1 root root 10, 236 Nov 9 19:53 control
[root@server ~]# mount /dev/mapper/lala /mnt # 想要重新掛載虛擬設備,發現掛載失敗,虛擬設備是臨時的,當關閉虛擬設備時,他就失效了
mount: special device /dev/mapper/lala does not exist
[root@server ~]# cryptsetup open /dev/vdb5 haha # 我們重新解密,生成虛擬設備
Enter passphrase for /dev/vdb5:
注:此處的密碼是第一次加密時設置的密碼
[root@server ~]# mount /dev/mapper/haha /mnt # 掛載成功
[root@server ~]# ls /mnt # 重新查看到內容
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
3.加密設備的開機掛載
[root@server ~]# vim /etc/fstab # 設定自動掛載
10 /dev/mapper/disk /mnt xfs defaults 0 0

[root@server ~]# vim /etc/crypttab # 編輯自動生成虛擬設備文件
1 disk /dev/vdb5 /root/vdb5pass 
[root@server ~]# vim /root/vdb5pass # 編輯密碼文件 ,此處的密碼不是隨意的,而是磁盤加密設定的密碼
1 ting@666
[root@server ~]# chmod 600 /root/vdb5pass # 修改密碼文件的權限
[root@server ~]# cryptsetup luksAddKey /dev/vdb5 /root/vdb5pass # 把密碼添加到luks加密中,即讓密碼文件生效
Enter any passphrase: # 必須通過認證才能添加成功
[root@server ~]# reboot
[root@server ~]# df # 查看到自動掛載 
4. 取消自動加密掛載
[root@server ~]# rm -fr /root/vdb5pass # 刪除密碼文件
[root@server ~]# vim /etc/fstab # 刪除對/dev/vdb5的永久掛載設置
[root@server ~]# vim /etc/crypttab # 刪除文件內容
[root@server ~]# umount /mnt/ # 解除掛載
[root@server ~]# cryptsetup close disk # 關閉解密
[root@server ~]# mkfs.xfs /dev/vdb5 -f # 強制格式化操作
[root@server ~]# mount /dev/vdb5 /mnt # 再次掛載,發現是一般的掛載,沒有加密
5.關於加密過程中出現的問題,以及解決方案
(1)加密失敗
[root@server ~]# cryptsetup luksFormat /dev/vdb5 # 當我想要給/dev/vdb5加密時,發現加密失敗
[root@server ~]# df # 結果發現/dev/vdb5掛載在/mnt下,也就是設備占用着 
[root@server ~]# umount /dev/vdb5 # 卸載掉就好了
(2)解密時,發現報錯
[root@server ~]# cryptsetup open /dev/vdb5 haha # 當我想要解密時,發現報錯,
Enter passphrase for /dev/vdb5:
Cannot use device /dev/vdb5 which is in use (already mapped or mounted).
[root@server ~]# mount -a # 檢測是否設備處於掛載狀態
[root@server ~]# dmsetup ls # 顯示當前的device mapper的信息
lala (252:0)
[root@server ~]# dmsetup remove lala # 刪除lala設備
[root@server ~]# dmsetup status # 再次顯示當前的device mapper的信息,此時解密就不會報錯了
No devices found
(3)命令執行不了,注意此處命令的大小寫
[root@server ~]# cryptsetup luksAddkey /dev/vdb5 /root/vdb5pass # 出現這種情況一定是命令寫錯了
[root@server ~]# cryptsetup luksAddKey /dev/vdb5 /root/vdb5pass # 這是正確的命令
