RHEL6 学习:使用 cryptsetup 给分区加密

今天学习了 RHEL 对硬盘分区加密的知识,在 RHEL 系统里可以通过使用 cryptsetup 工具
对硬盘分区进行加密,加密后的分区需要输入密码才能打开,可以把比较敏感的文件放在指定分
区中,并启用加密,从而增强了文件的安全性,下面演示下。

一 cryptsetup 给分区加密
--1.1 增加分区

 [root@redhatB ~]# fdisk -cu /dev/sdc

Command (m for help): p

Disk /dev/sdc: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb097ae92

Device Boot      Start         End      Blocks   Id  System
/dev/sdc1              63     4209029     2104483+  8e  Linux LVM
/dev/sdc2         4209030     8418059     2104515   8e  Linux LVM
/dev/sdc3         8418060    12627089     2104515   8e  Linux LVM
/dev/sdc4        12627090    20964824     4168867+   5  Extended
/dev/sdc5        12627153    14747669     1060258+  8e  Linux LVM
/dev/sdc6        14747733    16868249     1060258+  8e  Linux LVM

Command (m for help): n
First sector (16870298-20964824, default 16870298): 
Using default value 16870298
Last sector, +sectors or +size{K,M,G} (16870298-20964824, default 20964824): +1G

Command (m for help): p

Disk /dev/sdc: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb097ae92

Device Boot      Start         End      Blocks   Id  System
/dev/sdc1              63     4209029     2104483+  8e  Linux LVM
/dev/sdc2         4209030     8418059     2104515   8e  Linux LVM
/dev/sdc3         8418060    12627089     2104515   8e  Linux LVM
/dev/sdc4        12627090    20964824     4168867+   5  Extended
/dev/sdc5        12627153    14747669     1060258+  8e  Linux LVM
/dev/sdc6        14747733    16868249     1060258+  8e  Linux LVM
/dev/sdc7        16870298    18967449     1048576   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

备注:上例增加了分区 /dev/sdc7,大小为 1 GB。
   
--1.2 刷新kernel

[root@redhatB ~]# partx -a /dev/sdc
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6

[root@redhatB ~]# ll /dev/sdc*
brw-rw----. 1 root disk 8, 32 Jul 29 20:00 /dev/sdc
brw-rw----. 1 root disk 8, 33 Jul 22 20:51 /dev/sdc1
brw-rw----. 1 root disk 8, 34 Jul 22 20:51 /dev/sdc2
brw-rw----. 1 root disk 8, 35 Jul 22 20:51 /dev/sdc3
brw-rw----. 1 root disk 8, 36 Jul 22 20:51 /dev/sdc4
brw-rw----. 1 root disk 8, 37 Jul 22 20:51 /dev/sdc5
brw-rw----. 1 root disk 8, 38 Jul 22 20:51 /dev/sdc6
brw-rw----. 1 root disk 8, 39 Jul 29 20:01 /dev/sdc7

备注:使用命令 partx 刷新 kernel,使系统能读到新增分区 /dev/sdc7。
  
--1.3 对分区进行加密,并设置密码

 [root@redhatB ~]# cryptsetup luksFormat /dev/sdc7

WARNING!
========
This will overwrite data on /dev/sdc7 irrevocably.

Are you sure? (Type uppercase yes): YES
Enter LUKS passphrase: 
Verify passphrase:

备注:关于 cryptsetup 命令的用法,可以 man 下,这里关键选项"luksFormat",注意大小写。

--1.4 输入密码,打开分区

 [root@redhatB ~]# cryptsetup luksOpen   /dev/sdc7   secret
Enter passphrase for /dev/sdc7: 
No key available with this passphrase.
Enter passphrase for /dev/sdc7: 
You have new mail in /var/spool/mail/root

[root@redhatB ~]# ll /dev/mapper/secret 
lrwxrwxrwx. 1 root root 7 Jul 29 20:06 /dev/mapper/secret -> ../dm-3

备注:成功打开分区后,将分区映射成 /dev/mapper/secret,这里关键选项"luksOpen",
            注意大小写。
  
  
--1.5 格式化分区

 [root@redhatB ~]# mke2fs -t ext4 /dev/mapper/secret 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65408 inodes, 261632 blocks
13081 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

--1.6 挂载

 [root@redhatB ~]# mkdir -p /mnt/secret
