How to Use VeraCrypt on Command Line to Encrypt Drives on Ubuntu

4
14132

In this tutorial, you will learn how to use VeraCrypt on Command Line to Encrypt Drives on Ubuntu. In our previous article, we discussed how to install and use VeraCrypt to encrypt drives on Ubuntu.

We used the VeraCrypt GUI method. In this article, we are going to learn how to use VeraCrypt on command line interface to achieve the same encryption.

How to Use VeraCrypt on Command Line to Encrypt Drives on Ubuntu

To run VeraCrypt in text user interface, just type veracrypt and pass the -t/–text option.

The command line synopsis of the veracrypt command is;

veracrypt [OPTIONS] COMMAND
veracrypt [OPTIONS] VOLUME_PATH [MOUNT_DIRECTORY]

To learn more about VeraCrypt command line options, run veracrypt -h.

With VeraCrypt, you can create two types of volumes;

While creating each volume, you can choose to use an unused partition or just create a file-based volume to store your data.

Create VeraCrypt Outer Volume on Command Line

Launch veracrypt command from the terminal and pass option -c/–create and of course the -t/–text option. Most of the options will be requested if you don’t specify them on the command line and thus the setup becomes interactive.

sudo veracrypt -t -c

When you run the command, you are prompted to choose the type of volume. In this case, we are going to setup a Normal volume.

Volume type:
 1) Normal
 2) Hidden
Select [1]: 1

Next, enter the volume path. For example, /dev/sdb1.

CAUTION: If you are encrypting a partition, ensure that there is no data in it or the data has been backed up and backup confirmed. The process will wipe the partition clean and you will all data, if any was stored in it.

Note that I have unused partition, /dev/sdb1.

lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0    20G  0 disk 
└─sda1   8:1    0    20G  0 part /
sdb      8:16   0     4G  0 disk 
└─sdb1   8:17   0     4G  0 part 
sr0     11:0    1  1024M  0 rom  
sr1     11:1    1  1024M  0 rom

Thus, if you are using a partition, enter the device;

Enter volume path: /dev/sdb1

Define the Encryption as well as the hashing algorithms;

We choose the AES and SHA 512 as encryption and hashing algorithms respectively. You can also just press enter to accept the defaults.

Encryption Algorithm:
 1) AES
 2) Serpent
 3) Twofish
 4) Camellia
 5) Kuznyechik
 6) AES(Twofish)
 7) AES(Twofish(Serpent))
 8) Camellia(Kuznyechik)
 9) Camellia(Serpent)
 10) Kuznyechik(AES)
 11) Kuznyechik(Serpent(Camellia))
 12) Kuznyechik(Twofish)
 13) Serpent(AES)
 14) Serpent(Twofish(AES))
 15) Twofish(Serpent)
Select [1]: 1

Hash algorithm:
 1) SHA-512
 2) Whirlpool
 3) SHA-256
 4) Streebog
Select [1]: 1

Define the filesystem to format the volume with.

Filesystem:
 1) None
 2) FAT
 3) Linux Ext2
 4) Linux Ext3
 5) Linux Ext4
 6) NTFS
 7) exFAT
Select [2]: 5

In this case, we are going with Linux Ext4.

Next

  • you need to set the password for encrypting outer volume.
  • For PIM and keyfile path, press enter to set empty values.
  • Next, type random numbers that can be use to improve the cryptographic strength of the encryption key. In this case, you are required to enter at least 320 random numbers.
Enter password: Enter STRONGPASSWORD
Re-enter password: Re-Enter STRONGPASSWORD

Enter PIM: Enter

Enter keyfile path [none]: Enter

Please type at least 320 randomly chosen characters and then press Enter:
<PASTE  OR TYPE 320 CHARACTERS HERE >

Done: 100.000%  Speed:   54 MB/s  Left: 0 s          

The VeraCrypt volume has been successfully created.

To create the outer volume in non-interactive mode, run the command below;

sudo veracrypt -t -c --volume-type=normal /dev/sdb1 \
--encryption=aes --hash=sha-512 --filesystem=ext4 \
-p [email protected] --pim=0 -k "" --random-source=/dev/urandom

