Question

ssh gitlab to ec2

i have allready install and cinfigure gitlab on ubuntuserver in vertualbox and i will take ec2 instance on aws how my both are connecting ssh


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey 👋,

To connect your GitLab server installed on a virtual machine (like VirtualBox) to an EC2 instance via SSH:

https://www.digitalocean.com/community/tutorials/ssh-essentials-working-with-ssh-servers-clients-and-keys

You need to make sure that SSH is installed and running on the GitLab server (Ubuntu in your case).

  • Check if SSH is installed:

    sudo systemctl status ssh
    

    If SSH is not installed, install it with:

    sudo apt-get install openssh-server
    
  • Start SSH service (if not running):

    sudo systemctl start ssh
    sudo systemctl enable ssh
    

Generate an SSH key pair on your GitLab server. You will use this key to authenticate the connection with your EC2 instance.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

When prompted, press Enter to accept the default key location and provide a passphrase if desired. This will generate two files:

  • ~/.ssh/id_rsa (private key)
  • ~/.ssh/id_rsa.pub (public key)

You need to add the public key from the GitLab server to the authorized_keys file on the EC2 instance.

  • Copy the public key from your GitLab server to the clipboard:

    cat ~/.ssh/id_rsa.pub
    

    Copy the output.

  • SSH into your EC2 instance:

    ssh -i your-aws-key.pem ec2-user@<EC2-IP>
    
  • Add the public key from the GitLab server to the EC2 instance’s authorized keys:

    echo "your-public-key" >> ~/.ssh/authorized_keys
    

Make sure the permissions for the ~/.ssh/authorized_keys file are set correctly:

chmod 600 ~/.ssh/authorized_keys

On your GitLab server, test the connection to the EC2 instance using SSH:

ssh ec2-user@<EC2-IP>

This should log you into the EC2 instance without requiring a password, as it uses the SSH key for authentication.

If you want to use the EC2 instance from GitLab (for example, in CI/CD pipelines), you can configure GitLab to use SSH for deployment:

  1. Add the EC2 server as a remote repository in GitLab if needed.
  2. Add your SSH key (id_rsa) to GitLab’s CI/CD environment or a GitLab project so it can SSH into the EC2 instance during deployment.

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-continuous-deployment-pipeline-with-gitlab-on-ubuntu

Hope that this helps.

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more