Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Why do we need fakeroot command at all? Can't we simply use the sudo or su commands?

The man page says:

fakeroot - run a command in an environment faking root privileges for file manipulation

About.com says:

Gives a fake root environment. This package is intended to enable something like: dpkg-buildpackage -rfakeroot i.e. to remove the need to become root for a package build. This is done by setting LD_PRELOAD to libfakeroot.so, which provides wrappers around getuid, chown, chmod, mknod, stat, ..., thereby creating a fake root environment. If you don't understand any of this, you do not need fakeroot!

My question is, what special purpose does it solve that a simple su or sudo don't? For example, for repacking all installed packages in ubuntu we give following command:

$ fakeroot -u dpkg-repack `dpkg --get-selections | grep install | cut -f1`

Can we do the above command with sudo or su instead of fakeroot like this:

$ sudo dpkg-repack `dpkg --get-selections | grep install | cut -f1`

EDIT:

Running:

$ sudo dpkg-repack `dpkg --get-selections | grep install | cut -f1`

gives me this error:

control directory has bad permissions 700 (must be >=0755 and <=0775)

Any reason why?

share|improve this question

migrated from stackoverflow.com Mar 20 '11 at 9:01

This question came from our site for professional and enthusiast programmers.

    
it is a good idea, for security reason, to avoid doing as root everything that could be done as normal user, even if you can run sudo or su because it is your machine. fakeroot has two usages 1) it fools programs into believing you are indeed root user, which some badly written proprietary software may require even if not needed (usually Windows developer gone Linux) and 2) it allow emulating file mode and ownership changes which you wouldn't otherwise be able to do, mainly to create a tar file with correct permissions and ownership, useful for example when packaging software. –  pqnet Aug 12 at 10:03

6 Answers 6

up vote 19 down vote accepted

Imagine that you are a developer/package maintainer, etc. working on a remote server. You want to update the contents of a package and rebuild it, download and customize a kernel from kernel.org and build it, etc. While trying to do those things, you'll find out that some steps require you to have root rights (UID and GID 0) for different reasons (security, overlooked permissions, etc). But it is not possible to get root rights, since you are working on a remote machine (and many other users have the same problem as you). This is what exactly fakeroot does: it pretends an effective UID and GID of 0 to the environment which requires them.

In practice you never get real root privileges (in opposite to su and sudo that you mention).

share|improve this answer

AFAIK, fakeroot runs a command in an environment wherein it appears to have root privileges for file manipulation. This is useful for allowing users to create archives (tar, ar, .deb etc.) with files in them with root permissions/ownership. Without fakeroot one would need to have root privileges to create the constituent files of the archives with the correct permissions and ownership, and then pack them up, or one would have to construct the archives directly, without using the archiver.

fakeroot works by replacing the file manipulation library functions (chmod(), stat() etc.) by ones that simulate the effect the real library functions would have had, had the user really been root.

Synopsis :

 fakeroot [-l|--lib library] [--faked faked-binary] [--] [command]  

Check more here : fakeroot

share|improve this answer
    
@MaskTheSmokin: So fakeroot gives you super user power only for file manipulation operations,right. –  gkt Feb 24 '11 at 13:11
    
@gkt.pro : I guess, yes. –  herbalessence Feb 26 '11 at 11:19
4  
It does not really give super user power, it only fakes it - the program running in it thinks it has root privileges, while it really still uses the user's normal privileges. –  Paŭlo Ebermann Feb 26 '11 at 13:22
    
Where is the difference between the program running in it thinks it has root privileges and the program having root privileges? If I can do a rm -rf / and the program, running it thinks I have root privileges ... –  user unknown Mar 20 '11 at 15:52
3  
@userunknown You might be able to bypass rm's check that you have sufficient permissions, but the kernel itself wouldn't let you do it; the unlink system call would fail. It's not up to the application alone to handle permissions, or you'd be able to write your own application that doesn't check permissions and do whatever you want with it –  Michael Mrozek Oct 7 '12 at 18:26

To see clearly the difference between fakeroot and a real sudo / su, just do:

$ fakeroot
# echo "Wow I have root access" > root.tst
# ls -l root.tst
-rw-rw-r-- 1 root root   23 Oct 25 12:13 root.tst
# ls -l /root
ls: cannot open directory /root: Permission denied
# exit
$ ls -l root.tst
-rw-rw-r-- 1 ubuntu ubuntu 23 Oct 25 12:13 root.tst

As long as you are within the fakeroot shell, it looks like if you are root - as long as you do not try to do anything that really needs root privileges. And this is exactly what a packaging tool need to make packages that will make sense on any machine.

In fact, when you use fakeroot for packaging, what you want to achieve is to make the tools you run under fakeroot to see your files as owned by root. Nothing more, nothing less. So in fact, su or sudo will not work for getting the right file ownership.

share|improve this answer
    
Isn’t faker dangerous? If I create a file with the suid bit and rx perm, the file will be created owned by root, executable by anyone, as root! Or maybe setting the suid bit won’t work? –  Frizlab Oct 3 at 21:14

I've used it for package building scripts. I was not sure that the person running the script has root level access, but the script still needed to generate, say, a tar file which contained files that belong to root. The simplest way to do it was run the package building script under fakeroot, which tricked the archiver into believing that the files belong to root, and packed them up as such inside the archive. This way, when the package was unpacked to the destination machine (on a different machine altogether), the files didn't belong to weird or non-existing users.

Thinking about it, the only place I've seen this was for building some kind of archive: rootfs of embedded systems, tar.gz archives, rpm packages, .deb packages, etc.

share|improve this answer
    
fakeroot is a workaround tool for bugged packaging software: there is no reason you need to be root to create such packages, but since they don't allow you to specify file permissions in any other way than setting them directly into the filesystem beforehands you have no choice –  pqnet Aug 12 at 10:08

One common usage is to find out what files a failing binary really wanted to access. That is, finding out and fix or working around bugs caused by hard coded paths and improper exception handling.

share|improve this answer

You can use fakeroot without actually having root privileges. If you had su and/or sudo you would be able to destroy your system with a simple rm -rf /, but with fakeroot at most you would remove your home directory.

share|improve this answer
    
That doesn't explain the need for fakeroot. You can remove your home directory as yourself. –  JMCF125 Sep 16 '13 at 13:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.