View | Details | Raw Unified | Return to bug 219421
Collapse All | Expand All

(-)jails/chapter.xml (-685 / +10 lines)
Lines 487-499 Link Here
487
      <title>High-Level Administrative Tools in the &os; Ports
487
      <title>High-Level Administrative Tools in the &os; Ports
488
	Collection</title>
488
	Collection</title>
489
489
490
      <para>Among the many third-party utilities for jail
490
	<para>Manually creating and managing jails can quickly become
491
	administration, one of the most complete and useful is
491
	tedious and error-prone.  The ports collection contains some
492
	<package>sysutils/ezjail</package>.  It is a set of scripts
492
	utilities designed to simplify jail management.
493
	that contribute to &man.jail.8; management.  Please refer to
493
494
	<link xlink:href="&url.books.handbook;/jails-ezjail.html">the
494
	Their listing here doesn't imply an recommendation or
495
	  handbook section on <application>ezjail</application></link>
495
	endorsement.  Nothing more than a list of the different names
496
	for more information.</para>
496
	of jail utilities in the ports sysutils category that you may
497
	want to review.</para>
498
499
	<programlisting>bsdploy, cbsd, ezjail, iocage, iocell, jail-primer, jailadmin, qjail, warden</programlisting>
497
    </sect2>
500
    </sect2>
498
501
499
    <sect2 xml:id="jails-updating">
502
    <sect2 xml:id="jails-updating">
Lines 560-573 Link Here
560
      it provides a simple way to add, remove, and upgrade
563
      it provides a simple way to add, remove, and upgrade
561
      jails.</para>
564
      jails.</para>
562
565
563
    <note>
564
      <para>Simpler solutions exist, such as
565
	<application>ezjail</application>, which provides an easier
566
	method of administering &os; jails but is less versatile than
567
	this setup.  <application>ezjail</application> is covered in
568
	more detail in <xref linkend="jails-ezjail"/>.</para>
569
    </note>
570
571
    <para>The goals of the setup described in this section are:</para>
566
    <para>The goals of the setup described in this section are:</para>
572
567
573
    <itemizedlist>
568
    <itemizedlist>
Lines 955-1628 Link Here
955
    </sect2>
950
    </sect2>
956
  </sect1>
951
  </sect1>
957
952
958
  <sect1 xml:id="jails-ezjail">
959
    <info>
960
      <title>Managing Jails with
961
	<application>ezjail</application></title>
962
963
      <authorgroup>
964
	<author>
965
	  <personname>
966
	    <firstname>Warren</firstname>
967
	    <surname>Block</surname>
968
	  </personname><contrib>Originally contributed by </contrib>
969
	</author>
970
      </authorgroup>
971
    </info>
972
973
    <para>Creating and managing multiple jails can quickly become
974
      tedious and error-prone.  Dirk Engling's
975
      <application>ezjail</application> automates and greatly
976
      simplifies many jail tasks.  A <emphasis>basejail</emphasis> is
977
      created as a template.  Additional jails use
978
      &man.mount.nullfs.8; to share many of the basejail directories
979
      without using additional disk space.  Each additional jail takes
980
      only a few megabytes of disk space before applications are
981
      installed.  Upgrading the copy of the userland in the basejail
982
      automatically upgrades all of the other jails.</para>
983
984
    <para>Additional benefits and features are described in detail on
985
      the <application>ezjail</application> web site, <link
986
	xlink:href="https://erdgeist.org/arts/software/ezjail/"></link>.</para>
987
988
    <sect2 xml:id="jails-ezjail-install">
989
      <title>Installing <application>ezjail</application></title>
990
991
      <para>Installing <application>ezjail</application> consists of
992
	adding a loopback interface for use in jails, installing the
993
	port or package, and enabling the service.</para>
994
995
      <procedure xml:id="jails-ezjail-install-procedure">
996
	<step>
997
	  <para>To keep jail loopback traffic off the host's loopback
998
	    network interface <literal>lo0</literal>, a second
999
	    loopback interface is created by adding an entry to
1000
	    <filename>/etc/rc.conf</filename>:</para>
