# Shutdown Computer with Node-Red

**URL:** <https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468>\
**Category:** General\
**Created:** [6 February 2021 00:16 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468 "2021-02-06T00:16:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [6 February 2021 00:16 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/1 "2021-02-06T00:16:42Z")

</div>

Hi there,

I am desperately trying to use Node-Red to shutdown or restart my Debian system.  
Was trying to use the exec node with the command: `sudo shutdown now`  
And even though I used `sudo visudo` to add the permission to run this command without password, it doesn't run successfully.  
Node-Red always tells me it's missing the rights for this command to execute.

```auto
tried both variants:
'user_name ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown'
or
'ALL ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown'

```

As it's a Debian system I used the convenient script to install Node-Red under my user account, no root account.  
`ps axu` tells me the systemd of Node-Red is running under my username.  
Maybe the underlying node.js is running as a different users process and that's why it's not being granted the privileges to run the shutdown command?

**Node-Red error message:**

```auto
"sudo: no tty present and no askpass program specified"

```

Tried to run `sudo -S shutdown now` and this looked good at first, but then the process just waits forever for the password from stdin... process owner is root in this case... should be the normal user right?

I hope someone here knows how to just simply shutdown the local machine when a certain flow is triggered. Can't believe nobody ever has had this issue before.

Hope you guys can shed some light and help me with my problem.

Thanks!  
Newbow

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [6 February 2021 00:59 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/2 "2021-02-06T00:59:47Z")

</div>

That's weird.

I use the `exec` node to shut down my RasPies.

Though not _Debian_ they are of the same stock.

This is the `exec` node.

```auto
[{"id":"7a2fa446.881fb4","type":"exec","z":"45b9510b.47e11","command":"sudo shutdown -h now","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Shut down","x":870,"y":208,"wires":[[],[],["4585d71e.2c0468"]]}]

```

Just send a message into it and the machine shuts down.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 February 2021 01:56 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/3 "2021-02-06T01:56:28Z")

</div>

On my debian system:

```auto
home@home:~$ whereis shutdown
shutdown: /usr/sbin/shutdown /usr/share/man/man8/shutdown.8.gz /usr/share/man/man2/shutdown.2.gz

```

`/usr/sbin/shutdown` not `/sbin/shutdown`

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [6 February 2021 12:22 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/4 "2021-02-06T12:22:39Z")

</div>

Thanks for your help.  
Used your node, even though it's no different from mine and I tried it on both my debian server and the Raspberry Pi (debian system as well).  
The weird thing is, it's working fine with the Raspberry Pi but refuses to work with the native debian server.

I am not sure what's going on here ¯\_(ツ) \_/¯

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [6 February 2021 12:26 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/5 "2021-02-06T12:26:07Z")

</div>

Thanks,

changed that in the `/etc/sudoes.d/shutdown` file but no change whatsoever.  
Even tried to execute the full path to the command, but no luck.

---

<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:** [6 February 2021 13:27 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/6 "2021-02-06T13:27:55Z")

</div>

Are you running in Docker or maybe some other container?

What does  
`which shutdown`  
show?

Copy/paste the full contents of /etc/sudoers and all files in /etc/sudoers.d

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 February 2021 13:57 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/7 "2021-02-06T13:57:37Z")

</div>

Did you use visudo to make the change?

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [6 February 2021 14:12 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/8 "2021-02-06T14:12:07Z")

</div>

EDIT: No docker container, just plain native installed through the Node-Red install script for debian.

Weird, but `which shutdown` returns nothing on the debian machine...

```auto
newbow@Debian:~$ which shutdown
newbow@Debian:~$

```

If I try this same on the RPi it gives me the path to the command:

```auto
pi@raspberrypi:~ $ which shutdown
/usr/sbin/shutdown
pi@raspberrypi:~ $

```

There is somthing fishy about that I think.

Anyhow, here is the `/etc/sudoes` file of the **debian server** :

```auto
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

# includedir /etc/sudoers.d

```

This is the additional file in `/etc/sudoers.d/shutdown `:  
I was trying different variants, that's why there are things commented out.

