Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

An encrypted /home directory gets mounted automatically for me when I log in. I have a second internal hard drive that I've formatted and encrypted with Disk Utility. I want it to be automatically mounted when I login, just like my encrypted /home directory is. How do I do this?

There are several very similar questions here, but the answers don't apply to my situation. It might be best to close/merge my question here and edit the second one below, but I think it may have been abandoned (and therefore never to be marked as accepted).

This solution isn't a secure method, it circumvents the encryption.
This one requires editing fstab, which necessitates entering an additional password at boot. It's not automatic like mounting /home.
This question is very similar, but does not apply to an encrypted drive. The solution won't work for my needs.
Here is one but it's for NTFS drives, mine is ext4.

I can re-format and re-encrypt the second drive if a solution requires this. I've got all the data backed up elsewhere.

share|improve this question
What FS do you use for encryption ? LUKS or ecrytpfs ? – warl0ck Mar 21 '12 at 4:11
@AaronLewis I just use Ubuntu's Disk Utility to format the drive, then check the 'encrypt this drive' box. If I had to guess I would go with ecryptfs, since this is how /home is encrypted by default. Since it's so easy to do I've never looked into it. – Tom Brossman Mar 21 '12 at 8:10

4 Answers 4

up vote 8 down vote accepted

I was also looking for a way to automatically mount a second ecryptfs volume. The following collection of scripts and configuration modifications will securely and automatically mount your volume on login, either to the GUI or the CLI.

There is a better solution that's in the process of being created (though I think not quite ready for automatic mounting on user login, as such this script will have a limited shelf life.):

ecryptfs on too-small harddrive - how to add links into the encryption?

The security of the scripts depends on your home directory being encrypted with ecryptfs so that the script and files with the password to unwrap your passphrase are encrypted. If you leave your computer unlocked with a root shell open after log in it will be possible to access the passwords, however use of sudo NOPASSWD allows secure mounting of the partition without requiring password entry or leaving the passphrase in a file readable by the user.

One known deficiency of these scripts is that your second volume will not be unmounted on logout, as such it's not particularly suitable for multi user systems.

My solution is implemented with several parts, two shell scripts, one that performs the actual mounting and another that serves as a wrapper for it.

This is the wrapper script that validates if the directory is already mounted, if it isn't then it will call the mounting script using sudo:




grep -q $MOUNT_POINT /proc/mounts
if [ $? -eq 1 ]; then
  sudo /home/johnf/scripts/mount_other_ecryptfs

This script calls /home/johnf/scripts/mount_other_ecryptfs which is as follows.

Note that this script assumes that you have file name encryption enabled, if you don't it will be necessary to either modify the script to handle detection (look at ecryptfs-recover-private) or you could remove the ecryptfs_fnek_sig mount option.

The following is the /home/johnf/scripts/mount_other_ecryptfs script:


ECRYPTFS_SIG=`head -1 ${ENCRYPTED_VOLUME}//../.ecryptfs/Private.sig`
ECRYPTFS_FNEK_SIG=`tail -1 ${ENCRYPTED_VOLUME}//../.ecryptfs/Private.sig`

printf "%s" $MOUNT_PASSWORD | ecryptfs-insert-wrapped-passphrase-into-keyring ${ENCRYPTED_VOLUME}/../.ecryptfs/wrapped-passphrase
mount -t ecryptfs -o key=passphrase:passfile=${PASSFILE},ecryptfs_sig=${ECRYPTFS_SIG},ecryptfs_fnek_sig=${ECRYPTFS_FNEK_SIG},ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_passthrough=n ${ENCRYPTED_VOLUME} ${MOUNT_POINT}

You will also need to create a file with your password in it, this file will be used by the ecrypts mount command:



You need to modify the permissions on several files:

chmod +x /home/johnf/scripts/automount_ecryptfs
sudo chown root:root /home/johnf/scripts/mount_other_ecryptfs /home/johnf/scripts/ecryptfs_passphrase
sudo chmod a=x /home/johnf/scripts/mount_other_ecryptfs
sudo chmod 400 /home/johnf/scripts/ecryptfs_passphrase

Before creating the scripts you need to create a sudoers configuration to permit the execution of the mounting script using sudo without entering your sudo password.