1001
1002
	  <programlisting>cloned_interfaces="lo1"</programlisting>
1003
1004
	  <para>The second loopback interface <literal>lo1</literal>
1005
	    will be created when the system starts.  It can also be
1006
	    created manually without a restart:</para>
1007
1008
	  <screen>&prompt.root; <userinput>service netif cloneup</userinput>
1009
Created clone interfaces: lo1.</screen>
1010
1011
	  <para>Jails can be allowed to use aliases of this secondary
1012
	    loopback interface without interfering with the
1013
	    host.</para>
1014
1015
	  <para>Inside a jail, access to the loopback address
1016
	    <systemitem class="ipaddress">127.0.0.1</systemitem> is
1017
	    redirected to the first <acronym>IP</acronym> address
1018
	    assigned to the jail.  To make the jail loopback
1019
	    correspond with the new <literal>lo1</literal> interface,
1020
	    that interface must be specified first in the list of
1021
	    interfaces and <acronym>IP</acronym> addresses given when
1022
	    creating a new jail.</para>
1023
1024
	  <para>Give each jail a unique loopback address in the
1025
	    <systemitem
1026
	      class="ipaddress">127.0.0.0</systemitem><systemitem
1027
	      class="netmask">/8</systemitem> netblock.</para>
1028
	</step>
1029
1030
	<step>
1031
	  <para>Install
1032
	    <package role="port">sysutils/ezjail</package>:</para>
1033
1034
	  <screen>&prompt.root; <userinput>cd /usr/ports/sysutils/ezjail</userinput>
1035
&prompt.root; <userinput>make install clean</userinput></screen>
1036
	</step>
1037
1038
	<step>
1039
	  <para>Enable <application>ezjail</application> by adding
1040
	    this line to <filename>/etc/rc.conf</filename>:</para>
1041
1042
	  <programlisting>ezjail_enable="YES"</programlisting>
1043
	</step>
1044
1045
	<step>
1046
	  <para>The service will automatically start on system boot.
1047
	    It can be started immediately for the current
1048
	    session:</para>
1049
1050
	  <screen>&prompt.root; <userinput>service ezjail start</userinput></screen>
1051
	</step>
1052
      </procedure>
1053
    </sect2>
1054
1055
    <sect2 xml:id="jails-ezjail-initialsetup">
1056
      <title>Initial Setup</title>
1057
1058
      <para>With <application>ezjail</application> installed, the
1059
	basejail directory structure can be created and populated.
1060
	This step is only needed once on the jail host
1061
	computer.</para>
1062
1063
      <para>In both of these examples, <option>-p</option> causes the
1064
	ports tree to be retrieved with &man.portsnap.8; into the
1065
	basejail.  That single copy of the ports directory will be
1066
	shared by all the jails.  Using a separate copy of the ports
1067
	directory for jails isolates them from the host.  The
1068
	<application>ezjail</application> <acronym>FAQ</acronym>
1069
	explains in more detail: <link
1070
	  xlink:href="http://erdgeist.org/arts/software/ezjail/#FAQ"></link>.</para>
1071
1072
      <procedure xml:id="jails-ezjail-initialsetup-procedure">
1073
	<step>
1074
	  <stepalternatives>
1075
	    <step>
1076
	      <title>To Populate the Jail with &os;-RELEASE</title>
1077
1078
	      <para>For a basejail based on the &os; RELEASE matching
1079
		that of the host computer, use
1080
		<command>install</command>.  For example, on a host
1081
		computer running &os;&nbsp;10-STABLE, the latest
1082
		RELEASE version of &os;&nbsp;-10 will be installed in
1083
		the jail):</para>
1084
1085
	      <screen>&prompt.root; <userinput>ezjail-admin install -p</userinput></screen>
1086
	    </step>
1087
1088
	    <step>
1089
	      <title>To Populate the Jail with
1090
		<command>installworld</command></title>
1091
1092
	      <para>The basejail can be installed from binaries
1093
		created by <buildtarget>buildworld</buildtarget> on
1094
		the host with
1095
		<command>ezjail-admin update</command>.</para>
1096
1097
	      <para>In this example, &os;&nbsp;10-STABLE has been
