fixup links again, due to redirect taking over!
[gitolite-doc.git] / docs / sts.mkd
blobd275552cea9909ece0f4bdb50d860312815bf6a5
1 # ssh troubleshooting and tips
3 ----
5 **This page must be read in full the first time.  If you start from some nice
6 looking section in the middle it may not help you unless you're already an
7 expert at ssh**.
9 This page should help you troubleshoot ssh-related problems in installing and
10 accessing gitolite.  It also has a section of random ssh-related tips and
11 tricks that gitolite can do.
13 # IMPORTANT -- READ THIS FIRST
15 ## caveats
17   * Before reading this page, it is **mandatory** to read and **completely
18     understand** [this](glssh), which is a very detailed look at how gitolite
19     uses ssh's features on the server side.  Don't assume you know all that;
20     if you did, you wouldn't be needing *this* page either!
22   * This page, and others linked from this, together comprise all the help I
23     can give you in terms of the ssh aspect of using gitolite.  If you're
24     installing gitolite, you're a "system admin", like it or not.  Ssh is
25     therefore a necessary skill.  Please take the time to learn at least
26     enough to get passwordless access working.
28   * Please note that authentication is not really gitolite's job at all.  I'd
29     rather spend time on actual gitolite features, code, and documentation
30     than authentication (i.e., ssh, in the common case).
32     Surprised?  [This][auth] might help explain better.
34 [auth]: concepts/#authentication-and-authorisation
36 ## naming conventions used
38   * Your workstation is the **client**.  Your userid on the client does not
39     matter, and it has no relation to your gitolite username.
41   * The server is called **server** and the "hosting user" is **git**.  If
42     this is an RPM/DEB install, the hosting user is probably called
43     "gitolite", however we will use "git" in this page.
45 ## taking stock -- relevant files and directories
47   * The client has a `~/.ssh` containing a few keypairs.  It may also have a
48     `config` file.
50   * The client also has a clone of the "gitolite-admin" repo, which contains a
51     bunch of `*.pub` files in `keydir`.  We assume this clone is in `$HOME`;
52     if it is not, adjust instructions accordingly when needed.
54   * The git user on the server has a `~/.ssh/authorized_keys` file that the
55     ssh daemon uses to authenticate incoming users.  We often call this file
56     **authkeys** to save typing, and it always means the one on the server
57     (we're not interested in this file on the client side).
59   * The server also has a `~/.gitolite/keydir` which contains a bunch of
60     `*.pub` files.
62 ## normal gitolite key handling
64 Here's how normal gitolite key handling works:
66   * (On client) pub key changes like adding new ones, deleting old ones, etc.,
67     are done in the `keydir` directory in the gitolite-admin repo clone.  Then
68     the admin `git add`s and `git commit`s those changes, then `git push`es
69     them to the server.
71   * (On server) a successful push from the client makes git invoke the
72     post-update hook in the gitolite-admin repo.  This hook is installed by
73     gitolite, and it does a bunch of things which are quite transparent to
74     the admin, but we'll describe briefly here:
76       * The pubkey files from this push are checked-out into
77         `~/.gitolite/keydir` (and similarly the config files into
78         `~/.gitolite/conf`).
80       * The "compile" script then runs, which uses these files to populate
81         `~/.ssh/authorized_keys` on the server.
83         The authkeys file may have other, (non-gitolite) keys also.  Those
84         lines are preserved.  Gitolite only touches lines that are found
85         between gitolite's "marker" lines (`# gitolite start` and `# gitolite end`).
87 # common ssh problems
89 Since I'm pretty sure at least some of you didn't bother to read the
90 "IMPORTANT: PLEASE READ FIRST" section above, let me take a minute to point
91 you there again.  Especially the first bullet.
93 Done?  OK, read on...
95 The following problem(s) indicate that pubkey access is not working at all, so
96 you should start with [appendix 1][stsapp1].  If that doesn't fix the problem, continue
97 with the other appendices in sequence.
99   * Running any git clone/fetch/ls-remote or just `ssh git@server info` asks
100     you for a password.  (Or, if your sshd is set to use keys only, it just
101     disconnects without doing anything).
103 The following problem(s) indicate that your pubkey is bypassing gitolite and
104 going straight to a shell.  You should start with [appendix 2][sshkeys-lint]
105 and continue with the rest in sequence.  [Appendix 5][ybpfail] has some
106 background info.
108   * Running `ssh git@server info` gets you the output of the GNU 'info'
109     command instead of gitolite's version and access info.
111   * Running `git clone git@server:repositories/reponame` (note presence of
112     `repositories/` in URL) works.
114     [A proper gitolite key will only let you `git clone git@server:reponame`
115     (note absence of `repositories/`)]
117   * You are able to clone repositories but are unable to push changes back
118     (the error complains about the `GL_BINDIR` environment variable not being
119     set or about not being able to locate `Gitolite/Hooks/Update.pm`, and the
120     `hooks/update` failing in some way).
122     [If you run `git remote -v` you will find that your clone URL included the
123     `repositories/` described above!]
125   * Conversely, using the correct syntax, `git clone git@server:reponame`
126     (note absence of `repositories/` in the URL), gets you
127     `fatal: 'reponame' does not appear to be a git repository`, and yet
128     you are sure 'reponame' exists, you haven't mis-spelled it, etc.
130 # step by step
132 Since I'm pretty sure at least some of you didn't bother to read the
133 "IMPORTANT: PLEASE READ FIRST" section above, let me take a minute to point
134 you there again.  Especially the first bullet.
136 Done?  OK, now the general outline for ssh troubleshooting is this:
138   * Make sure the server's overall setup even *allows* pubkey based login.
139     I.e., check that git fetch/clone/ls-remote commands or a plain
140     `ssh git@server info` do NOT ask for a password.  If you do get asked for
141     a password, see [appendix 1][stsapp1].
143   * Match client-side pubkeys (`~/.ssh/*.pub`) with the server's authkeys
144     file.  To do this, run `sshkeys-lint`, which tells you in detail what key
145     has what access.  See [appendix 2][sshkeys-lint].
147   * At this point, we know that we have the right key, and that if sshd
148     receives that key, things will work.  But we're not done yet.  We still
149     need to make sure that this specific key is being offered/sent by the
150     client, instead of the default key.  See [appendix 3][stsapp3] and
151     [appendix 4][ssh-ha].
153 # random tips, tricks, and notes
155 ## giving shell access to gitolite users
157 Thanks to an idea from Jesse Keating, a single key can allow both gitolite
158 access *and* shell access.
160 <span class="gray">(v3.6.1+)</span> There are two ways to do this, both require uncommenting
161 and editing the 'Shell' line in the ENABLE list in the rc file.
163 If you have only a few users who need to get shell access, edit the line to
164 look like this (note the trailing comma!):
166     'Shell alice bob',
168 If you have lots of users, add them to some file accessible to gitolite, (one
169 per line, no extra whitespace), then specify the full path of the file.  For
170 example:
172     "Shell $ENV{HOME}/.gitolite.shell-users",
174 (Note in this case we have to use double quotes since we are using a variable
175 that needs to be interpolated into the value).
177 Then run `gitolite compile; gitolite trigger POST_COMPILE` or push a dummy
178 change to the admin repo.
180 ### the SHELL\_USERS\_LIST
182 If you're using gitolite v3.6 or below, it's slightly different.  You have to
183 enable the trigger by uncommenting the 'Shell' line in the ENABLE list, but
184 you cannot list the users directly on the 'Shell' line in the rc file, nor can
185 you put the file name on that line. Instead, you have to go to the variables
186 section in the rc file and set the SHELL\_USERS\_LIST variable to the
187 filename.  For example:
189     SHELL_USERS_LIST        =>  "$ENV{HOME}/.gitolite.shell-users",
191 Then run `gitolite compile; gitolite trigger POST_COMPILE` or push a dummy
192 change to the admin repo.
194 **NOTE**: to maintain backward compatibility, this method will continue to
195 work in 3.6.\*, but when 3.7 is released (whenever that may be), it will not
196 work, and you will have to use the new syntax described above.
198 ## distinguishing one key from another
200 Since a user can have [more than one key][multi-key], it is sometimes useful
201 to distinguish one key from another.  Sshd does not tell you even the
202 fingerprint of the key that finally matched, so normally all you have is the
203 `GL_USER` env var.
205 [multi-key]: basic-admin/#multiple-keys-per-user
207 However, if you replace
209     'ssh-authkeys',
211 in the ENABLE list with
213     'ssh-authkeys --key-file-name',
215 then an extra argument is added after the username in the "command" variable
216 of the authkeys file.  That is, instead of this:
218     command="/home/g3/gitolite/src/gitolite-shell u3",no-port-forwarding,...
220 you get this:
222     command="/home/g3/gitolite/src/gitolite-shell u3 keydir/u3.pub",no-port-forwarding,...
224 You can then write an INPUT trigger to do whatever you need with the file
225 name, which is in `$ARGV[1]` (the second argument).  The actual file is
226 available at `$ENV{GL_ADMIN_BASE}/$ARGV[1]` if you need its contents.
228 ## simulating ssh-copy-id
230 don't have `ssh-copy-id`?  This is broadly what that command does, if you want
231 to replicate it manually.  The input is your pubkey, typically
232 `~/.ssh/id_rsa.pub` from your client/workstation.
234   * It copies it to the server as some file.
236   * It appends that file to `~/.ssh/authorized_keys` on the server
237     (creating it if it doesn't already exist).
239   * It then makes sure that all these files/directories have go-w perms
240     set (assuming user is "git"):
242         /home/git/.ssh/authorized_keys
243         /home/git/.ssh
244         /home/git
246 [Actually, `sshd` requires that even directories *above* `~` (`/`, `/home`,
247 typically) also must be `go-w`, but that needs root.  And typically
248 they're already set that way anyway.  (Or if they're not, you've got
249 bigger problems than gitolite install not working!)]
251 ## problems with using non-openssh public keys
253 Gitolite accepts public keys only in openssh format.  Trying to use an "ssh2"
254 key (used by proprietary SSH software) will not be a happy experience.
255 src/triggers/post-compile/ssh-authkeys can be made to detect non-openssh
256 formats and automatically convert them; patches welcome!
258 The actual conversion command, if you want to just do it manually for now and
259 be done with it, is:
261     ssh-keygen -i -f /tmp/ssh2/YourName.pub > /tmp/openssh/YourName.pub
263 then use the resulting pubkey as you normally would in gitolite.
265 ## windows issues
267 On windows, I have only used msysgit, and the openssh that comes with it.
268 Over time, I have grown to distrust putty/plink due to the number of people
269 who seem to have trouble when those beasts are involved (I myself have never
270 used them for any kind of git access).  If you have unusual ssh problems that
271 just don't seem to have any explanation, try removing all traces of
272 putty/plink, including environment variables, etc., and then try again.
274 Thankfully, someone contributed [this](contrib/putty).
276 # appendix 1: ssh daemon asks for a password
278 >   **NOTE**: This section should be useful to anyone trying to get
279 >   password-less access working.  It is not necessarily specific to gitolite,
280 >   so keep that in mind if the wording feels a little more general than you
281 >   were expecting.
283 You have generated a keypair on your workstation (`ssh-keygen`) and copied the
284 public part of it (`~/.ssh/id_rsa.pub`, by default) to the server.
286 On the server you have appended this file to `~/.ssh/authorized_keys`.  Or you
287 ran something, like the `gitolite setup` step during a gitolite install, which
288 should have done that for you.
290 You now expect to log in without having to type in a password, but when you
291 try, you are being asked for a password.
293 This is a quick checklist:
295   * Make sure you're being asked for a password and not a pass*phrase*.  Do
296     not confuse or mistake a prompt saying
297     `Enter passphrase for key '/home/sitaram/.ssh/id_rsa':` for a password
298     prompt from the remote server!
300     When you create an ssh keypair using `ssh-keygen`, you have the option of
301     protecting it with a passphrase.  When you subsequently use that keypair
302     to access a remote host, your *local* ssh client needs to unlock the
303     corresponding private key, and ssh will probably ask for the passphrase
304     you set when you created the keypair.
306     You have two choices to avoid this prompt every time you try to use the
307     private key.  The first is to create keypairs *without* a passphrase (just
308     hit enter when prompted for one).  **Be sure to add a passphrase later,
309     once everything is working, using `ssh-keygen -p`**.
311     The second is to use `ssh-agent` (or `keychain`, which in turn uses
312     `ssh-agent`) or something like that to manage your keys.  Other than
313     discussing one more potential trouble-spot with ssh-agent (see below),
314     further discussion of ssh-agent/keychain is out of scope of this page.
316   * Ssh is very sensitive to permissions.  An extremely conservative setup is
317     given below, but be sure to do this on **both the client and the server**:
319         cd $HOME
320         chmod go-rwx .
321         chmod -R go-rwx .ssh
323   * Actually, every component of the path to `~/.ssh/authorized_keys` all the
324     way upto the root directory must be at least `chmod go-w`.  So be sure to
325     check `/` and `/home` also.
327   * While you're doing this, make sure the owner and group info for each of
328     these components are correct.  `ls -ald ~ ~/.ssh ~/.ssh/authorized_keys`
329     will tell you what they are.
331   * You may also want to check `/etc/ssh/sshd_config` to see if the "git" user
332     is allowed to login at all.  For example, if that file contains an
333     `AllowUsers` config entry, then only users mentioned in that line are
334     allowed to log in!
336   * While you're in there, check that file does NOT have a setting for
337     `AuthorizedKeysFile`.  See `man sshd_config` for details.  This setting is
338     a show stopper for gitolite to use ssh.
340   * Some OSs/distributions require that the "git" user should have a password
341     and/or not be a locked account.  You may want to check that as well.
343   * If your server is running SELinux, and you install gitolite to
344     `/var/gitolite` or another location unsupported by default SELinux
345     policies, then SELinux will prevent sshd from reading
346     `.ssh/authorized_keys`. Consider installing gitolite to
347     `/var/lib/gitolite`, which is a supported location by default SELinux
348     policies.
350   * If all that fails, log onto the server as root, `cd /var/log`, and look
351     for a file called `auth.log` or `secure` or some such name.  Look inside
352     this file for messages matching the approximate time of your last attempt
353     to login, to see if they tell you what is the problem.
355 # appendix 2: which key is which -- running sshkeys-lint
357 The sshkeys-lint program can be run on the server or the client.  Run it with
358 '-h' to get a help message.
360 On the server you can run `gitolite sshkeys-lint` and it will tell you, for
361 each key in the admin directory's keydir, what access is available.  This is
362 especially good at finding duplicate keys and such.
364 To run it on the client you have to copy the file src/commands/sshkeys-lint
365 from some gitolite clone, then follow these steps:
367   * Get a copy of `~/.ssh/authorized_keys` from the server and put it in
368     `/tmp/foo` or something.
370   * cd to `~/.ssh`.
372   * Run `/path/to/sshkeys-lint *.pub < /tmp/foo`.
374 Note that it is not trying to log in or anything -- it's just comparing
375 fingerprints as computed by `ssh-keygen -l`.
377 If the pubkey file you're interested in appears to have the correct access to
378 the server, you're done with this step.
380 Otherwise you have to rename some keypairs and try again to get the effect you
381 need.  Be careful:
383   * Do not just rename the ".pub" file; you will have to rename the
384     corresponding private key also (the one with the same basename but without
385     an extension).
387   * If you're running ssh-agent, you may have to delete (using `ssh-add -D`)
388     and re-add identities for it to pick up the renamed ones correctly.
390 ## typical cause(s)
392 The admin often has passwordless shell access to `git@server` already, and
393 then used that same key to get access to gitolite (i.e., copied that same
394 pubkey as YourName.pub and ran `gitolite setup` on it).
396 As a result, the same key appears twice in the authkeys file now, and since
397 the ssh server will always use the first match, the second occurrence (which
398 invokes gitolite) is ignored.
400 To fix this, you have to use a different keypair for gitolite access.  The
401 best way to do this is to create a new keypair, copy the pubkey to the server
402 as YourName.pub, then run `gitolite setup -pk YourName.pub` on the server.
403 Remember to adjust your agent identities using ssh-add -D and ssh-add if
404 you're using ssh-agent, otherwise these new keys may not work.
406 # appendix 3: ssh client may not be offering the right key
408   * Make sure the right private key is being offered.  Run ssh in very
409     verbose mode and look for the word "Offering", like so:
411         ssh -vvv user@host pwd 2> >(grep -i offer)
413     If some keys *are* being offered, but not the key that was supposed to be
414     used, you may be using ssh-agent (next bullet).  You may also need to
415     create some host aliases in `~/.ssh/config` ([appendix 4][ssh-ha]).
417   * (ssh-agent issues) If `ssh-add -l` responds with either "The agent has no
418     identities." or "Could not open a connection to your authentication
419     agent.", then you can skip this bullet.
421     However, if `ssh-add -l` lists *any* keys at all, then something weird
422     happens.  Due to a quirk in ssh-agent, ssh will now *only* use one of
423     those keys, *even if you explicitly ask* for some other key to be used.
425     In that case, add the key you want using `ssh-add ~/.ssh/YourName` and try
426     the access again.
428 # appendix 4: ssh host aliases
430 (or "making git use the right options for ssh")
432 The ssh command has several options for non-default items to be specified.
433 Two common examples are `-p` for the port number if it is not 22, and `-i` for
434 the public key file if you do not want to use just `~/.ssh/id_rsa` or such.
436 Git has two ssh-based URL syntaxes, but neither allows specifying a
437 non-default public key file.  And a port number is only allowed in one of
438 them.  (See `man git-clone` for details).  Finally, hosts often have to be
439 referred with IP addresses (such is life), or the name is very long, or hard
440 to remember.
442 Using a "host" para in `~/.ssh/config` lets you nicely encapsulate all this
443 within ssh and give it a short, easy-to-remember, name.  Example:
445 ```sshconfig
446 host gitolite
447     user git
448     hostname a.long.server.name.or.annoying.IP.address
449     port 22
450     identityfile ~/.ssh/id_rsa
453 Now you can simply use the one word `gitolite` (which is the host alias we
454 defined here) and ssh will infer all those details defined under it -- just
455 say `ssh gitolite` and `git clone gitolite:reponame` and things will work.
457 (By the way, the 'port' and 'identityfile' lines are needed only if you have
458 non-default values, although I put them in anyway just to be complete).
460 ## more than one keypair
462 If you have *more than one* pubkey with access to the *same* server, you
463 **must** use this method to make git pick up the right key.  There is no other
464 way to do this, as far as I know.
466 A typical example would be if you wanted shell access to the gitolite server
467 using one keypair, and gitolite-mediated access using another.  Here's how I
468 do that, where my "id\_rsa" keypair has shell access, and my "sitaram" keypair
469 has gitolite access:
471 ```sshconfig
472 host gitolite
473     user git
474     hostname gitolite.mydomain.com
475     port 22
476     identityfile ~/.ssh/sitaram
478 host gitolite-sh
479     user git
480     hostname gitolite.mydomain.com
481     port 22
482     identityfile ~/.ssh/id_rsa
485 Then I would use "ssh gitolite-ssh" to get a command line, and use the host
486 alias "gitolite" in git clone and other commands, as well as for gitolite
487 commands (like "ssh gitolite info").
489 Just to be clear, please note that this assumes the authorized keys file on
490 the gitolite hosting user has my "id\_rsa.pub" line, **without** the gitolite
491 related forced command and options.
493 [tut]: http://sites.google.com/site/senawario/home/gitolite-tutorial
495 # appendix 5: why bypassing gitolite causes a problem
497 When you bypass gitolite, you end up running your normal shell instead of the
498 special gitolite entry point script `gitolite-shell`.
500 This means commands (like 'info') are interpreted by the shell instead of
501 gitolite.
503 It also means git operations look for repos in `$HOME`.
505 However, gitolite places all your repos in `~/repositories`, and internally
506 prefixes this before calling the actual git command you invoked.  Thus, the
507 pathname of the repo that you use on the client is almost never the correct
508 pathname on the server.  (This is by design.  Don't argue...)
510 This means that, you get 2 kinds of errors if you bypass gitolite
512   * When you use `git@server:reponame` with a key that bypasses gitolite
513     (i.e., gets you a shell), this prefixing does not happen, and so the repo
514     is not found.  Neither a clone/fetch nor a push will work.
516   * Conversely, consider `git@server:repositories/reponame.git`.  The clone
517     operation will work -- you're using the full Unix path, and so the shell
518     finds the repo where you said it would be.  However, when you push,
519     gitolite's **update hook** kicks in, and fails to run because some of the
520     environment variables it is expecting are not present.
522 [stsapp1]: sts/#appendix-1-ssh-daemon-asks-for-a-password
523 [stsapp3]: sts/#appendix-3-ssh-client-may-not-be-offering-the-right-key
524 [sshkeys-lint]: sts/#appendix-2-which-key-is-which-running-sshkeys-lint
525 [ybpfail]: sts/#appendix-5-why-bypassing-gitolite-causes-a-problem
526 [ssh-ha]: sts/#appendix-4-ssh-host-aliases