LoginSignup
15
21

More than 3 years have passed since last update.

【CentOS 7】NFSサーバ構築の備忘録

Last updated at Posted at 2019-09-24

CentOS 7.5のホストでNFSサーバ/クライアントを構築した時の作業ログ。
年に1,2回しか作業せずにそのたびにやり方うろ覚えなのでメモ。
firewalldSELinuxの設定含む。

NFSサーバ

初期状態(NFS関連のパッケージ無し)

[root@nfsserver ~]# rpm -qa | grep nfs
[root@nfsserver ~]#

インストール

nfs-utilsパッケージを入れればOK

[root@nfsserver ~]# yum install nfs-utils
:
:
インストール:
  nfs-utils.x86_64 1:1.3.0-0.65.el7
依存性関連をインストールしました:
  gssproxy.x86_64 0:0.7.0-26.el7                       keyutils.x86_64 0:1.5.8-3.el7
  libbasicobjects.x86_64 0:0.1.1-32.el7                libcollection.x86_64 0:0.7.0-32.el7
  libevent.x86_64 0:2.0.21-4.el7                       libini_config.x86_64 0:1.3.1-32.el7
  libnfsidmap.x86_64 0:0.25-19.el7                     libpath_utils.x86_64 0:0.2.1-32.el7
  libref_array.x86_64 0:0.1.5-32.el7                   libtirpc.x86_64 0:0.2.4-0.16.el7
  libverto-libevent.x86_64 0:0.2.5-4.el7               quota.x86_64 1:4.01-19.el7
  quota-nls.noarch 1:4.01-19.el7                       rpcbind.x86_64 0:0.2.0-48.el7
  tcp_wrappers.x86_64 0:7.6-77.el7

完了しました!
[root@nfsserver ~]#

設定

共有用ディレクトリ作成

NFSで共有するディレクトリを作成する。
(既存のディレクトリを使用するなら作らなくて良い)

[root@nfsserver ~]# mkdir -p /export/nfs
[root@nfsserver ~]#

確認用ファイル作成

クライアントからマウントした時に確認する用のファイル作成。(記事用)

[root@nfsserver ~]# echo hello > /export/nfs/hello.txt
[root@nfsserver ~]# echo zaki > /export/nfs/zaki.txt
[root@nfsserver ~]# chown zaki:zaki /export/nfs/zaki.txt
[root@nfsserver ~]# ls -la /export/nfs/
合計 8
drwxr-xr-x. 2 root root 39  9月 22 20:02 .
drwxr-xr-x. 3 root root 17  9月 22 20:01 ..
-rw-r--r--. 1 root root  6  9月 22 20:01 hello.txt
-rw-r--r--. 1 zaki zaki  5  9月 22 20:02 zaki.txt
[root@nfsserver ~]#

設定ファイル作成

設定ファイルは項目ごとに/etc/exports.d/*.exportsファイルを作成すればよい。拡張子はexportsであること。
(See: man exports)

[root@nfsserver ~]# echo "/export/nfs  *(rw,no_root_squash)" > /etc/exports.d/export-nfs.exports
[root@nfsserver ~]# cat /etc/exports.d/export-nfs.exports
/export/nfs  *(rw,no_root_squash)
[root@nfsserver ~]#

TODO:オプションについて書く(root_squashとか)

設定反映と確認

exportfs -arで反映、exportfs -vで設定内容を出力する。

[root@nfsserver ~]# exportfs -v
[root@nfsserver ~]# exportfs -ar
[root@nfsserver ~]# exportfs -v
/export/nfs     <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
[root@nfsserver ~]#

起動

NFSサーバを起動する。

[root@nfsserver ~]# systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@nfsserver ~]# systemctl start nfs-server
[root@nfsserver ~]# systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: active (exited) since 日 2019-09-22 20:08:24 JST; 11s ago
  Process: 79689 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 79662 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 79661 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 79662 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

 9月 22 20:08:24 nfsserver systemd[1]: Starting NFS server and services...
 9月 22 20:08:24 nfsserver systemd[1]: Started NFS server and services.
[root@nfsserver ~]# cat /etc/exports.d/export-nfs
/export/nfs  *(rw,no_root_squash)
[root@nfsserver ~]#

OSブート時にも有効にするには

[root@nfsserver ~]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

NFSクライアント

手動マウント

マウントは通常通りmountコマンドを使用する。その際typesオプション(-t)にnfsを指定する。
このときクライアントにもnfs-utilsパッケージがないとエラーになる(後述)

[root@node1 ~]# mount -t nfs -o rw nfsserver:/export/nfs /mnt -v
mount.nfs: timeout set for Sun Sep 22 20:31:15 2019
mount.nfs: trying text-based options 'vers=4.1,addr=192.168.0.40,clientaddr=192.168.0.45'
[root@node1 ~]#
[root@node1 ~]# ls -al /mnt/
合計 8
drwxr-xr-x.  2 root root  39  9月 22 20:02 .
dr-xr-xr-x. 17 root root 224  9月 17 22:05 ..
-rw-r--r--.  1 root root   6  9月 22 20:01 hello.txt
-rw-r--r--.  1 zaki zaki   5  9月 22 20:02 zaki.txt
[root@node1 ~]#

:を忘れずに。(訳:忘れてた)

/etc/fstab

OS起動時に自動でマウントさせるには、上記手動マウントと同じ設定であれば、以下を/etc/fstabへ追記すればOK

nfsserver:/export/nfs /mnt nfs rw 0 0

エラーの場合の対処

何も出力されずに応答がない場合は、mountのオプションに-vを付けるとわかりやすい。

[root@node1 ~]# ls /mnt/
[root@node1 ~]# mount -t nfs -o rw nfsserver:/export/nfs /mnt
mount: wrong fs type, bad option, bad superblock on nfsserver:/export/nfs,
       missing codepage or helper program, or other error
       (for several filesystems (e.g. nfs, cifs) you might
       need a /sbin/mount.<type> helper program)

       In some cases useful info is found in syslog - try
       dmesg | tail or so.
[root@node1 ~]#

のエラーが出る場合 → nfsマウントするためのパッケージ不足

[root@node1 ~]# yum install nfs-utils

[root@node1 ~]# mount -t nfs -o rw nfsserver:/export/nfs /mnt -v
mount.nfs: timeout set for Sun Sep 22 20:26:51 2019
mount.nfs: trying text-based options 'vers=4.1,addr=192.168.0.40,clientaddr=192.168.0.45'
mount.nfs: mount(2): No route to host

のエラーが出る場合 → (サーバのnfs-serverサービスがちゃんと起動しておりpingsshが問題ないのであれば)サーバ側のfirewalld/iptablesで拒否されているので許可する。(またはoff)

[root@nfsserver ~]# firewall-cmd --add-service=nfs --permanent
success
[root@nfsserver ~]# firewall-cmd --reload
success
[root@nfsserver ~]#

設定できるサービスはこんな感じ

[root@nfsserver ~]# firewall-cmd --list-services
dhcpv6-client nfs ssh

iptablesの場合は…結構ややこしそうです。


[root@node1 ~]# mount -t nfs -o rw nfsserver:/export/nfs /mnt -v
mount.nfs: timeout set for Sun Sep 22 20:27:24 2019
mount.nfs: trying text-based options 'vers=4.1,addr=192.168.0.40,clientaddr=192.168.0.45'
mount.nfs: mount(2): Connection refused
mount.nfs: trying text-based options 'addr=192.168.0.40'
mount.nfs: prog 100003, trying vers=3, prot=6
mount.nfs: trying 192.168.0.40 prog 100003 vers 3 prot TCP port 2049
mount.nfs: portmap query failed: RPC: Remote system error - Connection refused

が出る場合 → サーバ側のSELinux設定

[root@nfsserver ~]# getenforce
Enforcing
[root@nfsserver ~]# setsebool -P nfs_export_all_rw on
[root@nfsserver ~]# systemctl restart nfs-server
[root@nfsserver ~]#

もしくは石川さんにごめんなさいする。

15
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
21