Create Hidden Volume within the Outer volume

Once the outer volume is created, you need to create the hidden volume within it. So repeat the above procedure but this time round;

sudo veracrypt -t -c
  • Define the volume type as hidden.
  • Enter volume path: /dev/sdb1, in our example.
  • Define the size of the hidden volume.
  • Set the encryption and hash algorithms
  • Define filesystem type.
  • Enter encryption password. Press enter for PIM and key file path.
  • Generate and enter 320 random characters.
  • If all is well, then your hidden volume should be successfully created.
Volume type:
 1) Normal
 2) Hidden
Select [1]: 2

IMPORTANT: Inexperienced users should use the graphical user interface to create a hidden volume. When using the text interface, the procedure described in the command line help must be followed to create a hidden volume.

Enter volume path: /dev/sdb1

Enter hidden volume size (sizeK/size[M]/sizeG/sizeT/max): 1G

Encryption Algorithm:
 1) AES
 2) Serpent
 3) Twofish
 4) Camellia
 5) Kuznyechik
 6) AES(Twofish)
 7) AES(Twofish(Serpent))
 8) Camellia(Kuznyechik)
 9) Camellia(Serpent)
 10) Kuznyechik(AES)
 11) Kuznyechik(Serpent(Camellia))
 12) Kuznyechik(Twofish)
 13) Serpent(AES)
 14) Serpent(Twofish(AES))
 15) Twofish(Serpent)
Select [1]: 1

Hash algorithm:
 1) SHA-512
 2) Whirlpool
 3) SHA-256
 4) Streebog
Select [1]: 1

Filesystem:
 1) None
 2) FAT
 3) Linux Ext2
 4) Linux Ext3
 5) Linux Ext4
 6) NTFS
 7) exFAT
 8) Btrfs
Select [2]: 5

Enter password: STRONGPASSWORDFORHIDDENVOLUME
Re-enter password: STRONGPASSWORDFORHIDDENVOLUME

Enter PIM: Enter 

Enter keyfile path [none]: Enter

Please type at least 320 randomly chosen characters and then press Enter:
<PASTE YOUR 320 CHARACTERS HERE >

Done: 100.000%  Speed:  256 MB/s  Left: 0 s         

The hidden VeraCrypt volume has been successfully created and is ready for use. If all the instructions have been followed and if the precautions and requirements listed in the section "Security Requirements and Precautions Pertaining to Hidden Volumes" in the VeraCrypt User's Guide are followed, it should be impossible to prove that the hidden volume exists, even when the outer volume is mounted.

