Applications of fakeroot and chroot

Why are there two utilities chroot and fakeroot when both can fake a privileged access to the user?

Asked By: Franc M


chroot actually does something very different from fakeroot. it stands for "change root" and means you can run a process which has the ‘/’ directory mapped to something different than the system. In most cases, this is an operating system installed on a different drive or partition. Within that chroot environment, the root privileges are actually real, in fact chroot can only be accessed by privileged processes. Fakeroot just gives a process the illusion of root access when in reality all files created by it will just belong to the original user.

Answered By: Paper Benni

Only fakeroot will "fake priviledged access to the user".

Invoking chroot can enable some user to run commands with regards to some root (/) directory tree different from the one currently in use on the system.

Let’s say that the desired root directory stands in /mnt then, in the chrooted env, firing /foo/bar will in fact launch /mnt/foo/bar and have bar behaving as if /mnt was the real root directory.
BUT, of course, if the user does not get appropriate access rights to /mnt /mnt/foo and /mnt/foo/bar then… it won’t just make it.
User does not automagically become root (user) or even a fake root user. Only the root base directory is changed.

BTW chroot utility comes with the coreutils package which is necessarily part of the most basic linux distribution. You generally need it when installing a new system from within another one.

fakeroot will create an environment in which the user can act as if it were root regarding file manipulations.
By "fake", you should understand that this will only be valid within that environment. For any other user, for the system itself… and for yourself after exiting, nothing will have changed. Let’s consider some example inspired from the man page :

$  whoami
$ fakeroot /bin/bash
#  whoami
# ls -ld / (note that this is the original (real) root directory)
drwxr-xr-x  20 root     root         1024 Jun 17 21:50 /
# chown me:users /
# chmod a+w /
# ls -ld /
drwxrwxrwx  20 me    users        1024 Jun 17 21:50 /
# exit
$ ls -ld /
drwxr-xr-x  20 root     root         1024 Jun 17 21:50 /

BTW contrarily to chroot, fakeroot comes as part of a package you’ll need to install specifically. You generally need it when archiving files to be deployed on other systems with user id 0.

Answered By: MC68020