Encrypt Hard Drive for Secure Storage in Linux Ubuntu 14.04

I need to be able to back up my data to an external hard drive that will be stored off site to protect my data if my house burns down or the contents are stolen. I also want the data to be private, so that means an encrypted hard drive. I am using Ubuntu 14.04. This machine is on a headless server so all commands are entered from the terminal over ssh. I have incorporated all of these commands into a python backup script for ease of use. Terminal code will look like this sentence. If you have monitor hooked up to your computer, gui tools are available.

  • References
  • Install cryptsetup
    • sudo apt-get install cryptsetup
  • Identify the correct hard drive to use. You will be erasing all data on the drive.
  • Fill the hard drive with random data. I saw arguments were this step is not required, however I felt it safer to do this and I am not in a rush. This step takes a long time.
    • run command as root
      • sudo -s
      • This method is fast if CPU supports AES-NI (hardware acceleration). see http://serverfault.com/questions/6440/is-there-an-alternative-to-dev-urandom
        • openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero | pv -pterb > /dev/sdj
      • This was another command posted but was not as fast
        • dd if=/dev/urandom of=/dev/sdj
      • I then played a youtube playlist in the background to help generate random data
  • Encrypt the hard drive
  • Verify the encryption is using luks
    • sudo cryptsetup -v isLuks /dev/disk/by-uuid/2228745a-0db3-48c7-b582-5a3ddf7e7c70
      • Output should be “Command successful.” if LUKS device
  • Open the encrypted device (decrypt/unlock the device)
    • Then first time the encrypted device is opened, a symbolic link called a “mapping” is created which becomes the name of the LUKS device.
      • For example I chose a descriptive name “backup.serv-offsite” and the LUKS device will be created at /dev/mapper/backup.serv-offsite. “/dev/mapper/backup.serv-offsite” will only be created when the LUKS device is opened.
    • sudo cryptsetup -v luksOpen /dev/disk/by-uuid/2228745a-0db3-48c7-b582-5a3ddf7e7c70 backup.serv-offsite
  • See if the LUKS device is already unlocked/open.
    • sudo cryptsetup status backup.serv-offsite
    • output if device is opened
      • /dev/mapper/backup.serv-offsite is active and is in use.
      • type: LUKS1
      • cipher: aes-xts-plain64
      • keysize: 512 bits
      • device: /dev/sde
      • offset: 4096 sectors
      • size: 3907025072 sectors
      • mode: read/write
    • output if device is closed
      • /dev/mapper/backup.serv-offsite is inactive.
  • Create a filesystem once device is opened
    • mkfs.ext4 /dev/mapper/backup.serv-offsite
  • Mount the filesystem
    • sudo mount --verbose -t ext4 /dev/mapper/media.serv-backup /media/bkup.mserv
      • /dev/mapper/media.serv-backup is the opened LUKS device
      • /media/bkup.mserv is the location mount point of the LUKS device
  • Umount the filesystem
    • sudo umount /media/bkup.mserv
  • Close the LUKS device so that the data stays private
    • sudo cryptsetup -v close media.serv-backup
  • Verify the LUKS device was closed, data is not available
    • sudo cryptsetup status backup.serv-offsite
  • Done

Leave a Reply

Your email address will not be published. Required fields are marked *