Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable sudo/root privilege from node.js script

I need in my node.js application sudo privileges to run server on port 80, or run some others startup administration stuff. But when I will do that I don't want this privileges anymore because then I will run some external libraries from node_modules.

So, how can I disable my administration privileges from node.js script?

like image 201
Tunga Avatar asked Dec 21 '25 07:12

Tunga


1 Answers

You need to use:

process.setgid('somegroup');
process.setuid('someuser');

See the docs:

  • https://nodejs.org/api/process.html#process_process_setgid_id
  • https://nodejs.org/api/process.html#process_process_setuid_id

See this article for more info:

  • https://thomashunter.name/blog/drop-root-privileges-in-node-js/

Other options

Another way to bind to low ports without running as root would be to give a capability to bind to low ports if your system supports it - using e.g. CAP_NET_BIND_SERVICE on Linux with something like:

sudo setcap 'cap_net_bind_service=+ep' /path/to/your/program

That way you will not have to run it as root and you will not have to change to a different user later.

like image 158
rsp Avatar answered Dec 23 '25 22:12

rsp