Okay folks, I am out of ideas here and need any help anyone is willing to give.
Have been trying to run some shell commands to back up the SD card and configuration directory. I can run the commands in a terminal all day long without issue, but they just wont run from HA.
Putting the commands in a shell file but that too does not work. I tried putting the .sh files in the configuration directory, that did not work. I have made sure they are executable. I have changed the owner of the files to hass:hass.
I simply just do not know what to do next short of giving hass root privileges. Itâs just plain frustrating because I know it can be done yet for some reason I have been unable to and none of the examples around have given much help. By all accounts that I can find, my configuration is set up exactly as it needs to be, I just donât know what else to look at or try.
Then activate it via the webui and see if the file appears. If that works you can start debugging your commands.
After that, you can address the scripts:
You will NEED root privileges to do a dd of the disk of course.
and I assume youâve tried the rsync as the hass user and that works correctly? If that is the case and your key isnât passworded (not just unlocked via your keychain but actually passwordless) then that command should work
I am not sure how to run the command as the hass user. I looked at the Zwave page and am not sure how to integrate that into the command. But let me try they touch test first.
Cool, so HA is working then, so itâll be something in your script that is failing. If you get to more than a few distinct words in your command I would put it in an external script (to avoid possible parsing issues and make the config more readable, and you can then modify scripts without reloading the config file as well).
Put your commands in a script, and see if they work when run as the hass user and do what you expect, then you should be good to call it within your shell_command directive
Okay, I put the commands in a script tried running them as hass and failed miserably and I am unsure how to give hass permission to run the file without giving permission as root or some other elevated permissions. I have chmod +x the script file and that has not helped either. I am dumbfounded as to how others do this so easily, what the heck am I missing here?
ls -lash /usr/bin/harpi3rsync.sh
4.0K -rwx--x--x 1 root root 174 Jan 9 06:37 /usr/bin/harpi3rsync.sh
I ran across information about that and gave it a shot and that did not work either, so either I did it wrong or its not the solution. Having never tried setuid/setgid before I am more inclined to believe I did it wrong.
pi@HARPi3:~ $ sudo reboot
>>> /etc/sudoers: syntax error near line 1 <<<
>>> /etc/sudoers: syntax error near line 1 <<<
sudo: parse error in /etc/sudoers near line 1
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
I managed to mount the SD card in another Linux machine I have and was able to navigate to the /etc folder, change the permissions of the file and delete it. I then copied the sudoers file from the Linux machine to the Pi SD card and thought I would just change the permissions once I had the Pi booted back up. Well, that isnât working:
sudo visudo -f sudoers
sudo: /etc/sudoers is owned by uid 1000, should be 0
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
It is an absolute shame that it is this difficult to do such a simple task as run a shell script. When I first read the documentation about being able to run a shell script I thought good God, they thought of absolutely everything. That part hasnât changed, but apparently the idea of being able to run a script did not extend to making it relatively easy to accomplish.
I try my best to do my due diligence to show that I am not just wanting someone to do it for me, but the documentation keeps biting me because the examples tend to leave you hanging. I know this particular task is being done by others in HA, so I know itâs possible, but apparently I am not bright enough to figure it out. And for such a beautiful, wonderfully powerful home automation system, but the average user just isnât going to invest this sort of time in trying to figure things out. I have been trying to figure this out a couple days before I made my post here. Thatâs just an unreasonable amount of time to have to invest to run a simple shell script.
Okay, I am back to normal, I think, well at least back to where I started. I had to run chmod --reference systemfile myfile and then chown â reference systemfile myfile
Basically, I mounted the Pi SD card in another Linux machine changed the permissions of the /etc/sudoers file so I could delete it. Then I copied the sudoers file from the Linux machine over to the /etc directory. Then I ran the two above-mentioned commands so that the ownership particularly and permissions matched that of another root owned file in that directory. Took the SD card and plugged it back into the Pi and rebooted.
So I took a long trip around OZ to get back to the point I was, which is a shell script file that wonât run.
@Tinkerer I agree, there are security issues, but how does one go about running a command that would require elevated privileges, such as the dd command, without an elevated user?
@StormStrikes what you would do is create a script owned by root, in your case this rsync script, then you add a sudoers line like this:
user hass = (root) NOPASSWD: /foo/bar/rsyncscript
This way the hass user can call sudo /foo/bar/rsyncscript and NOTHING ELSE. Since the script is owned by root, the root user is the only one who can modify the script (make sure itâs permissions are 755 and youâll be fine).
FYI, donât edit sudoers manually (or copying from another host like you did, since sudo (and ssh) are very particular about permissions to avoid potential security issues. Use visudo which does error checking before saving, otherwise you could end up with a broken file and unable to use sudo. Itâs also a good idea to have a root password so that you can login without having to do annoying recovery steps because you broke sudoers.
@justin8 Just a couple of things, because you have given some information that I have not seen yet.
First, add the line to sudoers just like you posted user hass = (root) NOPASSWD: /path/to/my/rsyncscript ?
I ask because I have not seen it formatted in the way you provided and I just want to make sure.
Lastly, I have been editing sudoers with visudo, however, I âthinkâ I somehow wiped out the contents of the file as when I finally got it open, there were only two curly braces in it. No content, no settings, nothing. Not sure how I did that, but I did see where I needed to use visudo to edit it and was doing so. But in my inexperience with such things I somehow goofed it up. Thankfully I had other machines with Linux on them.
Again, thank you for your post.
EDIT:
One last thing if I may impose upon you. I have the other script file to run as well the one that will us dd to make a backup image of the SD card.
Do I just add another line to the sudoers file for that, or do you somehow combine them in one line?