# Use Port 502 with Modbus-TCP

**URL:** <https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615>\
**Category:** General\
**Created:** [23 January 2021 16:38 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615 "2021-01-23T16:38:09Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![heino](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@heino](https://discourse.nodered.org/u/heino)\
**Post date:** [23 January 2021 16:38 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/1 "2021-01-23T16:38:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 January 2021 17:00 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/2 "2021-01-23T17:00:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 January 2021 17:03 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/3 "2021-01-23T17:03:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![heino](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@heino](https://discourse.nodered.org/u/heino)\
**Post date:** [23 January 2021 17:20 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/4 "2021-01-23T17:20:07Z")

</div>

Thanks for reply.

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 January 2021 18:09 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/5 "2021-01-23T18:09:43Z")

</div>

What error are you getting?

---

<div class="post-metadata">

**Author:** ![heino](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@heino](https://discourse.nodered.org/u/heino)\
**Post date:** [23 January 2021 18:24 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/6 "2021-01-23T18:24:39Z")

</div>

![fehlernodered](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a83a52e5913699c3c891bffbf1ed41761cc4e61c.jpeg)

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 January 2021 21:13 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/7 "2021-01-23T21:13:47Z")

</div>

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_...`.

---

<div class="post-metadata">

**Author:** ![heino](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@heino](https://discourse.nodered.org/u/heino)\
**Post date:** [24 January 2021 10:37 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/8 "2021-01-24T10:37:38Z")

</div>

I'm sorry for this.

This ist the command i used:

```auto
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:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 January 2021 10:47 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/9 "2021-01-24T10:47:03Z")

</div>

> [@heino](#):
>
> `sudo setcap -v CAP_NET_BIND_SERVICE=+eip /usr/bin/node`

Shouldn't the arg be quoted?

```bash
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`

---

<div class="post-metadata">

**Author:** ![heino](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@heino](https://discourse.nodered.org/u/heino)\
**Post date:** [24 January 2021 10:55 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/11 "2021-01-24T10:55:52Z")

</div>

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

```

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

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 January 2021 11:07 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/13 "2021-01-24T11:07:36Z")

</div>

> [@heino](#):
>
> `setcap -v`

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.

---

<div class="post-metadata">

**Author:** ![heino](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@heino](https://discourse.nodered.org/u/heino)\
**Post date:** [24 January 2021 11:21 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/14 "2021-01-24T11:21:27Z")

</div>

Sorry i'm a little confused.

```auto
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` ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 January 2021 11:46 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/15 "2021-01-24T11:46:12Z")

</div>

> [@heino](#):
>
> Sorry i'm a little confused.

So am I, why are you using nvm (node version manager)? I guess you didn't use the [recommended method](https://nodered.org/docs/getting-started/raspberrypi) 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.

---

<div class="post-metadata">

**Author:** ![heino](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@heino](https://discourse.nodered.org/u/heino)\
**Post date:** [24 January 2021 11:54 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/16 "2021-01-24T11:54:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 January 2021 12:04 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/17 "2021-01-24T12:04:28Z")

</div>

> [@heino](#):
>
> i try to delete the nvm

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

---

<div class="post-metadata">

**Author:** ![heino](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@heino](https://discourse.nodered.org/u/heino)\
**Post date:** [24 January 2021 12:10 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/18 "2021-01-24T12:10:56Z")

</div>

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

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

```

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

Thank you a lot.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 February 2021 12:11 UTC](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/19 "2021-02-07T12:11:25Z")

</div>

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