OS : CentOS 8
NFS는 네트워크로 연결 된 컴퓨터 간 디렉토리를 공유하기 위한 서버이다. 네트워크를 통해 마운트된 디렉토리를 공유하는 것인데, 암호화된 상태에서 데이터 통신을 제공하는 것이 아니기 때문에
기본적으로 권장되지는 않는다.
윈도우는 네트워크로 연결 된 컴퓨터 끼리 폴더(디렉토리)를 공유할 수 있지만, 리눅스는 대부분 명령으로 작업을 해결해야 하기 때문에 윈도우 보다는 좀 더 복잡한 과정을 거쳐야 한다.
대략 구축 과정은 다음과 같다:
- NFS 서버/클라이언트 OS 각각에 nfs-utils 패키지가 설치되었는지 확인한다
- 그리고 /etc/exports에 공유할 디렉터리와 접근을 허가할 컴퓨터 및 접근 권한을 지정한다
- 서비스를 실행한다
- 클라이언트에 showmount 명령을 실행해 서버에 공유된 디렉터리가 있는지 확인한다
- 클라이언트에서 mount 명령으로 서버에 공유된 디렉터리를 마운트한다.
클라이언트의 ~/myShare 이름의 디렉터리에 접근하면 자동으로 NFS 서버의 /share 디렉터리에 접근하는 효과를 내는 것이다.

서버(NFS)
공유 디렉토리 : /share
Client
마운트 디렉토리 : ~/myShare
서버
nfs-utils 패키지가 설치되었는지 확인하자.
[root@localhost ~]# rpm -qa nfs-utils
nfs-utils-2.3.3-14.el8.x86_64
vi 나 gedit으로 /etc/exports 파일을 열고 다음 내용을 넣어서 공유할 디렉토리를 추가한 후 저장하자.
/share 서버에 접근할 컴퓨터 IP주소(rw,sync)
/share 192.168.111.*(rw, sync)
/share 디렉토리 생성, chmod 명령으로 /share 디렉터리의 접근 권한은 707로 한다. 적당한 파일을 /share 디렉토리에 미리 복사하자.
[root@localhost ~]# mkdir /share
[root@localhost ~]# chmod 707 /share
[root@localhost ~]# cp /boot/vm* /share/
[root@localhost ~]# ls /share/
vmlinuz-0-rescue-5866bf09495b4feb9c141b603ae87873 vmlinuz-4.18.0-80.el8.x86_64
[root@localhost ~]#
nfs-server 서비스를 시작 및 상시 가동한다.
[root@localhost ~]# systemctl restart nfs-server.service
[root@localhost ~]# systemctl enable nfs-server.service
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
/share 디렉토리를 클라이언트가 마운트 가능한 상태로 만든다.
[root@localhost ~]# exportfs -v
/share 192.168.111.*(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
방화벽을 잠시 닫아둔다
systemctl stop firewalld
클라이언트
nfs-utils 패키지를 설치한다.
rpm -qa nfs-utils
서버에 공유된 디렉터리를 확인한다.
[root@localhost ~]# showmount -e 192.168.111.100
Export list for 192.168.111.100:
/share 192.168.111.*
myShare 디렉토리를 만들고 서버의 /share 디렉터리에 마운트할 디랙토리 /home/centos/myShare를 만들고 마운트한다.
그리고 서버에 공유된 디렉토리를 확인한다.
[centos@localhost ~]$ mkdir myShare
[centos@localhost ~]$ su -c 'mount -t nfs 192.168.111.100:/share myShare'
암호:
[centos@localhost ~]$ ls -l myShare/
합계 15384
-rwxr-xr-x. 1 root root 7872760 3월 22 15:48 vmlinuz-0-rescue-5866bf09495b4feb9c141b603ae87873
-rwxr-xr-x. 1 root root 7872760 3월 22 15:48 vmlinuz-4.18.0-80.el8.x86_64
클라이언트에서 마운트한 디렉토리에 적당한 파일을 복사한다.
[centos@localhost ~]$ cp /boot/vmlinuz-4* ~/myShare/testfile
서버에서 /share 디렉토리를 확인해보면 testfile을 비롯해 복사한 파일이 똑같이 생긴 것을 확인할 수 있다.[클라이언트의 /home/myShare/ 디렉토리가 서버의 /share 디렉토리에 마운트 되어 파일이 공유되는 것이다.
[root@localhost ~]# ls /share/
testfile vmlinuz-4.18.0-80.el8.x86_64
vmlinuz-0-rescue-5866bf09495b4feb9c141b603ae87873
su 명령으로 root사용자로 접속한다.
vi나 gedit으로 /etc/fstab 파일을 열고 맨 아래 줄에 다음 내용을 추가한다.
vi /etc/fstab
192.168.111.100:/share /home/centos/myShare nfs defaults 0 0
클라이언트를 재부팅 후 자동 마운트 되었는지 확인하고 읽기, 쓰기 작업이 되는지 확인하자.
클라이언트에서 한 작업이 서버에서도 마운트 되어서 적용되는지 확인하는 것이다.
클라이언트
[centos@localhost ~]$ ls myShare/
newfile vmlinuz-0-rescue-5866bf09495b4feb9c141b603ae87873
testfile vmlinuz-4.18.0-80.el8.x86_64
[centos@localhost ~]$ touch myShare/newfile
[centos@localhost ~]$ ls myShare/
newfile vmlinuz-0-rescue-5866bf09495b4feb9c141b603ae87873
testfile vmlinuz-4.18.0-80.el8.x86_64
서버
[root@localhost ~]# ls /share/
newfile vmlinuz-0-rescue-5866bf09495b4feb9c141b603ae87873
testfile vmlinuz-4.18.0-80.el8.x86_64
Reference