Twitterに投稿 はてなブックマークに追加 Yahoo!ブックマークに追加 Google Bookmarksに追加

目次 >> NFS

NFSの設定

ここでは、NFSの設定を説明する。
複数のLinuxサーバがLAN内で稼働している場合は、NFS(Network File System)でファイルを共有するのが簡単である。ここではNFSを使ってファイルをネットワーク内で共有する。sambaはWindowsやMac OS Xなどが存在する際の共有には便利であるが、Linux/UNIXのみの環境などでは、NFSで共有するのが簡単である。また、計算機クラスターなど、ネットワーク越しの高速なディスクアクセスが必要な場合にも有効である。

NFSでネットワークドライブを構成する - NFSサーバ側の設定

まずはサーバのインストール

$ sudo apt-get install nfs-kernel-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libgssglue1 libnfsidmap2 librpcsecgss3 nfs-common portmap
The following NEW packages will be installed:
  libgssglue1 libnfsidmap2 librpcsecgss3 nfs-common nfs-kernel-server portmap
0 upgraded, 6 newly installed, 0 to remove and 108 not upgraded.
Need to get 432 kB of archives.
After this operation, 1,716 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://us.archive.ubuntu.com/ubuntu/ natty/main libgssglue1 amd64 0.1-4ubuntu1 [21.7 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ natty/main libnfsidmap2 amd64 0.23-2 [32.1 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu/ natty/main librpcsecgss3 amd64 0.19-2 [36.3 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu/ natty/main portmap amd64 6.0.0-2ubuntu5 [31.1 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu/ natty/main nfs-common amd64 1:1.2.2-4ubuntu5 [196 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu/ natty/main nfs-kernel-server amd64 1:1.2.2-4ubuntu5 [115 kB]
Fetched 432 kB in 28s (15.4 kB/s)                                              
Preconfiguring packages ...
Selecting previously deselected package libgssglue1.
(Reading database ... 135575 files and directories currently installed.)
Unpacking libgssglue1 (from .../libgssglue1_0.1-4ubuntu1_amd64.deb) ...
Selecting previously deselected package libnfsidmap2.
Unpacking libnfsidmap2 (from .../libnfsidmap2_0.23-2_amd64.deb) ...
Selecting previously deselected package librpcsecgss3.
Unpacking librpcsecgss3 (from .../librpcsecgss3_0.19-2_amd64.deb) ...
Selecting previously deselected package portmap.
Unpacking portmap (from .../portmap_6.0.0-2ubuntu5_amd64.deb) ...
Selecting previously deselected package nfs-common.
Unpacking nfs-common (from .../nfs-common_1%3a1.2.2-4ubuntu5_amd64.deb) ...
Selecting previously deselected package nfs-kernel-server.
Unpacking nfs-kernel-server (from .../nfs-kernel-server_1%3a1.2.2-4ubuntu5_amd64.deb) ...
Processing triggers for ureadahead ...
Processing triggers for man-db ...
Setting up libgssglue1 (0.1-4ubuntu1) ...
Setting up libnfsidmap2 (0.23-2) ...
Setting up librpcsecgss3 (0.19-2) ...
Setting up portmap (6.0.0-2ubuntu5) ...
portmap start/running, process 6941
Setting up nfs-common (1:1.2.2-4ubuntu5) ...

Creating config file /etc/idmapd.conf with new version