1098
		built from source.  The jail directories are created.
1099
		Then <buildtarget>installworld</buildtarget> is
1100
		executed, installing the host's
1101
		<filename>/usr/obj</filename> into the
1102
		basejail.</para>
1103
1104
	      <screen>&prompt.root; <userinput>ezjail-admin update -i -p</userinput></screen>
1105
1106
	      <para>The host's <filename>/usr/src</filename> is used
1107
		by default.  A different source directory on the host
1108
		can be specified with <option>-s</option> and a path,
1109
		or set with <varname>ezjail_sourcetree</varname> in
1110
		<filename>/usr/local/etc/ezjail.conf</filename>.</para>
1111
	    </step>
1112
	  </stepalternatives>
1113
	</step>
1114
      </procedure>
1115
1116
      <tip>
1117
	<para>The basejail's ports tree is shared by other jails.
1118
	  However, downloaded distfiles are stored in the jail that
1119
	  downloaded them.  By default, these files are stored in
1120
	  <filename>/var/ports/distfiles</filename> within each
1121
	  jail.  <filename>/var/ports</filename> inside each jail is
1122
	  also used as a work directory when building ports.</para>
1123
      </tip>
1124
1125
      <tip>
1126
	<para>The <acronym>FTP</acronym> protocol is used by default
1127
	  to download packages for the installation of the basejail.
1128
	  Firewall or proxy configurations can prevent or interfere
1129
	  with <acronym>FTP</acronym> transfers.  The
1130
	  <acronym>HTTP</acronym> protocol works differently and
1131
	  avoids these problems.  It can be chosen by specifying a
1132
	  full <acronym>URL</acronym> for a particular download mirror
1133
	  in <filename>/usr/local/etc/ezjail.conf</filename>:</para>
1134
1135
	<programlisting>ezjail_ftphost=http://<replaceable>ftp.FreeBSD.org</replaceable></programlisting>
1136
1137
	<para>See <xref linkend="mirrors-ftp"/> for a list of
1138
	  sites.</para>
1139
      </tip>
1140
    </sect2>
1141
1142
    <sect2 xml:id="jails-ezjail-create">
1143
      <title>Creating and Starting a New Jail</title>
1144
1145
      <para>New jails are created with
1146
	<command>ezjail-admin create</command>.  In these examples,
1147
	the <literal>lo1</literal> loopback interface is used as
1148
	described above.</para>
1149
1150
      <procedure xml:id="jails-ezjail-create-steps">
1151
	<title>Create and Start a New Jail</title>
1152
1153
	<step>
1154
	  <para>Create the jail, specifying a name and the loopback
1155
	    and network interfaces to use, along with their
1156
	    <acronym>IP</acronym> addresses.  In this example, the
1157
	    jail is named <literal>dnsjail</literal>.</para>
1158
1159
	  <screen>&prompt.root; <userinput>ezjail-admin create <replaceable>dnsjail</replaceable> '<replaceable>lo1|127.0.1.1</replaceable>,<replaceable>em0</replaceable>|<replaceable>192.168.1.50</replaceable>'</userinput></screen>
1160
1161
	  <tip xml:id="jails-ezjail-raw-network-sockets">
1162
	    <para>Most network services run in jails without
1163
	      problems.  A few network services, most notably
1164
	      &man.ping.8;, use
1165
	      <emphasis>raw network sockets</emphasis>.  In jails, raw
1166
	      network sockets are disabled by default for security.
1167
	      Services that require them will not work.</para>
1168
1169
	    <para>Occasionally, a jail genuinely needs raw sockets.
1170
	      For example, network monitoring applications often use
1171
	      &man.ping.8; to check the availability of other
1172
	      computers.  When raw network sockets are actually needed
1173
	      in a jail, they can be enabled by editing the
1174
	      <application>ezjail</application>
1175
	      configuration file for the individual jail,
1176
	      <filename>/usr/local/etc/ezjail/<replaceable>jailname</replaceable></filename>.
1177
	      Modify the <literal>parameters</literal>
1178
	      entry:</para>
