今回は、CentOS8でnfsをインストールし、NFSサーバを構築します。

★NFSサーバ・・・共有ディレクトリを提供するサーバ。

手順

1.VirtualBoxでCentOS8の3号機を作成
2.CentOS8 3号機にログイン
3.nfs(サーバ)のインストール
4.セキュリティ設定
5.CentOS8 1号機にログイン
6.nfs(クライアント)のインストール
7.動作確認

1.VirtualBoxでCentOS8の3号機を作成

以下のページを参考に、3号機を作成します。
https://daredemose.com/linux/centos8_clone/

・名前:CE08PRD103
・IPアドレス:192.168.0.103

2.CentOS8 3号機にログイン

TeraTermで、CentOS8 3号機にrootでログインします。

3.nfs(サーバ)のインストール

# nfsのインストール
[root@CE08PRD101 ~]# dnf -y install nfs-utils
(省略)
インストール済み:
gssproxy-0.8.0-15.el8.x86_64
keyutils-1.5.10-6.el8.x86_64
libevent-2.1.8-5.el8.x86_64
libverto-libevent-0.3.0-5.el8.x86_64
nfs-utils-1:2.3.3-31.el8.x86_64
quota-1:4.04-10.el8.x86_64
quota-nls-1:4.04-10.el8.noarch
rpcbind-1.2.5-7.el8.x86_64
完了しました!

# 共有するディレクトリを作成
[root@CE08PRD101 ~]# mkdir /public

# 作成したディレクトリの確認
[root@CE08PRD103 ~]# ls -ld /public
drwxr-xr-x. 2 root root 6 9月 8 14:44 /public

# 設定ファイル編集
# 設定ファイルのバックアップ

[root@CE08PRD103 ~]# cp -p /etc/exports /etc/exports_20200908

# バックアップの確認
[root@CE08PRD103 ~]# ls -l /etc/exports*
-rw-r--r--. 1 root root 0 9月 10 2018 /etc/exports
-rw-r--r--. 1 root root 0 9月 10 2018 /etc/exports_20200908

# 設定ファイルの編集
[root@CE08PRD103 ~]# vi /etc/exports
/public 192.168.0.0/255.255.255.0(rw,async,no_root_squash)

# 編集後差分比較
[root@CE08PRD103 ~]# diff /etc/exports_20200908 /etc/exports
0a1
> /public 192.168.0.0/255.255.255.0(rw,async,no_root_squash)

# サービス起動
[root@CE08PRD101 ~]# systemctl enable --now nfs-server

# サービス起動確認
# Active:がactiveになっていることを確認する。
[root@CE08PRD101 ~]# systemctl status nfs-server
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Drop-In: /run/systemd/generator/nfs-server.service.d
mqorder-with-mounts.conf
Active: active (exited) since Tue 2020-09-08 14:57:23 JST; 29s ago
(省略)

4.セキュリティ設定

# ファイヤーウォールの許可設定
[root@CE08PRD101 ~]# firewall-cmd --add-service=nfs --permanent
success
[root@CE08PRD101 ~]# firewall-cmd --reload
success

# ファイヤーウォールの許可設定確認
# nfsが追加されたことを確認する。
[root@CE08PRD101 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: enp0s3
sources:
services: cockpit dhcpv6-client ftp http nfs ntp ssh
(省略)

5.CentOS8 1号機にログイン

TeraTermで、CentOS8 1号機にrootでログインします。

6.nfs(クライアント)のインストール

# nfsのインストール
[root@CE08PRD101 ~]# dnf -y install nfs-utils
(省略)
インストール済み:
gssproxy-0.8.0-15.el8.x86_64
keyutils-1.5.10-6.el8.x86_64
libevent-2.1.8-5.el8.x86_64
libverto-libevent-0.3.0-5.el8.x86_64
nfs-utils-1:2.3.3-31.el8.x86_64
quota-1:4.04-10.el8.x86_64
quota-nls-1:4.04-10.el8.noarch
rpcbind-1.2.5-7.el8.x86_64
完了しました!

# 共有ディレクトリをマウント
[root@node01 ~]# mount -t nfs 192.168.0.103:/public /mnt

# マウントの確認
[root@node01 ~]# df -hT
ファイルシス タイプ サイズ 使用 残り 使用% マウント位置
(省略)
192.168.0.103:/public nfs4 47G 1.8G 46G 4% /mnt

# 自動マウントの設定
[root@CE08PRD101 ~]# vi /etc/fstab
192.168.0.103:/public /mnt nfs defaults,nofail 0 0

7.動作確認

動作確認として、1号機で/public配下に、テストファイルを保存し、3号機でそのテストファイルが開けるか確認します。

以下、1号機で実施

# テストファイル作成
[root@CE08PRD101 ~]# cd /mnt ;pwd
/mnt

# テストファイル作成
[root@CE08PRD101 ~]# touch test.txt

# テストファイル確認
[root@CE08PRD101 ~]# ls -l test.txt
合計 0
-rw-r--r--. 1 root root 0 9月 8 16:50 test.txt

以下、3号機で実施

# テストファイル作成
# 1号機で作成したテストファイルが表示されればOK.
[root@CE08PRD103 ~]#ls -l /public/test.txt
-rw-r--r--. 1 root root 0 9月 8 16:50 /public/test.txt

i以上