Search code examples
jenkinsamazon-ec2sshcicd

ssh permission denied while running jenkins



+ ssh -i /home/ubuntu/myphpApp/key.pem [email protected] /bin/bash
Warning: Identity file /home/ubuntu/myphpApp/key.pem not accessible: Permission denied.
[email protected]: Permission denied (publickey).
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 255
Finished: FAILURE

trying to connect to ec2 from current server , not working. Tried with 400 600 777 keyfile permission, still not working.


Solution

  • /home/ubuntu/myphpApp/key.pem must have a 600 permission and be owned by the jenkins user. it will not work on a different permission, even if it's 777.

    Preferably use jenkins ssh credential by storing the privet key in an ssh privet key credential.

    withCredentials([sshUserPrivateKey(credentialsId: 'myphpapp', keyFileVariable: 'ssh')]) {
        sh 'ssh -i $ssh [email protected] /bin/bash'
    }