GOPHERSPACE.DE - P H O X Y
gophering on raymii.org
This is a text-only version of the following page on https://raymii.org:
---
Title       :  Get all SSH public keys from gitlab
Author      :  Remy van Elst
Date        :  26-08-2020
URL         :  https://raymii.org/s/snippets/Get_all_SSH_public_keys_from_gitlab.html
Format      :  Markdown/HTML
---



This small snippet gets all the SSH keys from a gitlab instance.
You need to be an administrator, then you can query all keys at once using
the API. On the web frontend you can only see the keys per user, not all at
once in an overview.

[If you like this snippet, consider sponsoring me by trying out a Digital Ocean
VPS. With this link you'll get $100 credit for 60 days). (referral link)][99]




I'm using `jq` to filter out specific fields of the JSON API output, but you can
omit that and just use shell (`awk`/`sed`) if you want to.

### SSH keys via the API

In [the API] you can query keys by ID. As far as I could find, there is no way to
find out how many keys there are, so you have to gamble that a bit, in my case 
500 was enough since the gitlab instace I used only has 400-ish users.

You also need a personal access token for your administrative account, which 
you can create via your gitlab profile, under `Settings` and then `Access Tokens`.

This is the full command:

for i in $(seq 1 500); do 
  curl --silent --header "PRIVATE-TOKEN: $your_token_here" "https://your_gitlab_URL.com/api/v4/keys/$i" | \
  jq -M -c -r '[.key, .user.name]'  | \
  grep -v null ;  
done

Replace the token and domain name by your domain name.

Example output:

["ssh-rsa AAAAB3NzaC1y[...]zmSDQ== key1","User Name"]
["ssh-rsa AAAAB3NzaC1y[...]GaE6cC1 key2","User Name"]
["ssh-ed25519 AAAAC3N[...]9W key5", "Other User Name"]

You can use the `|@sh` or `|@tsv` output modifier to get rid of the quotes and 
square brackets:

for i in $(seq 1 500); do 
  curl --silent --header "PRIVATE-TOKEN: $your_token_here" "https://your_gitlab_URL.com/api/v4/keys/$i" | \
  jq -M -c -r '[.key, .user.name]|@tsv'  | \
  grep -v null ;  
done


Output: 

ssh-ed25519 AAAAC3N[...]9W  key5 Other User Name



[1]: https://docs.gitlab.com/ee/api/keys.html

---

License:
All the text on this website is free as in freedom unless stated otherwise. 
This means you can use it in any way you want, you can copy it, change it 
the way you like and republish it, as long as you release the (modified) 
content under the same license to give others the same freedoms you've got 
and place my name and a link to this site with the article as source.

This site uses Google Analytics for statistics and Google Adwords for 
advertisements. You are tracked and Google knows everything about you. 
Use an adblocker like ublock-origin if you don't want it.

All the code on this website is licensed under the GNU GPL v3 license 
unless already licensed under a license which does not allows this form 
of licensing or if another license is stated on that page / in that software:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .

Just to be clear, the information on this website is for meant for educational 
purposes and you use it at your own risk. I do not take responsibility if you 
screw something up. Use common sense, do not 'rm -rf /' as root for example. 
If you have any questions then do not hesitate to contact me.

See https://raymii.org/s/static/About.html for details.