Creating config file /etc/default/nfs-common with new version
Adding system user `statd' (UID 116) ...
Adding new user `statd' (UID 116) with group `nogroup' ...
Not creating home directory `/var/lib/nfs'.
statd start/running, process 7167
gssd stop/pre-start, process 7196
idmapd stop/pre-start, process 7228
Setting up nfs-kernel-server (1:1.2.2-4ubuntu5) ...

Creating config file /etc/exports with new version

Creating config file /etc/default/nfs-kernel-server with new version
 * Exporting directories for NFS kernel daemon...
   ...done.
 * Starting NFS kernel daemon
   ...done.
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

(必要であれば)公開するディレクトリを作成する。ここでは例として/srv/nfsdirを公開ディレクトリとすることにする。

$ sudo mkdir /srv/nfsdir

/etc内にexportsというファイルがある。そのファイルに公開するディレクトリを書き込む。具体的には、

#特定のマシンのみに公開
/srv/nfsdir 192.168.20.1(rw,sync,no_subtree_check)
#特定のネットワークに公開
/srv/nfsdir2 192.168.0.0/255.255.255.0(rw,sync,no_subtree_check)

最初の項目は公開するディレクトリ、スペースを空けてクライアント側のIPアドレスもしくはホスト名を書き込む。その後スペースを空けずに「(」が続き、rwは読み書き用に、もしこれをroにすれば読み込み専用になる。その後の、syncとno_subtree_checkは書かなくてもデフォルトの設定でそのようになるが、NFSサーバ起動時に警告メッセージが表示されるので、書いておいた方がよい。

NFSサーバの起動、停止は/etc/init.d/nfs-kernel-serverで行う

起動

$ sudo /etc/init.d/nfs-kernel-server start

停止

$ sudo /etc/init.d/nfs-kernel-server stop

再起動

$ sudo /etc/init.d/nfs-kernel-server restart

NFSでネットワークドライブを構成する - NFSクライアント側の設定

次にクライアント側の設定であるが、まずはインストール。

$ sudo apt-get install nfs-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libgssglue1 libnfsidmap2 librpcsecgss3 portmap
The following NEW packages will be installed:
libgssglue1 libnfsidmap2 librpcsecgss3 nfs-common portmap
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 317 kB of archives.
After this operation, 1,278 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://us.archive.ubuntu.com/ubuntu/ natty/main libgssglue1 amd64 0.1-4ubuntu1 [21.7 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ natty/main libnfsidmap2 amd64 0.23-2 [32.1 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu/ natty/main librpcsecgss3 amd64 0.19-2 [36.3 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu/ natty/main portmap amd64 6.0.0-2ubuntu5 [31.1 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu/ natty/main nfs-common amd64 1:1.2.2-4ubuntu5 [196 kB]
Fetched 317 kB in 3s (85.0 kB/s)
Preconfiguring packages ...
Selecting previously deselected package libgssglue1.
(Reading database ... 134642 files and directories currently installed.)
Unpacking libgssglue1 (from .../libgssglue1_0.1-4ubuntu1_amd64.deb) ...
Selecting previously deselected package libnfsidmap2.
Unpacking libnfsidmap2 (from .../libnfsidmap2_0.23-2_amd64.deb) ...
Selecting previously deselected package librpcsecgss3.
Unpacking librpcsecgss3 (from .../librpcsecgss3_0.19-2_amd64.deb) ...
Selecting previously deselected package portmap.
Unpacking portmap (from .../portmap_6.0.0-2ubuntu5_amd64.deb) ...
Selecting previously deselected package nfs-common.
Unpacking nfs-common (from .../nfs-common_1%3a1.2.2-4ubuntu5_amd64.deb) ...
Processing triggers for ureadahead ...
Processing triggers for man-db ...
Setting up libgssglue1 (0.1-4ubuntu1) ...
Setting up libnfsidmap2 (0.23-2) ...
Setting up librpcsecgss3 (0.19-2) ...
Setting up portmap (6.0.0-2ubuntu5) ...
portmap start/running, process 2064
Setting up nfs-common (1:1.2.2-4ubuntu5) ...

Creating config file /etc/idmapd.conf with new version

Creating config file /etc/default/nfs-common with new version
Adding system user `statd' (UID 116) ...
Adding new user `statd' (UID 116) with group `nogroup' ...
Not creating home directory `/var/lib/nfs'.
statd start/running, process 2289
gssd stop/pre-start, process 2318
idmapd stop/pre-start, process 2350
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

手動でマウント

例えば、/uというディレクトリにマウントするには、

cd /

$ sudo mount -t nfs 192.168.20.8:/srv/nfsdir /u

とする。

fstabでマウント

起動時に自動的に接続するようにするには、/etc/fstabに書き込む。
たとえば

192.1680.10.12:/srv/nfsdir /u nfs rw 0 0

autofsでマウント

まずはautofsをインストールする

$ sudo apt-get install autofs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-2.6.32-21-generic linux-headers-2.6.32-21
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
autofs5
The following NEW packages will be installed:
autofs autofs5
0 upgraded, 2 newly installed, 0 to remove and 5 not upgraded.
Need to get 682kB of archives.
After this operation, 1,708kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://us.archive.ubuntu.com/ubuntu/ lucid/main autofs5 5.0.4-3.1ubuntu5 [664kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ lucid/main autofs 5.0.4-3.1ubuntu5 [18.1kB]
Fetched 682kB in 2s (255kB/s)
Selecting previously deselected package autofs5.
(Reading database ... 148548 files and directories currently installed.)
Unpacking autofs5 (from .../autofs5_5.0.4-3.1ubuntu5_amd64.deb) ...
Selecting previously deselected package autofs.
Unpacking autofs (from .../autofs_5.0.4-3.1ubuntu5_amd64.deb) ...
Processing triggers for man-db ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Setting up autofs5 (5.0.4-3.1ubuntu5) ...

Creating config file /etc/auto.master with new version

Creating config file /etc/auto.net with new version

Creating config file /etc/auto.misc with new version

Creating config file /etc/auto.smb with new version

Creating config file /etc/default/autofs with new version
autofs start/running, process 2177

Setting up autofs (5.0.4-3.1ubuntu5) ...

そしてまず、/etc/auto.masterファイルにマウントポイントとマップファイルの対応を書く。
書式は

マウントポイント マップファイル オプション

である。具体的には

/u /etc/auto.u --timeout 60

次にマップファイル(マップファイルは自由に設定できてここではauto.u)を設定する。

home --fstype=nfs,rsize=8192,wsize=8192,noatime  192.168.20.1:/srv/nfsdir/home

autofsの起動、停止、再起動はservice autofsで行う。

起動

$ service autofs start

停止

$ service autofs stop

再起動

$ service autofs restart

ホームディレクトリをネットワークディレクトリ上にする

ホームディレクトリを変更するには、通常のホームディレクトリの変更方法と変わらず、usermodかpasswdファイルをvipwで編集して変更する。

ユーザーfooのホームディレクトリを変更するには、

$ sudo usermod -d home_dir foo

のように打つ。

FAQ

ownerがおかしい

ディレクトリやファイルの所有権を整える最も簡単な方法は、サーバーでもクライアントでも同じ順序でユーザーを作成していくことである。そうすることによって、各ユーザーに割り当てられる固有の数値が同じになる。passwdファイルの中の数値を変更して行う方法もある。

サーバー、およびクライアントでユーザー固有の数値が同じであるのに、マウントしたディレクトリで下記のように数値が表示される場合がある。

$ ls -la
total 1627304
drwxr-xr-x 36 4294967294 4294967294      4096 2011-05-15 23:12 .
drwxr-xr-x  4 4294967294 4294967294      4096 2010-11-10 23:37 ..
drwxr-xr-x  2 4294967294 4294967294      4096 2011-02-03 13:27 abc
drwxr-xr-x  2 4294967294 4294967294      4096 2011-04-29 23:24 abs
drwxr-xr-x  2 4294967294 4294967294      4096 2011-05-13 11:54 alm
drwxr-xr-x  6 4294967294 4294967294      4096 2011-01-09 21:29 backup
-rw-------  1 4294967294 4294967294     39561 2011-05-15 18:36 .bash_history
-rwxr-xr-x  1 4294967294 4294967294      3608 2011-03-29 09:53 .bashrc

この場合、マウントする際に、nfsvers=3というオプションを追加してやると解決する可能性がある。

$ sudo mount -t nfs -o nfsvers=3 192.168.20.8:/srv/nfsdir /u

fstabであれば、下記のようにオプションを追加する

192.1680.10.12:/srv/nfsdir /u nfs rw,nfsvers=3 0 0
$ ls -la
total 1627304
drwxr-xr-x 36 foo   foo        4096 2011-05-15 23:39 .
drwxr-xr-x  4 root  root       4096 2010-11-10 23:37 ..
drwxr-xr-x  2 foo   foo        4096 2011-02-03 13:27 abc
drwxr-xr-x  2 foo   foo        4096 2011-04-29 23:24 abs
drwxr-xr-x  2 foo   foo        4096 2011-05-13 11:54 alm
drwxr-xr-x  6 foo   foo        4096 2011-01-09 21:29 backup
-rw-------  1 foo   foo       39561 2011-05-15 18:36 .bash_history
-rwxr-xr-x  1 foo   foo        3608 2011-03-29 09:53 .bashrc

リンク集


本文中のFC4はFedora ProjectのFedora Core 4を、FC5はFedora Core 5を、FC6はFedora Core 6をopenSUSEはNovellのSUSE Linux OSSを表します。Fedora7以降は、単にFedora7、Fedora8、Fedora9、Fedora10、Fedora11、Fedora12、Fedora13、Fedora14、Fedora15と表示しています。Ubuntuは、必要に応じて10.04、11.04のようにバージョン番号をつけて区別しています。MandrivaはMandriva Linuxを表します。

ここに登場するドメイン名やIPアドレスなどはフィクションです。実在の人物・団体等とは一切関係がありません。
実際に使用する際は、各自の環境に合わせて書き換えてください。
もし何か間違いなどありましたらこちらからご連絡ください
リンクに許可は不要です。
Copyright (C) 2011 Chikuma Engineering Co., Ltd. All Rights Reserved.