If I have an encrypted external disk (or an internal disk that is not in fstab), I see an entry for it in Nautilus -- with an entry like "X GB Encrypted Volume". I can click on this volume, and am prompted for a password to decrypt and mount the device.
But how do I do this from the command line?
This wiki page, and other docs I can find, only refer to GUI methods of decrypting the device; but this won't do in the context of headless servers or SSH logins. Is there a simple way to get devices to mount to automatic locations in /media
just like they would with the GUI?
(I'm not asking about encrypted home directories -- I'm aware of ecryptfs-mount-private
. This question is about additional encrypted volumes.)
Your volume is probably encrypted with LUKS, here's how to mount it:
You need:
To decrypt the volume:
Now you can mount it as usual:
To lock the container again, it needs to be unmounted first:
To automatically put it in the
/media
location, use the udisks toolThe steps in @Georg Schölly's answer did not work for me at the time, although they might work now, a few Ubuntu releases after. Back then, after the
sudo mount /dev/mapper/my_encrypted_volume /media/my_device
step I got the error:Unlocking and mounting the disk with udiskctl
Instead, I used
udisksctl
, a command-line interface that interacts with theudisksd
service.Here's what worked (
/dev/sdb5
is the partition on my hard disk marked ascrypt-luks
):After typing the first command, you'll be prompted for your encryption passphrase. Once the encrypted partition is unlocked, the second command will mount it. If that's successful, you'll end up with a message similar to this:
From there I could access the data :)
Locking the disk with udiskctl
Unmount the device:
You'll need to deactivate all logical volumes in the
ubuntu-vg
volume group first. Otherwise you'll get an error along the lines of 'Device busy' if you try to lock it (more info):Then you'll be able to lock back the encrypted partition
Notes
udisksctl
commands are executed withoutsudo
.Device mapper names: the
ubuntu--vg-root
naming might change across Ubuntu releases (e.g. I've seen it calledsystem-root
andubuntu-root
too). An easy way to find out the name is to run the following command after unlocking the LUKS partition:ls -la /dev/mapper
Then looking at the output of the
ls
command, the name you'll need will be generally the one symlinked to/dev/dm-1
Device mapper names, alternative: an alternative to the previous command is to run:
lsblk -e7
There you'll be able to see the device name mapping as a tree view. The
-e 7
option is used to exclude the loop devices (ID 7) created by installed snaps from the output. Simply to have less clutter.sudo lvs
command to find out the names of volume groups and logical volumessudo lvchange -an ubuntu-vg
command before you can lock it from the GUI.If you get this error:
run:
then activate all LVM you see
then re-run the mount:
One problem i ran into, was duplicate volume groups: Both my recovery system and the drive to be recovered were ubuntu systems with LVM. This is, why I had two
ubuntu-vg
volume groups (vgdisplay
would display both, each with their own UUID, but i couldn't get to their logical volumes).My solution builds on the answer of Georg:
sudo cryptsetup luksOpen /dev/sdaX my_encrypted_volume
sudo vgscan
should now pick up the contained volumes/groups.DRAGONS AHEAD: WE'RE NOW CHANGING THE VOLUME GROUP NAME. YOU WILL NOT BE ABLE TO BOOT THAT DRIVE AFTERWARDS!
use
sudo vgrename ubuntu-vg ubuntu-vg2
to rename the volume group.If you need to boot off that drive, you can do these steps again, but rename your volume group back to ubuntu-vg. A different possibility is to alter your boot configuration to the new vg-name.
Now that the duplicate vg-name is resolved, i can boot back into my regular system, redo the
cryptsetup...
,vgscan
and then mount/dev/mapper/ubuntu--vg2-root
anywhere you like.sdb1 here is an example you should input your device name, none of this commands will require root privileges
unlock encrypted disk
after inserting the correct passphrase it will output something like this: Unlocked /dev/sdb1 as /dev/dm-3
then mount it to /media/
it should output something like this: Mounted /dev/dm-3 at /media/yourUserName/sdb
to unmount it
to lock it again
All answers above took the assumption that the user already knows which partition is the encrypted one. Coming from someone that doesn't like the command line so much, I was expecting some user-friendly answer... So my 2cents here.
/dev/sdb4
).Then I tried to mount it like adviced above:
But got this error:
Ok, so I guess nautilus has already tried to mount it (because it actually prompted me for the password as I connected the USB, even if it didn't end up showing the decrypted tree). However, the error message is not really helpful because it doesn't tell me where it's already mapped/mounted. But this command helps in this case:
Aha! So it's
/dev/dm-3
.However when trying to mount it, it doesn't work:
After much tinkering, I found out that I was running into the
duplicate volume groups
problem (described above by @amenthes) because the commandssudo vgscan -v
andsudo vgdisplay
were showing two entries with the same volume group name. However, I found a better way to deal with it than his method (no need to boot into a LiveCD to rename volumegroups!), in this link, which I'll quote above (just in case that link gets broken...):If you run
ls -la /dev/mapper/
you should see aluks-xxxxxx-xxxxx-xxxx
or some such file. That's the mapping that was created when Ubuntu prompted for the encryption password with a dialog but failed to open it (all the dialog did was to callluksOpen
and map it to that /dev/mapper/luks-xxx file). Now:sudo pvdisplay
command. It should be /dev/mapper/luks-xxx-whatever.sudo pvs -o +vg_uuid
. The uuid will be the value displayed all the way to the right, containing 7 dash-delimited values. Copy those somewhere as we'll be using them in the next step. DO NOT CONFUSE UUIDS AND COPY DOWN THE WRONG ONE. Only copy the one for your current /dev/mapper/luks-xxx-whatever device.sudo vgrename UUIDOFYOURDISKHERE oldhd
You can change the "oldhd" to whatever you want so long as it's differing from the volume group name of your current disk. Performing this step removes the conflict with volume group names which will allow you to now make volumes available.vgchange -a y
to make the volumes active.sudo mkdir /media/<yourUserName>/someDir
sudo mount /dev/oldhd/root /mnt/oldhd
.ubuntu-vg
if you want the volume to still be bootable.I went into several paths from the previous answers and only combination of the previous answers worked for me. He what I did and what went OK, and what went wrong and my workaround.
I have an LUKS encrypted hard disk that I need to mount from a live boot USB for Ubuntu 15.10. To do so I started with the following command,
where sda3 is the encrypted partition. This command didn't work with me and I am not sure why, so I used the following command:
it worked with me and I didn't need to install it as it was there in the live boot.
Now, I need to mount the HD, and this was not a straight forward thing: I tried:
But the second command didn't work with me, and hence I have to find a work around which is the following:
That was my path .. but you can use the path
dev/mapper/ubuntu
and then double tab to see the rest of options. This mounted the HDD as:Then I used the following command to mount it as
/media/my_device
as following:which worked fine.
In Summary
For those of us who don't want to use a GUI tool even to determine which partition is encrypted.
find any encrypted partitions
-l
requests the "list" format - we don't need the tree-f
shows us the name of the file system toowe get something like
unlock the partition that we want (in my case
/dev/sdc2
)-b
means that we are giving the path to a block deviceafter entering the passphrase we get an affirmative response with the necessary info for the next step:
mount the newly created device (
dm
stand for device manager)Again we get an affirmative response with useful info:
(
g
being my username on this system,Data
is the label I used for that partition)It may be the case that your desktop system/file manager has already automatically mounted the device, or you did it yourself before. Then you get something like
This is no problem, you can access the data from the encrypted partition anyway.
ls /media/g/Data
unmount the device again (use the same name you used for mounting, the command is
unmount
, notumount
:-) )If the device is not busy you will get
Now lock the partition again (you have to remember the name of the partition)
You will get
optionally power down the complete external disk
With a graphical desktop you may get an error here:
In that case you can use
udisksctl
to unmount the partitions one by one until you succeed. Theudisksctl power-off
does not return any messages.You can mount it in two steps, and I have a script example.
Note: the service udiskctl will mount things under /media, it's more designed for desktop users mounting usb sticks. If you want to mount the device somewhere else, it's not the solution you are looking for.
Here is what I worked out. In this example, my encrypted device is a partition made with lvm, but this doesn't really matter. It is an ext4-formatted partition. In its encrypted form, it lives at
an encrypted partion is "opened" (decrypted) like this
(this is where you enter the passphrase)
the last argument is a temporary reference to the decrypted block device. The 'mapping' disappears when you reboot so you can choose a different name each time, if you want.
it is now visible as a device:
You can mount this device: we now have an ext4 device. To make it convenient, add a line in /etc/fstab
and make the mount point (in my case:
sudo mkdir /opt1
, and then setup permissions as you wish) If you used the name opt1_opened in Step 1, then this is the second step to mount it:and it's mounted.
Hence, a bash script:
The correct answer is
gio mount -d /dev/dm-x
(no sudo).Previous answers show a disconnect with the Nautilus or Nemo way of mounting, since you need to enter the LUKS passphrase even though it is previously cached in the user keyring from the GUI. Using
gio
automatically uses the passphrase previously stored by Nautilus or Nemo.For a more detailed answer, see https://unix.stackexchange.com/questions/394320/what-command-does-nemo-use-to-mount-drives/536842#536842