Adding user to chroot environment
Solution 1
Oh, I figured it out myself. Schroot has a feature called "sessions" where current environment can be preserved. Everytime one logs out without creating a "session", schroot brings everything to clean state and hence deletes the user as well. One can start a session using
schroot -c mychroot -b
Solution 2
Actually, there is a better workaround, that I found it schroot FAQ, in section Why is schroot overwriting configuration files in the chroot?
By default, schroot copies over the system NSS databases ('passwd', 'shadow', 'group', 'gshadow', 'services', 'protocols', 'networks', and 'hosts', etc.) into the chroot. The reason for this is that the chroot environment is not a completely separate system, and it copying them over keeps them synchronised. However, this is not always desirable, particularly if installing a package in the chroot creates system users and groups which are not present on the host, since these will disappear next time the databases are copied over.
In short: each time you start a new schroot session, or after host system reboot, schroot grabs /etc/passwd
and some other files from you real system and copies them into chroot, thus losing all new users from it. The suggested workaround is described in this FAQ: you can disable copying this files:
- Open
/etc/schroot/default/nssdatabases
(on your real system, of course) - You will see the lis of so called NSS databases, that are copied from real system.
- Comment lines
passwd
,group
andshadow
, so that the file looks like this:
/etc/schroot/default/nssdatabases
# System databases to copy into the chroot from the host system.
#
# <database name>
#passwd
#shadow
#group
services
protocols
networks
hosts
Now if you go back inside your chroot and create some users, or install some postgresql etc, new users won't be lost. Even after reboot!
Neo
Updated on September 17, 2022Comments
-
Neo almost 2 years
I've created a chroot system in my Ubuntu using schroot and debrootstrap, based on minimal ubuntu. However whenever I can't seem to add a new user into this chroot environment. Here is what happens.
- I enter schroot as root and add a new user.(Tried both adduser and useradd commands)
- The username lists up in /etc/passwd file and I can 'su' into the new user. So far so good.
- When I log out of schroot, and re-enter schroot, the user I created has vanished!! There is no mention of that user in /etc/passwd either.
How do I make the new user permanent?
-
Thom G over 13 yearsThis sounds strange. First try to be sure that the directories are correct, both when you're inside the chroot and outside. Then, do
cat /etc/passwd
after creating the user, then log out, andcat
the same file, now with chroot directory prefixed. Did it change?
-
NIA about 11 yearsThanks for idea, but note that anyway the users won't persist after system reboot. I added an answer which seems to deal with it.