Linux下的磁碟加密LUKS

來源:互聯網
上載者:User

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

工具:cryptsetup(預設已經安裝)

常用參數:luksFormat、luksOpen、luksClose、luksAddKey

使用cryptsetup對分區進行了加密後,這個分區就不再允許直接掛載。LUKS也是一種基於device mapper 機制的加密方案。如果要使用這個分區,必須對這個分區做一個映射,映射到/dev/mapper這個目錄裡去,我們只能掛載這個映射才能使用。然而做映射的時候是需要輸入解密密碼的。

註:要解除加密需要備份分區內的檔案再重新格式化LUKS分區

Crypsetup工具加密的特點:

Ø加密後不能直接掛載

Ø加密後硬碟丟失也不用擔心資料被盜

Ø加密後必須做映射才能掛載

格式化LUKS分區
[root@rhel6 ~]# cryptsetup luksFormat /dev/vda8 //將分區進行LUKS格式(變成LUKS分區)
WARNING!
========
This will overwrite data on /dev/vda8 irrevocably.

Are you sure? (Type uppercase yes): YES //輸入大寫的YES
Enter LUKS passphrase: //輸入兩次密碼
Verify passphrase:

映射分區
[root@rhel6 ~]# cryptsetup luksOpen /dev/vda8 luks_test //開啟LUKS分區,將在/dev/mapper/目錄中產生一個luks_test的檔案
Enter passphrase for /dev/vda8: //必須輸入luks密碼才能開啟LUKS分區

格式化、掛載、使用分區
[root@rhel6 ~]# mkfs.ext4 /dev/mapper/luks_test
[root@rhel6 ~]# mount /dev/mapper/luks_test /luks/

關閉映射,先卸載後關閉
[root@rhel6 ~]# umount /luks/
[root@rhel6 ~]# cryptsetup luksClose luks_test //關閉LUKS分區
[root@rhel6 ~]# mount /dev/vda8 /luks/ //無法直接掛載/dev/vda8分區
mount: unknown filesystem type 'crypto_LUKS'

實現開機自動掛載LUKS分區:
[root@rhel6 ~]# dd if=/dev/urandom of=keyfile bs=1k count=4
4+0 records in
4+0 records out
4096 bytes (4.1 kB) copied, 0.00206882 s, 2.0 MB/s
[root@rhel6 ~]# cryptsetup luksAddKey /dev/vda8 keyfile
Enter any passphrase:
[root@rhel6 ~]# vi /etc/crypttab
name /dev/vda8 /root/keyfile luks
[root@rhel6 ~]# vi /etc/fstab
/dev/mapper/name /luks ext4 _netdev 0 0

添加/移除/修改LUKS密碼
[root@rhel6 ~]# cryptsetup luksAddKey /dev/vda8
Enter any passphrase:
Enter new passphrase for key slot:
Verify passphrase:
[root@rhel6 ~]# cryptsetup luksRemoveKey /dev/vda8
Enter LUKS passphrase to be deleted:
[root@rhel6 ~]# cryptsetup luksAddKey /dev/vda8 keyfile
Enter any passphrase:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.