[root@redhatB ~]# mount -t ext4 /dev/mapper/secret /mnt/secret/
[root@redhatB ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_redhatb-lv_root
                      9.9G  3.6G  5.9G  38% /
tmpfs                 250M  264K  250M   1% /dev/shm
/dev/sda1             485M   31M  429M   7% /boot
/dev/sdb              9.9G  330M  9.1G   4% /pgdata_xc
/dev/mapper/vg1-pgdata1
                     1008M   34M  924M   4% /database/pgdata1
/dev/mapper/secret   1006M   18M  938M   2% /mnt/secret

备注:/mnt/secret 目录挂载成功。
 
  
--1.7 写入文件测试

 [root@redhatB ~]# cd /mnt/secret
[root@redhatB secret]# history > history.txt
[root@redhatB secret]# ls
history.txt  lost+found

--1.8 查看加密分区映射对应的分区。

 [root@redhatB mnt]# cryptsetup status secret
/dev/mapper/secret is active and is in use.
  type:  LUKS1
  cipher:  aes-cbc-essiv:sha256
  keysize: 256 bits
  device:  /dev/sdc7
  offset:  4096 sectors
  size:    2093056 sectors
  mode:    read/write

二 使用 cryptsetup 关闭分区
--2.1 umount

 [root@redhatB ~]# umount /mnt/secret
[root@redhatB ~]# ll /dev/mapper/secret 
lrwxrwxrwx. 1 root root 7 Jul 29 20:06 /dev/mapper/secret -> ../dm-3

--2.2 关闭分区

 [root@redhatB ~]# cryptsetup   luksClose   /dev/mapper/secret 
[root@redhatB ~]# ll /dev/mapper/secret

[root@redhatB ~]# ll /dev/mapper/secret 
ls: cannot access /dev/mapper/secret: No such file or directory

备注:这步可以理解成删除之前映射的分区 /dev/mapper/secret,这里选项关键字"luksClose",注意

大小写;关闭分区后,之前映射在文件 /dev/mapper/secret 已经不存在了。

三 总结
    
        本文演示了RHEL 对硬盘分区进行加密,mount,并且 umount 的过程,其中还可以设定密码文件
实现开机自动 mount, 这里不演示了。

RHEL6 学习:使用 cryptsetup 给分区加密的更多相关文章

  1. [cipher][archlinux][disk encryption][btrfs] 磁盘分区加密 + btrfs

    科普链接:https://wiki.archlinux.org/index.php/Disk_encryption 前面的链接关于硬盘加密,讲了几种,基本上就是选dm-crypt with LUKS ...

  2. 深入学习Oracle分区表及分区索引

    关于分区表和分区索引(About Partitioned Tables and Indexes)对于10gR2而言,基本上可以分成几类: •       Range(范围)分区 •       Has ...

  3. 【三思笔记】 全面学习Oracle分区表及分区索引

    [三思笔记]全面学习Oracle分区表及分区索引 2008-04-15 关于分区表和分区索引(About PartitionedTables and Indexes) 对于 10gR2 而言,基本上可 ...

  4. Linux学习之路—磁盘分区

    1.各硬件设备在Linux中的文件名 在Linux系统中,每个设备都被当做一个文件来对待,同时在Linux系统中,几乎所有的硬件设备文件都在/dev这个目录中. 设备 设备在Linux内的文件名 ID ...

  5. java工具类学习,系统中用户密码加密总结

    现在项目,用户注册登录部分很少有涉及到了,原因:现在热门开发框架都已经在底层帮我们做了一套用户注册,密码加密,登录认证,权限控制,缓存数据等基本功能. 这有利于项目的快速完成,只需要搬砖码畜们专注于业 ...

  6. RedHat 7.0 Linux 下划分区,分区加密,配额,逻辑卷管理

    1:如何划分区: 1:明确分区的对象:xxx :fdisk /dev/xxx 2:增加一个分区:n:选择主分区或者扩展分区,"p" or "e" :默认地方开始 ...

  7. 转:深入学习Oracle分区表及分区索引

    转自:http://database.ctocio.com.cn/tips/286/8104286.shtml 关于分区表和分区索引(About Partitioned Tables and Inde ...

  8. Cassandra1.2文档学习(4)——分区器

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...

  9. linux学习笔记之硬盘分区

    引用:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/03/2997098.html PS:本文仅对知识点作总结.详情请参考原文. 首先 ...

随机推荐

  1. ztree根据ztreeId【节点id】设置展开、选中、触发点击节点事件

    有时候我们要默认选中某个节点,根据ztreeId // 这里的ztreeId就是ztree存放在页面的元素id,比如div的id // treeObj就是ztree对象 var treeObj = $ ...

  2. CentOS7 安装mysql(YUM源方式)

    1.下载mysql源安装包 $ wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm   2.安装mysql ...

  3. cent7配置阿里yum源

    阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/CentOS系统更换软件安装源第一步:备份你的原镜像文件,以免出错后可以恢复.mv /etc/yum.repos.d ...

  4. luogu P3031 [USACO11NOV]高于中位数Above the Median (树状数组优化dp)

    链接:https://www.luogu.org/problemnew/show/P3031 题面: 题目描述 Farmer John has lined up his N (1 <= N &l ...

  5. 5个Linux性能命令

      使用几个命令就可以管理Linux系统的性能了,下面列出了5个最常用的Linux性能命令,包括top.vmstat.iostat.free和sar,它们有助于系统管理员快速解决性能问题. (1)to ...

  6. 自定义的C4P数据库连接池

    要想自己写一个数据库连接池 需要实现DataSoure这个类,然后重写getConnection 这个方法 public class C4p0 implements DataSource{ priva ...

  7. 【转载】Jave开发手册之正则表达式预编译

    今天又水一篇,java开发手册华山版 一.编程规约 (九)其它 第一条 解释:Pattern要定义为static final静态变量,以避免执行多次预编译. 错误用法: // 没有使用预编译 priv ...

  8. (十二)mybatis 查询缓存

    目录 什么是查询缓存 图解查询缓存 一级缓存 二级缓存 禁用二级缓存 刷新缓存 二级缓存应用场景 二级缓存局限性 什么是查询缓存 mybatis 在查询数据的时候,会将数据存储起来,下次再次查询相同的 ...

  9. Vue路由传参及传参后刷新导致参数消失处理

    项目功能需要,要从列表页跳转到第三方提供的URL上(这里第三方页面我是通过iframe引入在详情页,目的是点击返回时可以通过keepAlive让列表页不刷新,如果不通过iframe直接跳第三方链接,那 ...

  10. linux安装字体库(simSun为例)

    在开发过程中,发现报表打印,字体和默认字体不一样.本地开发环境(windows)没问题,但是远程linux一直出现这样的问题.经过排查发现linux没有安装中文字体库宋体. linux查看当前的字体库 ...