How to run a command in a chroot jail not as root and without sudo?

后端 未结 5 507
北海茫月
北海茫月 2020-12-07 22:56

I\'m setting up a minimal chroot and want to avoid having sudo or su in it but still run my processes as non-root. This is a bit of a trick as running chroot requiers root.

5条回答
  •  遥遥无期
    2020-12-08 00:02

    You can make use of linux capabilities to give your binary the ability to call chroot() w/o being root. As an example, you can do this to the chroot binary. As non-root, normally you'd get this:

    $ chroot /tmp/
    chroot: cannot change root directory to /tmp/: Operation not permitted
    

    But after you run the setcap command:

    sudo setcap cap_sys_chroot+ep /usr/sbin/chroot 
    

    It will let you do the chroot call.

    I don't recommend you do this to the system's chroot, that you instead do it to your own program and call chroot. That way you have more control over what is happening, and you can even drop the cap_sys_chroot privilege after you call it, so successive calls to chroot in your program will fail.

提交回复
热议问题