27 05 | 2011

Wrapper to use su as sudo (or sudo as su)

Written by Tanguy

Classified in : Homepage, Debian, Command line

The problem

Sudo logo: a shield with a hash sign, a lightning and SUDO written on

Some programs require root privileges for part of their work: most notably Debian building tools such as dpkg-buildpackage or pdebuild. So, several command can be used to obtain real or fake root privileges: su, sudo, fakeroot, etc. Among them, su is quite distinct, because it only takes a quoted command to pass it to a shell. That is why its interface is completely incompatible with the others':

$ su -c "pbuilder --build package.dsc"
$ sudo pbuild --build package.dsc

This is annoying when real root privileges are needed, for instance by pbuilder, a tool to build Debian packages inside a minimal chroot. Indeed, in such a case I only know one command to obtain the root privileges, sudo, and some people (read: me) prefer using su.

A wrapper

Long story short: as I saw no existing tool to do that (but I would be glad to hear if there was already one), I wrote a wrapper that emulates a (very basic) sudo-like interface by using su, and vice-versa. It is available from my Git repository:

$ git clone git://git.ortolo.eu/sudo-su.git

It is polymorphic, BusyBox-style: call it sudo-su and it will emulate sudo with su; call it su-sudo and it will emulate su with sudo. For instance:

$ pdebuild --pbuilderroot /usr/local/bin/sudo-su

It is WTFPL-licensed, it implements no options from sudo and su (except sudo's -c, of course), but I can do something better if it may be useful to other people.


saturday 28 may 2011 à 02:55 Ben Armstrong said : #1

While we're on the topic of sudo & su wrappers, although it does not solve the exact problem your wrapper does, you may be interested in su-to-root in the 'menu' package.

saturday 28 may 2011 à 10:11 KiBi said : #2

dpkg-buildpackage doesn't require root privileges.

saturday 28 may 2011 à 11:11 Elessar said : #3

@KiBi: Doesn't it? Well, it has a -rgain-root-command option, that might exist for something (and its manpage also explains why su is not suitable, by the way). I have some debian/rules that chown files, which can only done by root, I think. Or faked with fakeroot; this is what I usually do.

Anyway, pbuilder still requires real root privileges, and its wrapper pdebuild has an option to specify which command to use: in fact it is for pdebuild that I wrote my wrapper.

friday 03 june 2011 à 11:47 mirabilos said : #4

Oh, duh.

sudo sh -c "…"

But people who use that are as “stayed behind on the short bus” as NetBSD® pkgsrc®.

sunday 05 june 2011 à 19:07 Tanguy said : #5

@mirabilos: I do not understand you last sentence. :-(

But I sued sudo sh -c because this is what su does, so to mimic su's behaviour with sudo, this is the only solution I think.

Write a comment

What is the fourth letter of the word vnpeiy? :