Como encryptar nuestro disco duro externo con luks

(Ubuntu 16.04, luks, ext4, ssd)

 

Lo primero es eliminar la partición que traía el disco duro:

sergio@sergio-pc:~$ sudo -i
[sudo] password for sergio: 

root@sergio-pc:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Orden (m para obtener ayuda): p
Disk /dev/sdb: 477 GiB, 512110190592 bytes, 1000215216 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
Disklabel type: dos
Disk identifier: 0x00000000

Disposit.  Inicio Start      Final   Sectores  Size Id Tipo
/dev/sdb1  *       2048 1000214527 1000212480  477G  7 HPFS/NTFS/exFAT

Orden (m para obtener ayuda): d
Selected partition 1
La partición 1 ha sido eliminada.

 

Sin salir del fdisk, creamos una partición con el tamaño completo por defecto:

Orden (m para obtener ayuda): p
Disk /dev/sdb: 477 GiB, 512110190592 bytes, 1000215216 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
Disklabel type: dos
Disk identifier: 0x00000000

Orden (m para obtener ayuda): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Número de partición (1-4, default 1): 
First sector (2048-1000215215, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-1000215215, default 1000215215): 

Created a new partition 1 of type 'Linux' and of size 477 GiB.

Orden (m para obtener ayuda): p
Disk /dev/sdb: 477 GiB, 512110190592 bytes, 1000215216 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
Disklabel type: dos
Disk identifier: 0x00000000

Disposit.  Inicio Start      Final   Sectores  Size Id Tipo
/dev/sdb1          2048 1000215215 1000213168  477G 83 Linux

Orden (m para obtener ayuda): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

 

Por curiosidad, lanzamos una prueba de rendimiento de la partición antes de encryptar (luego haremos otra prueba tras la encriptación):

root@sergio-pc:~# hdparm -Tt /dev/sdb1

/dev/sdb1:
 Timing cached reads:   14308 MB in  2.00 seconds = 7159.02 MB/sec
 Timing buffered disk reads: 1244 MB in  3.00 seconds = 414.36 MB/sec

 

Encriptamos nuestra nueva partición:

root@sergio-pc:~# cryptsetup luksFormat -c aes-xts-plain64:sha512 -h sha512 /dev/sdb1

WARNING!
========
Sobrescribirá los datos en /dev/sdb1 de forma irrevocable.

Are you sure? (Type uppercase yes): YES
Introduzca frase contraseña: 
Verificar frase de paso: 

 

Abrimos el nuevo disposito creado para poder utilizarlo:

root@sergio-pc:~# cryptsetup luksOpen /dev/sdb1 backup-500ssd
Introduzca una contraseña para /dev/sdb1: 

 

Comprobamos el UUID del dispositivo para añadirlo al crypttab y que arranque automáticamente cuando inicie la máquina:

root@sergio-pc:~# blkid
[...]
/dev/sdb1: UUID="22984991-9ef2-4bd9-9c3d-1bfe03e91021" ENGINE="crypto_LUKS"

root@sergio-pc:~# vi /etc/crypttab 

root@sergio-pc:~# cat /etc/crypttab
[...]
backup-500ssd   UUID=22984991-9ef2-4bd9-9c3d-1bfe03e91021       none    luks

 

Creamos los volúmenes para utilizar posteriormente:

root@sergio-pc:~# pvcreate /dev/mapper/backup-500ssd 
  Physical volume "/dev/mapper/backup-500ssd" successfully created

root@sergio-pc:~# vgcreate vgbackup-500ssd /dev/mapper/backup-500ssd
  Volume group "vgbackup-500ssd" successfully created

root@sergio-pc:~# lvcreate -C n -l 100%FREE -n lvbackup-500ssd vgbackup-500ssd /dev/mapper/backup-500ssd
  Logical volume "lvbackup-500ssd" created.

root@sergio-pc:~# pvs
  PV                        VG              Fmt  Attr PSize   PFree
  /dev/mapper/backup-500ssd vgbackup-500ssd lvm2 a--  476,93g    0 
 [...]

root@sergio-pc:~# vgs
  VG              #PV #LV #SN Attr   VSize   VFree
  vgbackup-500ssd   1   1   0 wz--n- 476,93g    0 
 [...]

root@sergio-pc:~# lvs
  LV              VG              Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvbackup-500ssd vgbackup-500ssd -wi-a----- 476,93g                                                    
  [...]                                                  

root@sergio-pc:~# lvscan
  ACTIVE            '/dev/vgbackup-500ssd/lvbackup-500ssd' [476,93 GiB] inherit
 [...]

