# Force a command to be run as normal user

**URL:** <https://forum.crystal-lang.org/t/force-a-command-to-be-run-as-normal-user/6970>\
**Category:** Help & Support\
**Created:** [June 26, 2024, 6:35pm UTC](https://forum.crystal-lang.org/t/force-a-command-to-be-run-as-normal-user/6970 "2024-06-26T18:35:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 26, 2024, 6:35pm UTC](https://forum.crystal-lang.org/t/force-a-command-to-be-run-as-normal-user/6970/1 "2024-06-26T18:35:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Xen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/xen/32/2288_2.png) [@Xen](https://forum.crystal-lang.org/u/Xen)\
**Post date:** [June 26, 2024, 9:00pm UTC](https://forum.crystal-lang.org/t/force-a-command-to-be-run-as-normal-user/6970/2 "2024-06-26T21:00:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 27, 2024, 7:52am UTC](https://forum.crystal-lang.org/t/force-a-command-to-be-run-as-normal-user/6970/3 "2024-06-27T07:52:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Xen](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/xen/32/2288_2.png) [@Xen](https://forum.crystal-lang.org/u/Xen)\
**Post date:** [June 27, 2024, 9:48am UTC](https://forum.crystal-lang.org/t/force-a-command-to-be-run-as-normal-user/6970/4 "2024-06-27T09:48:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 27, 2024, 6:31pm UTC](https://forum.crystal-lang.org/t/force-a-command-to-be-run-as-normal-user/6970/6 "2024-06-27T18:31:05Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![luislavena](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/luislavena/32/572_2.png) [@luislavena](https://forum.crystal-lang.org/u/luislavena)\
**Post date:** [June 27, 2024, 9:41pm UTC](https://forum.crystal-lang.org/t/force-a-command-to-be-run-as-normal-user/6970/7 "2024-06-27T21:41:54Z")

</div>

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:

> <https://github.com/crystal-lang/crystal/issues/7829>
>
> There have been some PR's adding features for querying and changing the user and… group of a process, originally as part of #5615 then it's reboot #5627 and now there's #7822, which is a completely new approach.
> 
> We've had discussions about the design of these APIs directly in the PRs, but I'd like to discuss the general design in separate and focus the PR on the implementation.
> 
> From #5615
> \`\`\`cr
> Process.become(user : User)
> Process.become(group : Group)
> Process.user : User
> Process.group : Group
> \`\`\`
> 
> From #5627
> \`\`\`cr
> Process.owner=(user : System::User)
> Process.group=(group : System::Group)
> Process.user : System::User
> Process.group : System::Group
> \`\`\`
> 
> From #7822
> \`\`\`cr
> Process.uid=(uid : LibC::UidT)
> Process.uid : LibC::UidT
> Process.euid : LibC::UidT
> Process.gid=(gid : LibC::GidT)
> Process.gid : LibC::GidT
> Process.egid : LibC::GidT
> Process.setresuid(uid : LibC::UidT)
> Process.setresgid(gid : LibC::GidT)
> \`\`\`
> 
> The most important difference between these proposals (besides varying feature support) is clearly whether they work on plain uid/gid or on \`System::User\`/\`System::Group\` instances (see #7738 for that API). Using \`LibC::UidT\`/\`LibC::GidT\` is obviously not portable to non-POSIX platforms, but returning \`Int32\` (or \`String\`) could work. Int values are used for example in the \[Ruby\](https://ruby-doc.org/core-2.6.1/Process.html) and \[Golang\](https://golang.org/pkg/os/#Getuid) implementations.
> 
> Both approaches are good. Maybe we could even want both, I don't know.
> 
> It seems pretty difficult to create a portable interface, that can work for non-POSIX platforms. Most API's I've seen only support POSIX and more ore less closely resemble the POSIX calls.
> So, ideas for cross-platform implementation would be great =)

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.

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [June 27, 2024, 11:23pm UTC](https://forum.crystal-lang.org/t/force-a-command-to-be-run-as-normal-user/6970/8 "2024-06-27T23:23:47Z")

</div>

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

```crystal
LibC.setuid 123

```
