Renaming User inside Ubuntu offline Guest VM from the Host

What I need is a way to renaming a user inside an Ubuntu Guest VM without running the VM but rather having it mounted (offline).


More detailed Description

In my VDI each Computer runs a very simple Linux Host System (various versions, mostly Ubuntu 13.04 or OpenSuSE 12.3 but depends on the client). The users will not actually use this OS but rather an Ubuntu 20.04 VM that is automatically started after some configurations are done.

I want to rename the local user that is deposited in the VM before they logon to it. (Actually auto-logon is activated, so the logon happens without user interaction.) I tried doing this by a script using the usermod command (stored in the VM) that executes at bootup, but this does not seem work. (Neither using a cronjob or a systemd service worked, but that's a completely different problem.)

So what I am trying to do now is mounting the VM before it starts and editing it offline. Problem with that is that I cannot execute commands inside an offline VM (or can I?), so usermod is not an option. Instead it is possible to edit files with libguestfs and the command virt-edit. Is there a way to edit the username just by editing some files in the VM?

I tried just editing /etc/passwd and (for autologon) /etc/lightdm/lightdm.conf but just with that it did not work. More specifically the system failed to autologon the user. Though the new username was shown in the login GUI. Manual login attempts (entering the password) were denied due to a supposedly wrong password using the same password as before.

Note: I do have root access on both the host and the guest.

For Clarification: I do not necessarily need to rename the /home/username folder, although it is okay if that would make it easier.


Solution 1:

If the guest used basic passwd database and not another different backend, you can mount the virtual disk and follow the various guides on the net to create users manually.
For example:
https://unix.stackexchange.com/questions/153225/what-steps-to-add-a-user-to-a-system-without-using-useradd-adduser
and then:
https://unix.stackexchange.com/questions/81240/manually-generate-password-for-etc-shadow