How to reset the password for an lxc container

If you find yourself stuck trying to login to an lxc container because you've forgotten the password, here's a simple fix.

With the container stopped, on the host machine (assuming it's ubuntu - other linux flavours may vary slightly), navigate as root to:

/var/lib/lxc/$container_name/rootfs/etc

Then edit the shadow file (perhaps make a backup first), and remove the hash which corresponds to the user's password, leaving the separating colons in place e.g.:

ubuntu::16372:0:99999:7:::

You should then be able to start the container and log in as that user without a password.

Now you should set a decent password using passwd and make a proper note of it this time. (Remember that the user in the container can do quite a lot of things which affect the host OS, so it's really worth setting a decent password).

Posting for a careless friend who has had to do this more than once ;)