Reboot Pi with script

I cant understand for the life of me why this command wont work…I am using it as stated above
shell_command:
pi: “sudo reboot”

If i enter it from the putty it reboots

By default I don’t think user Hass has sudo permissions. If you ssh in you are likely logged in as user pi. Try switching to user hass and it likely won’t work through ssh either.

You can give hass sudo permission but I don’t recall the steps to do it. A bit of Google fu will get you there.

1 Like

$ sudo adduser hass sudo

and then a reboot. “hass” being the user created by the AIO installer.

Seems to do it, hass shows as being in the sudo group

pi@HARP3:~ $ groups hass
hass : hass dialout sudo

but still doesn’t seem to let hass execute shell commands from within Hass. I should mention that in looking up how to do this there were all sorts of warnings to NEVER do this but I do not really understand how permissions work and the dangers.

After testing you can remove hass from sudo group by

$ sudo deluser hass sudo

1 Like

Keep in mind hass likely still has a password for sudo use. You would need to remove that too or add it into the shell command.

1 Like

THx for the replies, but for the life of me i cant find out what the hass password is, i installed HA with AIO script.
Does anyone know?

I’m using the AIO install and have the same problem which at least makes me feel less like I’ve broken something. I’m going to try a fresh install on a spare SD card and see if that lets me run a shell command when I have some time.

Not helpful sorry, but in my searching I found this post which says the hass account has no password.

There is no password for hass from what I have read. There is a way to not prompt for the password when sudo is used. Pi has this permission by default and hass can be given the same permissions.

@mefistofelis Rather than do something that breaks security on the pi, try something different. You can execute commands remotely using a script. On Mac or Linux you can use a key based ssh login to remotely run a script. On Windows, I think you can do the same with plink (part of the Putty package). A double click on shortcut on your desktop could do the reboot.

1 Like

I was struggling to get this done also and managed to get it done this way

Hope it helps someone

4 Likes

This works well for me, thanks a lot!

I’ve been trying to setup something like this for a while (but failed).

I’m getting the following error when trying to execute this:

2018-09-26 20:49:51 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `sudo reboot`, return code: 1
NoneType: None

What platform are you running on? HassIO (resinOS), HassIO (HassOS), Virtual Enviroment, Hassbian?

This option likely won’t work for the first two options.

I’m running Hassbian on RP3

The problem here is that you are trying to sudo from homeassistant not as the pi user since HA runs under the homeassistant account. Only pi is automatically granted sudo access.

One solution is to allow home assistant permission to run /sbin/shutdown by adding the following to /etc/sudoers.d/020_homeassistant_hassbian-scripts
You can also just add a file. For example you can make a new file named 025_homeassistant_custom-scripts and place it in the same /etc/sudoers.d folder

%homeassistant ALL= NOPASSWD: /sbin/shutdown *

now you can create the shutdown command as

shell_command:
  reboot_pi: '/usr/bin/sudo /sbin/shutdown -r now'
  shutdown_pi: '/usr/bin/sudo /sbin/shutdown now'

I also add the ability to script a HA restart by adding the following

%homeassistant ALL= NOPASSWD: /bin/systemctl restart [email protected]
shell_command:
  reboot_pi: '/usr/bin/sudo /sbin/shutdown -r now'
  shutdown_pi: '/usr/bin/sudo /sbin/shutdown now'
  ha_restart: '/usr/bin/sudo /bin/systemctl restart [email protected]'
5 Likes

I don’t quite understand, can you eleborate?

It would help if you explained what part you didn’t understand. It really depends on your level of unix administration experience. Guessing that is low let me try to elaborate…

Hassbian has two default users.
pi - standard pi user in all instances of raspbian (has sudo access to become root)
homeassistant - special user used to run HA (does NOT have sudo access to become root, this is GOOD)

when you ssh into hassbian you do so as pi user.
Then you run hashell to become homeassistant. hashell is an alias it just runs the command to sudo to the new user “alias hashell=‘sudo su -s /bin/bash homeassistant’”

When HA runs it runs as “homeassistant” user. This means HA can’t do anything that requires a root user. That is by design or bad things could be injected into your server so you don’t want to change homeassistant to have full root access.

But you want HA to issue the command “sudo shutdown -r now”. But sudo is a request to become root, homeassistant user is not allowed to do that.

The information I provided in my last post allows you to grant permission to homeassistant user to run ONLY shutdown, as root.

So by adding the permission to sudoers.d for homeassistant to run shutdown as root the command should now work.

I hope that explains why the commands I provided would solve the issue. If not please state the parts you are not understanding.

I figured this all out because I have System Admin experience and I googled the heck out of why this wasn’t working when I first attempted. :slight_smile:

2 Likes

@smart thanks for your very detailed explenation. Loud and clear!

I did not understand that 020_homeassistant_hassbian_scripts was actually a file so I was in the assumption you made a typo there.
I managed to add the lines now using sudo nano /etc/sudoers.d/020_homeassistant_hassbian-scripts and all is working fine now.

Thanks a lot for your help (Y).

2 Likes

BTW: The reasoning for my suggestion to create a new file is that you will not have to worry about an update overwriting the 020_homeassistant_hassbian_scripts file. By creating your own file you can be sure it will remain.

2 Likes

This is just weird! I have tried several different versions, with the one you have, and every time I add a new file and add anything to it, like the simplest command:

ALL ALL=(root) NOPASSWD: /sbin/reboot

Then I mess up all sudo access to the Pi. It says that there’s a syntax error in the file, and no valid sudoers source found.

Edit: I managed to fix it by using Visudo. And I think I know what I did wrong, I believe there is a tab there that I didn’t get through the text here.

@Mastiff not sure what is wrong on your pi. Did you try the steps I provided above?

I use

%homeassistant ALL= NOPASSWD: /sbin/shutdown *, /bin/systemctl restart [email protected]

and it works fine