磁盘分区加密

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值