Gitlab Deploy though SSH



This content originally appeared on DEV Community and was authored by Booranasak Kanthong

1.) Generate an SSH Key Locally (or Use an Existing One)

Run the following command in your local terminal to generate a new SSH key:

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

Simply press Enter through the prompts to accept the defaults and create the key pair.

2.) Add the Public Key to the VM Server

On your VM server, append the contents of the generated public key (typically located at ~/.ssh/id_rsa.pub) to the authorized_keys file:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

3.) Add the Private Key to Your GitLab Repository

To allow GitLab CI/CD jobs to connect to your VM via SSH:

3.1) Go to your GitLab repository.
3.2) Navigate to Settings → CI/CD → Variables.
3.3) Click “Add variable”.
3.4) Set the key (e.g., SSH_PRIVATE_KEY) and paste your private key (from ~/.ssh/id_rsa) as the value.
3.5) Make sure to mask and protect the variable as appropriate.

Never share your private key. Treat it like a password.


This content originally appeared on DEV Community and was authored by Booranasak Kanthong