歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下的磁盤加密LUKS

Linux下的磁盤加密LUKS

日期:2017/2/28 14:50:21   编辑:Linux教程

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:

Copyright © Linux教程網 All Rights Reserved