when ecryptfs, change name … how?

I recently changed my name… and after a few months of seeing my old name reflected on my terminal prompt, decided to do something about it.

Now, I use Linux with ecryptfs to encrypt my user’s home directory on all my computers, often in addition to hardware-based whole-disk encryption.

Changing your account name and moving your homedir can be done in a few steps:

Step 1: create a temp user with admin (sudo) privileges. For convenience, I did this through the Gnome Settings Panel, rather than command line.

Step 2: reboot. You should do this to close any open files in your homedir.

Step 3: log in to the temp user, update your main user’s user name and group name, and move home dir (it’s easier than it sounds)

sudo usermod -d /home/<newname> -m <oldname>
sudo groupmod -n <newname> <oldname>

Step 4 (*): log back in to your user and delete the temp user — but if you’re using encryption, don’t do this yet!

This process isn’t so simple with an encrypted home dir! There’s one more step to get ecryptfs working again, and one step after that to get my apps (eg, steam) working as well.


Step 4: fix ecryptfs links

The ecryptfs package maintains several shadow directories in /home/.ecryptfs/and in /home/$user/ that need to be updated by hand.

The aforementioned usermod command will not suffice, so while you’re still logged in to the temp user, run the following to update all the symlinks that ecryptfs needs:

sudo mv /home/.ecryptfs/<oldname> /home/.ecryptfs/<newname>
sudo sed -i 's/<oldname>/<newname>/' /home/.ecryptfs/<newname>/Private.mnt
sudo unlink /home/<newname>/.Private
sudo unlink /home/<newname>/.ecryptfs
sudo ln -s /home/.ecryptfs/<newname>/.ecryptfs .ecryptfs
sudo ln -s /home/.ecryptfs/<newname>/.Private .Private

With ecryptfs linked up properly, I was able to log into my account (with the updated name) and ecryptfs automatically decrypted my homedir again. Yay!

But… some things still didn’t work, and it turns out there were a lot of symlinks in dot-folders within my homedir that used the full path. Like steam.

So there’s one more step!

Step 5: Find all the broken links and updated them.

There were enough broken links that I wrote a shell script to fix them all at once. You can just copy this script into file and run it from your home dir, and it will update the path in any symlinks, replacing that part of the path which is the old name with your new name. It won’t affect any links that don’t contain your old name.

#!/bin/bash
# simple tool to remap symlinks after renaming a home directory
# use this AFTER remapping ~/.ecryptfs and ~/.Private

OLD_NAME=${1:-}
NEW_NAME=${2:-}

if [[ -z "$NEW_NAME" ]]; then
	echo "Usage: find ./ -lname '*OLD*' -printf \"%p %l\\n\" | $0 OLD NEW"
	exit
fi

remap() {
	src=$1
	dst=$(echo $2 | sed "s/$OLD_NAME/$NEW_NAME/")
	echo Repointing $src to $dst
	unlink $src
	ln -s $dst $src
}

while read line; do
	remap $line
done

That’s it! I hope this helps & saves a little bit of time 🙂