source: general/prog/gitserver.xml@ 427b46a

11.0 11.1 11.2 11.3 12.0 12.1 kea ken/TL2024 ken/inkscape-core-mods ken/tuningfonts lazarus lxqt plabs/newcss plabs/python-mods python3.11 qt5new rahul/power-profiles-daemon renodr/vulkan-addition trunk upgradedb xry111/intltool xry111/llvm18 xry111/soup3 xry111/test-20220226 xry111/xf86-video-removal
Last change on this file since 427b46a was 3741962, checked in by Thomas Trepl <thomas@…>, 3 years ago

Add a note to hide absolte paths to projects

git-svn-id: svn://svn.linuxfromscratch.org/BLFS/trunk/BOOK@24110 af4574ff-66df-0310-9fd7-8a98e5e911e0

  • Property mode set to 100644
File size: 14.5 KB
Line 
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
10<sect1 id="gitserver" xreflabel="Running a Git Server">
11 <?dbhtml filename="gitserver.html"?>
12
13 <sect1info>
14 <othername>$LastChangedBy$</othername>
15 <date>$Date$</date>
16 </sect1info>
17
18 <title>Running a Git Server</title>
19
20 <sect2 role="package">
21 <title>Introduction</title>
22
23 <para>
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"/>.
28 </para>
29
30 <bridgehead renderas="sect3">Server Dependencies</bridgehead>
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">
41 <title>Setting up a Git Server</title>
42
43 <para>
44 The following instructions will install a
45 <application>git</application> server. It will be set
46 up to use <application>OpenSSH</application> as the secure
47 remote access method.
48 </para>
49
50 <para>
51 Configuration of the server consists of the following steps:
52 </para>
53
54 <sect3>
55 <title>1. Setup Users, Groups, and Permissions</title>
56
57 <para>
58 You will need to be user <systemitem class='username'>root</systemitem>
59 for the initial portion of configuration. Create the <systemitem
60 class="username">git</systemitem> user and group and set and unusable
61 password hash with the following commands:
62 </para>
63
64<screen role="root"><userinput>groupadd -g &gitgid; git &amp;&amp;
65useradd -c "git Owner" -d /home/git -m -g git -s /usr/bin/git-shell -u &gituid; git &amp;&amp;
66sed -i '/^git:/s/^git:[^:]:/git:NP:/' /etc/shadow</userinput></screen>
67
68 <para>
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
74 allowing access to the git repository using ssh keys.
75 </para>
76
77<screen role="root"><userinput>install -o git -g git -dm0700 /home/git/.ssh &amp;&amp;
78install -o git -g git -m0600 /dev/null /home/git/.ssh/authorized_keys</userinput></screen>
79
80 <para>
81 For any developer who should have access to the repository
82 add his/her public ssh key to <filename>/home/git/.ssh/authorized_keys</filename>.
83 First, prepend some options to prevent users from using the
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 &amp;&amp;
89cat &lt;user-ssh-key&gt; &gt;&gt; /home/git/.ssh/authorized_keys</userinput></screen>
90
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
97<screen role="nodump"><userinput>git config --system init.defaultBranch trunk</userinput></screen>
98
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" &gt;&gt; /etc/shells</userinput></screen>
108
109 </sect3>
110
111 <sect3>
112 <title>2. Create a git repository</title>
113
114 <para>
115 The repository can be anywhere on the filesystem. It is
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
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
131<screen role="root"><userinput>install -o git -g git -m755 -d /srv/git/project1.git &amp;&amp;
132cd /srv/git/project1.git &amp;&amp;
133git init --bare &amp;&amp;
134chown -R git:git .</userinput></screen>
135
136 </sect3>
137
138 <sect3>
139 <title>3. Populate the repository from a client system</title>
140
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
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>
156 A minimal configuration should be available on the developer's
157 system specifying its user name and the email address.
158 Create this minimal config file on client side:
159 </para>
160
161<screen role="nodump"><userinput>cat &gt; ~/.gitconfig &lt;&lt;EOF
162[user]
163 name = &lt;users-name&gt;
164 email = &lt;users-email-address&gt;
165EOF</userinput></screen>
166
167 <para>
168 On the developer's machine, setup some files to be pushed
169 to the repository as the initial content:
170 </para>
171
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
179<screen role="nodump"><userinput>mkdir myproject
180cd myproject
181git init --initial-branch=trunk
182git remote add origin git@gitserver:/srv/git/project1.git
183cat &gt;README &lt;&lt;EOF
184This is the README file
185EOF
186git add README
187git commit -m 'Initial creation of README'
188git push --set-upstream origin trunk</userinput></screen>
189
190 <para>The initial content is now pushed to the server and
191 is available for other users. On the current machine, the
192 argument <literal>--set-upstream origin trunk</literal> is
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
207cd project1
208vi README
209git commit -am 'Fix for README file'
210git push</userinput></screen>
211
212 <note>
213 <para>
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
219 <filename>~/.gitconfig</filename>) is recorded in the
220 changesets.
221 </para>
222 </note>
223
224 <para>
225 Access is restricted by the public keys added to git's
226 <filename>authorized_keys</filename> file and there is no
227 option for the public to export/clone the repository. To
228 enable this, continue with step 4 to set up the git server
229 for public read-only access.
230 </para>
231
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
247 </sect3>
248
249 <sect3 id="gitserver-init">
250 <title>4. Configure the Server</title>
251
252 <para>
253 The setup described above makes a repository available for
254 authenticated users (via providing the ssh public key file).
255 There is also a simple way to publish the
256 repository to unauthenticated users &mdash; of course without write
257 access.
258 </para>
259
260 <para>
261 The combination of access via ssh (for authenticated users) and
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
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:
277 </para>
278
279 <indexterm zone="gitserver gitserver-init" revision="sysv">
280 <primary sortas="f-git">git</primary>
281 </indexterm>
282
283<screen role="root" revision="sysv"><userinput>make install-git-daemon</userinput></screen>
284
285 <para revision="systemd">
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:
289 </para>
290
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
297 <para>
298 In order to allow <application>git</application> to export a
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
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>.
315 This configuration file will be imported if it exists, meaning it is
316 optional. The file can look like:</para>
317<screen revision="sysv">
318# Begin /etc/sysconfig/git-daemon
319
320# Specify the location of the git repository
321GIT_BASE_DIR="/srv/git/"
322
323# Directories added to whitelist
324DFT_REPO_DIR="$GIT_BASE_DIR"
325
326# Add extra options which will appended to the 'git daemon'
327# command executed in the boot script
328GIT_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.
337 </para>
338
339 <para>
340 There are only three options to set in the configuration file:
341 <itemizedlist>
342 <listitem>
343 <para>
344 GIT_BASE_DIR=&lt;dirname&gt;
345 </para>
346 <para>Specify the location of the git repositories.
347 Relative paths used when accessing the daemon will
348 translated relative to this directory.
349 </para>
350 </listitem>
351 <listitem>
352 <para>
353 DFT_REPO_DIR=&lt;dirname&gt;
354 </para>
355 <para>This directory is added to the white list of allowed
356 directories. This variable can hold multiple directory
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=&lt;options&gt;
363 </para>
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=&lt;port
369 number&gt;</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>.
372 </para>
373 </listitem>
374 </itemizedlist>
375 </para>
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>
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.
390 </para>
391
392 </sect3>
393
394 </sect2>
395
396</sect1>
Note: See TracBrowser for help on using the repository browser.