Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to SSH into my EC2 instance via Pipeline

Kunal Dubey
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 8, 2023

So, I have been struggling with this issue over the weekend and have tried everything that was available on the internet. 

The Issue: I need to SSH into my EC2 instance but I'm unable to.
Error: Permission Denied Permission denied (publickey). 

Things I've Done: 
1. Tried pasting the private public key pair that I have generated from the AWS console (.pem )
2. When the above way didn't work I tried generating the key pairs from the bitbucket pipeline console and pasted the public key in the .ssh/authorized_keys 
3. Fetched hosts and added them in my pipeline configuration 
4. tried ssh -t ( didn't work ) |
5. tried ssh - i ~./ssh/config root@ip ( didn't work ) 

pipelines:
default:
- step:
name: Deploy to AWS EC2
deployment: development
caches:
- node
script:
- sudo apt install ssh-askpass
- ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts
- ssh $root@$ip 'sudo su && cd /home/ubuntu/scripts && sh deployment.sh'

Can someone please help me out here?

 

2 answers

0 votes
Igor Stoyanov
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 13, 2023

@Kunal Dubey hi. In additional to @Theodora Boudale response you could also try to follow this medium guide, where you could use one of the ssh type pipes: rsync-deploy.

To discover more ssh type pipes check the Marketplace.

 

Regards, Igor

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 9, 2023

Hi Kunal and welcome to the community!

The 'Permission denied' error indicates an issue with SSH authentication. Can you please follow these steps:

1. Open the repo on Bitbucket Cloud website, go to Repository settings > section PIPELINES, and select SSH keys

2. Fetch the fingerprint of the EC2 instance on that page, in the section Known hosts

If there are any errors, please let us know what error you see.

3. Generate an SSH key pair on that page by selecting the option Generate keys

4. Copy the public key to ~/.ssh/authorized_keys on the EC2 instance, for the user $root you are connecting with

5. Add the option -vvv in the ssh command in your yml as follows:

- ssh -vvv $root@$ip 'sudo su && cd /home/ubuntu/scripts && sh deployment.sh'

The -vvv argument will print verbose output that may give an indication of the cause of this error. Afterwards, when you run a build, please post the full output of the ssh command here (after sanitizing any private/sensitive data) so we can check any errors that may be reported.

You can also remove the ssh-keyscan command from your yml file. You are not trying to connect to Bitbucket Cloud, but even if you were, Bitbucket Cloud's host key is automatically added to known_hosts so this command is not needed.

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events