I don't think there is the generic all solving answer to this problem. You cannot be two users at once. Whenever you run a command the shell would have to guess as which user that command should be executed. When you create a file it would have to guess who it should belong to and so on and so forth.
I do your 5th paragraph. I have a script in /usr/local/bin
(i called it becomeme
) which sources my /home/user/.bashrc
and adapts PATH, LD_LIBRARY_PATH, MANPATH and PKG_CONFIG_PATH, to include paths to my home directory. I also put some wrappers, for example to replace vi
with HOME=/home/me vi
. I basically wrap stuff to search my user's home directory for config. For the simple use cases bashrc, vi and screen it works fairly well.
This however only works to a certain extent, because accessing my home directory as another user generally means that user needs to have write permissions for some files and directotires, which I don't want. So it's more a hack for most common usecases, vi
and screen
mostly.
It's not very complicated, as I just need to remember to type becomeme
after switching to another user. For root this worked fairly quickly, for other users I had to give permission to the files in my home first.
Another approach, which I haven't tried yet, but may work for your use case:
Create another user with UID 0 and your user's home. You simply need to add a line to /etc/passwd
and /etc/shadow
. For example if your root-username was admin, the corresponding line in /etc/passwd
might look like this:
admin:x:0:0:My personalized root account:/home/mynormaluser:/bin/bash
Then you type passwd admin
to create an entry in /etc/shadow
. If you log in as admin
now, it will be UID 0, i.e. root, but have your normal home directory and therefore its settings from your normal user. It also has its own password, you don't need to share. This doesn't work for NFS-home though (see root_squash).
While this can work, it may create some minor issues with file permissions, whenever you create files as user admin
in your home directory. You could work around those issues with proper creation masks and a designated group you share only between your user and admin
.