--- title: FreeBSD 12で自宅サーバの構築 jail内基本システム編 tags: FreeBSD author: Chun3 slide: false --- ## はじめに FreeBSD 12を使用した自宅サーバの構築を行った。FreeBSD 12はリリースされて間もない状態ですが、カーネルに標準でVIMAGEが取り込まれDNSの外向きと、内向きを別々のシステム(jail+VIMAGE)が同一ホストで構築できることから採用することにしました。その時の構築手順を備忘録の意味も含め、具体的にいくつかに渡り説明する。 * [基本システム編その1](https://qiita.com/Chun3/items/2f891e5993c91b58ae5c) - 背景 - 基本のインストール - 日本語環境の設定 * [基本システム編その2](https://qiita.com/Chun3/items/1e8fe5071c4a78d37e66) - セキュリティ - ftpサーバ * [基本システム編その3](https://qiita.com/Chun3/items/e5cc6a69542153649dc0) - jail - 基本システム編おわり * [jail内基本システム編](https://qiita.com/Chun3/items/4b11b9acdd080ecdab6c)(この記事) - 基本の設定(jail1・jail2共通) * [DNS編](https://qiita.com/Chun3/items/95ad3113202d111ee53c) - DNSの構築におけるこれまでの問題 - bind9インストール - NTPサーバ * [Let's Encrypt編](https://qiita.com/Chun3/items/4814c25c09756690133f) - Let's Encryptについて - certbotのインストール - 各種設定 - 証明書の取得 * [Webサーバ編](https://qiita.com/Chun3/items/286e567952cd9faf279f) - インストール(apache2.4・webalizer・php7.3・postgresql11.2・mysql8.0) - 設定 * [メールサーバ編](https://qiita.com/Chun3/items/dc0e176c9de4f6b4bfb8) - インストール(cyrus-sasl・postfix・dovecot・procmail・pflogsumm・policyd-spf) - 設定・起動 - spfレコード送信設定 * [ファイルサーバ編](https://qiita.com/Chun3/items/356b7fadd7aa5280ec65) - sambaの選択 - samba3のインストール * [メールサーバ編その2](https://qiita.com/Chun3/items/4c15ee889b052df67bbd) - 概要 - OpenDKIM - OpenDMARC - ClamAV (clamav-milter) - Spamassasin (spamass-milter) ##基本の設定(jail1・jail2共通) jail内の基本設定を行っていくが、基本的にはホストのインストールと同等の部分は手順だけを記述する(jail1で実行したログを記載するがjail2も同様)。 ###rootのパスワードの設定 jail1にログインし最初にパスワードを設定する。 ``` # qjail console jail1 Last login: Fri Feb 15 22:08:10 on pts/0 FreeBSD 12.0-RELEASE-p3 GENERIC Welcome to your FreeBSD jail. jail1 /root >passwd Changing local password for root New Password: Retype New Password: ``` rootのプロンプトに#が出るよう以下のように変更する。 ```~/.cshrc - set prompt = "`/bin/hostname -s` %/ >" + set prompt = "`/bin/hostname -s` %/ # " ``` ###ファイヤーウォールの設定 ``` jail1 /root # ipfw show 65535 10 892 deny ip from any to any ``` ipfwが全てdenyになっているので前節で設定した内容を適用する。 まず使用宣言。 ```sh:/etc/rc.conf firewall_enable="YES" firewall_type="/etc/firewall.tab" firewall_logging="YES" ``` 5200番については、ホスト側と違いブリッジ接続により経由がないため、ローカルネットではなく自分自身にしている。なお、jqil1とjail2ではポートの使い方が違うので最適に変更したほうがいいが、大きな問題とは思えないのでこれで行く。 ```sh:/etc/firewall.tab add 5000 allow ip from 192.168.1.0/24 to any add 5200 allow tcp from any to me established # hosts,DNS,NTP add 6000 allow udp from any to me 53,123 add 6100 allow udp from any 53,123 to me add 6400 allow tcp from any to me 53,123 add 6500 allow tcp from any 53,123 to me # ftp,ssh,smtp,http,https,smtps,smtp,imaps,pop3s add 7000 allow tcp from any to me 20-22,25,80,443,465,587,993,995 # add 60000 deny tcp from any to me add 60100 deny udp from any to me add 60400 deny ip from any to me ``` 設定をipfwに読み込ませるreloadを使いたいところですが、このコマンドはサポートされていないようなので以下のように再起動する。 ``` jail1 /root # service ipfw restart Flushed all rules. 00100 allow ip from any to any via lo0 00200 deny ip from any to 127.0.0.0/8 00300 deny ip from 127.0.0.0/8 to any 00400 deny ip from any to ::1 00500 deny ip from ::1 to any 00600 allow ipv6-icmp from :: to ff02::/16 00700 allow ipv6-icmp from fe80::/10 to fe80::/10 00800 allow ipv6-icmp from fe80::/10 to ff02::/16 00900 allow ipv6-icmp from any to any icmp6types 1 01000 allow ipv6-icmp from any to any icmp6types 2,135,136 05000 allow ip from 192.168.1.0/24 to any 05200 allow tcp from any to me established 06000 allow udp from any to me 53,123 06100 allow udp from any 53,123 to me 06400 allow tcp from any to me 53,123 06500 allow tcp from any 53,123 to me 07000 allow tcp from any to me 20-22,25,80,443,465,587,993,995 60000 deny tcp from any to me 60100 deny udp from any to me 60400 deny ip from any to me Firewall rules loaded. Firewall logging enabled. ``` ### blacklistd ftp・ssh・smtpなどのDoS攻撃を防止する方法としてblacklistdを使用する。 例によってい使用宣言 ```sh:/etc/rc.conf blacklistd_enable="YES" blacklistd_flags="-r" ``` -rフラグは、システムを再起動しても再度ルールを適用してブロックを開始する。なお、-fを指定すると再起動時にそれまでの履歴が消去される。ルールはデフォルから追加している。 ```/etc/blacklistd.conf # $FreeBSD: releng/12.0/usr.sbin/blacklistd/blacklistd.conf 336977 2018-07-31 16:39:38Z brd $ # # Blacklist rule # adr/mask:port type proto owner name nfail disable [local] ssh stream * * * 3 24h ftp stream * * * 3 24h smtp stream * * * 3 24h smtps stream * * * 3 24h submission stream * * * 3 24h imaps stream * * * 3 24h pop3s stream * * * 3 24h #6161 stream tcp6 christos * 2 10m * * * * * 3 60 # adr/mask:port type proto owner name nfail disable [remote] #129.168.0.0/16 * * * = * * #6161 = = = =/24 = = #* stream tcp * = = = ``` 開始する ``` jail1 /root # service blacklistd start Starting blacklistd. ``` ### jail用ports collectionの初期設定 jail内でのportsの使い方はデフォルトで以下のように設定されている。 ```/etc/make.conf WRKDIRPREFIX= /var/ports DISTDIR= /var/ports/distfiles PACKAGES= /var/ports/packages INDEXDIR= /var/ports ``` そこで以下のように準備する。 ``` jail1 /root # mkdir -p /var/ports/distfiles jail1 /root # mkdir -p /var/ports/packages jail1 /root # cp -p /usr/ports/INDEX-12 /var/ports jail1 /root # pkg install portupgrade Updating FreeBSD repository catalogue... pkg: Repository FreeBSD load error: access repo file(/var/db/pkg/repo-FreeBSD.sqlite) failed: No such file or directory [jail1] Fetching meta.txz: 100% 944 B 0.9kB/s 00:01 [jail1] Fetching packagesite.txz: 100% 6 MiB 607.4kB/s 00:11 Processing entries: 100% FreeBSD repository update completed. 32074 packages processed. All repositories are up to date. The following 9 package(s) will be affected (of 0 checked): New packages to be INSTALLED: portupgrade: 2.4.16,2 ruby: 2.4.5_1,1 libyaml: 0.2.1 libunwind: 20170615 libffi: 3.2.1_3 indexinfo: 0.3.1 libedit: 3.1.20170329_2,1 ruby24-bdb: 0.6.6_5 db5: 5.3.28_7 Number of packages to be installed: 9 The process will require 95 MiB more space. 22 MiB to be downloaded. [jail1] [1/9] Fetching portupgrade-2.4.16,2.txz: 100% 92 KiB 94.5kB/s 00:01 [jail1] [2/9] Fetching ruby-2.4.5_1,1.txz: 100% 9 MiB 529.5kB/s 00:17 [jail1] [3/9] Fetching libyaml-0.2.1.txz: 100% 66 KiB 67.4kB/s 00:01 [jail1] [4/9] Fetching libunwind-20170615.txz: 100% 113 KiB 115.7kB/s 00:01 [jail1] [5/9] Fetching libffi-3.2.1_3.txz: 100% 34 KiB 35.0kB/s 00:01 [jail1] [6/9] Fetching indexinfo-0.3.1.txz: 100% 6 KiB 6.1kB/s 00:01 [jail1] [7/9] Fetching libedit-3.1.20170329_2,1.txz: 100% 123 KiB 125.6kB/s 00:01 [jail1] [8/9] Fetching ruby24-bdb-0.6.6_5.txz: 100% 363 KiB 186.1kB/s 00:02 [jail1] [9/9] Fetching db5-5.3.28_7.txz: 100% 12 MiB 426.5kB/s 00:30 Checking integrity... done (0 conflicting) [jail1] [1/9] Installing indexinfo-0.3.1... [jail1] [1/9] Extracting indexinfo-0.3.1: 100% [jail1] [2/9] Installing libyaml-0.2.1... [jail1] [2/9] Extracting libyaml-0.2.1: 100% [jail1] [3/9] Installing libunwind-20170615... [jail1] [3/9] Extracting libunwind-20170615: 100% [jail1] [4/9] Installing libffi-3.2.1_3... [jail1] [4/9] Extracting libffi-3.2.1_3: 100% [jail1] [5/9] Installing libedit-3.1.20170329_2,1... [jail1] [5/9] Extracting libedit-3.1.20170329_2,1: 100% [jail1] [6/9] Installing ruby-2.4.5_1,1... [jail1] [6/9] Extracting ruby-2.4.5_1,1: 100% [jail1] [7/9] Installing db5-5.3.28_7... [jail1] [7/9] Extracting db5-5.3.28_7: 100% [jail1] [8/9] Installing ruby24-bdb-0.6.6_5... [jail1] [8/9] Extracting ruby24-bdb-0.6.6_5: 100% [jail1] [9/9] Installing portupgrade-2.4.16,2... [jail1] [9/9] Extracting portupgrade-2.4.16,2: 100% Message from ruby-2.4.5_1,1: ==== Some of the standard commands are provided as separate ports for ease of upgrading: devel/ruby-gems: gem - RubyGems package manager devel/rubygem-rake: rake - Ruby Make And some of the standard libraries are provided as separate ports since they require extra dependencies: databases/ruby-gdbm: GDBM module Install them as occasion demands. ``` ここで/usr/ports/INDEX-12を/var/portsにコピーしているが、最初にportsを使った(portinstall)ときに自動でできるだろうと思ったが、以下のようなエラーが連続的に発生し終了しなかった。 ``` ---> Session started at: Mon, 04 Feb 2019 20:03:20 +0900 Fetching the ports index ... Certificate verification failed for /C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3 34370572288:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:/usr/src/crypto/openssl/ssl/statem/statem_clnt.c:1925: fetch: https://www.FreeBSD.org/ports/INDEX-12.bz2: Authentication error Certificate verification failed for /C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3 34370572288:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:/usr/src/crypto/openssl/ssl/statem/statem_clnt.c:1925: fetch: https://www.FreeBSD.org/ports/INDEX-12.bz2: Authentication error Certificate verification failed for /C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3 ... ``` portinstallユーティリティでindexをダウンロードするURLにhttpsとなっているが認証できないようです。これを以下のようにすると正常にダウンロードができた。~~どちらかを修正してもらうとありがたい。~~(2019/3/3修正) ``` fetch http://www.FreeBSD.org/ports/INDEX-12.bz2 ``` しかし、前節で実施したportsnapで/usr/ports(/usr/jails/sharedfs/usr/ports)にダウンロードしているのでコピーすることとした。 fetchが標準ではhttpsからダウンロードすることができないことがわかった。以下のように証明書をインストールすれば可能になった。 ``` jail1 /root # pkg install ca_root_nss Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 1 package(s) will be affected (of 0 checked): New packages to be INSTALLED: ca_root_nss: 3.42.1 Number of packages to be installed: 1 290 KiB to be downloaded. [jail1] [1/1] Fetching ca_root_nss-3.42.1.txz: 100% 290 KiB 148.5kB/s 00:02 Checking integrity... done (0 conflicting) [jail1] [1/1] Installing ca_root_nss-3.42.1... [jail1] [1/1] Extracting ca_root_nss-3.42.1: 100% Message from ca_root_nss-3.42.1: ********************************* WARNING ********************************* FreeBSD does not, and can not warrant that the certification authorities whose certificates are included in this package have in any way been audited for trustworthiness or RFC 3647 compliance. Assessment and verification of trust is the complete responsibility of the system administrator. *********************************** NOTE ********************************** This package installs symlinks to support root certificates discovery by default for software that uses OpenSSL. This enables SSL Certificate Verification by client software without manual intervention. If you prefer to do this manually, replace the following symlinks with either an empty file or your site-local certificate bundle. * /etc/ssl/cert.pem * /usr/local/etc/ssl/cert.pem * /usr/local/openssl/cert.pem *************************************************************************** ``` ``` jail1 /root # fetch https://www.FreeBSD.org/ports/INDEX-12.bz2 INDEX-12.bz2 2323 kB 70 kBps 33s ``` ### sshの設定 外部からsshでログインするための一般ユーザアカウントを作成し進める。 ``` jail1 /root # adduser Username: hoge Full name: hoge hoge Uid (Leave empty for default): 1000 Login group [hoge]: Login group is hoge. Invite naka into other groups? []: wheel Login class [default]: Shell (sh csh tcsh nologin) [sh]: csh Home directory [/home/hoge]: Home directory permissions (Leave empty for default): Use password-based authentication? [yes]: Use an empty password? (yes/no) [no]: Use a random password? (yes/no) [no]: Enter password: Enter password again: Lock out the account after creation? [no]: Username : hoge Password : ***** Full Name : hoge hoge Uid : 1000 Class : Groups : hoge wheel Home : /home/hoge Home Mode : Shell : /bin/csh Locked : no OK? (yes/no): yes adduser: INFO: Successfully added (hoge) to the user database. Add another user? (yes/no): no Goodbye! jail1 /root >su hoge /root > ``` プロンプトを分かりやすくするため%に変更する。 ```sh:~/.cshrc - set prompt = "%/ >" + set prompt = "`/bin/hostname -s` %/ % " ``` ``` jail1 /root % ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/hoge/.ssh/id_rsa): Created directory '/home/hoge/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/hoge/.ssh/id_rsa. Your public key has been saved in /home/hoge/.ssh/id_rsa.pub. The key fingerprint is: SHA256:sqwi8l7H2M7uC/5/Axs4FCn0NEfA4PTOyg67cChtvfI hoge@jail1 The key's randomart image is: +---[RSA 2048]----+ | .+o++o | | o.++o | | ..o. | | o. | | .+.S | | o o *ooo | |+ = B *. + | |++o* B . o | |.+==E+Bo.. . | +----[SHA256]-----+ ``` ```sh:/etc/ssh/sshd_config PermitRootLogin forced-commands-only ``` ```sh:/etc/rc.conf sshd_enable="YES" sshd_flags="-o UseBlacklist=yes" ``` ``` jail1 /root # service sshd start Generating RSA host key. 2048 SHA256:gNA9qvynGV/Rt3UgrUILMma0i3kXxo+9kG4n+verYH8 root@jail1 (RSA) Generating ECDSA host key. 256 SHA256:+eeLAztl1afarobO1DTxnCboh36y9jxoMdSAyA6LMyM root@jail1 (ECDSA) Generating ED25519 host key. 256 SHA256:qPhCqle4rgLLk/Etnt4BwhxuAJSCCtOSLHa1I7Nk5eI root@jail1 (ED25519) Performing sanity check on sshd configuration. Starting sshd. ``` ### ftpサーバ ftpについては、以下の設定をすることによりftpが使用でき連携される。最初に使用宣言。 ```sh:/etc/rc.conf inetd_enable="YES" ``` 最後の-Bはデフォルトでは無くblacklistdと連携するためのオプションである ```/etc/inetd.conf ftp stream tcp nowait root /usr/libexec/ftpd ftpd -l -B ``` inetdを立ち上げ、blacklistdと連携するため再起動する ``` jail1 /root # service inetd start Starting inetd. jail1 /root # service blacklistd restart Stopping blacklistd. Waiting for PIDS: 2224. Starting blacklistd. ``` ### OS・portsのメインテナンス jailのOSとportsのアップデートはqjailで以下のコマンドでできるが、何れもホストで実施する事になり、この部分は別途整理する。 ``` # qjail update -b # qjail update -p ``` ### rootへのメールを自分宛にする ```sh:/etc/aliases root: hoge1@exsample.jp ``` ``` jail1 /root # newaliases ``` ### pkgのレポシトリ ``` jail1 /root # mkdir -p /usr/local/etc/pkg/repos/ jail1 /root # echo 'FreeBSD: { url: "pkg+http://pkg.FreeBSD.org/${ABI}/latest" }' > /usr/local/etc/pkg/repos/FreeBSD.conf jail1 /root # pkg update Updating FreeBSD repository catalogue... pkg: Repository FreeBSD has a wrong packagesite, need to re-create database [jail1] Fetching meta.txz: 100% 944 B 0.9kB/s 00:01 [jail1] Fetching packagesite.txz: 100% 6 MiB 290.8kB/s 00:23 Processing entries: 100% FreeBSD repository update completed. 31964 packages processed. All repositories are up to date. ``` ### japanese/less ``` jail1 /root # pkg install japanese/less Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 1 package(s) will be affected (of 0 checked): New packages to be INSTALLED: ja-less: 382.262.03.01 Number of packages to be installed: 1 191 KiB to be downloaded. [jail1][1/1] Fetching ja-less-382.262.03.01.txz: 100% 191 KiB 195.8kB/s 00:01 Checking integrity... done (0 conflicting) [jail1][1/1] Installing ja-less-382.262.03.01... [jail1][1/1] Extracting ja-less-382.262.03.01: 100% ``` ```~/.cshrc alias less /usr/local/bin/jless setenv LC_CTYPE ja_JP.eucJP setenv PAGER /usr/local/bin/jless ``` ### libiconv ``` jail1 /root # portinstall libiconv 省略 ``` ![libiconv.jpg](https://qiita-image-store.s3.amazonaws.com/0/233460/5ec6af77-ef61-68ef-8587-cda5c9e90721.jpeg) ``` jail1 /root # pkg lock libiconv Locking libiconv-1.14_11 ```