BigW Consortium Gitlab

README.md 7.02 KB
Newer Older
1 2
# SSH

3 4 5 6
Git is a distributed version control system, which means you can work locally
but you can also share or "push" your changes to other servers.
Before you can push your changes to a GitLab server
you need a secure communication channel for sharing information.
7 8 9 10 11 12

The SSH protocol provides this security and allows you to authenticate to the
GitLab remote server without supplying your username or password each time.

For a more detailed explanation of how the SSH protocol works, we advise you to
read [this nice tutorial by DigitalOcean](https://www.digitalocean.com/community/tutorials/understanding-the-ssh-encryption-and-connection-process).
13 14 15

## Locating an existing SSH key pair

16
Before generating a new SSH key pair check if your system already has one
17 18 19 20
at the default location by opening a shell, or Command Prompt on Windows,
and running the following command:

**Windows Command Prompt:**
21

22 23 24
```bash
type %userprofile%\.ssh\id_rsa.pub
```
25

26
**Git Bash on Windows / GNU/Linux / macOS / PowerShell:**
27

28 29 30
```bash
cat ~/.ssh/id_rsa.pub
```
31

32
If you see a string starting with `ssh-rsa` you already have an SSH key pair
33 34
and you can skip the generate portion of the next section and skip to the copy
to clipboard step.
35 36
If you don't see the string or would like to generate a SSH key pair with a
custom name continue onto the next step.
37

38 39 40 41 42 43
>
**Note:** Public SSH key may also be named as follows:
- `id_dsa.pub`
- `id_ecdsa.pub`
- `id_ed25519.pub`

44
## Generating a new SSH key pair
45

46
1. To generate a new SSH key pair, use the following command:
47

48
    **Git Bash on Windows / GNU/Linux / macOS:**
49

50
    ```bash
51
    ssh-keygen -t rsa -C "your.email@example.com" -b 4096
52
    ```
53

54
    **Windows:**
55

56
    Alternatively on Windows you can download
57
    [PuttyGen](http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html)
58
    and follow [this documentation article][winputty] to generate a SSH key pair.
59

60
1. Next, you will be prompted to input a file path to save your SSH key pair to.
61

62
    If you don't already have an SSH key pair use the suggested path by pressing
63 64
    enter. Using the suggested path will normally allow your SSH client
    to automatically use the SSH key pair with no additional configuration.
65

66 67 68
    If you already have a SSH key pair with the suggested file path, you will need
    to input a new file path and declare what host this SSH key pair will be used
    for in your `.ssh/config` file, see [**Working with non-default SSH key pair paths**](#working-with-non-default-ssh-key-pair-paths)
69
    for more information.
70

71 72 73 74
1. Once you have input a file path you will be prompted to input a password to
   secure your SSH key pair. It is a best practice to use a password for an SSH
   key pair, but it is not required and you can skip creating a password by
   pressing enter.
75

76
     >**Note:**
77 78
     If you want to change the password of your SSH key pair, you can use
     `ssh-keygen -p <keyname>`.
79

80
1. The next step is to copy the public SSH key as we will need it afterwards.
81

82
    To copy your public SSH key to the clipboard, use the appropriate code below:
83

84
    **macOS:**
85

86 87 88
    ```bash
    pbcopy < ~/.ssh/id_rsa.pub
    ```
89

90
    **GNU/Linux (requires the xclip package):**
91

92 93 94 95 96 97 98 99 100
    ```bash
    xclip -sel clip < ~/.ssh/id_rsa.pub
    ```

    **Windows Command Line:**

    ```bash
    type %userprofile%\.ssh\id_rsa.pub | clip
    ```
101

102
    **Git Bash on Windows / Windows PowerShell:**
103

104 105 106
    ```bash
    cat ~/.ssh/id_rsa.pub | clip
    ```
107

108 109
1. The final step is to add your public SSH key to GitLab.

110
    Navigate to the 'SSH Keys' tab in your 'Profile Settings'.
111 112 113 114 115 116
    Paste your key in the 'Key' section and give it a relevant 'Title'.
    Use an identifiable title like 'Work Laptop - Windows 7' or
    'Home MacBook Pro 15'.

    If you manually copied your public SSH key make sure you copied the entire
    key starting with `ssh-rsa` and ending with your email.
117 118 119 120
    
1. Optionally you can test your setup by running `ssh -T git@example.com`
   (replacing `example.com` with your GitLab domain) and verifying that you
   receive a `Welcome to GitLab` message.
121 122 123 124

## Working with non-default SSH key pair paths

If you used a non-default file path for your GitLab SSH key pair,
125 126 127 128 129
you must configure your SSH client to find your GitLab private SSH key
for connections to your GitLab server (perhaps `gitlab.com`).

For your current terminal session you can do so using the following commands
(replacing `other_id_rsa` with your private SSH key):
130

131 132 133 134 135 136 137 138 139 140 141
**Git Bash on Windows / GNU/Linux / macOS:**

```bash
eval $(ssh-agent -s)
ssh-add ~/.ssh/other_id_rsa
```

To retain these settings you'll need to save them to a configuration file.
For OpenSSH clients this is configured in the `~/.ssh/config` file for some
operating systems.
Below are two example host configurations using their own SSH key:
142 143 144 145 146

```
# GitLab.com server
Host gitlab.com
RSAAuthentication yes
147
IdentityFile ~/.ssh/config/private-key-filename-01
148 149 150 151

# Private GitLab server
Host gitlab.company.com
RSAAuthentication yes
152
IdentityFile ~/.ssh/config/private-key-filename
153 154
```

155 156 157
Due to the wide variety of SSH clients and their very large number of
configuration options, further explanation of these topics is beyond the scope
of this document.
158 159 160 161 162

Public SSH keys need to be unique, as they will bind to your account.
Your SSH key is the only identifier you'll have when pushing code via SSH.
That's why it needs to uniquely map to a single user.

163 164
## Deploy keys

165 166
Deploy keys allow read-only or read-write (if enabled) access to one or
multiple projects with a single SSH key pair.
167 168 169 170 171 172

This is really useful for cloning repositories to your Continuous
Integration (CI) server. By using deploy keys, you don't have to setup a
dummy user account.

If you are a project master or owner, you can add a deploy key in the
173 174
project settings under the section 'Repository'. Specify a title for the new
deploy key and paste a public SSH key. After this, the machine that uses
175 176
the corresponding private SSH key has read-only or read-write (if enabled) 
access to the project.
177

178
You can't add the same deploy key twice using the form.
179 180 181
If you want to add the same key to another project, please enable it in the
list that says 'Deploy keys from projects available to you'. All the deploy
keys of all the projects you have access to are available. This project
182
access can happen through being a direct member of the project, or through
183
a group.
184

185 186
Deploy keys can be shared between projects, you just need to add them to each
project.
karen Carias committed
187

188 189 190 191
## Applications

### Eclipse

192
How to add your SSH key to Eclipse: https://wiki.eclipse.org/EGit/User_Guide#Eclipse_SSH_Configuration
193 194

[winputty]: https://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter8.html#pubkey-puttygen
195 196 197 198 199 200 201 202 203 204 205 206

## Troubleshooting

If on Git clone you are prompted for a password like `git@gitlab.com's password:`
something is wrong with your SSH setup.

- Ensure that you generated your SSH key pair correctly and added the public SSH
  key to your GitLab profile
- Try manually registering your private SSH key using `ssh-agent` as documented 
  earlier in this document
- Try to debug the connection by running `ssh -Tv git@example.com`
  (replacing `example.com` with your GitLab domain)