1179
1180
	    <programlisting>export jail_<replaceable>jailname</replaceable>_parameters="allow.raw_sockets=1"</programlisting>
1181
1182
	    <para>Do not enable raw network sockets unless services in
1183
	      the jail actually require them.</para>
1184
	  </tip>
1185
	</step>
1186
1187
	<step>
1188
	  <para>Start the jail:</para>
1189
1190
	  <screen>&prompt.root; <userinput>ezjail-admin start <replaceable>dnsjail</replaceable></userinput></screen>
1191
	</step>
1192
1193
	<step>
1194
	  <para>Use a console on the jail:</para>
1195
1196
	  <screen>&prompt.root; <userinput>ezjail-admin console <replaceable>dnsjail</replaceable></userinput></screen>
1197
	</step>
1198
      </procedure>
1199
1200
      <para>The jail is operating and additional configuration can be
1201
	completed.  Typical settings added at this point
1202
	include:</para>
1203
1204
      <procedure>
1205
	<step>
1206
	  <title>Set the
1207
	    <systemitem class="username">root</systemitem>
1208
	    Password</title>
1209
1210
	  <para>Connect to the jail and set the
1211
	    <systemitem class="username">root</systemitem> user's
1212
	    password:</para>
1213
1214
	  <screen>&prompt.root; <userinput>ezjail-admin console <replaceable>dnsjail</replaceable></userinput>
1215
&prompt.root; <userinput>passwd</userinput>
1216
Changing local password for root
1217
New Password:
1218
Retype New Password:</screen>
1219
	</step>
1220
1221
	<step>
1222
	  <title>Time Zone Configuration</title>
1223
1224
	  <para>The jail's time zone can be set with &man.tzsetup.8;.
1225
	    To avoid spurious error messages, the &man.adjkerntz.8;
1226
	    entry in <filename>/etc/crontab</filename> can be
1227
	    commented or removed.  This job attempts to update the
1228
	    computer's hardware clock with time zone changes, but
1229
	    jails are not allowed to access that hardware.</para>
1230
	</step>
1231
1232
	<step>
1233
	  <title><acronym>DNS</acronym> Servers</title>
1234
1235
	  <para>Enter domain name server lines in
1236
	    <filename>/etc/resolv.conf</filename> so
1237
	    <acronym>DNS</acronym> works in the jail.</para>
1238
	</step>
1239
1240
	<step>
1241
	  <title>Edit <filename>/etc/hosts</filename></title>
1242
1243
	  <para>Change the address and add the jail name to the
1244
	    <literal>localhost</literal> entries in
1245
	    <filename>/etc/hosts</filename>.</para>
1246
	</step>
1247
1248
	<step>
1249
	  <title>Configure <filename>/etc/rc.conf</filename></title>
1250
1251
	  <para>Enter configuration settings in
1252
	    <filename>/etc/rc.conf</filename>.  This is much like
1253
	    configuring a full computer.  The host name and
1254
	    <acronym>IP</acronym> address are not set here.  Those
1255
	    values are already provided by the jail
1256
	    configuration.</para>
1257
	</step>
1258
      </procedure>
1259
1260
      <para>With the jail configured, the applications for which the
1261
	jail was created can be installed.</para>
1262
1263
      <tip>
1264
	<para>Some ports must be built with special options to be used
1265
	  in a jail.  For example, both of the network monitoring
1266
	  plugin packages
1267
	  <package role="port">net-mgmt/nagios-plugins</package> and
1268
	  <package role="port">net-mgmt/monitoring-plugins</package>
1269
	  have a <literal>JAIL</literal> option which must be enabled
1270
	  for them to work correctly inside a jail.</para>
1271
      </tip>
1272
    </sect2>
1273
1274
    <sect2 xml:id="jails-ezjail-update">
1275
      <title>Updating Jails</title>
1276
1277
      <sect3 xml:id="jails-ezjail-update-os">
1278
	<title>Updating the Operating System</title>
1279
1280
	<para>Because the basejail's copy of the userland is shared by
1281
	  the other jails, updating the basejail automatically updates
1282
	  all of the other jails.  Either source or binary updates can
