磁盤分區加密

1、分區加密
cryptsetup命令:manage plain dm-crypt and LUKS encrypted volumes,創建加密文件系統

(1)對設備進行加密並掛載到/mnt上
[root@localhost ~]# fdisk /dev/vdb 分區
[root@localhost ~]# partprobe
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 512000 vdb1
11 0 3655680 sr0
[root@localhost ~]# mount /dev/vdb1 /mnt/ ##分區必須格式化,否則掛載不成功
mount: wrong fs type, bad option, bad superblock on /dev/vdb1,
missing codepage or helper program, or other error

   In some cases useful info is found in syslog - try
   dmesg | tail or so.

[root@localhost ~]# cryptsetup luksFormat /dev/vdb1 ##Initializes a LUKS partition and sets the initial
passphrase (初始化一個鎖設備並設置初始化的密碼)

WARNING!

This will overwrite data on /dev/vdb1 irrevocably.

Are you sure? (Type uppercase yes): YES (大寫)
Enter passphrase: 輸入密碼
Verify passphrase: 再次輸入
[root@localhost ~]# cryptsetup open /dev/vdb1 hello
解密,該處hello爲自定義,可修改爲其他
Enter passphrase for /dev/vdb1:
[root@localhost ~]# ll /dev/mapper/hello ##解密後會生成/dev/mapper/hello文件
lrwxrwxrwx. 1 root root 7 Nov 8 14:30 /dev/mapper/hello -> ../dm-0
[root@localhost ~]# mkfs.xfs /dev/mapper/hello
格式化設備文件
[root@localhost ~]# mount /dev/mapper/hello /mnt
掛載格式化的設備文件到/mnt上
[root@localhost ~]# df -h
查看掛載的設備
Filesystem Size Used Avail Use% Mounted on
… … … … … …
/dev/mapper/hello 495M 26M 470M 6% /mnt
[root@localhost ~]# touch /mnt/file{1..3}
在掛載的目錄上創建文件
[root@localhost mnt]# ls /mnt
file1 file10 file2 file3

(2)卸載掛載在/mnt上的設備,並關閉加密的容器
[root@localhost ~]# umount /mnt 卸載
[root@localhost ~]# ls /mnt/ 查看之前掛載文件不存在
[root@localhost ~]# cryptsetup close hello
[root@localhost ~]# ll /dev/mapper/hello ##解密後會生成/dev/mapper/hello文件不存在
ls: cannot access /dev/mapper/hello: No such file or directory

(3)、再次使用時,打開加密的容器即可,其名稱可與之前的不同
[root@localhost ~]# cryptsetup open /dev/vdb1 hello ##打開加密的容器,
Enter passphrase for /dev/vdb1:
[root@localhost ~]# mount /dev/mapper/hello /mnt/ ##掛載格式化的設備文件到/mnt上
[root@localhost ~]# ls /mnt/ 之前建立的文件掛載後依舊存在
file file2 file3

2、自動掛載加密設備
[root@localhost ~]# vim /root/passwd ##創建密碼文件
[root@localhost ~]# cryptsetup luksAddKey /dev/vdb1 /root/passwd ##把密碼文件與設備關聯

Enter any passphrase: ##輸入密碼進行關聯
[root@localhost ~]# vim /etc/crypttab 設定開機解密
hello /dev/vdb1 /root/passwd 系統自動讀取密碼記錄文件
解密後名稱 設備 密碼文件

[root@localhost ~]# vim /etc/fstab ##開機自動掛載加密設備
/dev/mapper/hello /mnt ext4 defaults 0 0
設備名 掛載點 掛載格式 掛載參數 是否備份 是否檢測

3、刪除該加密的步驟
[root@localhost ~]# umount /mnt/ ##卸載之前掛載的設備
[root@localhost ~]# rm -fr /root/passwd ##刪除密碼文件
[root@localhost ~]# rm -fr /etc/crypttab ##刪除開機解密
[root@localhost ~]# vim /etc/fstab ##刪除開機自動掛載加密設備的內容
[root@localhost ~]# cryptsetup close world ##關閉加密的容器
通過格式化破壞加密文件系統
[root@localhost ~]# fdisk /dev/vdb ##刪除設備/dev/vdb1
[root@localhost ~]# partprobe
[root@localhost ~]# cat /proc/partitions
major minor #blocks name

253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
11 0 3655680 sr0

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