在本文中,我将说明如何在基于Linux的计算机或笔记本电脑上使用cryptsetup来给磁盘加解密。考虑到你要准备一个物理分区用来加密后,该分区上原有的数据会被破坏掉。我们会用cryptsetup 创建 LUKS 的虚拟加密盘(逻辑卷):所谓的“虚拟加密盘”,就是说这个盘并不是对应物理分区,而是对应一个虚拟分区(逻辑卷)。这个虚拟分区,说白了就是一个大文件。虚拟分区有多大,这个文件就有多大。“虚拟加密盘”的一个主要好处在于——可以拷贝复制。

安装cryptsetup实用程序

$ sudo apt install cryptsetup

创建一个文件作为容器

下面用 dd 命令创建1GB(1024MB)的大文件,该文件位于 /root/luks.vol 路径。执行如下命令需要切换到root用户。

su
dd if=/dev/zero of=/root/luks.vol bs=1M count=1024

或者还可以使用 fallocate 命令创建容器文件。对于特别大的容器文件,性能高于dd 命令。

fallocate -l 64G /root/luks.vol

LUKS 方式配置该文件容器

# cryptsetup -y -v luksFormat /root/luks.vol
WARNING!
========
This will overwrite data on /root/luks.vol irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase for /root/luks.vol: 
Verify passphrase: 
Command successful.

该命令初始化卷,并设置初始密钥或密码短语。请注意,密码无法恢复,请不要忘记密码。键入以下命令可创建映射:

# cryptsetup luksOpen /root/luks.vol backup2
Enter passphrase for /root/luks.vol: 

成功验证后,可以看到映射名称/dev/mapper/backup2

# ls -l /dev/mapper/backup2
lrwxrwxrwx 1 root root 7 4月  16 16:13 /dev/mapper/backup2 -> ../dm-2

可以使用以下命令查看映射的状态:

# cryptsetup -v status backup2
/dev/mapper/backup2 is active.
  type:    LUKS1
  cipher:  aes-xts-plain64
  keysize: 256 bits
  key location: dm-crypt
  device:  /dev/loop1
  loop:    /root/luks.vol
  sector size:  512
  offset:  4096 sectors
  size:    134213632 sectors
  mode:    read/write
Command successful.

可以使用以下命令打印LUKS标头:

# cryptsetup luksDump /root/luks.vol

Version:        1
Cipher name:    aes
Cipher mode:    xts-plain64
Hash spec:      sha256
Payload offset: 4096
MK bits:        256
MK digest:      22 f3 56 98 21 f7 14 ac 9a 3e 18 3d df ef 03 4f 9f 6a 92 e5 
MK salt:        21 ec c3 5b 96 b1 27 2a 97 c2 48 32 33 b1 96 17 
                81 94 40 0c 40 1b 2a 21 2d 8e fc dd 0a 95 42 33 
MK iterations:  207392
UUID:           7fe8ba74-6234-4c98-8a34-54bf5d61973a

Key Slot 0: ENABLED
        Iterations:             3318278
        Salt:                   55 38 50 25 ec 5f c7 9b b4 bf db c2 f9 25 df 04 
                                fa 4b 3b 56 16 4d 9d 99 d0 2b 7b 88 e9 80 81 66 
        Key material offset:    8
        AF stripes:             4000
Key Slot 1: DISABLED
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED

格式化LUKS分区

接下来,创建一个文件系统,即格式化文件系统,输入:

# mkfs.ext4 /dev/mapper/backup2
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 16776704 4k blocks and 4194304 inodes
Filesystem UUID: e25fb2c1-2020-421c-b57d-81622436bef3
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done  

要将新文件系统挂载到/backup2,请输入:

# mkdir /backup2
# mount /dev/mapper/backup2 /backup2
# df -H
......
/dev/mapper/backup2              63G   53M   60G   1% /backup2
# cd /backup2
# ls -l

如何卸载和保护数据?

键入以下命令:

# umount /backup2
# cryptsetup luksClose backup2

在基于LUKS的分区上运行fsck
输入如下命令:

# umount /backup2
# fsck -vy /dev/mapper/backup2
# mount /dev/mapper/backup2 /backu2

更改加密分区的LUKS密码

输入以下命令

### see key slots, max -8 i.e. max 8 passwords can be setup for each device ####
# cryptsetup luksDump /root/luks.vol
# cryptsetup luksAddKey /root/luks.vol
Enter any existing passphrase: 
Enter new passphrase for key slot: 
Verify passphrase: 

删除旧密码

# cryptsetup luksRemoveKey /dev/xvdc

结论
现在,你将拥有一个用于所有数据的加密分区,下面是使用这种方法对磁盘加密的优缺点对比。
优点:

  • LUKS对整个块设备进行加密,因此非常适合保护移动设备的内容,例如可移动存储介质(usb笔)或笔记本电脑磁盘驱动器。
  • 你还可以将其与nas服务器一起使用以保护备份。
    缺点:
  • LUKS仅支持最多8个密码。
  • 对于需要文件级加密的应用程序,也不建议使用LUKS
Logo

更多推荐