linux分區加密及掛載


linux分區與加密



redhat企業6中有強大的圖形分區工具,這裏我分享下我所瞭解的通過命令行對磁盤進行分區


1、分區

複製代碼

[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

這裏我用虛擬機作演示

我們先按m獲取幫助

複製代碼

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 新建一個分區

d 刪除一個分區

t 改變分區類型 (進去後按l可以查看所有類型)

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  

接下來我們就可以對我們的分區進行加密了

2、分區加密


這是對新分區進行加密,並設置密碼

這裏要注意的地方是大小寫

複製代碼

[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/crypttab
name /dev/vda* /root/name
# echo 密碼 >/root/name
# chown root /roo/name
# chmod 600 /root/name
# cryptsetup luksAddKey /dev/vda* /root/name
要求輸入密碼
完成


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章