Search code examples
bashpermissionssudo

permission denied reading from file with <, even with sudo in use


I need to run a script from server A in Server B. After ssh into server B, I ran the following command:

sudo ssh root@ip_A 'bash -s' < root/work/task.sh

I am getting the error below:

-bash: /root/work/task.sh: Permission denied. 

On server A, I have done sudo chmod 777 task.sh.

Please thanks.


Solution

  • This is one of the few places where cat adds value even when not concatenating multiple files:

    sudo cat /root/work/task.sh | ssh root@ip_A 'bash -s'
    

    Because redirections such as < are run by the shell before the program being invoked is started, sudo can't change the permissions used for such redirections (it hasn't started yet!). By contrast, sudo cat somefile runs sudo first, then cat, which then opens somefile; since sudo runs first in that case, escalated permissions are available.