[7a8cc527] | 1 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
| 2 | <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
|
---|
| 3 | "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
|
---|
| 4 | <!ENTITY % general-entities SYSTEM "../../general.ent">
|
---|
| 5 | %general-entities;
|
---|
| 6 | <!ENTITY gitgid "58">
|
---|
| 7 | <!ENTITY gituid "58">
|
---|
| 8 | ]>
|
---|
| 9 |
|
---|
[78f55b5] | 10 | <sect1 id="gitserver" xreflabel="Running a Git Server">
|
---|
[7a8cc527] | 11 | <?dbhtml filename="gitserver.html"?>
|
---|
| 12 |
|
---|
| 13 | <sect1info>
|
---|
| 14 | <date>$Date$</date>
|
---|
| 15 | </sect1info>
|
---|
| 16 |
|
---|
[78f55b5] | 17 | <title>Running a Git Server</title>
|
---|
[7a8cc527] | 18 |
|
---|
| 19 | <sect2 role="package">
|
---|
[78f55b5] | 20 | <title>Introduction</title>
|
---|
[7a8cc527] | 21 |
|
---|
| 22 | <para>
|
---|
[78f55b5] | 23 | This section will describe how to set up, administer and secure a
|
---|
| 24 | <application>git</application> server. <application>Git</application>
|
---|
[8558044] | 25 | has many options available. For more detailed documentation see
|
---|
| 26 | <ulink url="https://git-scm.com/book/en/v2"/>.
|
---|
[7a8cc527] | 27 | </para>
|
---|
| 28 |
|
---|
[78f55b5] | 29 | <bridgehead renderas="sect3">Server Dependencies</bridgehead>
|
---|
[7a8cc527] | 30 |
|
---|
| 31 | <bridgehead renderas="sect4">Required</bridgehead>
|
---|
| 32 | <para role="required">
|
---|
| 33 | <xref linkend="git"/> and
|
---|
| 34 | <xref linkend="openssh"/>
|
---|
| 35 | </para>
|
---|
| 36 |
|
---|
| 37 | </sect2>
|
---|
| 38 |
|
---|
| 39 | <sect2 role="configuration">
|
---|
[aacba80f] | 40 | <title>Setting up a Git Server</title>
|
---|
[7a8cc527] | 41 |
|
---|
| 42 | <para>
|
---|
| 43 | The following instructions will install a
|
---|
[78f55b5] | 44 | <application>git</application> server. It will be set
|
---|
[7a8cc527] | 45 | up to use <application>OpenSSH</application> as the secure
|
---|
| 46 | remote access method.
|
---|
| 47 | </para>
|
---|
| 48 |
|
---|
| 49 | <para>
|
---|
[78f55b5] | 50 | Configuration of the server consists of the following steps:
|
---|
[7a8cc527] | 51 | </para>
|
---|
| 52 |
|
---|
| 53 | <sect3>
|
---|
| 54 | <title>1. Setup Users, Groups, and Permissions</title>
|
---|
| 55 |
|
---|
| 56 | <para>
|
---|
[78f55b5] | 57 | You will need to be user <systemitem class='username'>root</systemitem>
|
---|
| 58 | for the initial portion of configuration. Create the <systemitem
|
---|
[29d3017a] | 59 | class="username">git</systemitem> user and group and set and unusable
|
---|
| 60 | password hash with the following commands:
|
---|
[7a8cc527] | 61 | </para>
|
---|
| 62 |
|
---|
| 63 | <screen role="root"><userinput>groupadd -g &gitgid; git &&
|
---|
[1e72aaad] | 64 | useradd -c "git Owner" -d /home/git -m -g git -s /usr/bin/git-shell -u &gituid; git &&
|
---|
[29d3017a] | 65 | sed -i '/^git:/s/^git:[^:]:/git:NP:/' /etc/shadow</userinput></screen>
|
---|
[7a8cc527] | 66 |
|
---|
| 67 | <para>
|
---|
[b9c353b] | 68 | Putting in an unusable password hash (replacing the <literal>!</literal>
|
---|
[29d3017a] | 69 | by <literal>NP</literal>) unlocks the account but it cannot be used
|
---|
[8558044] | 70 | to login via password authentication. That is required by
|
---|
[29d3017a] | 71 | <application>sshd</application> to work properly.
|
---|
| 72 | Next, create some files and directories in the home directory of the git user
|
---|
[78f55b5] | 73 | allowing access to the git repository using ssh keys.
|
---|
[7a8cc527] | 74 | </para>
|
---|
[8558044] | 75 |
|
---|
[7a8cc527] | 76 | <screen role="root"><userinput>install -o git -g git -dm0700 /home/git/.ssh &&
|
---|
[dc905ac] | 77 | install -o git -g git -m0600 /dev/null /home/git/.ssh/authorized_keys</userinput></screen>
|
---|
[7a8cc527] | 78 |
|
---|
| 79 | <para>
|
---|
| 80 | For any developer who should have access to the repository
|
---|
[78f55b5] | 81 | add his/her public ssh key to <filename>/home/git/.ssh/authorized_keys</filename>.
|
---|
[b4be4cf1] | 82 | First, prepend some options to prevent users from using the
|
---|
[7a8cc527] | 83 | connection to git for port forwarding to other machines
|
---|
| 84 | the git server might reach.
|
---|
| 85 | </para>
|
---|
| 86 |
|
---|
| 87 | <screen role="nodump"><userinput>echo -n "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty " >> /home/git/.ssh/authorized_keys &&
|
---|
[78f55b5] | 88 | cat <user-ssh-key> >> /home/git/.ssh/authorized_keys</userinput></screen>
|
---|
[7a8cc527] | 89 |
|
---|
[bbeb20f] | 90 | <para>
|
---|
[8558044] | 91 | It is also useful to set the default name of the initial branch
|
---|
[bbeb20f] | 92 | of new repositories by modifying the git configuration. As the
|
---|
| 93 | <systemitem class='username'>root</systemitem> user, run:
|
---|
| 94 | </para>
|
---|
| 95 |
|
---|
[1e72aaad] | 96 | <screen role="nodump"><userinput>git config --system init.defaultBranch trunk</userinput></screen>
|
---|
[bbeb20f] | 97 |
|
---|
[c4908df] | 98 | <para>
|
---|
| 99 | Finally add the <filename>/usr/bin/git-shell</filename> entry to
|
---|
| 100 | the <filename>/etc/shells</filename> configuration file. This shell
|
---|
| 101 | has been set in the <systemitem class='username'>git</systemitem>
|
---|
| 102 | user profile and is to make sure that only git related actions
|
---|
| 103 | can be executed:
|
---|
| 104 | </para>
|
---|
[8558044] | 105 |
|
---|
[c4908df] | 106 | <screen role="root"><userinput>echo "/usr/bin/git-shell" >> /etc/shells</userinput></screen>
|
---|
| 107 |
|
---|
[7a8cc527] | 108 | </sect3>
|
---|
| 109 |
|
---|
| 110 | <sect3>
|
---|
[aacba80f] | 111 | <title>2. Create a git repository</title>
|
---|
[7a8cc527] | 112 |
|
---|
| 113 | <para>
|
---|
[78f55b5] | 114 | The repository can be anywhere on the filesystem. It is
|
---|
[7a8cc527] | 115 | important that the git user has read/write access to that
|
---|
| 116 | location. We use <filename class="directory">/srv/git</filename>
|
---|
| 117 | as base directory. Create a new <application>git</application>
|
---|
| 118 | repository with the following commands (as the
|
---|
| 119 | <systemitem class="username">root</systemitem> user):
|
---|
| 120 | </para>
|
---|
| 121 |
|
---|
[b4be4cf1] | 122 | <note>
|
---|
| 123 | <para>
|
---|
| 124 | In all the instructions below, we use <emphasis>project1</emphasis>
|
---|
[8558044] | 125 | as an example repository name. You should name your repository
|
---|
[b4be4cf1] | 126 | as a short descriptive name for your specific project.
|
---|
| 127 | </para>
|
---|
| 128 | </note>
|
---|
| 129 |
|
---|
[78f55b5] | 130 | <screen role="root"><userinput>install -o git -g git -m755 -d /srv/git/project1.git &&
|
---|
| 131 | cd /srv/git/project1.git &&
|
---|
| 132 | git init --bare &&
|
---|
[7a8cc527] | 133 | chown -R git:git .</userinput></screen>
|
---|
[8558044] | 134 |
|
---|
[aacba80f] | 135 | </sect3>
|
---|
| 136 |
|
---|
| 137 | <sect3>
|
---|
| 138 | <title>3. Populate the repository from a client system</title>
|
---|
[7a8cc527] | 139 |
|
---|
[b4be4cf1] | 140 | <note>
|
---|
| 141 | <para>
|
---|
| 142 | All the instructions in this section and the next should
|
---|
| 143 | be done on a user system, not the server system.
|
---|
| 144 | </para>
|
---|
| 145 | </note>
|
---|
| 146 |
|
---|
[7a8cc527] | 147 | <para>
|
---|
| 148 | Now that the repository is created, it can be used by the
|
---|
| 149 | developers to put some files into it. Once the ssh key of
|
---|
| 150 | the user is imported to git's <filename>authorized_keys</filename>
|
---|
| 151 | file, the user can interact with the repository.
|
---|
| 152 | </para>
|
---|
| 153 |
|
---|
| 154 | <para>
|
---|
[78f55b5] | 155 | A minimal configuration should be available on the developer's
|
---|
[aacba80f] | 156 | system specifying its user name and the email address.
|
---|
[7a8cc527] | 157 | Create this minimal config file on client side:
|
---|
| 158 | </para>
|
---|
| 159 |
|
---|
| 160 | <screen role="nodump"><userinput>cat > ~/.gitconfig <<EOF
|
---|
| 161 | [user]
|
---|
| 162 | name = <users-name>
|
---|
| 163 | email = <users-email-address>
|
---|
| 164 | EOF</userinput></screen>
|
---|
| 165 |
|
---|
[78f55b5] | 166 | <para>
|
---|
| 167 | On the developer's machine, setup some files to be pushed
|
---|
[7a8cc527] | 168 | to the repository as the initial content:
|
---|
| 169 | </para>
|
---|
| 170 |
|
---|
[b4be4cf1] | 171 | <note>
|
---|
| 172 | <para>
|
---|
[8558044] | 173 | The <emphasis>gitserver</emphasis> term used below
|
---|
| 174 | should be the host name (or ip address) of the git server.
|
---|
[b4be4cf1] | 175 | </para>
|
---|
| 176 | </note>
|
---|
| 177 |
|
---|
[7a8cc527] | 178 | <screen role="nodump"><userinput>mkdir myproject
|
---|
| 179 | cd myproject
|
---|
[bbeb20f] | 180 | git init --initial-branch=trunk
|
---|
[7a8cc527] | 181 | git remote add origin git@gitserver:/srv/git/project1.git
|
---|
| 182 | cat >README <<EOF
|
---|
| 183 | This is the README file
|
---|
| 184 | EOF
|
---|
| 185 | git add README
|
---|
| 186 | git commit -m 'Initial creation of README'
|
---|
[bbeb20f] | 187 | git push --set-upstream origin trunk</userinput></screen>
|
---|
[7a8cc527] | 188 |
|
---|
| 189 | <para>The initial content is now pushed to the server and
|
---|
[8558044] | 190 | is available for other users. On the current machine, the
|
---|
[bbeb20f] | 191 | argument <literal>--set-upstream origin trunk</literal> is
|
---|
[7a8cc527] | 192 | now no longer required as the local repository is now
|
---|
| 193 | connected to the remote repository. Subsequent pushes
|
---|
| 194 | can be performed as
|
---|
| 195 | </para>
|
---|
| 196 |
|
---|
| 197 | <screen role="nodump"><userinput>git push</userinput></screen>
|
---|
[8558044] | 198 |
|
---|
[7a8cc527] | 199 | <para>
|
---|
| 200 | Other developers can now clone the repository and do
|
---|
| 201 | modifications to the content (as long as their ssh keys
|
---|
| 202 | has been installed):
|
---|
| 203 | </para>
|
---|
| 204 |
|
---|
| 205 | <screen role="nodump"><userinput>git clone git@gitserver:/srv/git/project1.git
|
---|
| 206 | cd project1
|
---|
| 207 | vi README
|
---|
| 208 | git commit -am 'Fix for README file'
|
---|
| 209 | git push</userinput></screen>
|
---|
| 210 |
|
---|
| 211 | <note>
|
---|
| 212 | <para>
|
---|
[78f55b5] | 213 | This is a very basic server setup based on
|
---|
| 214 | <application>OpenSSH</application> access. All developers are using
|
---|
| 215 | the <systemitem class="username">git</systemitem> user to perform
|
---|
[2c87187] | 216 | actions on the repository and the changes users are committing can be
|
---|
[5f729f8] | 217 | distinguished as the local user name (see
|
---|
[7a8cc527] | 218 | <filename>~/.gitconfig</filename>) is recorded in the
|
---|
[3741962] | 219 | changesets.
|
---|
| 220 | </para>
|
---|
[7a8cc527] | 221 | </note>
|
---|
| 222 |
|
---|
[78f55b5] | 223 | <para>
|
---|
| 224 | Access is restricted by the public keys added to git's
|
---|
[7a8cc527] | 225 | <filename>authorized_keys</filename> file and there is no
|
---|
| 226 | option for the public to export/clone the repository. To
|
---|
[b4be4cf1] | 227 | enable this, continue with step 4 to set up the git server
|
---|
| 228 | for public read-only access.
|
---|
[7a8cc527] | 229 | </para>
|
---|
| 230 |
|
---|
[3741962] | 231 | <para>
|
---|
[8558044] | 232 | In the URL used to clone the project, the absolute path (here
|
---|
[3741962] | 233 | <filename>/srv/git/project1.git</filename>) has to be specified
|
---|
| 234 | as the repository is not in git's home directory but in
|
---|
| 235 | <filename class="directory">/srv/git</filename>. To get rid of the
|
---|
| 236 | need to expose the structure of the server installation, a symlink
|
---|
| 237 | can be added in git's home directory for each project like this:
|
---|
| 238 | </para>
|
---|
| 239 | <screen role="nodump"><userinput>ln -svf /srv/git/project1.git /home/git/</userinput></screen>
|
---|
| 240 |
|
---|
| 241 | <para>
|
---|
| 242 | Now, the repository can be cloned using
|
---|
| 243 | </para>
|
---|
| 244 | <screen role="nodump"><userinput>git clone git@gitserver:project1.git</userinput></screen>
|
---|
| 245 |
|
---|
[7a8cc527] | 246 | </sect3>
|
---|
| 247 |
|
---|
[c4908df] | 248 | <sect3 id="gitserver-init">
|
---|
[aacba80f] | 249 | <title>4. Configure the Server</title>
|
---|
[7a8cc527] | 250 |
|
---|
| 251 | <para>
|
---|
| 252 | The setup described above makes a repository available for
|
---|
| 253 | authenticated users (via providing the ssh public key file).
|
---|
[8558044] | 254 | There is also a simple way to publish the
|
---|
[d6de80b] | 255 | repository to unauthenticated users — of course without write
|
---|
[7a8cc527] | 256 | access.
|
---|
| 257 | </para>
|
---|
[aacba80f] | 258 |
|
---|
[7a8cc527] | 259 | <para>
|
---|
[aacba80f] | 260 | The combination of access via ssh (for authenticated users) and
|
---|
[7a8cc527] | 261 | the export of repositories to unauthenticated users via the
|
---|
| 262 | daemon is in most cases enough for a development site.
|
---|
| 263 | </para>
|
---|
[8558044] | 264 |
|
---|
[7a8cc527] | 265 | <note>
|
---|
| 266 | <para>
|
---|
| 267 | The daemon will be reachable at port <literal>9418</literal>
|
---|
| 268 | by default. Make sure that your firewall setup allows
|
---|
| 269 | access to that port.
|
---|
| 270 | </para>
|
---|
| 271 | </note>
|
---|
| 272 |
|
---|
[c4908df] | 273 | <para revision="sysv">
|
---|
| 274 | To start the server at boot time, install the git-daemon
|
---|
| 275 | bootscript included in the <xref linkend="bootscripts"/> package:
|
---|
[7a8cc527] | 276 | </para>
|
---|
| 277 |
|
---|
[c4908df] | 278 | <indexterm zone="gitserver gitserver-init" revision="sysv">
|
---|
| 279 | <primary sortas="f-git">git</primary>
|
---|
| 280 | </indexterm>
|
---|
[5362771] | 281 |
|
---|
[c4908df] | 282 | <screen role="root" revision="sysv"><userinput>make install-git-daemon</userinput></screen>
|
---|
[78f55b5] | 283 |
|
---|
| 284 | <para revision="systemd">
|
---|
[c4908df] | 285 | To start the server at boot time, install the
|
---|
| 286 | <filename>git-daemon.service</filename> unit from the
|
---|
| 287 | <xref linkend="systemd-units"/> package:
|
---|
[78f55b5] | 288 | </para>
|
---|
| 289 |
|
---|
[c4908df] | 290 | <indexterm zone="gitserver gitserver-init" revision="systemd">
|
---|
| 291 | <primary sortas="f-gitserve">gitserve</primary>
|
---|
| 292 | </indexterm>
|
---|
| 293 |
|
---|
| 294 | <screen role="root" revision="systemd"><userinput>make install-git-daemon</userinput></screen>
|
---|
| 295 |
|
---|
[7a8cc527] | 296 | <para>
|
---|
[b4be4cf1] | 297 | In order to allow <application>git</application> to export a
|
---|
[7a8cc527] | 298 | repository, a file named <filename>git-daemon-export-ok</filename>
|
---|
| 299 | is required in each repository directory on the server. The
|
---|
[2c87187] | 300 | file needs no content, just its existence enables, its absence
|
---|
[7a8cc527] | 301 | disables the export of that repository.
|
---|
| 302 | </para>
|
---|
| 303 |
|
---|
| 304 | <screen role="root"><userinput>touch /srv/git/project1.git/git-daemon-export-ok</userinput></screen>
|
---|
| 305 |
|
---|
[52b47970] | 306 | <para revision="sysv">
|
---|
| 307 | The script to start the git daemon uses some default values
|
---|
| 308 | internally. Most important is the path to the repository
|
---|
| 309 | directory which is set to <filename class="directory">/srv/git</filename>.
|
---|
| 310 | In case you have for whatever reason created the repository in a
|
---|
| 311 | different location, you'll need to tell the boot script where the
|
---|
| 312 | repository is to be found. This can be achieved by creating a
|
---|
| 313 | configuration file named <filename>/etc/sysconfig/git-daemon</filename>.
|
---|
[dc905ac] | 314 | This configuration file will be imported if it exists, meaning it is
|
---|
| 315 | optional. The file can look like:</para>
|
---|
[52b47970] | 316 | <screen revision="sysv">
|
---|
| 317 | # Begin /etc/sysconfig/git-daemon
|
---|
| 318 |
|
---|
| 319 | # Specify the location of the git repository
|
---|
| 320 | GIT_BASE_DIR="/srv/git/"
|
---|
| 321 |
|
---|
| 322 | # Directories added to whitelist
|
---|
| 323 | DFT_REPO_DIR="$GIT_BASE_DIR"
|
---|
| 324 |
|
---|
| 325 | # Add extra options which will appended to the 'git daemon'
|
---|
| 326 | # command executed in the boot script
|
---|
| 327 | GIT_DAEMON_OPTS=""
|
---|
| 328 |
|
---|
| 329 | # End /etc/sysconfig/git-daemon
|
---|
| 330 | </screen>
|
---|
| 331 | <para revision="systemd">
|
---|
| 332 | Along with the <filename>git-daemon.service</filename> unit, a
|
---|
| 333 | configuration file named <filename>/etc/default/git-daemon</filename>
|
---|
| 334 | has been installed. Review this configuration file to match your
|
---|
| 335 | needs.
|
---|
[c4908df] | 336 | </para>
|
---|
| 337 |
|
---|
[52b47970] | 338 | <para>
|
---|
| 339 | There are only three options to set in the configuration file:
|
---|
| 340 | <itemizedlist>
|
---|
| 341 | <listitem>
|
---|
| 342 | <para>
|
---|
| 343 | GIT_BASE_DIR=<dirname>
|
---|
| 344 | </para>
|
---|
[d186c30] | 345 | <para>Specify the location of the git repositories.
|
---|
[8558044] | 346 | Relative paths used when accessing the daemon will
|
---|
[d186c30] | 347 | translated relative to this directory.
|
---|
[52b47970] | 348 | </para>
|
---|
| 349 | </listitem>
|
---|
| 350 | <listitem>
|
---|
| 351 | <para>
|
---|
| 352 | DFT_REPO_DIR=<dirname>
|
---|
| 353 | </para>
|
---|
[d186c30] | 354 | <para>This directory is added to the white list of allowed
|
---|
| 355 | directories. This variable can hold multiple directory
|
---|
[52b47970] | 356 | names but is usually set equal to <literal>GIT_BASE_DIR</literal>.
|
---|
| 357 | </para>
|
---|
| 358 | </listitem>
|
---|
| 359 | <listitem>
|
---|
| 360 | <para>
|
---|
| 361 | GIT_DAEMON_OPTS=<options>
|
---|
| 362 | </para>
|
---|
[d186c30] | 363 | <para>
|
---|
| 364 | In case special options to the <command>git daemon</command>
|
---|
| 365 | command are needed, they have to be specified in this setting.
|
---|
| 366 | One example might be to adjust the port number where daemon is
|
---|
| 367 | listening. In this case, add <literal>--port=<port
|
---|
| 368 | number></literal> to this variable. For more information
|
---|
| 369 | about which options can be set, take a look at the output of
|
---|
| 370 | <command>git daemon --help</command>.
|
---|
[52b47970] | 371 | </para>
|
---|
| 372 | </listitem>
|
---|
| 373 | </itemizedlist>
|
---|
| 374 | </para>
|
---|
[dc905ac] | 375 |
|
---|
| 376 | <para>
|
---|
| 377 | After starting the daemon, unauthenticated users can clone exported
|
---|
| 378 | repositories by using
|
---|
| 379 | </para>
|
---|
| 380 | <screen role="nodump"><userinput>git clone git://gitserver/project1.git</userinput></screen>
|
---|
| 381 |
|
---|
| 382 | <para>
|
---|
[8558044] | 383 | As the base directory is <filename class="directory">/srv/git</filename>
|
---|
[d186c30] | 384 | by default (or set to a custom value in the configuration),
|
---|
| 385 | <application>git</application> interprets the incoming path
|
---|
| 386 | (/project1.git) relative to that base directory so that the repository
|
---|
| 387 | in <filename class="directory">/srv/git/project1.git</filename> is
|
---|
| 388 | served.
|
---|
[dc905ac] | 389 | </para>
|
---|
[8558044] | 390 |
|
---|
[7a8cc527] | 391 | </sect3>
|
---|
| 392 |
|
---|
| 393 | </sect2>
|
---|
| 394 |
|
---|
| 395 | </sect1>
|
---|