Force a command to be run as normal user

Hello guys, I have a question , is there any option for Process.run to be run as a normal user , even the crystal code was call by root ?

Because I did a function normally suppose to be run as root, but in few case, I need when that function call Process .run to run it as normal user.

A quick dip in the man pages suggests running runuser -u <user> -- <command>. Making Crystal drop privileges is probably involved and pretty low level.

1 Like

I find another way, but I have still one question. Is it possible to get the current logged user ?

id -u to get the uid of the user, id -nu to get the name.

So I think I need to do in another way. I am facing a problem actually.

Normally all of my command called by my coded package manager , or most of them are supposed to be called as root.

The problem I am facing recently is when the time come ti install libre office … I don’t know why , this is the only package require to be build as normal user …

Any suggestions ?

Normally in C and Linux, you would leverage on something like setuid(2) and setgid(2) to demote your current user (root) to run something as another one.

There is an old proposal on this subject from 2019 in GitHub:

While none of that has landed in Crystal yet, perhaps you can take a look to one of the PRs and use that code part of your project.

I hope that helps.

Cheers.

Until a higher-level API lands in Crystal, you can use the libc version:

LibC.setuid 123