How to add an SSH key to a user's account via the terminal/bash executable?


Hopefully this question is regarded well, but I don’t know how vague or stupid my question may be.

I am creating a bash executable, which creates an SSH key, and uploads it to a user’s Gitlab account. I am aware of how to create the SSH key via the bash executable: ssh-keygen -o -f ~/.ssh/id_rsa , and I also know how to retrieve from it, however I don’t know how to upload it to a user’s Gitlab account.

I have found multiple documentations for uploading a user’s SSH to Git hub however not Gitlab (I assume mostly similar…?). So I would use this for Git hub curl -u "USERNAME:PASSWORD" --data "{\"title\": \"TITLE\", \"key\": \"$(cat ~/.ssh/\"}" , and I would make USERNAME, PASSWORD, and TITLE input fields for the user to customize.

I want to say that it would be as simple for Gitlab (I found this POST /users/:id/keys on their API site (, but don’t know how to implement it as a curl command), but I don’t know how closely related Gitlab and Github are.

I tried to include as much as I know in this question, however forgive me if it is way too vague. Any help in the right direction does help. Thank you :slight_smile:

This was a question from the Ubuntu Stack Exchange, but it seems like it can be answered here as well!