{{tag>gentoo linux}} # samba サーバーをたてる 言わずと知れたファイルサーバーですね。いまうちの環境で動いている samba は……バージョン不明です。linux カーネル は 3.7.10 で、コンパイルしたのは2013年5月5日らしいですw 別に外のネットワークに晒しているわけでもないし安定して動いているんですが、さすがにもう怖くて迂闊に `emerge` とか投げられないですし、カーネルのバージョンアップとかもとんでもなく苦労しそうなので、丸ごと作り直すことにしました。 この作業に先立ってディスクリソースとかの移行は[別の記事](move_lvm_disk_to_new_server)のとおりに済ませてあります。 さて。samba とかそのへんのメジャーどころに関しては gentoo のドキュメントの豊富さがありがたいです。[Samba - gentoo wiki](https://wiki.gentoo.org/wiki/Samba)に従ってやっていきます。 ## カーネルコンフィグ gentoo wiki の言うとおりにします。 ``` --- Network File Systems <*> CIFS support (advanced network filesystem, SMBFS successor) [*] CIFS statistics [*] Extended statistics [ ] Support legacy servers which use weaker LANMAN security ( [ ] Kerberos/SPNEGO advanced session setup (NEW) [*] CIFS extended attributes [*] CIFS POSIX Extensions [*] Provide CIFS ACL support [*] Enable CIFS debugging routines (NEW) [ ] Enable additional CIFS debugging routines (NEW) [ ] DFS feature support (NEW) [*] SMB2 and SMB3 network file system support [ ] SMB3.1.1 network file system support (Experimental) (NE ``` で、make してインストールして一連のお約束を実行します。 ``` # make && make modules_install # mount /boot # cp /usr/src/linux/arch/x86_64/boot/bzImage /boot/kernel-4.9.16-gentoo-fs01.20170410.01 # grub-mkconfig Generating grub configuration file ... Found linux image: /boot/kernel-4.9.16-gentoo-fs01.20170410.01 Found linux image: /boot/kernel-4.9.6-gentoo-r1-fs01.20170410.01 done # umount /boot # systemctl reboot ``` 再起動したら、gentoo wiki に書いてある Note を確認してみます。 ``` # dmesg | grep CIFS # ``` なにも引っかからないので良しとしましょう。 ## samba のインストール ``` # emerge -av samba ~だばー(略)~ [ebuild N ] net-nds/openldap-2.4.44::gentoo USE="berkdb crypt ipv6 ssl syslog tcpd -cxx -debug -experimental -gnutls -iodbc -kerberos -kinit (-libressl) -minimal -odbc -overlays -perl -samba -sasl (-selinux) -slp -smbkrb5passwd -static-libs" ABI_X86="(64) -32 (-x32)" 0 KiB [ebuild N ] dev-libs/libverto-0.2.5-r1::gentoo USE="libev threads -glib -libevent -static-libs -tevent" ABI_X86="(64) -32 (-x32)" 342 KiB [ebuild N ] dev-util/cmake-3.7.2::gentoo USE="ncurses -doc -emacs -qt5 -system-jsoncpp {-test}" 0 KiB [ebuild N ] dev-python/traceback2-1.4.0::gentoo PYTHON_TARGETS="python2_7 python3_4 (-pypy) (-pypy3) (-python3_5) (-python3_6)" 16 KiB [ebuild N ] app-crypt/mit-krb5-1.14.2::gentoo USE="keyutils pkinit threads -doc (-libressl) -openldap (-selinux) {-test} -xinetd" ABI_X86="(64) -32 (-x32)" 11978 KiB [ebuild N ] sys-libs/ldb-1.1.26:0/1.1.26::gentoo USE="-doc" ABI_X86="(64) -32 (-x32)" PYTHON_TARGETS="python2_7" 1234 KiB [ebuild N ] net-libs/socket_wrapper-1.1.6::gentoo USE="{-test}" ABI_X86="(64) -32 (-x32)" 53 KiB [ebuild N ] sys-libs/nss_wrapper-1.1.3::gentoo USE="{-test}" ABI_X86="(64) -32 (-x32)" 53 KiB [ebuild N ] sys-libs/uid_wrapper-1.2.1::gentoo USE="{-test}" ABI_X86="(64) -32 (-x32)" 305 KiB [ebuild N ] dev-python/unittest2-1.1.0::gentoo PYTHON_TARGETS="python2_7 python3_4 (-pypy) (-pypy3) (-python3_5) (-python3_6)" 80 KiB [ebuild N ] dev-python/testtools-1.8.0::gentoo USE="-doc {-test}" PYTHON_TARGETS="python2_7 python3_4 (-pypy) (-pypy3) (-python3_5) (-python3_6)" 204 KiB [ebuild N ] dev-python/subunit-1.2.0-r1::gentoo USE="-static-libs {-test}" ABI_X86="(64) -32 (-x32)" PYTHON_TARGETS="python2_7 python3_4 (-pypy) (-pypy3) (-python3_5) (-python3_6)" 450 KiB [ebuild N ] net-fs/samba-4.2.14::gentoo USE="acl pam system-mitkrb5 systemd -addc -addns -ads -aio -avahi -client -cluster -cups -dmapi -fam -gnutls -iprint -ldap -quota (-selinux) -syslog {-test} -winbind" ABI_X86="(64) -32 (-x32)" PYTHON_TARGETS="python2_7" 20400 KiB Total: 30 packages (30 new), Size of downloads: 39611 KiB The following USE changes are necessary to proceed: (see "package.use" in the portage(5) man page for more details) # required by net-fs/samba-4.2.14::gentoo # required by samba (argument) >=sys-libs/ntdb-1.0-r1 python # required by sys-libs/ldb-1.1.26::gentoo # required by net-fs/samba-4.2.14::gentoo # required by samba (argument) >=sys-libs/tdb-1.3.8 python # required by sys-libs/ldb-1.1.26::gentoo # required by net-fs/samba-4.2.14::gentoo # required by samba (argument) >=sys-libs/tevent-0.9.28 python Would you like to add these changes to your config files? [Yes/No] y ``` ……えらいたくさん入ってきますねw 個人的には USE は `-ldap` なのに openldap が入ってくるのが気になりますが、まぁ最近の samba ってワリと AD 環境でも耐えうるようになってるみたいだし……しょうがないかなと。 あと、強制 USE 変更が出てるので `y` を返した後で一度 `dispatch-conf` しないとですね。use-new の `u` で /etc/package.use/ 下にファイルを作ってもらいました。そして再度…… ``` # emerge -av samba ~だばー(略)~ [ebuild N ] net-fs/samba-4.2.14::gentoo USE="acl pam system-mitkrb5 systemd -addc -addns -ads -aio -avahi -client -cluster -cups -dmapi -fam -gnutls -iprint -ldap -quota (-selinux) -syslog {-test} -winbind" ABI_X86="(64) -32 (-x32)" PYTHON_TARGETS="python2_7" 20400 KiB Total: 30 packages (30 new), Size of downloads: 39611 KiB Would you like to merge these packages? [Yes/No] y ``` しばし待ちですね。 ## うどんワールド gentoo wiki では、せっかく samba 入れたんだからグローバル USE に `samba` 足してうどんワールド((`emerge -uDN world`))しなよ的なことが書いてありますね。まぁ必須じゃなさそうですが、どれくらい影響があるのかちょっと確認してみます。`USE="${USE} samba" emerge -uDNpv world` すればいいですね。 ``` # USE="${USE} samba" emerge -uDNpv world These are the packages that would be merged, in order: Calculating dependencies... done! [ebuild R ] net-misc/ntp-4.2.8_p9::gentoo USE="ipv6 readline samba* ssl threads -caps -debug (-libressl) -openntpd -parse-clocks (-selinux) -snmp -vim-syntax -zeroconf" 7087 KiB [ebuild R ] net-nds/openldap-2.4.44::gentoo USE="berkdb crypt ipv6 samba* ssl syslog tcpd -cxx -debug -experimental -gnutls -iodbc -kerberos -kinit (-libressl) -minimal -odbc -overlays -perl -sasl (-selinux) -slp -smbkrb5passwd -static-libs" ABI_X86="(64) -32 (-x32)" 0 KiB Total: 2 packages (2 reinstalls), Size of downloads: 7087 KiB ``` ……ntp に samba が絡むんですか? openldap も、そもそも samba に引っ張られて入ってきたのに……なんかもにょりますw ### net-misc/ntp [gentoo packages](https://packages.gentoo.org/packages/net-misc/ntp)で調べてみました。 > Provide support for Samba's signing daemon (needed for Active Directory domain controllers) ……だそうです。まぁ設定しても ntpd として害はなさそう。 ### net-nds/openldap 同じく[gentoo packages](https://packages.gentoo.org/packages/net-nds/openldap)で。 > Add support for SAMBA (Windows File and Printer sharing) ……なんか納得いかない!!w まぁこれも害はなさそうですね。 まぁ、どちらにしろ致命傷にはならなそうなのでグローバル USE に追加してうどんワールドしときますか。/etc/portage/make.conf の最下行にこんなふうに追記して、うどんワールドしときました。 ``` # samba @17.04.10 USE="${USE} samba" ``` ## cifs-utils なんとなく、e2fsprogs とか dosfstools とかその辺に近いパッケージっぽいですよね。使うかどうかわからないけれど、gentoo wiki でオススメされているようなので入れておきます。 ``` # emerge -av cifs-utils These are the packages that would be merged, in order: Calculating dependencies... done! [ebuild N ] sys-libs/libcap-ng-0.7.8::gentoo USE="-python -static-libs" PYTHON_TARGETS="python2_7 python3_4 (-python3_5)" 438 KiB [ebuild N ] virtual/krb5-0-r1::gentoo ABI_X86="(64) -32 (-x32)" 0 KiB [ebuild N ] net-fs/cifs-utils-6.4::gentoo USE="acl ads caps caps-ng -creds" 384 KiB Total: 3 packages (3 new), Size of downloads: 822 KiB Would you like to merge these packages? [Yes/No] y ``` ## samba の設定 ここまできたら /etc/samba/smb.conf.default なんてファイルができています。うちの環境ではもともと動いていた samba さんがいるので、このデフォルトの設定ファイルをコピーして、もともと動いていた samba さんの設定ファイルの内容をマージしていけば良さそうです。ただ毎回忘れて悩むのが smbusers とか pdbedit あたりなので、そこだけ書き残します。 ### smb.conf の認証設定 大体の場合は、こんな設定を入れると思います。 ``` security = user ``` `share` はともかく、他の設定は別の認証サーバーが必要だったりするので、個人宅ファイルサーバーではなかなか使わない(※ 趣味でやる場合は除く)と思います。 さて `user` の設定は…… * samba サーバーに各個人の linux アカウントが必要 * ただし samba ユーザーのパスワードは linux アカウントのパスワードとは別管理(samba → linux アカウント方向で同期することもできる) ……あたりを忘れないようにしておけば良いでしょうか。 ### pdbedit samba ユーザーのパスワードを設定するコマンドです。 ``` # pdbedit -a manimani new password: <パスワード> retype new password: <パスワード> ``` とすることで、ユーザー manimani の samba パスワードを設定することができます。この時、前述のとおり samba サーバーに manimani という linux アカウントが存在している必要があります。 存在する samba ユーザーを一覧表示するには `pdbedit -L` 詳細表示するには `pdbedit -v` `pdbedit -Lv` とか。 ちなみに、古い解説記事に書かれているような smbpasswd なんてファイルはとうの昔から存在しません…… ``` passdb backend = tdbsam ``` smb.conf ではこんな設定になっていると思います。たぶんこの tdb の実体は /var/lib/samba/ 下にある xxx.tdb のどれかです。 なお samba サーバーに……この例でいえば linux アカウント manimani でログインしている場合は、従来どおり `smbpasswd` コマンドを使って自分の samba パスワードを変更できるようです。 ### smbusers これは、samba ユーザーのエイリアスを設定するためのファイルです。smb.conf 内でこんな風に指定します。 ``` username map = /etc/samba/smbusers ``` このファイルに、例えば…… ``` manimani ManiManinosuke ``` ……とか設定しておくと、ManiManinosuke というユーザー名で samba にアクセスがあった場合は、samba ユーザー(= linux アカウント)の manimani として処理してくれます。 こんなことをしてなにが得なのかと言うと、この設定を使うことで windows 端末から samba サーバーに接続する際のパスワード入力を省略できるケースがあるからです。 ![windows と samba の関係](setup_samba.001.png) どうやら windows は smb で共有フォルダにアクセスに行く時に、まず最初に windows のログオンユーザーとパスワードを使ってアクセスにいくみたいです。例の場合は windows に ManiManinosuke というユーザーで、パスワード Vulnerable でログオンしています。一方 samba 側では samba ユーザー(= linux アカウント)が manimani という windows 側のユーザーとは異なるユーザー名であっても、間に smbusers を噛ませることで同一ユーザーとして扱うことができるようになります。 なので、pdbedit で samba のパスワードを windows 側のパスワードと同じ Vulnerable にしておけば、ユーザー名とパスワードの入力ダイアログを表示することなくアクセスできるようになります。