skip to Main Content

RPi Node-Red: automatically running node-red, as root

This tutorial shows you how to have node-red start and run as root instead of a normal user.

This is necessary for the RGB Matrix node: node-red-contrib-easybotics-led-matrix

Open a terminal and paste in the following lines:

sudo systemctl enable nodered.service
sudo npm config set unsafe-perm true

Now Node-Red will start automatically when the Raspberry Pi boots up.
The second line tells npm that we intended to run it as root; and that its okay to install nodes in protected directories.

The systemd service that we enabled is really just a type of script that lives in a file somewhere.

So if we want to change the user that runs node-red, we need to edit the file, this can be done with this fancy one liner:

d=/lib/systemd/system/nodered.service && sudo sed "s/User=pi/User=root/;s/Group=pi/Group=root/" $d > tmp && sudo mv -f tmp $d

When pasted into the terminal and ran, this bash expression will edit the nodered.service file and change the user to ‘root’.

If you have flows and nodes that you wish to keep, you can use this command to edit your node-red settings to keep using your old home-directory:
But be warned, this one line will only work after node-red has run atleast once as root; seeing as this is when the settings file it is editing is created.
An easy way to ensure this is to reboot after doing the one liner above, and then run the one liner below; and then finally reboot again.

d=/root/.node-red/settings.js && sudo sed "/.*userDir:*./c\userDir: '\/home\/pi\/.node-red\/'," $d > tmp && sudo mv -f tmp $d

Some nodes, such as the led-matrix require the soundcard to be disaabled
there’s a command for that too:

d=/boot/config.txt && sudo sed "/.*dtparam=audio=on*./c\dtparam=audio=off" $d > tmp && sudo cp -f tmp $d


Back To Top