In Linux, the disk encryption LUKS (LinuxUnifiedKeySetup) provides a standard for Linux hard disk encryption. It can be used not only for different Linux releases, but also for multiple users/passwords. Because its encryption key is independent of the password, if the password is lost, we can quickly change the password without re-encrypting the hard disk. By providing a standard disk format, it not only facilitates the compatibility between distributions, but also provides security management for multiple user passwords. The encrypted volume must be decrypted before it can be mounted.
Disk encryption in Linux
LUKS (Linux Unified Key Setup) provides a standard for Linux hard disk encryption. It not only applies to different Linux Release versions, but also supports multiple users/passwords. Because its encryption key is independent of the password, if the password is lost, we can quickly change the password without re-encrypting the hard disk. By providing a standard disk format, it not only facilitates the compatibility between distributions, but also provides security management for multiple user passwords. You must decrypt the encrypted volume before mounting the file system.
Tool: cryptsetup (installed by default)
Common parameters: luksFormat, luksOpen, luksClose, and luksAddKey
After the partition is encrypted using cryptsetup, the partition is no longer allowed to be directly mounted. LUKS is also an encryption scheme based on device mapper mechanism. To use this partition, you must map it to the/dev/mapper directory. We can only mount this ing for use. However, during the ing, you need to enter the decryption password.
Encryption features of the Crypsetup tool:
? Cannot be directly mounted after Encryption
? After encryption, you do not need to worry about data theft if the hard disk is lost.
? After encryption, you must map to mount
Steps:
(1) Prepare the environment:
1. Check whether the cryptsetup software is installed:
[Root @ localhost ~] # Rpm-qa | grep cryptsetup
Cryptsetup-luks-libs-1.2.0-7.el6.i686
Cryptsetup-luks-1.2.0-7.el6.i686
2. Create a disk partition:
[Root @ localhost ~] # Fdisk-cu/dev/sdb
Command (m for help): n # create a partition
Command action
E extended
P primary partition (1-4)
P # select primary Partition
Partition number (1-4): 1 # Partition number is 1
First sector (2048-41943039, default 2048): # Enter here
Using default value 2048
Last sector, + sectors or + size {K, M, G} (2048-41943039, default 41943039): + 5G # The given size is 5G
Command (m for help): w # Keep changes to the hard disk and exit
The partition table has been altered!
Calling ioctl () to re-read partition table.
Syncing disks.
[Root @ localhost ~] # Partx-a/dev/sdb # Let the system re-read the Partition Table of Disk/dev/sdb
(2) encrypted partition:
Steps:
1. Use the created partition to format it as an encrypted partition:
Cryptsetup luksFormat/dev/sdb1
WARNING!
==========
This will overwrite data on/dev/sdb1 irrevocably.
Are you sure? (Type uppercase yes): YES # note that the value must be in uppercase. YES
Enter LUKS passphrase: # Enter the password
Verify passphrase: # Enter again
Command successful.
2. ing partitions:
[Root @ localhost ~] # Cryptsetup luksOpen/dev/sdb1 fage_crypt # map sdb1 to fage_crypt
Enter passphrase for/dev/sdb1: # Enter the encryption password here
[Root @ localhost ~] # Ls/dev/mapper/fage_crypt # view the mapped Disk
/Dev/mapper/fage_crypt
[Root @ localhost ~] # Cryptsetup status/dev/mapper/fage_crypt # view the status of the encrypted Partition
/Dev/mapper/fage_crypt is active.
Type: LUKS1
Cipher: aes-cbc-essiv: sha256
Keysize: 256 bits
Device:/dev/sdb1
Offset: 4096 sectors
Size: 10481664 sectors
Mode: read/write
3. Format and mount the partition:
[Root @ localhost ~] # Mkdir/fage_data # create a directory
[Root @ localhost ~] # Mkfs. ext4/dev/mapper/fage_crypt # File System in the format of ext4
[Root @ localhost ~] # Mount/dev/mapper/fage_crypt/fage_data/# mount the encrypted partition to the/fage_data directory
[Root @ localhost ~] # Cd/fage_data/
[Root @ localhost fage_data] # touch fage. file
[Root @ localhost fage_data] # ls
Fage. file lost + found
4. Disable the ing partition:
[Root @ localhost ~] # Umount/fage_data/# detach a partition
[Root @ localhost ~] # Ls/dev/mapper/fage_crypt
/Dev/mapper/fage_crypt
[Root @ localhost ~] # Cryptsetup luksClose fage_crypt # disable encrypted partitions
[Root @ localhost ~] # Ls/dev/mapper/# No fage_crypt device found again
Control VolGroup-lv_root VolGroup-lv_swap
5. Set automatic mounting upon startup
Generate a key file. If you want to manually enter a password when starting the instance, do not generate a key file.
# Touch/root/. fage_crypt # create an encrypted file. For security reasons, this file is set to a hidden file.
[Root @ localhost ~] # Cryptsetup luksAddKey/dev/sdb1/root/. fage_crypt # Add a key file for the/dev/sdb1 partition. fage_crypt
Enter any passphrase: # Enter the encryption password for the partition, which must be consistent with the password at the time of creation.
[Root @ localhost ~] # Cat/root/. fage_crypt
[Root @ localhost ~] # File/root/. fage_crypt # Verification
/Root/. fage_crypt: empty
Set startup
[Root @ localhost ~] # Vim/etc/crypttab # Add the following lines to the "encrypted Block devices" table "crypttab"
Fage_crypt/dev/sdb1/root/. fage_crypt
##### Fage_crypt is the ing name,/dev/sdb1 is the encrypted device,/root /. fage_crypt is the password file. If you want to manually enter the password when starting the instance, leave the password file empty.
[Root @ localhost ~] # Vim/etc/fstab # edit add encryption sub-boot startup
/Dev/mapper/fage_crypt ext4 defaults 0 0
Now the encrypted partition is complete !!