[SOLVED] "Need to be root" - Sending ssh command remotly

Hi,
I am trying to turn off my ubuntu server remotely. Openhab is running on my raspberry and Openhab runs by “openhab” user. I could manage to ssh from rpi with openhab user without password, but when I run exec command I get the following error:

val String results = executeCommandLine("ssh openhab@192.168.0.17 shutdown -h now", 5000)
logInfo("Exec test", results)
shutdown: Need to be root

If I run command with sudo I get the following error:

sudo: no tty present and no askpass program specified

Do you know what do I wrong, ow could I grant sufficient permissions to ‘openhab’ user on fileserver?
Thank you

I think this is not correct … the User Openhab is on Raspberry and not on Ubuntu ?

openhab User für sudo Scripte aktivieren!
	sudo nano /etc/sudoers.d/011_openhab-nopasswd
		openhab ALL = (root) NOPASSWD: /etc/openhab2/scripts/shutdown.sh

If you follow this Instructions… than you give the Openhab User on raspberry the access to sudo the shutdown.sh
Note: SSH with Keys must work…

Here an German Tutorial … if this not your Native Language… Search with Google for SSH with Keys

http://www.schlittermann.de/doc/ssh.html

@nattila1 as explained in this thread you must first execute one sudo command as user openhab/openhabian from the command line to get root working with the exec binding. Or enable the bash for the user.

From your error I think the problem is slightly different than some of the suggestions. The error is from the ubuntu server not the openhab raspberry pi. This means that the ssh is working fine but the user called openhab on ubuntu doesn’t have root privileges. So you need to make sure the sudo is for the openhab user on the ubuntu server. You want the sudo as part of the command, there doesn’t seem any need to add it in font of the ssh.

val String results = executeCommandLine("ssh openhab@192.168.0.17 sudo shutdown -h now", 5000)

If you are using that command and get the sudo errors follow the instructions by others in this thread but apply it to your ubuntu server.

Edit: Also what the other person said. Did you make a user on your ubuntu server called openhab? Otherwise check what user to login as.

i think you have to use the “-t” flag:

ssh -t openhab@192.168.0.17 "sudo shutdown -h now"

to set it inside "executeCommandLine you should escape the "s:

val String results = executeCommandLine(“ssh -t openhab@192.168.0.17 \“sudo shutdown -h now\””, 5000)

I created an “openhab” user on Ubuntu server as well.

Ok … if you manually logged in via ssh ? type sudo shutdown -h now … this worked ?

Hi Guys,

Thank you very much for your suoport. Thank you very much for your suoport. I solved this issue the following way: First, by following this tutorial I managed to be able to login from rpi to server without password. It was a bit tricky because I couldn’t switch to ‘openhab’ user on rpi therefore I run every command as a different user in the name of ‘openhab’ user.

http://www.linuxproblem.org/art_9.html

sudo -u openhab ssh-keygen -t rsa

Then was able to login but I still couldn’t shutdown the server, and I always got the following error: Need to be root

On the server I modified sudoers with visudo command and added the following line to it:
myuser ALL = NOPASSWD: /sbin/shutdown, /sbin/poweroff, /sbin/halt, /sbin/reboot

Now, via terminal I am able to shutdown the remote server:
sudo -u openhab ssh myuser@192.168.0.17 sudo shutdown -h now

It should work with exec binding as well.

Thank you,

1 Like

Hi All

Trying to get this to work to shutdown a remote linux machine.

I can login just find without a password now by using keys

ssh kris@192.168.1.254
[2.4.4-RELEASE][kris@pfSense.server.ddns.net]/home/kris: exit
logout

But cannot for the life of me turn this into an executeCommandLine rule


rule "Shutdown Firewall"
when
        Item shutdownfirewall changed to ON
then
        shutdownfirewall.postUpdate(OFF)
        Thread::sleep(100)
        executeCommandLine("sudo -u kris ssh kris@192.168.1.254 shutdown -h now")
end

This fails and ive tried just about everything other combination without success

When i execute ssh nsautomate@192.168.1.254 sudo shutdown -h now

it works just fine from the CLI

Kris
try replacing the spaces in your command with @
if that doesn’t work, perhaps start a new thread as this one is old and marked as solved

No joy :frowning:
Will do!

1 Like

Try it that way…

openHAB Rule

    "Shutdown Firewall"
    when
            Item shutdownfirewall changed to ON
    then
            shutdownfirewall.postUpdate(OFF)
            Thread::sleep(100)
            executeCommandLine("bash /etc/openhab2/scripts/firewall_shutdown.sh")
    end

Shell Script

sudo nano /etc/openhab2/scripts/firewall_shutdown.sh

#!/bin/bash
ssh kris@192.168.1.254 'sudo shutdown -h now'

Set execution rights

sudo chmod u+x /etc/openhab2/scripts/firewall_shutdown.sh

Also check this Post :slight_smile:

No change im fraid

visudo has this



openhab ALL=(ALL) NOPASSWD: ALL
kris ALL=(ALL) NOPASSWD: ALL

If i run the command from the Oh2 server, ssh kris@192.168.1.254 ‘sudo shutdown -h now’ it works perfectly.

Show me the results of ls -lah /etc/openhab2/scripts/firewall_shutdown.sh

Perhaps User Openhab can’t execute the command

Execute sudo chown openhab:openhab /etc/openhab2/scripts/firewall_shutdown.sh

Try this command in Shell… Works or not?

sudo -u openhab ssh kris@192.168.1.254 'sudo shutdown -h now'

Yes, but asks me to login requires a password etc, but works when Ive entered that.

Problem found , I think…

The User kris use the ssh-keys … and needs no password

The User openhab don’t use the ssh-keys and needs a password … that’s why the Rule/Script runs in a Error

Take a look here…

https://klenzel.de/1871

Good German Tutorial :slight_smile:

But im not logging in as openhab, im logging in as kris. Even translated the URL doesnt make much sense :confused:

Even if it uses openhab as part of the rule, the sudo visudo was done and openhab added to allow the commands