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