I would like to start off by saying this is not the best idea from a security standpoint as the password to mount the Veracrypt drives will exist in clear text within a script. This is why it is extremely important that the computers operating system hard drive is itself encrypted with something like dm-crypt to start. The reason for this is simple. If the location of the script is not encrypted at rest and the systems OS drive is copied or stolen the password to the encrypted Veracrypt drive(s) will be completely exposed.
Now that everyone has been warned this script comes in handy if a computer has several hard drives with encrypted Veracrypt partitions that have to be manually mounted using a password every time it is rebooted. This script will automatically mount each encrypted drive once the owner of the script logs in to the system.
Sidenote: This script has only been tested with the command line version of Veracrypt specifically the veracrypt-1.16-setup-console-x64 and veracrypt-1.17-setup-console-x64 installers.
First, install expect.
sudo apt-get install expect
Second, create the script.
cd ~ touch .mount.sh chmod +x .mount.sh vim .mount.sh
Third, past the syntax below in to the script and save it making sure to replace yourpasswordhere with your Veracrypt password. Also, make sure each spawn line maps to the correct Veracrypt partition and the correct name of your Veracrypt partition .
#!/bin/bash ### mount veracrypt volumes expect <<EOF spawn /usr/bin/veracrypt /dev/sdb /media/veracryptvolumename1 expect "Enter password" send "yourpasswordhere\n" expect "Enter PIM for " send "\n" expect "Enter keyfile" send "\n" expect "Protect hidden volume" send "\n" expect eof; sleep 5 EOF expect <<EOF spawn /usr/bin/veracrypt /dev/sdc /media/veracryptvolumename2 expect "Enter password" send "yourpasswordhere\n" expect "Enter PIM for " send "\n" expect "Enter keyfile" send "\n" expect "Protect hidden volume" send "\n" expect eof; sleep 5 EOF
Alternatively, you can use this script to load your favorite Veracrypt partitions if all use the same password.
#!/bin/bash ### mount favorite veracrypt volumes expect <<EOF spawn /usr/bin/veracrypt --auto-mount=favorites expect "Enter password" send "yourpasswordhere\n" expect "Enter PIM for " send "\n" expect "Enter keyfile" send "\n" expect "Protect hidden volume" send "\n" expect eof; sleep 30 EOF
Then, create a Veracrypt group and add your user to it.
sudo groupadd veracrypt sudo usermod -a -G veracrypt yourusername
Next, allow the Veracrypt group to run Veracrypt without a password and allow your user to run the .mount.sh script without a password by adding the following lines to the /etc/sudoers file. I recommend using the visudo command for this step.
%veracrypt ALL=(root) NOPASSWD:/usr/bin/veracrypt yourusername ALL=(ALL) NOPASSWD:/home/yourusername/.mount.sh
Last, set the script to run at login by using Ubuntu’s Startup Applications program. Create a new startup application by clicking the add button and pointing it to the directory of the .mount.sh script (as pictured below). Make sure it matches the script location in the /etc/sudoers file.
That’s it! Reboot the system and if the drives auto-mount within the first minute or so after login then the script works.
