Linux下磁盘加密luks使用

使用luks加密磁盘,磁盘须要映射才能够挂载,映射的时候须要输入密码验证。luks相对因而对硬盘上了一把锁,要想打开就得有相应的钥匙,若是打开了就能够随意操做了,和普通硬盘同样。linux

使用方法以下:vim

0、分区   建立一个磁盘分区/dev/sdb1,不进行格式化  安全

[root@localhost]#fdisk /dev/sdb1  

显示当前分区信息   Command (m for help):p  

删除分区   Command (m for help):d  

开始分区   Command (m for help):n  

  Command action     e   extended     p   primary partition (1-4)   p  

  Partition number (1-4): 1  

  First sector (1-104865, default 1): 回车  

  Last sector, +sectors +size{M,K,G} (1-104865, default 104865): 回车  

  保存退出   wq  

 一、加密分区  bash

[root@localhost]# cryptsetup -v -y -c aes-cbc-plain luksFormat /dev/sdb1    

  WARNING!    

  ========    

  This will overwrite data on /dev/sdb1 irrevocably.    

  Are you sure? (Type uppercase yes): YES  --> 注意:这里必须是大写的YES  

  Enter LUKS passphrase:  

  Verify passphrase:   Command successful.

 二、映射分区  app

[root@localhost]# cryptsetup luksOpen /dev/sdb1 sx_disk  //把sdb1映射为sx_disk   

  Enter passphrase for /dev/sdb1:  

[root@localhost]# ll -d /dev/mapper/sx_disk   

  lrwxrwxrwx. 1 root root 7  6月 25 03:24 /dev/mapper/sx_disk -> ../dm-0   //查看

[root@localhost]# cryptsetup status /dev/mapper/sx_disk //查看映射分区状态  

  /dev/mapper/

  /dev/mapper/sx_disk is active.   

  type:  LUKS1   cipher:  aes-cbc-plain  

  keysize: 256 bits   device:  /dev/sdb1   

  offset:  4096 sectors   

  size:    16767701 sectors   

  mode:    read/write

 三、挂载使用  加密

[root@localhost]# mkdir /mnt/sx_disk  

[root@localhost]# mkfs.ext3 /dev/mapper/sx_disk  

[root@localhost]# mount /dev/sdb1 /mnt/sx_disk/   //直接挂载是不能够的   

  mount: unknown filesystem type 'crypto_LUKS'  

[root@localhost]# mount /dev/mapper/sx_disk /mnt/sx_disk/ //挂载映射设备,挂载成功

 四、设置开机自动挂载   生成密钥文件,www.linuxidc.com 若是想开机时手动输入密码能够不生成  orm

[root@localhost]# touch /root/cryptpasswd  

[root@localhost]# cryptsetup luksAddKey /dev/sdb1 /root/cryptpasswd    

  Enter any passphrase:  

[root@localhost]# cat /root/cryptpasswd  //直接查看密钥为空

 五、设置开机自动解密  blog

[root@localhost]# vim /etc/crypttab  

[root@localhost]# cat /etc/crypttab  

  sx_disk /dev/sdb1 /root/cryptpasswd   //sx_disk为映射名称,/dev/sdb1是加密设备设备,/root/cryptpasswd为密码文件,若是想开机手动输入密码,密码文件处空着便可  

[root@localhost]# vim /etc/fstab  

[root@localhost]# tail -1 /etc/fstab  

   /dev/mapper/sx_disk       /mnt/sx_disk             ext4 defaults   0 0

 六、程序设置开机启动,在/etc/rc.local里面添加下面内容  ip

[root@localhost]vim /etc/rc.local   /bin/bash "程序路径"  

 七、关闭映射,先卸载后关闭,注意:卸载后开机不会本身加载密码解锁的,这项在项目中不使用。   须要先退出目录  md5

[root@localhost]# cd /root  

[root@localhost]# umount /mnt/sx_disk/  

[root@localhost]# cryptsetup luksClose sx_disk  //关闭映射  

[root@localhost]# ll /dev/mapper/  //映射设备已经不见了

  总用量 0   crw-rw----. 1 root root 10, 58  6月 25 03:01 control

 八、禁止进入单用户模式   给grub加个密码,增禁止他人以单用户模式进入系统. 有2个方式:   vim /boot/grub/grub.conf  或者  vim /etc/grub.conf(/etc/grub.conf是/boot/grub/grub.conf的符号连接),

一、明文方式:在splashimage这个参数下一行添加: password=密码。

   保存后从新启动计算机,再次登陆到GRUB菜单页面的时候就会发现,这时已经不能直接使用e命令编辑启动标签了,须先使用p命令,输入正确的密码后才可以对启动标签进行编辑.    

   咱们设置了明文密码也不是很安全的,若是他人获得了明文密码后仍然能够修改GRUB启动标签从而修改root密码。

二、MD5加密方式:在终端中输入grub-md5-crypt回车,这时系统会要求输入两次相同的密码,以后系统便会输出MD5码。  

   你们只须要将生成的MD5密文复制下来,在splashimage这个参数下一行添加: password --md5 MD5密文好比:              

   #grub-md5-crypt     

         splashimage=(hd0,0)/grub/splash.xpm.gz                 

   password --md5 $1$xI6vS$Wi5pi8JyORUNnj3/0Yq2/0                 

         hiddenmenu   

        保存后从新启动计算机,再次登陆到GRUB菜单页面的时候就会发现,这时已经不能直接使用e命令编辑启动标签了,须先使用p命令,输入正确的密码后才可以对启动标签进行编辑。
相关文章
相关标签/搜索