nvm-sh / nvm

Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions
MIT License
78.44k stars 7.86k forks source link

File Permissions Issue when running nvm as root #2918

Open sjoudry opened 1 year ago

sjoudry commented 1 year ago

Operating system and version:

Running in docker - Ubuntu 20.04. I've added the Dockerfile and docker-compose.yml file at the end of the issue summary.

nvm debug output:

```sh # nvm debug nvm --version: v0.39.2 $SHELL: /bin/bash $SHLVL: 1 whoami: 'root' ${HOME}: /root ${NVM_DIR}: '${HOME}/.nvm' ${PATH}: ${NVM_DIR}/versions/node/v16.18.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin $PREFIX: '' ${NPM_CONFIG_PREFIX}: '' $NVM_NODEJS_ORG_MIRROR: '' $NVM_IOJS_ORG_MIRROR: '' shell version: 'GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)' uname -a: 'Linux 5.10.124-linuxkit #1 SMP Thu Jun 30 08:19:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux' checksum binary: 'sha256sum' OS version: Ubuntu 20.04.5 LTS curl: not found wget: /usr/bin/wget, GNU Wget 1.20.3 built on linux-gnu. git: not found grep: /usr/bin/grep (grep --color=auto), grep (GNU grep) 3.4 awk: /usr/bin/awk, awk: not an option: --version sed: /usr/bin/sed, sed (GNU sed) 4.7 cut: /usr/bin/cut, cut (GNU coreutils) 8.30 basename: /usr/bin/basename, basename (GNU coreutils) 8.30 rm: /usr/bin/rm, rm (GNU coreutils) 8.30 mkdir: /usr/bin/mkdir, mkdir (GNU coreutils) 8.30 xargs: /usr/bin/xargs, xargs (GNU findutils) 4.7.0 nvm current: v16.18.0 which node: ${NVM_DIR}/versions/node/v16.18.0/bin/node which iojs: which npm: ${NVM_DIR}/versions/node/v16.18.0/bin/npm npm config get prefix: ${NVM_DIR}/versions/node/v16.18.0 npm root -g: ${NVM_DIR}/versions/node/v16.18.0/lib/node_modules ```

nvm ls output:

```sh # nvm ls -> v16.18.0 default -> 16.18.0 (-> v16.18.0) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v16.18.0) (default) stable -> 16.18 (-> v16.18.0) (default) lts/* -> lts/gallium (-> v16.18.0) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.12 (-> N/A) lts/fermium -> v14.20.1 (-> N/A) lts/gallium -> v16.18.0 ```

How did you install nvm?

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

What steps did you perform?

I have a docker container running Ubuntu 20.04 and nvm (using the command above). I have started with a blank slate and ran these commands:

And then inside the container (I have an .nvmrc file with v16.18.0 as the version):

What happened?

The npm install failed:

# npm install -g gatsby-cli
npm WARN deprecated @types/keyv@4.2.0: This is a stub types definition. keyv provides its own type definitions, so you do not need this installed.
npm ERR! code 127
npm ERR! path /root/.nvm/versions/node/v16.18.0/lib/node_modules/gatsby-cli/node_modules/lmdb
npm ERR! command failed
npm ERR! command sh -c -- node-gyp-build-optional-packages
npm ERR! sh: 1: node-gyp-build-optional-packages: Permission denied

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-10-19T20_07_07_702Z-debug-0.log

What did you expect to happen?

I expected the gatsby-cli module and all dependencies to be installed successfully.

Is there anything in any of your profile files that modifies the PATH?

No.

Docker information

The contents of my Dockerfile:

# https://hub.docker.com/_/ubuntu
FROM ubuntu:20.04

# Prevent TZ data interaction.
ENV TZ=America/Toronto
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install packages.
RUN apt-get update &&  \
  apt-get install -y software-properties-common wget vim zip make gcc
RUN apt-get upgrade -y

# Install dependencies.
RUN wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

# Create working directories.
RUN mkdir -p /app
WORKDIR /app

# Prevent the container from stopping (This is a workaround). See
# https://stackoverflow.com/questions/30209776/docker-container-will-automatically-stop-after-docker-run-d
ENTRYPOINT ["tail"]
CMD ["-f","/dev/null"]

The contents of my docker-compose.yml file:

services:
  node:
    container_name: test-node
    build:
      context: .
      dockerfile: ./docker/node/Dockerfile
    env_file: .env

Workaround/Root Cause