```auto
# User newbow is allowed to execute halt and reboot without password
# newbow ALL=(ALL) NOPASSWD: usr/sbin/poweroff, usr/sbin/reboot, usr/sbin/shutdown
newbow ALL=(ALL) NOPASSWD: ALL

```

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [6 February 2021 14:15 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/9 "2021-02-06T14:15:21Z")

</div>

No I did not use visudo. I am not touching the `/etc/sudoers` file itself.  
I read that you're supposed to create a file in `/etc/sudoers.d/` instead, which I did.  
This file contains my deviating permissions (see post above, were I answered Colin).

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 February 2021 14:25 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/10 "2021-02-06T14:25:06Z")

</div>

Hmm, I only ever use visudo, that file/those files are too easy to mess up. On my old Pi servers, I just always allowed every command to be run without a password. But now I've moved to a better server, I have also improved security and only allow certain commands with certain parameters. So the list runs to multiple lines. I didn't really need to but it is good discipline and good learning.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 February 2021 14:30 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/11 "2021-02-06T14:30:11Z")

</div>

> [@newbow](#):
>
> Weird, but `which shutdown` returns nothing on the debian machine...

Same for me. Use `whereis` instead.

> [@newbow](#):
>
> There is somthing fishy about that I think.

No. I'm using Debian Buster on an old Lenovo laptop for my home server.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9efd0989313cad5ae4fa873b1d4a0034c6eff3e9.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ecad3c324b4c081dc0c110cac8b17859613d6ef.png)

That is what I have and it is working fine. The home user runs Node-RED on bootup and the listed commands work fine from within Node-RED.

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [6 February 2021 15:21 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/12 "2021-02-06T15:21:59Z")

</div>

> [@TotallyInformation](#):
>
> Hmm, I only ever use visudo, that file/those files are too easy to mess up. On my old Pi servers, I just always allowed every command to be run without a password. But now I've moved to a better server, I have also improved security and only allow certain commands with certain parameters. So the list runs to multiple lines. I didn't really need to but it is good discipline and good learning.

That's the point here, I also just want to allow the shutdown, nothing more. Security is important for me as well. But visudo only allows to edit the `/etc/sudoers` file. The thing is though, that I always read you're not supposed to edit the file directly but create a file in `/etc/sudoers.d/` with 0440 permission and define your alternate rules there.

---

<div class="post-metadata">

**Author:** ![newbow](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@newbow](https://discourse.nodered.org/u/newbow)\
**Post date:** [6 February 2021 15:25 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/13 "2021-02-06T15:25:40Z")

</div>

How did you install NR? Using the script, or manual install via npm?

Did you try to shut it down with Node-Red? Does is work?

Damn I really don't like to reinstall and setup my server again to get this working, but this seems like the only option here...

---

<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:** [6 February 2021 16:06 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/14 "2021-02-06T16:06:40Z")

</div>

If you run `sudo shutdown -h now` in a terminal, as the node-red user does it ask for the password?  
I too have never used the sudoers.d files, nor have I seen any recommendations to do that.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 February 2021 16:43 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/15 "2021-02-06T16:43:46Z")

</div>

> [@newbow](#):
>
> That's the point here, I also just want to allow the shutdown, nothing more. Security is important for me as well. But visudo only allows to edit the `/etc/sudoers` file

Do that first. Get it working and then move the working code to `/etc/sudoers.d/`.

> [@newbow](#):
>
> How did you install NR? Using the script, or manual install via npm?
> 
> Did you try to shut it down with Node-Red? Does is work?

I never shut down my server unless I absolutely have to. I certainly don't want to allow that from Node-RED. However, the other sudo-based commands I listed work fine without a password and I've certainly done it on a Pi.

If my other suggestions aren't working, I suggest that you use the newer `systemctl poweroff` and `systemctl reboot` commands instead. These are probably better anyway.

---

<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 April 2021 16:43 UTC](https://discourse.nodered.org/t/shutdown-computer-with-node-red/40468/16 "2021-04-07T16:43:54Z")

</div>

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