Add the following to /etc/sudoers (or a file in /etc/sudoers.d). You will want to replace johnf with your username. It's necessary to use an absolute path to the mounting script.

johnf   ALL = NOPASSWD: /home/johnf/scripts/mount_other_ecryptfs

The final step is to call the automount_ecryptfs script on login.

On Ubuntu Unity (and probably gnome) use the Startup Applications applet to create a new startup program that calls /home/johnf/scripts/automount_ecryptfs.

To automatically mount the second ecryptfs volume on login into a bash shell you will want to modify your ~/.bashrc file. Add the following:


With this configuration in place you should now automatically mount your second ecryptfs volume.

share|improve this answer
Wow, great answer! Upvoted and accepted. Can't test it yet but this looks very complete. – Tom Brossman Mar 21 '12 at 8:26

Building on @johnf's answer but using mount.ecryptfs_private instead:

  • encrypted /home/bob/ (eg. on a SSD), using Ubuntu's normal encrypted home dir magic.
  • encrypted /media/hdd/bob_extra/ (eg. on a HDD), to be mounted to /home/bob/extra. This should automount on login, just like the home dir does.
  • use the same keys/credentials for both.

create it

mkdir /media/hdd/bob_extra
cp /home/bob/.ecryptfs/Private.sig /home/bob/.ecryptfs/extra.sig
echo "/media/hdd/bob_extra /home/bob/extra ecryptfs none 0 0" > /home/bob/.ecryptfs/extra.conf

test it

mount.ecryptfs_private extra

running mount, you should see:

/media/hdd/bob_extra on /home/bob/extra type ecryptfs (ecryptfs_check_dev_ruid,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_unlink_sigs,ecryptfs_sig=12345678abcdef,ecryptfs_fnek_sig=abcdef12345678)

to unmount:

sudo umount /media/hdd/bob_extra

setup automount

Create /home/bob/bin/automount_ecryptfs.extra, which will mount it if it hasn't been mounted already.



grep -q $MOUNT_POINT /proc/mounts
if [ $? -eq 1 ]; then
  mount.ecryptfs_private extra

Make it executable (chmod +x), then add it into /home/bob/.bashrc:


Then add it to Gnome's Startup Applications as well.

share|improve this answer
+1 Thanks for the answer. I have a far more complicated setup now with multiple SSDs and symlinks to a shared drive, so I won't be able to test this. I'm hopeful encrypting multiple HDDs will be as simple as encrypting /home someday. – Tom Brossman Jul 19 '12 at 20:05

I'm afraid that this isn't going to be a popular answer...

It's not going to be possible to automatically mount any encrypted partition, without circumventing the security of the encryption itself.

Think about what "automatically" means, and understand that automatically means that they will get to see your data too.

share|improve this answer
I used 'Securely' subjectively, I could remove it from the question. I just want the encrypted drive full of photos to be mounted when I log in, just like /home is. I know that this is theoretically possible, since I have a third (also encrypted) drive as a destination for Déjà Dup. That drive only gets mounted when the backup utility fires up. Fully automatic, I have the key saved so I don't enter it each time. I'm comfortable with having all my keys handled automatically after I enter my password at the login screen. Any idea how to make this happen? Thanks. – Tom Brossman Feb 19 '12 at 11:41
Automatically in this context means that it should be mounted after you enter your login password. You login password can be the key, or more often the key to the key, to your encrypted partition. This is reasonable secure (giving a good login password). – Javier Rivera Mar 21 '12 at 14:08
It will not be automatically mounted in another computer, which is the reason why most of us encrypt some disks. In my case it is a disk I use to make backups. I work in a shared space and know that one of you guys with a lot of knowledge will be able to circumvent whatever I do with physical access to my disk/computer: I just want to reduce the accessibility to my files so that anybody else can not do it. – Ramon Suarez Mar 28 '13 at 15:00

This two-line script works for me; ~/scripts/


cryptsetup open --type luks --key-file /home/raul/keys/storage_keyfile /dev/sdb1 storage
mount /dev/mapper/storage /storage

I have added:

sudo /home/raul/scripts/' 

to Startup Applications and

sudo ALL=NOPASSWD: /home/raul/scripts/' 

to /etc/sudoers. When using this approach, first create the mount point and make relevant modifications to the above script.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.