WARNING: IF YOU DO NOT PROTECT THE HIDDEN VOLUME (FOR INFORMATION ON HOW TO DO SO, REFER TO THE SECTION "PROTECTION OF HIDDEN VOLUMES AGAINST DAMAGE" IN THE VERACRYPT USER'S GUIDE), DO NOT WRITE TO THE OUTER VOLUME. OTHERWISE, YOU MAY OVERWRITE AND DAMAGE THE HIDDEN VOLUME!

To create a hidden volume in non-interactive mode, run the command below;

sudo veracrypt -t -c --volume-type=hidden /dev/sdb2 \
--size=500M --encryption=aes --hash=sha-512 \
--filesystem=ext4 -p [email protected] --pim=0 \
-k "" --random-source=/dev/urandom

Mount the VeraCrypt Outer and Inner Volumes

Once you are done creating the volumes, you can now mount them and write your files or data to them as you wish.

To mount the outer volume, run the command below. When prompted for a password, use the password set for the outer volume above.

sudo veracrypt /dev/sdb1 /mnt/

Password wizard pops up to enter the outer volume encrypted password.

Once mounted, verify.

df -hT /mnt
Filesystem             Type  Size  Used Avail Use% Mounted on
/dev/mapper/veracrypt1 ext4  3.9G   16M  3.7G   1% /mnt

To mount otvolume prompting only for its password:

sudo veracrypt -t -k "" --pim=0 --protect-hidden=no /dev/sdb1 /mnt/sdb1/

List the mounted VeraCrypt volumes.

veracrypt -l
1: /dev/sdb1 /dev/mapper/veracrypt1 /mnt/

To unmount the volume,

sudo veracrypt -d /dev/sdb1

To mount the hidden volume, run veracrypt command as shown above but this time, use the password defined for the hidden volume.

Seen how easy it is to use VeraCrypt on command line to encrypt your storage volumes? You can now create or put your sensitive data into the hidden volume. Enjoy the power of encryption.

That conclude our guide on how to use VeraCrypt on Command Line to Encrypt Drives on Ubuntu.

Other Tutorials

Encrypt Files and Directories with eCryptFS on Ubuntu 20.04

Encrypt Drives with LUKS in Linux

4 COMMENTS

  1. Hello,

    Yesterday I followed this tutorial, and I ended up mistakenly “erase” 1.5 Terabytes of data (share with my colleagues)!

    I wanted to quickly test Veracrypt (on Unbuntu 18) to create a encrypted volume in command line mode.

    What I really wanted to do is an encrypted file container. I realized afterward that this tutorial does not cover my use case.

    Please note that the step “Enter volume path” is missing in this tutorial.
    I did not really know what to give, so I entered the path to the folder where I wanted the file to be created.

    First I tried to specify the path to an inexistant directory “/data/new_dir/” but veracrypt returned the following error:

    Error: Is a directory:
    /data/new_dir/

    VeraCrypt::File::Open:276
    Segmentation fault (core dumped).

    Then I retried with “/dev/sda1” (which is /data/ on my machine) and Veracrypt started formatting and encrypting my entire 1.5To disk full of data without any warninig!
    This is a problem since the disk is shared with many colleagues, so I “erased” their data. It seems we could retrieve some of the data with “sudo fsck -y -b 819200 /dev/sda1”.
    Please let me know if you have any advice to retrieve more data, I would be very glad to know more about it!

    I then tried the same procedure with the graphical interface and saw the clear warning “Warning: this partition/disk will be formatted and all data stored on it will be lost”.
    And it also better explains what will be done : encrypt the entire drive.

    So, it would be great if you modify your tutorial to warn users about the potential data loss which might happen, and to clearly explain what will be done: formatting and encryption of the entire drive (even if it is already pretty clear, I must admit).

    For information, here is what I have done (now I get that it was quite stupid) :

    [email protected]:~$ veracrypt -t -c
    Volume type:
    1) Normal
    2) Hidden
    Select [1]: 1

    Enter volume path: /dev/sda1

    Encryption Algorithm:
    1) AES
    2) Serpent
    3) Twofish
    4) Camellia
    5) Kuznyechik
    6) AES(Twofish)
    7) AES(Twofish(Serpent))
    8) Camellia(Kuznyechik)
    9) Camellia(Serpent)
    10) Kuznyechik(AES)
    11) Kuznyechik(Serpent(Camellia))
    12) Kuznyechik(Twofish)
    13) Serpent(AES)
    14) Serpent(Twofish(AES))
    15) Twofish(Serpent)
    Select [1]: 1

    Hash algorithm:
    1) SHA-512
    2) Whirlpool
    3) SHA-256
    4) Streebog
    Select [1]: 1

    Filesystem:
    1) None
    2) FAT
    3) Linux Ext2
    4) Linux Ext3
    5) Linux Ext4
    6) NTFS
    7) exFAT
    8) Btrfs
    Select [2]: 5

    Enter password:
    Re-enter password:

    Enter PIM:

    Enter keyfile path [none]:

    Please type at least 320 randomly chosen characters and then press Enter:

    Done: 1,456% Speed: 440 M^[email protected]:~$
    [email protected]:~$

    Thanks a lot for providing this tutorial and reading my feedback,
    Arthur Masson

  2. I also made a feature request on Veracrypt forum to add a warning explaining everything and confirmation step in command line mode.

LEAVE A REPLY

Please enter your comment!
Please enter your name here