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