1283
	  be used.</para>
1284
1285
	<para>To build the world from source on the host, then
1286
	  install it in the basejail, use:</para>
1287
1288
	<screen>&prompt.root; <userinput>ezjail-admin update -b</userinput></screen>
1289
1290
	<para>If the world has already been compiled on the host,
1291
	  install it in the basejail with:</para>
1292
1293
	<screen>&prompt.root; <userinput>ezjail-admin update -i</userinput></screen>
1294
1295
	<para>Binary updates use &man.freebsd-update.8;.  These
1296
	  updates have the same limitations as if
1297
	  &man.freebsd-update.8; were being run directly.  The most
1298
	  important one is that only -RELEASE versions of &os; are
1299
	  available with this method.</para>
1300
1301
	<para>Update the basejail to the latest patched release of
1302
	  the version of &os; on the host.  For example, updating from
1303
	  RELEASE-p1 to RELEASE-p2.</para>
1304
1305
	<screen>&prompt.root; <userinput>ezjail-admin update -u</userinput></screen>
1306
1307
	<para>To upgrade the basejail to a new version, first
1308
	  upgrade the host system as described in <xref
1309
	    linkend="freebsdupdate-upgrade" />.  Once the host has
1310
	  been upgraded and rebooted, the basejail can then be
1311
	  upgraded.  &man.freebsd-update.8; has no way of determining
1312
	  which version is currently installed in the basejail, so the
1313
	  original version must be specified.  Use &man.file.1; to
1314
	  determine the original version in the basejail:</para>
1315
1316
	<screen>&prompt.root; <userinput>file /usr/jails/basejail/bin/sh</userinput>
1317
/usr/jails/basejail/bin/sh: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), dynamically linked (uses shared libs), for FreeBSD 9.3, stripped</screen>
1318
1319
	<para>Now use this information to perform the upgrade from
1320
	  <literal>9.3-RELEASE</literal> to the current version of
1321
	  the host system:</para>
1322
1323
	<screen>&prompt.root; <userinput>ezjail-admin update -U -s <replaceable>9.3-RELEASE</replaceable></userinput></screen>
1324
1325
	<para>After updating the basejail, &man.mergemaster.8; must
1326
	  be run to update each jail's configuration files.</para>
1327
1328
	<para>How to use &man.mergemaster.8; depends on the purpose
1329
	  and trustworthiness of a jail.  If a jail's services or
1330
	  users are not trusted, then &man.mergemaster.8; should only
1331
	  be run from within that jail:</para>
1332
1333
	<example xml:id="jails-ezjail-update-mergemaster-untrusted">
1334
	  <title>&man.mergemaster.8; on Untrusted Jail</title>
1335
1336
	  <para>Delete the link from the jail's
1337
	    <filename>/usr/src</filename> into the basejail and
1338
	    create a new <filename>/usr/src</filename> in the jail
1339
	    as a mountpoint.  Mount the host computer's
1340
	    <filename>/usr/src</filename> read-only on the jail's
1341
	    new <filename>/usr/src</filename> mountpoint:</para>
1342
1343
	  <screen>&prompt.root; <userinput>rm /usr/jails/<replaceable>jailname</replaceable>/usr/src</userinput>
1344
&prompt.root; <userinput>mkdir /usr/jails/<replaceable>jailname</replaceable>/usr/src</userinput>
1345
&prompt.root; <userinput>mount -t nullfs -o ro /usr/src /usr/jails/<replaceable>jailname</replaceable>/usr/src</userinput></screen>
1346
1347
	  <para>Get a console in the jail:</para>
1348
1349
	  <screen>&prompt.root; <userinput>ezjail-admin console <replaceable>jailname</replaceable></userinput></screen>
1350
1351
	  <para>Inside the jail, run <command>mergemaster</command>.
1352
	    Then exit the jail console:</para>
1353
1354
	  <screen>&prompt.root; <userinput>cd /usr/src</userinput>
1355
&prompt.root; <userinput>mergemaster -U</userinput>
1356
&prompt.root; <userinput>exit</userinput></screen>
1357
1358
	  <para>Finally, unmount the jail's