root@sergio-pc:~# mkfs.ext4 /dev/mapper/vgbackup--500ssd-lvbackup--500ssd 
mke2fs 1.42.13 (17-May-2015)
Se está creando un sistema de ficheros con 125025280 bloques de 4k y 31260672 nodos-i
UUID del sistema de ficheros: c0c0b651-5367-437c-bde2-09033a54d2d5
Respaldo del superbloque guardado en los bloques: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000

Reservando las tablas de grupo: hecho                           
Escribiendo las tablas de nodos-i: hecho                           
Creando el fichero de transacciones (32768 bloques): hecho
Escribiendo superbloques y la información contable del sistema de ficheros: hecho    

 

Hacemos la prueba de rendimiento tras la encriptación y vemos que hemos perdido un poco:

root@sergio-pc:~# hdparm -Tt /dev/mapper/vgbackup--500ssd-lvbackup--500ssd 

/dev/mapper/vgbackup--500ssd-lvbackup--500ssd:
 Timing cached reads:   12250 MB in  2.00 seconds = 6128.36 MB/sec
 Timing buffered disk reads: 956 MB in  3.00 seconds = 318.35 MB/sec

 

Si tenemos interés en ver otros rendimientos con otros tipos de encriptaciones, podemos ejecutar:

root@ubuntu:~# cryptsetup benchmark
# Tests are approximate using memory only (no storage IO).
PBKDF2-sha1      1087734 iterations per second
PBKDF2-sha256     728177 iterations per second
PBKDF2-sha512     601247 iterations per second
PBKDF2-ripemd160  675628 iterations per second
PBKDF2-whirlpool  248242 iterations per second
#  Algorithm | Key |  Encryption |  Decryption
     aes-cbc   128b   606.6 MiB/s  2632.3 MiB/s
 serpent-cbc   128b    80.7 MiB/s   519.2 MiB/s
 twofish-cbc   128b   181.4 MiB/s   305.1 MiB/s
     aes-cbc   256b   452.6 MiB/s  2019.5 MiB/s
 serpent-cbc   256b    83.2 MiB/s   525.4 MiB/s
 twofish-cbc   256b   184.4 MiB/s   336.0 MiB/s
     aes-xts   256b  2230.1 MiB/s  2242.8 MiB/s
 serpent-xts   256b   522.8 MiB/s   502.2 MiB/s
 twofish-xts   256b   325.8 MiB/s   332.0 MiB/s
     aes-xts   512b  1723.5 MiB/s  1680.5 MiB/s
 serpent-xts   512b   523.5 MiB/s   506.4 MiB/s
 twofish-xts   512b   326.9 MiB/s   331.7 MiB/s

 

Comprobamos que ya tenemos montada la partición:

root@sergio-pc:~# mount
[...]
/dev/mapper/vgbackup--500ssd-lvbackup--500ssd on /media/sergio/c0c0b651-5367-437c-bde2-09033a54d2d5 type ext4 (rw,nosuid,nodev,relatime,data=ordered,uhelper=udisks2)

 

Le damos permiso a nuestro usuario de sistema operativo:

root@sergio-pc:~# cd /media/sergio/

root@sergio-pc:/media/sergio# ls -lrt
total 32
drwxr-xr-x  3 root   root    4096 dic 18 12:54 c0c0b651-5367-437c-bde2-09033a54d2d5
drwxrwx--- 13 sergio sergio  4096 dic 18 15:22 2TB
drwxrwxrwx  1 sergio sergio 24576 dic 18 15:42 CELVIN Drive

root@sergio-pc:/media/sergio# chown -R sergio:sergio c0c0b651-5367-437c-bde2-09033a54d2d5/

root@sergio-pc:/media/sergio# ls -l
total 32
drwxrwx--- 13 sergio sergio 4096 dic 18 15:22 2TB
drwxr-xr-x 3 sergio sergio 4096 dic 18 12:54 c0c0b651-5367-437c-bde2-09033a54d2d5
drwxrwxrwx 1 sergio sergio 24576 dic 18 15:42 CELVIN Drive

 

Comprobamos que aparece un nombre muy largo y poco descriptivo, lo cambiamos:

root@sergio-pc:/media/sergio# e2label /dev/mapper/vgbackup--500ssd-lvbackup--500ssd backup-500ssd

 

Desmontamos el volumen y lo volvemos a montar:

root@sergio-pc:~# df -h
S.ficheros                                    Tamaño Usados  Disp Uso% Montado en
[...]
/dev/mapper/vgbackup--500ssd-lvbackup--500ssd   470G    82G  364G  19% /media/sergio/backup-500ssd

root@sergio-pc:~# cd /media/sergio/
root@sergio-pc:/media/sergio# ls
2TB  backup-500ssd

 

Ya lo tenemos listo para usar 🙂

 

Añadiendo valor con Arumel!!