【问题标题】:pm2 - Startup script for non-root user permission deniedpm2 - 非 root 用户权限的启动脚本被拒绝
【发布时间】:2021-11-19 02:38:35
【问题描述】:

我刚刚安装了 pm2(在 Debian 9 上),我需要运行少数用户的 NodeJS 服务器。每个用户都有自己的 NodeJS 服务器,我需要在它们下运行。不在根目录下。

例如我有用户“user1”:

  • 我可以正常控制pm2
  • user1 上的此命令用于启用启动脚本: pm2 startup
  • 然后我将输出的命令复制粘贴到 root 并执行...
  • 现在为给定用户创建启动脚本

pm2 仍然可以在 user1 上运行,但重启后我无法控制 pm2 并且我收到此错误: [PM2][ERROR] Permission denied, to give access to current user: $ sudo chown user1:user1 /home/user1/.pm2/rpc.sock /home/user1/.pm2/pub.sock

如果我在 root 上执行此命令,我可以再次控制 user1 上的 pm2,但所有 NodeJS 服务器都以 root 身份执行!

每次重启我都需要“授予”权限..

如何解决这个问题?

【问题讨论】:

    标签: node.js root pm2


    【解决方案1】:

    我不确定以下方法是否存在任何风险,但它确实解决了我面临的同样问题。

    1- open new terminal (fresh, without sudo permission)
    2- sudo chmod -R 777 /home/user1/.pm2 (gives permission to the .pm2 folder)
    3- pm2 start index.js (Note that there is no sudo here)
    4- pm2 save
    5- pm2 startup
    6- Restart your computer and check pm2 monit
    7- It should list your index process now. If not, try from 1-5, 8, and then 6.
    8- paste the command listed in step 5
    

    【讨论】:

    • 为什么像这样的每个问题最终都会有人建议向服务器添加一揽子 777 权限...
    【解决方案2】:

    我自己发现了为什么它不起作用。我的用户名带有点 (domain.com) 并且 systemd 没有将其识别为有效的用户名...因为该服务是在 root 下启动的。

    快速解决方法是在 systemd 中找到自动生成的服务,并将用户名替换为 id -u <user> 找到的用户 ID

    【讨论】:

      【解决方案3】:

      您可能使用 root 启动了 pm2 任务。因此,请通过以下方式检查您正在使用的系统进程:

      sudo systemctl -a
      

      如果您找到一个单位名称 pm2-root,那就是您需要删除的单位名称。运行命令sudo systemctl stop pm2-root 和sudo systemctl disable pm2-root。然后使用您的用户名开始该过程。

      确保您使用 pm2-sammy 并以 sammy 作为您的用户名。

      【讨论】:

        【解决方案4】:

        尝试使用sudo pm2 <command>

        【讨论】:

          猜你喜欢
          • 2020-05-12
          • 2022-01-08
          • 1970-01-01
          • 1970-01-01
          • 2017-03-27
          • 1970-01-01
          • 2012-10-20
          • 2011-08-14
          • 1970-01-01
          相关资源
          最近更新 更多