<P>Recovery of an (en)crypted home directory in a 'buntu based system </P>
<P>by 5150</P>
<P><BR>
</P>
<P>This is going to be the archetypal “How I Did It” episode because if fulfills the criterion of dealing with an issue most listeners will most likely never have to resolve, but might be invaluable to those few who some day encounter the same problem, how to recover an encrypted home folder on an Ubuntu system.</P>
<P>I enabled home folder encryption on installation of a Linux Mint 8 system some years back and it never gave me trouble until the day that it did.  Suddenly, my login would be accepted, but then I would come right back to GDM.  Finally I dropped into a text console to try to recover the contents of my home folder, and instead found two files, Access-Your-Private-Data.desktop and README.txt .  README.txt explained that I had arrived in my current predicament because my user login and password for some reason were no longer decrypting my home folder (Ubuntu home folder encryption is tied to your login, no additional password is required).  Honestly, until I lost access to my files, I 'd forgotten that I'd opted for encryption.  I found two
articles that described similar methods of recovery.  I'd tried that following their instructions and failed, probably because I was mixing and matching what seemed to be the easiest steps to implement from the two articles.  When I took another look at the material weeks later, I discovered I missed a link in the comments that led me to an improved method added at Ubuntu 11.04 that saves several steps:
<A HREF="https://blog.dustinkirkland.com/2011/04/introducing-ecryptfs-recover-private.html">https://blog.dustinkirkland.com/2011/04/introducing-ecryptfs-recover-private.html</A></P>
<OL>
<LI><P>Boot to an Ubuntu distribution CD (11.04 or later)</P>
<LI><P>Create a mount point and mount the hard drive.  Of course, if you configured you drive(s) with multiple data partitions (root, /home,
etc) you would have to mount each separately to recover all the contents of your drive, but you only have to worry about decrypting your home directory.  If you use LVM, and your home directory spans several physical drives or logical partitions, I suspect things could get interesting.</P>
<OL>
<LI><P>$sudo mkdir /media/myhd</P>
<OL>
<LI><P>/media is owned by root, so modifying it requires elevation</P>
</OL>
<LI><P>You need to confirm how your hardrive is registered with the OS.  I just ran Disk Utility and confirmed that my hard drive was parked
at /dev/sda, that meant that my single data partition would be at /dev/sda1</P>
<LI><P>$sudo mount /dev/sda1 /media/myhd</P>
<LI><P>Do a list on /media/myhd to confirm the drive is mounted</P>
<OL>
<LI><P>$ls /media/myhd</P>
</OL>
<LI><P>The new recovery command eliminates the need to re-create your old user</P>
<OL>
<LI><P>$sudo ecryptfs-recover-private           (yes, ecrypt not e<U>n</U>crypt)</P>
<LI><P>You will have to wait a few minutes while the OS searches your hard drive for encrypted folders</P>
<OL>
<LI><P>When a folder is found, you will see </P>
<P>INFO:
Found [/media/myhd/home/.ecryptfs/<I>username</I>/.Private].</P>
<P>Try to recover this directory? [Y/n]</P>
<UL>
<LI><P>Respond “Y”</P>
</UL>
<LI><P>You will be prompted for you old password</P>
<LI><P>You should see a message saying your data was mounted read only at </P>
<P>/tmp/ecryptfs.{SomeStringOfCharacters}</P>
<UL>
<LI><P>I missed the mount point at first, I was look for my files in /media/myhd/home/<I>myusername</I></P>
</UL>
</OL>
</OL>
<LI><P>If you try to list the files in /tmp/ecryptfs.{SomeStringOfCharacters}, you will get a “Permission Denied” error.  This because your old user owns these files, not your distribution CD login</P>
<OL>
<LI><P>[You will probably want to copy “/tmp/ecryptfs.{SomeStringOfCharacters}” into your terminal buffer as you will need to reference it in commands.  You can select if with your mouse in the “Success” message and copy it with &lt;Ctrl&gt;&lt;Alt&gt;c, paste it later with &lt;Ctrl&gt;&lt;Alt&gt;v</P>
<LI><P>I tried to take ownership of /tmp/ecryptfs.{SomeStringOfCharacters}, I should have thought that would have worked.</P>
<OL>
<LI><P>From my command prompt, I can see my user name is “ubuntu”</P>
<LI><P>$ sudo chown -R ubuntu /tmp/ecryptfs.{SomeStringOfCharacters}</P>
<UL>
<LI><P>-R takes ownership of subdirectories recursively</P>
<LI><P>It's a good time to get a cup of coffee</P>
</UL>
</OL>
</OL>
<LI><P>Next, we need to copy the files in our home directory to another location, I used an external USB drive (it was automounted under /media when I plugged it in).  If you had space on the original hard drive, I suppose you could create a new user and copy the files to the new home folder.  I decided to take the opportunity to upgrade my distro.  Some of the recovered files will wind up on my server and some on my newer laptop.</P>
<OL>
<LI><P>One could run Ubuntu's default file manager as root by issuing “sudo nautilus &amp;” from the command line (the “&amp;” sends the
process to the background so you can get your terminal prompt back)</P>
<OL>
<LI><P>Before copying, be sure to enable “View Hidden Files” so the configuration files and directories in you home directory will be recovered as well.  As I said, there are select configuration files and scripts in /etc I will want to grab as well.</P>
</OL>
<LI><P>I had trouble with Nautilus stopping on a file it couldn't copy, so I used cp from the terminal so the process wouldn't stop every
time it needed additional input.</P>
<OL>
<LI><P>$ cp -Rv  /tmp/ecryptfs.{SomeStringOfCharacters} /media/<I>USBDrive</I>/<I>Recovered</I></P>
<UL>
<LI><P>Of course the destination will depend on what you've named your USB drive and what folder (if any) you created to hold your recovered files</P>
<LI><P>-Rv copies subdirectories recursively and verbosely, otherwise the drive activity light may be your only indication of progress.  The cp command automatically copies hidden files as well.</P>
<LI><P>Because of the file ownership difficulties, I could only copy the decrypted home folder in its entirety, 
</P>
</UL>
</OL>
<LI><P> I still had trouble with access do to to ownership once I detached the external drive and remounted it on my Fedora laptop, but I
took care of that with:</P>
<OL>
<LI><P>$ su -c 'chown -R <I>mylogin</I>/media/<I>USBDrive</I>/<I>Recovered'</I></P>
</OL>
</OL>
</OL>
</OL>
