Skip to content

Commit 302fbbd

Browse files
Provide instructions on how to use a specific SSH key (#9500)
* Adding instructions to use a specific SSH key * Making paragraph easier to read * Rewording * Rewriting note * Rewriting (more specific) * Implementing review suggestions * Implementing suggestions to improve clarity --------- Co-authored-by: Rosie Yohannan <[email protected]>
1 parent ebdcffd commit 302fbbd

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

docs/guides/modules/integration/pages/add-ssh-key.adoc

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ See the following VCS-specific docs for additional details on creating SSH keys:
1818
****
1919

2020
[#steps-to-add-additional-ssh-keys]
21-
== 1. Add an additional SSH key to your project
21+
== Add an additional SSH key to your project
2222

2323
CircleCI cannot decrypt SSH keys, therefore every new key must have an empty passphrase. The below examples are for macOS.
2424

@@ -35,7 +35,7 @@ If you have a GitLab integration, you will find that an additional SSH key alrea
3535
. Select *Add SSH Key*.
3636

3737
[#add-ssh-keys-to-a-job]
38-
== 2. Add SSH keys to a job
38+
== Add SSH keys to a job
3939

4040
Even though all CircleCI jobs use `ssh-agent` to automatically sign all added SSH keys, you *must* use the `add_ssh_keys` key to actually add keys to a container.
4141

@@ -63,9 +63,49 @@ All fingerprints in the `fingerprints` list must correspond to keys that have be
6363
To checkout additional repositories from within your job, ensure that you run the `checkout` command *before* `add_ssh_keys`; otherwise, `CIRCLE_CI_REPOSITORY_URL` will be empty. Also ensure that the private key is added to the CircleCI project and that the public key has been added to the additional repositories that you want to checkout from within your job.
6464

6565
[#adding-multiple-keys-with-blank-hostnames]
66-
== Adding multiple keys with blank hostnames
66+
=== Adding multiple keys with blank hostnames
6767

68-
If you need to add multiple SSH keys with blank hostnames to your project, you will need to make some changes to the default SSH configuration provided by CircleCI. In the scenario where you have multiple SSH keys that have access to the same hosts, but are for different purposes the default `IdentitiesOnly no` is set causing connections to use `ssh-agent`. This will always cause the first key to be used, even if that is the incorrect key. If you have added the SSH key to a container, you will need to either set `IdentitiesOnly no` in the appropriate block, or you can remove all keys from the `ssh-agent` for this job using `ssh-add -D`, and reading the key added with `ssh-add /path/to/key`.
68+
If you add multiple SSH keys with blank hostnames to your project, you will need to make some changes to the default SSH configuration provided by CircleCI.
69+
70+
If you have multiple SSH keys for different purposes that have access to the same hosts, the default `IdentitiesOnly no` is set, which causes connections to use `ssh-agent`.
71+
72+
In this scenario the first key is used regardless of whether it is the correct key. If you have added the SSH key to a container, you will need to either set `IdentitiesOnly yes` in the appropriate block, or you can remove all keys from the `ssh-agent` for the job using `ssh-add -D` and read the added key using `ssh-add /path/to/key`.
73+
74+
[#using-specific-ssh-keys-for-a-job]
75+
=== Using specific SSH keys for a job
76+
77+
[NOTE]
78+
====
79+
SSH keys are named using MD5 fingerprints with colons removed from the hash.
80+
81+
To retrieve the MD5 fingerprint of an SSH key, run this command locally:
82+
83+
`ssh-keygen -l -E md5 -f ~/.ssh/your_key_name`
84+
85+
The filename follows this format:
86+
87+
`id_rsa_[MD5_fingerprint_without_colons]`
88+
89+
Example:
90+
91+
- SSH key MD5 fingerprint: `a3:f9:82:c5:1d:47:9b:e6:88:32:a1:bc:f4:29:7e:15`.
92+
93+
- SSH key filename: `id_rsa_a3f982c51d479be68832a1bcf4297e15`.
94+
====
95+
96+
97+
If you have added multiple SSH keys for the same hostname to your project and want to use a specific key in your SSH commands, follow these steps:
98+
99+
. Add the SSH key to the job using the `add_ssh_keys` step.
100+
. Use the `-i` parameter to specify the exact SSH key file path in your SSH commands.
101+
. Add the `-o "IdentitiesOnly=yes"` option to prevent SSH from using other keys from the SSH agent.
102+
103+
Example:
104+
105+
[source,bash]
106+
----
107+
ssh -i ~/.ssh/id_rsa_a3f982c51d479be68832a1bcf4297e15 -o "IdentitiesOnly=yes" user@hostname
108+
----
69109

70110
[#see-also]
71111
== See also

0 commit comments

Comments
 (0)