Use Port 502 with Modbus-TCP

Hi,

i want to run the Modbus Flex Server from the module node-red-contrib-modbus on an Raspberry Pi with Raspbian. But whenever i use the default port 502, Node-Red crashes. I found out that, ports below 1024 can not used without root. Then i found the command setcap 'cap_net_bind_service=+ep' [path to node] to have access without running as root.

But now i tried it but it won't work either. I'm not sure i'm using the correct path.

I'm using setcap 'cap_net_bind_service=+ep' /home/pi/.node-red/node_modules/node-red-contrib-modbus/modbus/modbus-flex-server.js

Maybe someone can tell me what i'm doing wrong or give me a solution.
I searched for a while and can't another way to solve it.

Best Regards
Heino

I have not done that but I am sure it will not be the path to the node, it may be the path to the nodejs executable. In which case, on a pi, it may be /usr/bin/node so you could try that.

Note that this may be a security risk as it means any nodejs application will have those access rights. Whether that is an issue for you depends on the environment you are running in.

Thanks for reply.

I tried the path but it also doesn't work. Do you think there is another way to solve this problem?

What error are you getting?

fehlernodered

As soon as i change the port to 502 this error come up.

Where possible please copy/paste terminal output rather than screenshot. When pasting use the </> button at the top of the forum entry window. Thanks.

Can you copy/paste the setcap command you are using and any output it produces please. Also run the command with -v and show us that too. So netcap -v 'cap_net_....

I'm sorry for this.

This ist the command i used:

pi@raspberrypi:~ $ sudo setcap -v CAP_NET_BIND_SERVICE=+eip /usr/bin/node
/usr/bin/node: OK

And this is the output i get from Node-Red:

pi@raspberrypi:~ $ ./node-red
24 Jan 11:31:44 - [info]

Willkommen bei Node-RED!
===================

24 Jan 11:31:44 - [info] Node-RED Version: v1.2.7
24 Jan 11:31:44 - [info] Node.js  Version: v14.15.4
24 Jan 11:31:44 - [info] Linux 5.4.79-v7l+ arm LE
24 Jan 11:31:45 - [info] Paletten-Nodes werden geladen
24 Jan 11:31:48 - [info] Einstellungsdatei: /home/pi/.node-red/settings.js
24 Jan 11:31:48 - [info] Kontextspeicher: 'default' [ module=memory]
24 Jan 11:31:48 - [info] Benutzerverzeichnis: /home/pi/.node-red
24 Jan 11:31:48 - [warn] Projekte inaktiviert: editorTheme.projects.enabled=false
24 Jan 11:31:48 - [info] Flow-Datei: /home/pi/.node-red/flows_raspberrypi.json
24 Jan 11:31:48 - [info] Server wird jetzt auf http://127.0.0.1:1880/ ausgeführt.
24 Jan 11:31:48 - [info] Flows starten
24 Jan 11:31:48 - [info] Flows gestartet
24 Jan 11:31:48 - [red] Uncaught Exception:
24 Jan 11:31:48 - Error: listen EACCES: permission denied 169.254.231.56:502
    at Server.setupListenHandle [as _listen2] (net.js:1301:21)
    at listenInCluster (net.js:1366:12)
    at doListen (net.js:1503:7)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)

Shouldn't the arg be quoted?

sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/node

(not 100% certain the quotes make any difference)

Also, can you double check the output of which node returns /usr/local/bin/node

sudo setcap -v 'cap_net_bind_service=+ep' /usr/bin/node      /usr/bin/node differs in [i]
sudo setcap -v 'cap_net_bind_service=+ep' /usr/local/bin/node
/usr/local/bin/node differs in [pe]

The -v is used to verify that the setcap has worked. Do it without -v first then with to check it.
As suggested, use which node to find where the executable is.

Sorry i'm a little confused.

pi@raspberrypi:~ $ which node
/home/pi/.nvm/versions/node/v14.15.4/bin/node

Should i now use this path for the command?
sudo setcap 'cap_net_bind_service=+ep' /home/pi/.nvm/versions/node/v14.15.4/bin/node and then sudo setcap -v 'cap_net_bind_service=+ep' /home/pi/.nvm/versions/node/v14.15.4/bin/node ?

So am I, why are you using nvm (node version manager)? I guess you didn't use the recommended method of installing node-red and nodejs on a pi.
Unless you have a good reason I suggest removing the nvm s/w, remove the .nvm folder and run the install script. That will ensure you have a consistent set of tools.
Then use which node again to check that is it using /usr/bin/node and run setcap again with that path.

Node-Red was already preinstalled i think. After that i used the recommended method with the install script.

But thanks i try to delete the nvm and run the script again.

You probably need to uninstall nvm, not just delete the folder.

I tried to remove the npm and installed node-red with the script. Now i think i get the correct path.

pi@raspberrypi:~ $ which node
/usr/bin/node

Then i used the setcap again and now it finally worked.

Thank you a lot.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.