linux分区加密及挂载


linux分区与加密



redhat企业6中有强大的图形分区工具,这里我分享下我所了解的经过命令行对磁盘进行分区html


一、分区linux

复制代码

[root@burvis ~]# fdisk /dev/sda

WARNING: DOS
-compatible mode is deprecated. It's strongly recommended to
       
switch off the mode (command 'c') and change display units to
        sectors (command 'u').

Command (m
for help):

复制代码

这里若是你用虚拟机的话能够用 # fdisk -cu /dev/vda安全

这里我用虚拟机做演示app

咱们先按m获取帮助ide

复制代码

Command action
  a   toggle a bootable flag
  b   edit bsd disklabel
  c   toggle the dos compatibility flag
  d   delete a partition
  l   list known partition types
  m   print this menu
  n   add a new partition
  o   create a new empty DOS partition table
  p   print the partition table
  q   quit without saving changes
  s   create a new empty Sun disklabel
  t   change a partition's system id
  u   change display
/entry units
  v   verify the partition table
  w   write table to disk and exit
  x   extra functionality (experts only)

复制代码

这里主要用到的有工具

n 新建一个分区post

d 删除一个分区ui

t 改变分区类型 (进去后按l能够查看全部类型)this

p 查看分区情况加密

w 写入并退出

q 退出不写入

m 获取帮助

接下来咱们p查看分区信息

复制代码

Command (m for help): p

Disk
/dev/vda: 6442 MB, 6442450944 bytes
16 heads, 63 sectors/track, 12483 cylinders, total 12582912 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:
0x0008f7fd

  Device Boot      Start        
End      Blocks   Id  System
/dev/vda1   *        2048      526335      262144   83  Linux
/dev/vda2          526336     9914367     4694016   8e  Linux LVM

复制代码

按n新建一个分区

Command (m for help): n
Command action
  e   extended
  p   primary partition (
1-4)

这里按e为扩展分区,p为主分区

咱们建一个扩展分区

输入e

选择第三个分区

选择起始点,能够直接回车选择默认

选择终止点,这里能够像我同样用+1G,表示给1G的空间,或者能够直接输入终止点的位置

e
Partition number (
1-4): 3
First sector (
9914368-12582911, default 9914368):
Using
default value 9914368
Last sector,
+sectors or +size{K,M,G} (9914368-12582911, default 12582911): +1G

咱们再输入p看一下分区信息

   Device Boot      Start      End      Blocks   Id  System
/dev/vda1   *        2048      526335      262144   83  Linux
/dev/vda2          526336     9914367     4694016   8e  Linux LVM
/dev/vda3         9914368    12011519     1048576    5  Extended

而后咱们再到扩展分区中去新建分区

按n

复制代码

Command (m for help): n
Command action
  l   logical (5 or over)
  p   primary partition (1-4)
l
First sector (9916416-12011519, default 9916416):
Using default value 9916416
Last sector, +sectors or +size{K,M,G} (9916416-12011519, default 12011519): +200M

复制代码

这里选择l,咱们给他200M的空间

输入p再看下分区信息。

复制代码

Command (m for help): p

Disk
/dev/vda: 6442 MB, 6442450944 bytes
16 heads, 63 sectors/track, 12483 cylinders, total 12582912 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:
0x0008f7fd

  Device Boot      Start        
End      Blocks   Id  System
/dev/vda1   *        2048      526335      262144   83  Linux
/dev/vda2          526336     9914367     4694016   8e  Linux LVM
/dev/vda3         9914368    12011519     1048576    5  Extended
/dev/vda5         9916416    10326015      204800   83  Linux

复制代码

ok,咱们已经分出了一个200M的分区,按w写入并退出

这个时候咱们能够ll一下/dev/vda*

发现以前分出的vda3与vda5没有显示

这里咱们用如下partx刷新添加

[root@burvis ~]# partx -a /dev/vda
BLKPG: Device or resource busy
error adding partition
1
BLKPG: Device or resource busy
error adding partition
2

咱们在ll一下能够看到已经出来了

[root@server27 ~]# ll /dev/vda
vda   vda1  vda2  vda3  vda5  

接下来咱们就能够对咱们的分区进行加密了

二、分区加密


这是对新分区进行加密,并设置密码

这里要注意的地方是大小写

复制代码

[root@burvis ~]# cryptsetup luksFormat /dev/vda5    

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

Are you sure
? (Type uppercase yes): YES     //YES必定要大写
Enter LUKS passphrase:                     //密码
Verify passphrase:                         //确认密码

复制代码

这个时候咱们的分区已经被加密,咱们要使用必须先解密


这里经过luksOpen来进行解密 后面‘burvis’能够写随意的名字,而后会将加密卷/dev/vda5解锁为/dev/mapper/burvis,以后,咱们用的都是/dev/mapperburvis了,而不是/dev/vda5

[root@burvis ~]# cryptsetup luksOpen /dev/vda5 burvis
Enter passphrase for /dev/vda5:                 //输入密码

而后对解密的卷进行格式化

[root@server27 ~]# mkfs.ext4 /dev/mapper/burvis

接下来就是把分区挂载到咱们的系统上

咱们能够建一个挂载的目录,而后把分区挂上去

[root@burvis ~]# mkdir /burvis
[root@burvis ~]# mount /dev/mapper/burvis /burvis

如今咱们能够df一下

复制代码

[root@server27 ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vgsrv-root
                     
3418812   2250692    994452  70% /
tmpfs                  
510760       260    510500   1% /dev/shm
/dev/vda1               253871     30358    210406  13% /boot
/dev/mapper/vgsrv-home
                       
253871     10268    230496   5% /home
/dev/mapper/burvis      196339      5646    180556   4% /burvis

复制代码

当咱们用完咱们的加密分区后,就能够卸载它而后再锁住,这样数据就又会保护起来

咱们要先卸载咱们挂上去的分区,而后再锁住,若是不能锁住,能够尝试下把挂载目录删了

[root@burvis ~]# umount /dev/mapper/burvis 
[root@burvis ~]# cryptsetup luksClose burvis

若是下次先再用,就重复luksOpen就能够了

鉴于开机自动挂载加密分区不安全,没有什么意义,这里就很少加介绍,只附上个人笔记

永久挂载
修改fstab
/dev/mapper/name /***  **** **** **


编辑/etc/crypttab
vi /etc/crypttab
name /dev/vda*      这样子开机会要求输入密码

若是不想输入密码先编辑一个密码放置的文件 好比/root/name#vi /etc/crypttabname /dev/vda* /root/name# echo 密码 >/root/name# chown root /roo/name# chmod 600 /root/name# cryptsetup luksAddKey /dev/vda* /root/name要求输入密码完成

相关文章
相关标签/搜索