Linux下的磁盤加密LUKS

LUKS(Linux Unified Key Setup)爲Linux硬盤分區加密提供了一種標準,它不僅能通用於不同的Linux發行版本,還支持多用戶/口令。因爲它的加密密鑰獨立於口令,所以如果口令失密,我們可以迅速改變口令而無需重新加密真個硬盤。通過提供一個標準的磁盤上的格式,它不僅方便之間分佈的兼容性,而且還提供了多個用戶密碼的安全管理。必須首先對加密的捲進行解密,才能掛載其中的文件系統。

工具:cryptsetup(默認已經安裝)
常用參數:luksFormat、luksOpen、luksClose、luksAddKey
 
使用cryptsetup對分區進行了加密後,這個分區就不再允許直接掛載。LUKS也是一種基於device mapper 機制的加密方案。如果要使用這個分區,必須對這個分區做一個映射,映射到/dev/mapper這個目錄裏去,我們只能掛載這個映射才能使用。然而做映射的時候是需要輸入解密密碼的。
注:要解除加密需要備份分區內的文件再重新格式化LUKS分區
 
Crypsetup工具加密的特點:
Ø加密後不能直接掛載
Ø加密後硬盤丟失也不用擔心數據被盜
Ø加密後必須做映射才能掛載
 
  1. 格式化LUKS分區 
  2. [root@rhel6 ~]# cryptsetup luksFormat /dev/vda8                     //將分區進行LUKS格式(變成LUKS分區) 
  3. WARNING! 
  4. ======== 
  5. This will overwrite data on /dev/vda8 irrevocably. 
  6.  
  7. Are you sure? (Type uppercase yes): YES                      //輸入大寫的YES 
  8. Enter LUKS passphrase:                                   //輸入兩次密碼 
  9. Verify passphrase:  
  10.  
  11. 映射分區 
  12. [root@rhel6 ~]# cryptsetup luksOpen /dev/vda8 luks_test             //打開LUKS分區,將在/dev/mapper/目錄中生成一個luks_test的文件 
  13. Enter passphrase for /dev/vda8:                                   //必須輸入luks密碼才能打開LUKS分區 
  14.  
  15. 格式化、掛載、使用分區 
  16. [root@rhel6 ~]# mkfs.ext4 /dev/mapper/luks_test  
  17. [root@rhel6 ~]# mount /dev/mapper/luks_test /luks/ 
  18.  
  19. 關閉映射,先卸載後關閉 
  20. [root@rhel6 ~]# umount /luks/ 
  21. [root@rhel6 ~]# cryptsetup luksClose luks_test                      //關閉LUKS分區 
  22. [root@rhel6 ~]# mount /dev/vda8 /luks/                           //無法直接掛載/dev/vda8分區 
  23. mount: unknown filesystem type 'crypto_LUKS' 
  24.  
  25. 實現開機自動掛載LUKS分區: 
  26. [root@rhel6 ~]# dd if=/dev/urandom of=keyfile bs=1k count=4 
  27. 4+0 records in 
  28. 4+0 records out 
  29. 4096 bytes (4.1 kB) copied, 0.00206882 s, 2.0 MB/s 
  30. [root@rhel6 ~]# cryptsetup luksAddKey /dev/vda8 keyfile 
  31. Enter any passphrase:  
  32. [root@rhel6 ~]# vi /etc/crypttab  
  33. name    /dev/vda8       /root/keyfile   luks 
  34. [root@rhel6 ~]# vi /etc/fstab
  35. /dev/mapper/name        /luks                   ext4    _netdev         0 0 
  36.  
  37. 添加/移除/修改LUKS密碼 
  38. [root@rhel6 ~]# cryptsetup luksAddKey /dev/vda8 
  39. Enter any passphrase:  
  40. Enter new passphrase for key slot:  
  41. Verify passphrase:  
  42. [root@rhel6 ~]# cryptsetup luksRemoveKey /dev/vda8 
  43. Enter LUKS passphrase to be deleted:  
  44. [root@rhel6 ~]# cryptsetup luksAddKey /dev/vda8 keyfile 
  45. Enter any passphrase:  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章