0

I am writing a script that relies on pkexec as the machine is hardened and sudo will not work. I allow pkexec to temporarily not prompt the user for a password, so any command will work directly.

If I manually log in to SSH and have a session open and run a command like: pkexec service iptables restart it works, however, if I try doing something like ssh [email protected] "pkexec service iptables restart" I get the following error:

Cannot run program service: no such file or directory

So it has something to do with me passing pkexec as a parameters.

2
  • Did you specify a path? ...."pkexec /sbin/service iptables restart" Commented Sep 20, 2016 at 2:02
  • OK, converted into a full answer with explanation as to why this works. Commented Sep 20, 2016 at 21:41

1 Answer 1

0

When you do a ssh remotehost some_command type command your $PATH may not be set to the values that you expect.

For example, we can see the PATH in my current login shell doesn't match the remote PATH, even though I'm logging into the same machine:

$ echo $PATH
/home/sweh/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/u/bin:/usr/local/bin:/usr/bin/X11:/etc:/usr/local/sbin:/sbin:/usr/sbin

$ ssh localhost 'echo $PATH'
/usr/local/bin:/usr/bin

For this reason it's normal to specify the full path name to commands.

In this instance the error message Cannot run program service: no such file or directory means that the pkexec command was found (it's probably on the default PATH) but the service command wasn't.

So specify /sbin/service... eg

ssh [email protected] "pkexec /sbin/service iptables restart"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .