Twitterに投稿 はてなブックマークに追加 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 libtirpc1 rpcbind
The following NEW packages will be installed:
  libgssglue1 libnfsidmap2 libtirpc1 nfs-common rpcbind
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 418 kB of archives.
After this operation, 1,361 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://us.archive.ubuntu.com/ubuntu/ precise/main libgssglue1 amd64 0.3-4 [22.3 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ precise/main libtirpc1 amd64 0.2.2-5 [84.2 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu/ precise/main rpcbind amd64 0.2.0-7ubuntu1 [42.7 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu/ precise/main libnfsidmap2 amd64 0.25-1ubuntu2 [32.0 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu/ precise/main nfs-common amd64 1:1.2.5-3ubuntu3 [236 kB]
Fetched 418 kB in 1s (269 kB/s)     
Selecting previously unselected package libgssglue1.
(Reading database ... 177269 files and directories currently installed.)
Unpacking libgssglue1 (from .../libgssglue1_0.3-4_amd64.deb) ...
Selecting previously unselected package libtirpc1.
Unpacking libtirpc1 (from .../libtirpc1_0.2.2-5_amd64.deb) ...
Selecting previously unselected package rpcbind.
Unpacking rpcbind (from .../rpcbind_0.2.0-7ubuntu1_amd64.deb) ...
Selecting previously unselected package libnfsidmap2.
Unpacking libnfsidmap2 (from .../libnfsidmap2_0.25-1ubuntu2_amd64.deb) ...
Selecting previously unselected package nfs-common.
Unpacking nfs-common (from .../nfs-common_1%3a1.2.5-3ubuntu3_amd64.deb) ...
Processing triggers for man-db ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Setting up libgssglue1 (0.3-4) ...
Setting up libtirpc1 (0.2.2-5) ...
Setting up rpcbind (0.2.0-7ubuntu1) ...
 Removing any system startup links for /etc/init.d/rpcbind ...
portmap start/running, process 2524
Setting up libnfsidmap2 (0.25-1ubuntu2) ...
Setting up nfs-common (1:1.2.5-3ubuntu3) ...

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

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

手動でマウント

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

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

とする。

fstabでマウント

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

書式は、
(デバイス) (マウントポイント) (ファイルシステム) (マウントオプション) (dumpオプション) (fsckオプション)
の順で各項目間を空白で区切って書く。
デバイスの項目は、サーバのアドレス:ディレクトリ、
マウントポイントは、マウントポイントとなるクライアントファイルシステム内のディレクトリ、
ファイルシステムは、この場合nfsを、
dumpオプションはdumpコマンドの実行対象としないので0を、
fsckオプションは、システム起動時にfsckチェックする必要は無いので0を指定する。

マウントオプションは下記のようなものがある
rw - 読み書き用で開く
ro - 読み込み専用で開く

たとえば、fstabに下記のように設定すると、

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

192.168.10.12というIPアドレスのサーバ内の/srv/nfsdirというディレクトリを/uというディレクトリに読み書き用でマウントする。

autofsでマウント

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

$ sudo apt-get install autofs
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  autofs5
Suggested packages:
  smbfs
The following NEW packages will be installed:
  autofs autofs5
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 662 kB of archives.
After this operation, 1,588 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://us.archive.ubuntu.com/ubuntu/ precise/main autofs5 amd64 5.0.6-0ubuntu5 [659 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ precise/main autofs amd64 5.0.6-0ubuntu5 [2,624 B]
Fetched 662 kB in 1s (450 kB/s)     
Selecting previously unselected package autofs5.
(Reading database ... 177370 files and directories currently installed.)
Unpacking autofs5 (from .../autofs5_5.0.6-0ubuntu5_amd64.deb) ...
Selecting previously unselected package autofs.
Unpacking autofs (from .../autofs_5.0.6-0ubuntu5_amd64.deb) ...
Processing triggers for ureadahead ...
Processing triggers for man-db ...
Setting up autofs5 (5.0.6-0ubuntu5) ...

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 3554
Setting up autofs (5.0.6-0ubuntu5) ...

そしてまず、/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

rootなのに、nfsサーバ内のファイルにアクセスできない

rootであるにも関わらず、nfsでマウントしたサーバ内のファイルにアクセスできないことがある。
これはセキュリティー上、rootユーザは、nfsディレクトリ内ではnfsnobodyユーザーとして扱われるからである。
nfsディレクトリ内でもrootとして扱うようにするには、nfsサーバのexportsファイル内での設定の際に、no_root_squashを付加する。
具体的には

/srv/nfsdir 192.168.20.1(rw,sync,no_subtree_check)

/srv/nfsdir 192.168.20.1(rw,sync,no_subtree_check,no_root_squash)

の様にする。

リンク集

最終更新日


本文中の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は、必要に応じて20.04、21.04のようにバージョン番号をつけて区別しています。

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