LUKS是為Linux硬盤加密標准。通過提供一個標准的磁盤上的格式,它不僅方便之間分布的兼容性,而且還提供了多個用戶密碼的安全管理。必須首先對加密的卷進行解密,才能掛載其中的文件系統。
系統加密:【工具:cryptsetup;常用參數:luksFormat、luksOpen、luksClose、luksAddKey】【涉及文件:/etc/crypttab;自己創建密碼文件】
注意:上述參數大小寫不能亂用
好了,來試試這個強大的工具吧~
一、劃出一個分區進行測試(fdisk)
--->這裡以我的機子為例,因為我把空間全做成lvm了,所以分區就不一樣啦,我們取300M做實驗吧。
[[email protected] Desktop]# vgdisplay
--- Volume group ---
VG Name vol0
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 27
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 4
Open LV 3
Max PV 0
Cur PV 2
Act PV 2
VG Size 55.22 GiB
PE Size 32.00 MiB
Total PE 1767
Alloc PE / Size 928 / 29.00 GiB
Free PE / Size 839 / 26.22 GiB (還26G,夠了吧,我只需要300M做實驗)
VG UUID ZAGsPK-tIBO-08bd-RoMp-fPeC-Ei5l-x3asx6
[[email protected] Desktop]# lvcreate -L 300M -n testlv /dev/vol0
Rounding up size to full physical extent 320.00 MiB
Logical volume "testlv" created
--->ok,生成testlv分區,320M(跟PE有關)
二、分區加密(cryptsetup luksFormat,注意分區先不要格式化!)
[[email protected] Desktop]# cryptsetup luksFormat /dev/vol0/testlv
WARNING!
========
This will overwrite data on /dev/vol0/testlv irrevocably.
Are you sure? (Type uppercase yes): YES (注意,只能是大寫字母)
Enter LUKS passphrase: (輸入密碼)
Verify passphrase: (確認密碼)
三、好了,密碼創建好了,我們解密了把它格式化一下吧,格式化了才能掛載呀。(不解密的話是不能對它進行操作的)
1、[[email protected] Desktop]# cryptsetup luksOpen /dev/vol0/testlv testlv
Enter passphrase for /dev/vol0/testlv: (輸入密碼進行驗證!)--->注意,命令最後寫了一個映射的設備名“testlv”,來看看吧
[[email protected] Desktop]# ll /dev/mapper/testlv /dev/vol0/testlv
lrwxrwxrwx. 1 root root 7 Aug 4 15:17 /dev/mapper/testlv -> ../dm-7
lrwxrwxrwx. 1 root root 7 Aug 4 15:17 /dev/vol0/testlv -> ../dm-6--->兩個其實是同一個的。命令中必須得加上映射的名稱,可以自己man一下cryptsetup
2、[[email protected] Desktop]# mkfs.ext4 /dev/mapper/testlv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
81600 inodes, 325632 blocks
16281 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
40 block groups
8192 blocks per group, 8192 fragments per group
2040 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.--->ok,很快就格式化好了,那就新建一個目錄掛載吧!
3、[[email protected] Desktop]# mkdir /testlv
[[email protected] Desktop]# mount /dev/mapper/testlv /testlv/
[[email protected] Desktop]# df -h /testlv/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/testlv 308M 11M 283M 4% /testlv
--->想要開機自動掛載?寫道fstable裡吧
[[email protected] Desktop]# vim /etc/fstab (加上這一行)
/dev/mapper/testlv /testlv ext4 defaults 0 0【就這樣重啟是不會掛載上的,為什麼? 沒有寫入配置文件告訴系統我有加密分區。好,再繼續測試】