When nvm installs node, the permissions are maintained from the tarball and this is causing the problem:

# pwd
/root/.nvm/versions/node/v16.18.0

# ls -al
total 828
drwxr-xr-x 6 root root   4096 Oct 19 16:06 ./
drwxr-xr-x 3 root root   4096 Oct 19 16:06 ../
-rw-r--r-- 1 1001 1001 692851 Oct 12 08:00 CHANGELOG.md
-rw-r--r-- 1 1001 1001  88740 Oct 12 08:00 LICENSE
-rw-r--r-- 1 1001 1001  35345 Oct 12 08:00 README.md
drwxr-xr-x 2 1001 1001   4096 Oct 19 16:07 bin/
drwxr-xr-x 3 1001 1001   4096 Oct 12 08:00 include/
drwxr-xr-x 3 1001 1001   4096 Oct 12 08:00 lib/
drwxr-xr-x 4 1001 1001   4096 Oct 12 08:00 share/

Running chown before npm install fixes the issue:

However, since nvm is managing node, it should also be managing the permissions.

ljharb commented 1 year ago

I would expect that to work as well. (Note that there is an nvm dockerfile you could try as well)

What's the ownership of $HOME and $NVM_DIR look like?

sjoudry commented 1 year ago

The directories are both owned by root:

# ls -al /
total 72
drwxr-xr-x   1 root root 4096 Oct 20 17:00 ./
drwxr-xr-x   1 root root 4096 Oct 20 17:00 ../
-rwxr-xr-x   1 root root    0 Oct 20 17:00 .dockerenv*
drwxr-xr-x   8 root root  256 Oct 20 09:50 app/
lrwxrwxrwx   1 root root    7 Sep 22 12:47 bin -> usr/bin/
drwxr-xr-x   2 root root 4096 Apr 15  2020 boot/
drwxr-xr-x   5 root root  340 Oct 21 09:20 dev/
drwxr-xr-x   1 root root 4096 Oct 20 17:00 etc/
drwxr-xr-x   2 root root 4096 Apr 15  2020 home/
lrwxrwxrwx   1 root root    7 Sep 22 12:47 lib -> usr/lib/
lrwxrwxrwx   1 root root    9 Sep 22 12:47 lib32 -> usr/lib32/
lrwxrwxrwx   1 root root    9 Sep 22 12:47 lib64 -> usr/lib64/
lrwxrwxrwx   1 root root   10 Sep 22 12:47 libx32 -> usr/libx32/
drwxr-xr-x   2 root root 4096 Sep 22 12:47 media/
drwxr-xr-x   2 root root 4096 Sep 22 12:47 mnt/
drwxr-xr-x   2 root root 4096 Sep 22 12:47 opt/
dr-xr-xr-x 212 root root    0 Oct 21 09:20 proc/
drwx------   1 root root 4096 Oct 20 17:23 root/
drwxr-xr-x   1 root root 4096 Oct 20 09:32 run/
lrwxrwxrwx   1 root root    8 Sep 22 12:47 sbin -> usr/sbin/
drwxr-xr-x   2 root root 4096 Sep 22 12:47 srv/
drwxr-xr-x  11 root root  352 Oct 14 10:20 ssl/
dr-xr-xr-x  13 root root    0 Oct 21 09:20 sys/
drwxrwxrwt   1 root root 4096 Oct 20 17:21 tmp/
drwxr-xr-x   1 root root 4096 Oct 20 09:32 usr/
drwxr-xr-x   1 root root 4096 Sep 22 12:50 var/

# ls -al /root
total 44
drwx------ 1 root root 4096 Oct 20 17:23 ./
drwxr-xr-x 1 root root 4096 Oct 20 17:00 ../
-rw-r--r-- 1 root root 3303 Oct 20 09:32 .bashrc
-rw------- 1 root root    0 Oct 20 17:23 .node_repl_history
drwxr-xr-x 4 root root 4096 Oct 20 17:11 .npm/
drwxr-xr-x 8 root root 4096 Oct 20 17:11 .nvm/
-rw-r--r-- 1 root root  161 Dec  5  2019 .profile
-rw------- 1 root root 8708 Oct 20 17:23 .viminfo
-rw-r--r-- 1 root root  241 Oct 20 17:21 .wget-hsts
PorcoRosso85 commented 1 year ago

I encount same issue, too. And ownership is also the same.

williamfzc commented 11 months ago

Same here. Really weird.