1359
	    <filename>/usr/src</filename>:</para>
1360
1361
	  <screen>&prompt.root; <userinput>umount /usr/jails/<replaceable>jailname</replaceable>/usr/src</userinput></screen>
1362
	</example>
1363
1364
	<example xml:id="jails-ezjail-update-mergemaster-trusted">
1365
1366
	  <title>&man.mergemaster.8; on Trusted Jail</title>
1367
1368
	  <para>If the users and services in a jail are trusted,
1369
	    &man.mergemaster.8; can be run from the host:</para>
1370
1371
	  <screen>&prompt.root; <userinput>mergemaster -U -D /usr/jails/<replaceable>jailname</replaceable></userinput></screen>
1372
	</example>
1373
      </sect3>
1374
1375
      <sect3 xml:id="jails-ezjail-update-ports">
1376
	<title>Updating Ports</title>
1377
1378
	<para>The ports tree in the basejail is shared by the other
1379
	  jails.  Updating that copy of the ports tree gives the other
1380
	  jails the updated version also.</para>
1381
1382
	<para>The basejail ports tree is updated with
1383
	  &man.portsnap.8;:</para>
1384
1385
	<screen>&prompt.root; <userinput>ezjail-admin update -P</userinput></screen>
1386
      </sect3>
1387
    </sect2>
1388
1389
    <sect2 xml:id="jails-ezjail-control">
1390
      <title>Controlling Jails</title>
1391
1392
      <sect3 xml:id="jails-ezjail-control-stop-start">
1393
	<title>Stopping and Starting Jails</title>
1394
1395
	<para><application>ezjail</application> automatically starts
1396
	  jails when the computer is started.  Jails can be manually
1397
	  stopped and restarted with <command>stop</command> and
1398
	  <command>start</command>:</para>
1399
1400
	<screen>&prompt.root; <userinput>ezjail-admin stop <replaceable>sambajail</replaceable></userinput>
1401
Stopping jails: sambajail.</screen>
1402
1403
	<para>By default, jails are started automatically when the
1404
	  host computer starts.  Autostarting can be disabled
1405
	  with <command>config</command>:</para>
1406
1407
	<screen>&prompt.root; <userinput>ezjail-admin config -r norun <replaceable>seldomjail</replaceable></userinput></screen>
1408
1409
	<para>This takes effect the next time the host computer is
1410
	  started.  A jail that is already running will not be
1411
	  stopped.</para>
1412
1413
	<para>Enabling autostart is very similar:</para>
1414
1415
	<screen>&prompt.root; <userinput>ezjail-admin config -r run <replaceable>oftenjail</replaceable></userinput></screen>
1416
      </sect3>
1417
1418
      <sect3 xml:id="jails-ezjail-control-backup">
1419
	<title>Archiving and Restoring Jails</title>
1420
1421
	<para>Use <command>archive</command> to create a
1422
	  <filename>.tar.gz</filename> archive of a jail.  The file
1423
	  name is composed from the name of the jail and the current
1424
	  date.  Archive files are written to the archive directory,
1425
	  <filename>/usr/jails/ezjail_archives</filename>.  A
1426
	  different archive directory can be chosen by setting
1427
	  <varname>ezjail_archivedir</varname> in the configuration
1428
	  file.</para>
1429
1430
	<para>The archive file can be copied elsewhere as a backup, or
1431
	  an existing jail can be restored from it with
1432
	  <command>restore</command>.  A new jail can be created from
1433
	  the archive, providing a convenient way to clone existing
1434
	  jails.</para>
1435
1436
	<para>Stop and archive a jail named
1437
	  <literal>wwwserver</literal>:</para>
1438
1439
	<screen>&prompt.root; <userinput>ezjail-admin stop <replaceable>wwwserver</replaceable></userinput>
1440
Stopping jails: wwwserver.
1441
&prompt.root; <userinput>ezjail-admin archive <replaceable>wwwserver</replaceable></userinput>
1442
&prompt.root; <userinput>ls /usr/jails/ezjail-archives/</userinput>
1443
wwwserver-201407271153.13.tar.gz</screen>
1444
1445
	<para>Create a new jail named
