6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【2023年7月版】NFS でファイル共有構築

Last updated at Posted at 2023-07-02

はじめに

NFS でファイル共有しよう

サーバは nfs01(192.168.100.240)
クライアントは nfs02(192.168.100.241)

共有するディレクトリは、/export/nfs/share

環境

$ uname -a
Linux nfs01 5.15.0-76-generic #83-Ubuntu SMP Thu Jun 15 19:21:56 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux
$ cat /proc/version
Linux version 5.15.0-76-generic (buildd@bos02-arm64-019) (gcc (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #83-Ubuntu SMP Thu Jun 15 19:21:56 UTC 2023
$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.2 LTS"
$ cat /etc/os-release 
PRETTY_NAME="Ubuntu 22.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.2 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

サーバ側の設定

サービスのインストール

$ sudo apt update
$ sudo apt install nfs-kernel-server

共有フォルダの作成とパーミッションの設定

$ sudo mkdir -p /export/nfs/share
$ sudo chown nobody:nogroup /export/nfs/share
$ sudo chmod 777 /export/nfs/share

設定ファイルの編集

192.168.100.0/24 からアクセスできるように設定

$ sudo vim /etc/exports
$ diff /etc/exports.backup /etc/exports
10a11
> /export/nfs/share 192.168.100.0/255.255.255.0(rw,sync,no_subtree_check)

サービスの再起動

$ sudo systemctl restart nfs-kernel-server

共有の確認

$ sudo exportfs
/export/nfs/share
		192.168.100.0/255.255.255.0

クライアント側の設定

サービスのインストール

$ sudo apt update
$ sudo apt install nfs-common

nfsのマウント

hard(デフォルト)でのマウント

$ sudo mkdir -p /mnt/nfs/share
$ sudo mount -t nfs 192.168.100.240:/export/nfs/share /mnt/nfs/share
$ nfsstat -m
/mnt/nfs/share from 192.168.100.240:/export/nfs/share
 Flags:	rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.100.241,local_lock=none,addr=192.168.100.240

softでのマウント
初回のタイムアウトまでに5秒待ち(timeo=50)、その後5回再送を試みる(retrans=5)
それでも応答がない場合は、エラーを返し、プロセスはブロックされることなく進行する

$ sudo mkdir -p /mnt/nfs/share
$ sudo mount -t nfs -o soft,timeo=50,retrans=5 192.168.100.240:/export/nfs/share /mnt/nfs/share
$ nfsstat -m
/mnt/nfs/share from 192.168.100.240:/export/nfs/share
 Flags:	rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,soft,proto=tcp,timeo=50,retrans=5,sec=sys,clientaddr=192.168.100.241,local_lock=none,addr=192.168.100.240

クライアント側の設定(macOS12.6)

$ mount -t nfs -o vers=4.0 192.168.100.240:/export/nfs/share/ /Users/dev/nfs

nfsの確認

サーバ

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
  :
  :
/dev/sdb                           2.0G  3.4M  1.8G   1% /export
$ dd if=/dev/random of=/export/nfs/share/abcd.dat bs=1024 count=102400 status=progress
102400+0 records in
102400+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.428285 s, 245 MB/s
$ ls -lt
total 102400
-rw-rw-r-- 1 dev dev 104857600 Jul  2 13:16 abcd.dat
$ md5sum /export/nfs/share/abcd.dat 
bdb310dd88144e5e694767650e162d11  abcd.dat

クライアント

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
  :
  :
192.168.100.240:/export/nfs/share  2.0G  3.3M  1.8G   1% /mnt/nfs/share
$ ls -lt
total 102400
-rw-rw-r-- 1 dev dev 104857600 Jul  2 13:16 abcd.dat
$ md5sum /export/nfs/share/abcd.dat 
bdb310dd88144e5e694767650e162d11  abcd.dat

うまくいかないとき

  • ufw などのポートが閉じていないかどうかを確認する
  • /etc/exports の記述が間違っていないかどうか確認する
  • 共有するフォルダのパーミッションと所有権を確認する
  • mount の ip指定などを確認する
  • showmount -e 192.168.100.240 で nfs が見れるか確認する

さいごに

かんたんでしたね

6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?