Start app as root with pm2
Asked Answered
N

7

34

I have a daemon that must be run as root on startup.

I use pm2 to start other apps but can not figure out if it can start an app as root. Can it be done?

If not, what are my options?

Noami answered 30/1, 2016 at 17:57 Comment(0)
O
44

I had problems with sudo pm2 start api, but this was since pm2 was already running without sudo privileges, therefor you need to run:

pm2 kill
sudo pm2 start api

This kills the pm2 deamon first, so that it starts in sudo, but then you need sudo for ALL pm2 commands afterwards, like: sudo pm2 ls

Orthogenetic answered 9/2, 2018 at 14:10 Comment(1)
Perfect! I had pm2 running as ubuntu user, once I ran these commands my instance of pm2 is now running as root as needed to run https on my node server using port 3000.Mecke
D
29

If you only need your daemon to be run as root in order to access a port number (such as 80 or 443), the pm2 documentation recommends using authbind. So, if you want the user yourusername to have access to port 80, run:

$ sudo apt-get install authbind
$ sudo touch /etc/authbind/byport/80
$ sudo chown yourusername /etc/authbind/byport/80
$ sudo chmod 755 /etc/authbind/byport/80
$ authbind --deep pm2 update

And then use authbind --deep pm2 instead of pm2. The documentation suggests setting up an alias.

Dispenser answered 14/2, 2017 at 19:35 Comment(2)
are you assuming linux?Disciplinary
Yes, and apt-get is only available on distributions based on Debian.Dispenser
M
15

I would recommend:

sudo pm2 start index.js

OR

pm2 start 'http-server' /var/www -p 80
sudo pm2 startup
pm2 save

OR

pm2 start 'which http-server' /var/www -p 80

To start it on your HTTP Port

Also, I always put -i 0 at the end - this starts up as many worker processes as you have cores. Check THIS

It is not always necessary to start PM2 as root. If you have PM2 as root and the cli module installed, security is a big risk. This is only required if you're starting your app on a port between 1 and 1024

Maltose answered 30/1, 2016 at 18:27 Comment(0)
S
7

Wasted about an hour

On AWS EC2 machine, one system was in inconsistent state due to earlier installations, that forced sudo elevations in the application for all commands to OS, like sh, etc.

pm2 was running as root:

ps aux | grep pm2
# root ... PM2 v4.1.2: God Daemon (/root/.pm2)

Now pm2 is running as ubuntu:

ps aux | grep pm2
# ubuntu  ...  PM2 v4.1.2: God Daemon (/home/ubuntu/.pm2)

Below commands worked:

sudo pm2 kill
sudo npm remove pm2 -g

sudo npm i -g pm2@latest
sudo pm2 update
sudo chown -R ubuntu:ubuntu /home/ubuntu/.pm2

Hope that helps

Staggs answered 1/2, 2020 at 4:40 Comment(0)
P
0

You should start pm2 as a root, (sudo pm2 start app.js), then your app will start as a root

Poll answered 30/1, 2016 at 18:17 Comment(1)
This does work but it seems to require that I run the other apps as root aswell.Noami
S
0

First, install pm2 globally. Then set root permissions to pm2 using this command

sudo chown ubuntu:ubuntu /home/ubuntu/.pm2/rpc.sock /home/ubuntu/.pm2/pub.sock
Southwesterly answered 28/2, 2018 at 7:23 Comment(2)
Why are you passing in ubuntu:ubuntu? In a different distribution would it be root:user_to_give_root_access?Danas
I used an ec2 instance in aws and I logged as ubuntu user, for a root user your answer will work. I just gave an example of how it works. Thank you for pointing out.Southwesterly
A
0

you might consider routing your traffic with iptables, since there is a reason behind this errror

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
Anserine answered 16/3, 2019 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.