# Write rights on USB stick

**URL:** <https://discourse.nodered.org/t/write-rights-on-usb-stick/73481>\
**Category:** General\
**Created:** [10 January 2023 16:44 UTC](https://discourse.nodered.org/t/write-rights-on-usb-stick/73481 "2023-01-10T16:44:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw\_te\_r](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jw_te_r/32/69700_2.png) [@jw\_te\_r](https://discourse.nodered.org/u/jw_te_r)\
**Post date:** [10 January 2023 16:44 UTC](https://discourse.nodered.org/t/write-rights-on-usb-stick/73481/1 "2023-01-10T16:44:15Z")

</div>

Hi all,

I have a bit of a noob question; couldn't find the answer yet:  
I'm running Node-Red on a Linux NUC pc, and I want to be able to write some logging info to a USB stick. So I began with nodes to determine whether a USB stick is present, and find out its address:

 ![Screenshot from 2023-01-10 17-38-21](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5f5b7705943b3337f836fc5f90a78c0e87e267d0.png)

```auto
[
    {
        "id": "e9ee1b34.cfb5f8",
        "type": "exec",
        "z": "ca11a0459a7c29b7",
        "command": "",
        "addpay": true,
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "oldrc": false,
        "name": "",
        "x": 330,
        "y": 700,
        "wires": [
            [
                "9ab2c2f4af80e0fe",
                "2ca80c4b9170e354"
            ],
            [
                "bbdbf46cec725a29"
            ],
            [
                "3c6554e354978de1"
            ]
        ]
    },
    {
        "id": "bbdbf46cec725a29",
        "type": "debug",
        "z": "ca11a0459a7c29b7",
        "name": "debug 9",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 540,
        "y": 780,
        "wires": []
    },
    {
        "id": "3c6554e354978de1",
        "type": "debug",
        "z": "ca11a0459a7c29b7",
        "name": "debug 10",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 540,
        "y": 840,
        "wires": []
    },
    {
        "id": "9ab2c2f4af80e0fe",
        "type": "function",
        "z": "ca11a0459a7c29b7",
        "name": "Address of USB stick",
        "func": "var response = [null, null, null];\nvar usbStickFound = Boolean(msg.payload != \"\");\n\nvar newMsg = { payload: usbStickFound };\n\nflow.set(\"usbStickFound\", usbStickFound);\n\nresponse[0] = newMsg;\nif (usbStickFound)\n{\n var usbStickName = msg.payload.trim();\n flow.set(\"usb_stick\", usbStickName);\n newMsg = { payload: \"/media/jw/\" + msg.payload };\n response[2] = newMsg;\n}\nelse\n{\n newMsg = { payload: \"Geen USB stick gedetecteerd.\" };\n response[1] = newMsg;\n}\nreturn response;\n\n\n",
        "outputs": 3,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 580,
        "y": 720,
        "wires": [
            [
                "689f2a274e302539"
            ],
            [
                "b014d243d5220a1b"
            ],
            []
        ]
    },
    {
        "id": "b014d243d5220a1b",
        "type": "ui_toast",
        "z": "ca11a0459a7c29b7",
        "position": "dialog",
        "displayTime": "0",
        "highlight": "",
        "sendall": true,
        "outputs": 1,
        "ok": "OK",
        "cancel": "",
        "raw": false,
        "className": "",
        "topic": "Let op!",
        "name": "Foutmelding",
        "x": 1010,
        "y": 720,
        "wires": [
            []
        ]
    },
    {
        "id": "ba9bcba6d5c99313",
        "type": "function",
        "z": "ca11a0459a7c29b7",
        "name": "Try to find usb stick",
        "func": "msg.payload = \"ls /media/jw\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 190,
        "y": 800,
        "wires": [
            [
                "e9ee1b34.cfb5f8"
            ]
        ]
    },
    {
        "id": "2ca80c4b9170e354",
        "type": "debug",
        "z": "ca11a0459a7c29b7",
        "name": "debug 16",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 540,
        "y": 660,
        "wires": []
    },
    {
        "id": "124fc669055f8694",
        "type": "inject",
        "z": "ca11a0459a7c29b7",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 720,
        "wires": [
            [
                "ba9bcba6d5c99313"
            ]
        ]
    },
    {
        "id": "689f2a274e302539",
        "type": "debug",
        "z": "ca11a0459a7c29b7",
        "name": "debug 17",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 760,
        "y": 660,
        "wires": []
    }
]

```

.. which works nicely, but only as long as I'm logged in on the pc. I would like to be able to always write to a USB stick from the NR dashboard. Is that possible?

---

<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:** [10 January 2023 17:12 UTC](https://discourse.nodered.org/t/write-rights-on-usb-stick/73481/2 "2023-01-10T17:12:00Z")

</div>

The problem is that you are relying on the automatic mounting of the stick under `/media/<username>/`, which doesn't happen if you are not logged in.

You will need to automatically mount it. If you always use the same stick you can do that with an entry in `/etc/fstab`. If it is not always the same stick then I am not sure how to do it.  
If it is the same stick each time then you can find the UUID of the stick using  
`sudo blkid`  
and can then put, in /etc/fstab, something like  
`UUID=129d9cb2-ecdf /media/usbstick ext4 defaults,nofail 0 0`  
That assumes that you have formatted it with ext4. It mounts at /media/usbstick obviously. I think you need to create that folder manually first.  
Keep a copy of the original fstab because if you make a mess of fstab it may not boot, and you will have to put the card in a PC and replace the original fstab.

---

<div class="post-metadata">

**Author:** ![jw\_te\_r](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jw_te_r/32/69700_2.png) [@jw\_te\_r](https://discourse.nodered.org/u/jw_te_r)\
**Post date:** [10 January 2023 18:06 UTC](https://discourse.nodered.org/t/write-rights-on-usb-stick/73481/3 "2023-01-10T18:06:17Z")

</div>

Thanks for the answer. Unfortunately it's not the answer I was hoping for.  
I want to give the user the possibility to harvest logging data from a standalone system on his own usb device, without giving him access to Linux.  
I always thought Node-Red uses the permissions of the user that started the process, but it seems to be a little bit more complicated ...

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [10 January 2023 18:10 UTC](https://discourse.nodered.org/t/write-rights-on-usb-stick/73481/4 "2023-01-10T18:10:54Z")

</div>

Copying log files to a usb stick when it is inserted doesn’t really need Node-RED. But you may also want to do some checks on the stick first before allowing the export of data to an unknown device. You can use things like udev rules to mount the stick and call a script to do the export.

---

<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:** [10 January 2023 21:30 UTC](https://discourse.nodered.org/t/write-rights-on-usb-stick/73481/5 "2023-01-10T21:30:38Z")

</div>

> [@jw\_te\_r](#):
>
> I always thought Node-Red uses the permissions of the user that started the process, but it seems to be a little bit more complicated

It isn't a permission issue, it is that the software that mounts the stick only runs when the user is logged in. Although node-red runs as that user, the user is not logged in.

Google found this, which might do what you want. I can't vouch for it, bit it does not look unreasonable. [Automount USB devices on Linux using UDEV and Systemd | Andrea Fortuna](https://andreafortuna.org/2019/06/26/automount-usb-devices-on-linux-using-udev-and-systemd/)

---

<div class="post-metadata">

**Author:** ![jw\_te\_r](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jw_te_r/32/69700_2.png) [@jw\_te\_r](https://discourse.nodered.org/u/jw_te_r)\
**Post date:** [11 January 2023 13:33 UTC](https://discourse.nodered.org/t/write-rights-on-usb-stick/73481/6 "2023-01-11T13:33:10Z")

</div>

Thanks @Colin and @dceejay , that explains a lot. I'll have to look into this udev and systemd article, because I'm not a Linux expert.

---

<div class="post-metadata">

**Author:** ![jw\_te\_r](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jw_te_r/32/69700_2.png) [@jw\_te\_r](https://discourse.nodered.org/u/jw_te_r)\
**Post date:** [23 January 2023 12:25 UTC](https://discourse.nodered.org/t/write-rights-on-usb-stick/73481/7 "2023-01-23T12:25:45Z")

</div>

> [@Colin](#):
>
> Google found this, which might do what you want. I can't vouch for it, bit it does not look unreasonable. [Automount USB devices on Linux using UDEV and Systemd | Andrea Fortuna](https://andreafortuna.org/2019/06/26/automount-usb-devices-on-linux-using-udev-and-systemd/)

I had to tweak it a little bit, but it works nicely. Thanks again!

---

<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:** [6 February 2023 12:26 UTC](https://discourse.nodered.org/t/write-rights-on-usb-stick/73481/8 "2023-02-06T12:26:20Z")

</div>

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