1446
	  <literal>wwwserver-clone</literal> from the archive created
1447
	  in the previous step.  Use the <filename>em1</filename>
1448
	  interface and assign a new <acronym>IP</acronym> address to
1449
	  avoid conflict with the original:</para>
1450
1451
	<screen>&prompt.root; <userinput>ezjail-admin create -a /usr/jails/ezjail_archives/wwwserver-201407271153.13.tar.gz <replaceable>wwwserver-clone</replaceable> 'lo1|127.0.3.1,em1|192.168.1.51'</userinput></screen>
1452
      </sect3>
1453
    </sect2>
1454
1455
    <sect2 xml:id="jails-ezjail-example-bind">
1456
      <title>Full Example: <application>BIND</application> in a
1457
	Jail</title>
1458
1459
      <para>Putting the <application>BIND</application>
1460
	<acronym>DNS</acronym> server in a jail improves security by
1461
	isolating it.  This example creates a simple caching-only name
1462
	server.</para>
1463
1464
      <itemizedlist xml:id="jails-ezjail-example-bind-assumptions">
1465
	<listitem>
1466
	  <para>The jail will be called
1467
	    <literal>dns1</literal>.</para>
1468
	</listitem>
1469
1470
	<listitem>
1471
	  <para>The jail will use <acronym>IP</acronym> address
1472
	    <literal>192.168.1.240</literal> on the host's
1473
	    <literal>re0</literal> interface.</para>
1474
	</listitem>
1475
1476
	<listitem>
1477
	  <para>The upstream <acronym>ISP</acronym>'s DNS servers are
1478
	    at <literal>10.0.0.62</literal> and
1479
	    <literal>10.0.0.61</literal>.</para>
1480
	</listitem>
1481
1482
	<listitem>
1483
	  <para>The basejail has already been created and a ports tree
1484
	    installed as shown in
1485
	    <xref linkend="jails-ezjail-initialsetup"/>.</para>
1486
	</listitem>
1487
      </itemizedlist>
1488
1489
      <example xml:id="jails-ezjail-example-bind-steps">
1490
	<title>Running BIND in a Jail</title>
1491
1492
	<para>Create a cloned loopback interface by adding a line to
1493
	  <filename>/etc/rc.conf</filename>:</para>
1494
1495
	<programlisting>cloned_interfaces="lo1"</programlisting>
1496
1497
	<para>Immediately create the new loopback interface:</para>
1498
1499
	<screen>&prompt.root; <userinput>service netif cloneup</userinput>
1500
Created clone interfaces: lo1.</screen>
1501
1502
	<para>Create the jail:</para>
1503
1504
	<screen>&prompt.root; <userinput>ezjail-admin create dns1 'lo1|127.0.2.1,re0|192.168.1.240'</userinput></screen>
1505
1506
	<para>Start the jail, connect to a console running on it, and
1507
	  perform some basic configuration:</para>
1508
1509
	<screen>&prompt.root; <userinput>ezjail-admin start dns1</userinput>
1510
&prompt.root; <userinput>ezjail-admin console dns1</userinput>
1511
&prompt.root; <userinput>passwd</userinput>
1512
Changing local password for root
1513
New Password:
1514
Retype New Password:
1515
&prompt.root; <userinput>tzsetup</userinput>
1516
&prompt.root; <userinput>sed -i .bak -e '/adjkerntz/ s/^/#/' /etc/crontab</userinput>
1517
&prompt.root; <userinput>sed -i .bak -e 's/127.0.0.1/127.0.2.1/g; s/localhost.my.domain/dns1.my.domain dns1/' /etc/hosts</userinput></screen>
1518
1519
	<para>Temporarily set the upstream <acronym>DNS</acronym>
1520
	  servers in <filename>/etc/resolv.conf</filename> so ports
1521
	  can be downloaded:</para>
1522
1523
	<programlisting>nameserver 10.0.0.62
1524
nameserver 10.0.0.61</programlisting>
1525
1526
	<para>Still using the jail console, install
1527
	  <package role="port">dns/bind99</package>.</para>
