UbuntuにNFSを設定してみる

UbuntuNFSを設定してみようかなと。

参考にしたサイトはこちら。

まずはサーバー側の設定から。 nfs-kernel-server パッケージが必要みたいなのでインストールします。

bookstore@bookstoreUbuntu:~$ dpkg -l | grep nfs-kernel-server
bookstore@bookstoreUbuntu:~$ apt search nfs-kernel-server
ソート中... 完了
全文検索... 完了
nfs-kernel-server/focal-updates,focal-security 1:1.3.4-2.5ubuntu3.3 amd64
  NFS カーネルサーバ用サポート
bookstore@bookstoreUbuntu:~$ sudo apt install nfs-kernel-server

NFSに割り当てるディレクトリを作成します。ディレクトリのパーミッションは777にします。

bookstore@bookstoreUbuntu:~$ mkdir -p /export/share
bookstore@bookstoreUbuntu:~$ chmod 777 /export/share
bookstore@bookstoreUbuntu:~$ ls -l /export
total 4
drwxrwxrwx 2 bookstore bookstore 4096  9月 23 11:32 share

/etc/exports ファイルに下記のエントリを追加します。

bookstore@bookstoreUbuntu:~$ cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
#       to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)
#
/export            192.168.50.0/24(rw,fsid=0,insecure,no_subtree_check,async)
/export/share      192.168.50.0/24(rw,nohide,insecure,no_subtree_check,async)

サービスを再起動します。

bookstore@bookstoreUbuntu:~$ systemctl restart nfs-kernel-server
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'nfs-server.service'.
Authenticating as: bookstore,,, (bookstore)
Password:
==== AUTHENTICATION COMPLETE ===
bookstore@bookstoreUbuntu:~$ systemctl status nfs-kernel-server
● nfs-server.service - NFS server and services
     Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
    Drop-In: /run/systemd/generator/nfs-server.service.d
             └─order-with-mounts.conf
     Active: active (exited) since Wed 2020-09-23 11:37:42 JST; 17s ago
    Process: 2176 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
    Process: 2177 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
   Main PID: 2177 (code=exited, status=0/SUCCESS)

 9月 23 11:37:41 bookstoreUbuntu systemd[1]: Starting NFS server and services...
 9月 23 11:37:42 bookstoreUbuntu systemd[1]: Finished NFS server and services.

次はクライアントを設定していきます。クライアントはMacです。

 bash-3.2$ sw_vers -productVersion
 10.15.6

nfs ディレクトリをNFSサーバーの /export/share ディレクトリにマウントします。このマウントはNFSサーバーのファイアーウォールが機能していたら接続が拒否されました。必要なポートが空いていないみたいなのでそれは後で調べるとして一旦ファイアーウォールはストップさせた状態でやってます。

bash-3.2$ sudo mount_nfs 192.168.50.35:/export/share ~/nfs

これでNFSの設定ができたのでファイルを共有してみます。クライアント側で一時ファイルを作成。

bash-3.2$ touch ~/nfs/hello-from-mac

NFSサーバーでファイルを確認。うまくいってますね。

bookstore@bookstoreUbuntu:~$ ls /share/
hello-from-mac