Repository navigation
alias node for sudoers #43
Description
Activity
Very neat idea. I don't want to add it to my nvm repo since it's a little invasive, but it's not hard for someone who wants to customize their environment.
I usually just source the nvm.sh file as root after running "sudo su" or modify root's .bashrc. I want more than just node when I run sudo and this way I get everything in the $NVM_BIN folder.
Reacted by Dan Levy, Cyrus Frost and donotlbYou are right, it might be a bit invasive and probably shouldn't be active in the release, but maybe just have it posted here or somewhere in future docs, so that people could find it easy.
Or maybe it might be better to have it in the script, but commented out, and people who need it, would just uncomment it - just a thought.It took me a bit to figure this out, so probably this could save some time for others.
Modifying root's bashrc wasn't an option for my case, since we had a set-up with several users, each with its own node/nvm, and wanted to allow quick running through sudo (sudo node app_name), where necessary.
Including nvm.sh or the path/s in root's .bashrc would allow only for one single setup to run through sudo.This is fantastic, I made the changes and it works like a charm.
What if I wanted to do something like
sudo ENV1=val1 ENV2=val2 node server.js?I am able to do that with other tools (I remember that forever never had an issue with it), however in my nvm-ized environment, I get
sudo: node: command not found.Any help would be much appreciated!
Does "nvm install" need sudo to run error-free?
Reacted by AndriiHlukhovskyi and arbabnadirbahijaan-debug@zakdances no, nvm installs node into a local folder inside your git clone of the nvm repo. Unless your user doesn't have write access to your own clone, then it should work fine without sudo. In fact, using sudo can break things since it creates files as the root user.
Reacted by AndriiHlukhovskyi and sonya60Hm, just stumbled upon this one when trying to install grunt globally.
Like @mpotra suggested, I had to also alias
npmlike so:
alias node='$NVM_BIN/node'
alias npm='$NVM_BIN/npm'So that means that not a single node package intended for global availablity can be installed when using nvm by default, right? That should make quite some users having this scenario, I imagine.
How about inventing a special nvm mode so the aliases only get activated when users explicitly enter that mode?
When they leave the mode again, the aliases could get disabled.Reacted by Joel Douglass and MEBTTEReacted by AndriiHlukhovskyiThis seems common enough that it should be added to the readme.
Reacted by Stephen Tetreault, Olivier Peltre and AlexanderReacted by AndriiHlukhovskyiYes, needing sudo on *nix systtems is very common.
As a suggestion, you could try a solution similar to what ruby version manager does http://rvm.io/integration/sudo
Reacted by Stephen Tetreault and AndriiHlukhovskyiI was just told in the git mailing list that you should only very rarely use sudo. But I see it all the time. Too many opinions about it.
Reacted by AndriiHlukhovskyiUsing
sudoto install node is perfectly reasonable and common. Running node itself as root is frowned upon. Unfortunatelynvmmakes this very difficult.Reacted by AndriiHlukhovskyi and Osama ShegemIf anyone is looking for an easy way to install nvm globally: https://github.com/xtuple/nvm
Reacted by AndriiHlukhovskyiThere is no need to install node with sudo once you give the
nvmdirectory the right permissions.
For example:
create a user group callednvm, add every user that need to use nvm to this group and change the permissions of your nvm dir to group writeable:chmod 2775 /usr/local/nvm(the first digit (2) is thesetgidbit, explained here, which ensures the group for execution in this directory stays the same)Reacted by Alexander Gomez+1 for nvmsudo solution proposed by @treehau5
Reacted by AndriiHlukhovskyi+1 for @koenpunt point on group access.
Reacted by AndriiHlukhovskyi10 remaining items
@mightypenguin
nvmis not meant to be used across user accounts. Each user should have its own$NVM_DIR.Reacted by Oskar JaskólskiSo even if I wrote the code to make this cleanly possible there is no way you would consider pulling my changes?
Reacted by Stephen TetreaultReacted by Ilia Baryshnikov@mightypenguin i'd be happy to take a look at it, but I'm very skeptical it could be cleanly possible due to all the permissions issues within nvm-managed node dirs.
@tetreault you should never use
sudofor privileged ports; https://thomashunter.name/blog/using-authbind-with-node-js/ is a much safer approach.Reacted by Stephen Tetreault, Chris Crujedo and Oskar JaskólskiI believe
npm runruns things as nobody, and it's unable tosudo(for safety).on debian (or ubuntu) based sytem in addition to setting the alias as @mpotra said you might need to run
sudo ln -s "$(which node)" /usr/local/bin/nodefor npm to work in sudo modeReacted by Greg Price, Mario Jankovic, gitlun, André Paiva, Pedro Lucas Oliva Braz, Craig McInroy, Oleg Charnyshevich, Timo Bubigo, Kevin Song and AnatoliiReacted by Greg Price, André Paiva and WestermeisterReacted by Leonardo Melo, Oskar Jaskólski, André Paiva and Vikasauthbind is great and all, but what about things like raw-socket? I use net-ping which pulls in raw-socket which can only work as root. Would some libcap thing work to bypass the "haxor crafty packet injection" usermode block?
"can only work as root" seems like a reason to not use a piece of software, tbh.
Reacted by Matt LarsonSo it appears that setting the CAP_NET_RAW for the node executable will open just a big enough hole for raw-socket to operate without root, maybe, but I"m skeptical (untested because I'm running it as root because I'm not paranoid).
sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)"Microsoft" is also a reason not to use a piece of software, but lots of poor souls use that, because they have no choice. I'd love to go fix everyone elses' software but sometimes you just need root to solve headaches and get it working NOW and in ALL CASES ensured.
And if it's just plain simpler to run as root and I accept the ramifications of doing so, it should be trivial. Maybe even a supported method to drop privs (like Apache httpd does?) so the whole process doesn't have to run as root (just the socket thread). And anyway this applet literally can't be exploited / never heard of a privilege escalation problem with Node.js / why be so paranoid when it is clearly unnecessary? Best practice is not the only practice and it's never best for every use case... such as on a VM where yippee you got root and can't do anything, gg hacker bruh.
At any rate, the way npm works is beyond the scope of this repo.
If you want
nvmto work for a root user, then that root user needs to have its own installation ofnvm.on debian (or ubuntu) based sytem in addition to setting the alias as @mpotra said you might need to run
sudo ln -s "$(which node)" /usr/local/bin/nodefor npm to work in sudo modesudo ln -s "$(which node)" /usr/local/bin/node
outputs
ln: failed to create symbolic link '/usr/local/bin/node': File existsI wanted to run some old-legacy sdk. This
sdkshould use old node v6.
Following command did not work for me:sudo somesdkCommand 'node' not foundAs suggested I switched to sudo:
sudo -sThen I read nvm.sh for current user (e.g.
someuser):source /home/someuser/.nvm/nvm.shAfter this I have
nvmfor sudo (for current session, which is enough for me)root@domain:~# nvm Node Version Manager (v0.35.2)Here is an ad-hoc workaround you may want to use to run commands temporarily:
$ sudo PATH=$PATH bash -c "node ..."
For instance:
$ sudo PATH=$PATH bash -c "which node npm pnpm" /home/alex/.nvm/versions/node/v13.10.1/bin/node /home/alex/.nvm/versions/node/v13.10.1/bin/npm /home/alex/.nvm/versions/node/v13.10.1/bin/pnpm
Reacted by mohammadreza berneti and Alexander Gomezsudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node" sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"Source - https://stackoverflow.com/questions/21215059/cant-use-nvm-from-root-or-sudo
Reacted by Arnaud, Skrynnikov Andrey, Inverse, br3ntor, aderchox and accplanAh this was really a pain as I thought the issue was with my script and wasted a few hours on it 😪
Reacted by Alexander Gomez
When sudoing node, 'command not found' is issued, because on most systems PATH is reset on sudo, for security reasons.
This makes 'sudo node' not work anymore.
Simply adding the following line at the end of the nvm.sh script, would fix it:
alias node='$NVM_BIN/node'
//possibly adding an alias for npm as well?
This assumes .bashrc for user contains these 3 lines:
. ~/.nvm/nvm.sh
enable aliases in sudo
alias sudo='sudo '