Permission issue while executing an ssh task in azure pipeline

2.5k views Asked by At

What I am trying to do is to run a few lines of shell script in a remote machine via an azure pipeline. I used the ssh Deployment Task to accomplish this. I have used the script path argument to point the .sh file that contains the script that should be ran. The ssh task was able to connect to the remote host, but the following permission error pops up. Can someone tell me what's going wrong here. The .sh file that i am using was created in the Linux box itself and has got the permission level set to 777 before moving to the repo.

There is an another CopyFilesOverSSH@0 task in the pipeline in the same stage which works perfectly without any permission issues for the same user.

2021-12-31T12:41:42.1763039Z ##[section]Starting: SSH
2021-12-31T12:41:42.1894277Z ==============================================================================
2021-12-31T12:41:42.1894676Z Task         : SSH
2021-12-31T12:41:42.1895010Z Description  : Run shell commands or a script on a remote machine using SSH
2021-12-31T12:41:42.1895347Z Version      : 0.189.0
2021-12-31T12:41:42.1895637Z Author       : Microsoft Corporation
2021-12-31T12:41:42.1896023Z Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/deploy/ssh
2021-12-31T12:41:42.1896437Z ==============================================================================
2021-12-31T12:41:42.8200834Z Trying to establish an SSH connection to ***@80.xxx.xxx.xxx:22
2021-12-31T12:41:43.1333018Z Successfully connected.
2021-12-31T12:41:43.5698433Z ##[error]Failed to copy script to remote machine. Error: Error: put: Permission denied //checkFileAvailability.sh.
2021-12-31T12:41:43.6050230Z ##[section]Finishing: SSH
2

There are 2 answers

1
Andy Li-MSFT On

Firstly, if you want to copy files to the remote machine, then it's recommend to use Copy Files Over SSH task. This task allows you to connect to a remote machine using SSH and copy files matching a set of minimatch patterns from specified source folder to target folder on the remote machine. Supported protocols for file transfer are SFTP and SCP via SFTP.

For the SSH Deployment task. This task enables you to connect to a remote machine using SSH and run commands or a script.

According to your error message, the SSH is successfully connected, but failed to copy script to the remote machine. It appears that the service account doesn't have the permission to copy the specified file to the specific path on the remote machine. Please check your source file path permission settings. Please also try to using inline script instead of the Script File to check if it works.

0
Fyodor Yemelyanenko On

I had the same issue when run SSH script task under a user which was not a root. So for inline script to run under different user, that user should have:

  • Read/Write/Execute access to root folder, as TFS put all commands into generated bash script file and copy it to target machine root folder (below is another command, which is executed on already copied script file)

    tr -d '\015' <./sshscript_099d4e8c-44ac-482d-b1bf-84a52c7ab810> ./sshscript_099d4e8c-44ac-482d-b1bf-84a52c7ab810._unix

  • User should have home directory as TFS switch to it

So to fix this issue I have granted rwx permissions to everyone for the root folder

chmod 777 /
ls -ld /
drwxrwxrwx 20 root root 4096 Feb 10 14:54 /

And make sure that home folder for my user exists