You are viewing a plain text version of this content. The canonical link for it is here.
Posted to docs@httpd.apache.org by Apache Wiki <wi...@apache.org> on 2011/04/29 16:36:56 UTC

[Httpd Wiki] Update of "NonRootPortBinding" by FrancisGaliegue

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Httpd Wiki" for change notification.

The "NonRootPortBinding" page has been changed by FrancisGaliegue.
http://wiki.apache.org/httpd/NonRootPortBinding

--------------------------------------------------

New page:
== What you need ==

You need two things:

 * an operating system with POSIX capabilities;
 * utilities to manipulate these capabilities.

Here, we will use:

 * Linux - any distribution will do, as long as it has a 2.6.24 kernel compiled with POSIX capabilities (so beware if you use a custom kernel);
 * the setcap(8) and getcap(8) utilities.

We suppose that you have Apache installed, and that the httpd binary is {{{/usr/sbin/httpd}}}. Some distributions put it in another location (Debian, for instance, uses {{{/usr/sbin/apache2}}}).

== First step: add capabilities to the httpd binary ==

The capabilities are added per file. This is why we need to modify the httpd binary itself. The capability we need to add is {{{CAP_NET_BIND_SERVICE}}}, which is explicitly defined as the capacity for an executable to bind to a port less than 1024.

You need to be root to do that, so first, be root. Then, add the capability to the httpd binary:

{{{
setcap cap_net_bind_service=+ep /usr/sbin/httpd
}}}

Check that the capability is added:

{{{
root@myhost # getcap /usr/sbin/httpd
/usr/sbin/httpd = cap_net_bind_service+ep
}}}

== Second step: preparing your environment ==

NOTE: this section assumes a Fedora, Red Hat or derivate distribution. Adapt instructions below accordingly.

Login as root. Choose a user with which you want to run Apache (create one if needed). It can be a system only user (ie, no shell), but for testing purposes:

 * we will use a regular user, named {{{test}}},
 * we will use the bundled package configuration.

Start by ensuring that httpd is NOT currently running ({{{service httpd status}}}). Then copy over the system apache configuration to the user's home (we use it since it binds to port 80 by default, which is what we want to test):

{{{
root@myhost # cp -a /etc/httpd ~test/
chown -R test ~test/httpd
}}}

Then login as {{{test}}}. Modify the environment:

{{{
# logs and run are symlinks to directories the test user cannot write to, remove the symlinks and create directories instead
test@myhost $ cd httpd
test@myhost $ rm -f logs run
test@myhost $ mkdir logs run
}}}

Now, test that you can run it:
{{{
test@myhost $ pwd
/home/test/httpd
test@myhost $ httpd -d $(pwd) -DNO_DETACH
}}}

Apache will then run in the foreground. If it quits immediately, check the console output, or {{{logs/error_log}}}: fix errors (very probably a permission problem) and try again.

You're done!

== Reverting ==

Use {{{setcap}}} again, to remove the capability:

{{{
root@myhost # setcap cap_net_bind_service=-ep /usr/sbin/httpd
}}}

== Caveats ==

 * with this setup, _any_ nonprivileged user can now run Apache on privileged ports. So, be very careful about what you do. Additionally, you can further restrict execution of the httpd binary, either using standard credentials ({{{chmod}}}, {{{chown}}} et al) or, even better, ACLs;
 * if you upgrade Apache, changes you have made to httpd will be lost, you'll need to do them again...

---------------------------------------------------------------------
To unsubscribe, e-mail: docs-unsubscribe@httpd.apache.org
For additional commands, e-mail: docs-help@httpd.apache.org