Friday, January 8, 2016

sudo absolute path within script - bash, linux, ubuntu

inside a script, sudo does something weird with the path used to execute it's command argument. this is on a default install of ubuntu - not sure if the sudoers file affects it. i'm not talking about the path that the command sees in its environment, but the path used to find the command to run


  • sudo: cmd_on_my_path: command not found


this is counter-intuitive, but i guess makes sense if i think about it enough - sudo only sees the command name, not the full path, and presumably for security they don't want to respect $PATH

but in the case in which the user has full sudo rights, i don't see how this helps anything. so in my script i've replaced "sudo myCommand" with:

  • sudo "$(which myCommand)"
not enough of a guru to know if this is perfect or really safe, so buyer beware. but wanted to post something - doesn't appear to be a good answer available via google

No comments: