Is there a way for non-root processes to bind to “privileged” ports on Linux?

后端 未结 24 1545
予麋鹿
予麋鹿 2020-11-22 02:04

It\'s very annoying to have this limitation on my development box, when there won\'t ever be any users other than me.

I\'m aware of the standard workarounds, but non

24条回答
  •  醉梦人生
    2020-11-22 03:03

    File capabilities are not ideal, because they can break after a package update.

    The ideal solution, IMHO, should be an ability to create a shell with inheritable CAP_NET_BIND_SERVICE set.

    Here's a somewhat convoluted way to do this:

    sg $DAEMONUSER "capsh --keep=1 --uid=`id -u $DAEMONUSER` \
         --caps='cap_net_bind_service+pei' -- \
         YOUR_COMMAND_GOES_HERE"
    

    capsh utility can be found in libcap2-bin package in Debian/Ubuntu distributions. Here's what goes on:

    • sg changes effective group ID to that of the daemon user. This is necessary because capsh leaves GID unchanged and we definitely do not want it.
    • Sets bit 'keep capabilities on UID change'.
    • Changes UID to $DAEMONUSER
    • Drops all caps (at this moment all caps are still present because of --keep=1), except inheritable cap_net_bind_service
    • Executes your command ('--' is a separator)

    The result is a process with specified user and group, and cap_net_bind_service privileges.

    As an example, a line from ejabberd startup script:

    sg $EJABBERDUSER "capsh --keep=1 --uid=`id -u $EJABBERDUSER` --caps='cap_net_bind_service+pei' -- $EJABBERD --noshell -detached"
    

提交回复
热议问题