1528
1529
	<screen>&prompt.root; <userinput>make -C /usr/ports/dns/bind99 install clean</userinput></screen>
1530
1531
	<para>Configure the name server by editing
1532
	  <filename>/usr/local/etc/namedb/named.conf</filename>.</para>
1533
1534
	<para>Create an Access Control List (<acronym>ACL</acronym>)
1535
	  of addresses and networks that are permitted to send
1536
	  <acronym>DNS</acronym> queries to this name server.  This
1537
	  section is added just before the <literal>options</literal>
1538
	  section already in the file:</para>
1539
1540
	<programlisting>...
1541
// or cause huge amounts of useless Internet traffic.
1542
1543
acl "trusted" {
1544
	192.168.1.0/24;
1545
	localhost;
1546
	localnets;
1547
};
1548
1549
options {
1550
...</programlisting>
1551
1552
	<para>Use the jail <acronym>IP</acronym> address in the
1553
	  <literal>listen-on</literal> setting to accept
1554
	  <acronym>DNS</acronym> queries from other computers on the
1555
	  network:</para>
1556
1557
	<programlisting>	listen-on	{ 192.168.1.240; };</programlisting>
1558
1559
	<para>A simple caching-only <acronym>DNS</acronym> name server
1560
	  is created by changing the <literal>forwarders</literal>
1561
	  section.  The original file contains:</para>
1562
1563
	<programlisting>/*
1564
	forwarders {
1565
		127.0.0.1;
1566
	};
1567
*/</programlisting>
1568
1569
	<para>Uncomment the section by removing the
1570
	  <literal>/*</literal> and <literal>*/</literal> lines.
1571
	  Enter the <acronym>IP</acronym> addresses of the upstream
1572
	  <acronym>DNS</acronym> servers.  Immediately after the
1573
	  <literal>forwarders</literal> section, add references to the
1574
	  <literal>trusted</literal> <acronym>ACL</acronym> defined
1575
	  earlier:</para>
1576
1577
	<programlisting>	forwarders {
1578
		10.0.0.62;
1579
		10.0.0.61;
1580
	};
1581
1582
	allow-query       { any; };
1583
	allow-recursion   { trusted; };
1584
	allow-query-cache { trusted; };</programlisting>
1585
1586
	<para>Enable the service in
1587
	  <filename>/etc/rc.conf</filename>:</para>
1588
1589
	<programlisting>named_enable="YES"</programlisting>
1590
1591
	<para>Start and test the name server:</para>
1592
1593
	<screen>&prompt.root; <userinput>service named start</userinput>
1594
wrote key file "/usr/local/etc/namedb/rndc.key"
1595
Starting named.
1596
&prompt.root; <userinput>/usr/local/bin/dig @192.168.1.240 freebsd.org</userinput></screen>
1597
1598
	<para>A response that includes</para>
1599
1600
	<screen>;; Got answer;</screen>
1601
1602
	<para>shows that the new <acronym>DNS</acronym> server is
1603
	  working.  A long delay followed by a response
1604
	  including</para>
1605
1606
	<screen>;; connection timed out; no servers could be reached</screen>
1607
1608
	<para>shows a problem.  Check the configuration settings and
1609
	  make sure any local firewalls allow the new
1610
	  <acronym>DNS</acronym> access to the upstream
1611
	  <acronym>DNS</acronym> servers.</para>
1612
1613
	<para>The new <acronym>DNS</acronym> server can use itself for
1614
	  local name resolution, just like other local computers.  Set
1615
	  the address of the <acronym>DNS</acronym> server in the
1616
	  client computer's
1617
	  <filename>/etc/resolv.conf</filename>:</para>
1618
1619
	<programlisting>nameserver 192.168.1.240</programlisting>
1620
1621
	<para>A local <acronym>DHCP</acronym> server can be configured
1622
	  to provide this address for a local <acronym>DNS</acronym>
1623
	  server, providing automatic configuration on
1624
	  <acronym>DHCP</acronym> clients.</para>
1625
      </example>
1626
    </sect2>
1627
  </sect1>
1628
</chapter>
953
</chapter>

Return to bug 219421