安装 nfs 服务器

1
[root@debian /root]#  apt-get install nfs-kernel-server

修改服务端配置文件

如果只使用客户端功能,可忽略此步操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@debian /root]#  nano /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)
#
# 增加以下代码设置共享文件夹

/home/username/mynfs 192.168.8.8(rw,sync,no_root_squash)

/home/username/mynfs 是我主机上的文件夹,是 nfs 服务器的根目录;

192.168.8.8 是我客户端的 ip;
rw,读写权限,sync,数据同步写入内存和硬盘,no_root_squash,NFS 服务器共享目录用户的属性,如果用户是 root,那么对于这个共享目录来说就具有 root 的权限。

检查配置是否有错

1
[root@debian /root]#  source /etc/exports

无错误提示,配置文件正确。

启动服务

1
2
3
[root@debian /root]#  /etc/init.d/nfs-kernel-server restart

[ ok ] Restarting nfs-kernel-server (via systemctl): nfs-kernel-server.service.

客户端挂载

1
[root@debian /root]#  mount -n -o nolock 192.168.8.8:/home/username/mynfs /home/username/nfs/

192.168.8.8:/home/username/mynfs挂载到/home/username/nfs/

检查是否挂载上了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@debian /root]#  df -h

文件系统 容量 已用 可用 已用% 挂载点
udev 2.0G 0 2.0G 0% /dev
tmpfs 395M 6.0M 389M 2% /run

/dev/mapper/debian--vg-root 35G 5.3G 28G 16% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup

/dev/sda1 236M 58M 166M 26% /boot

192.168.8.8:/home/username/mynf 2.7T 390G 2.3T 15% /home/username/nfs/
tmpfs 395M 28K 395M 1% /run/user/116
tmpfs 395M 0 395M 0% /run/user/1000

测试是否可以共享文件

客户端写:

1
2
3
4
[root@debian /home/username/nfs]#  touch testfile

[root@debian /home/username/nfs]# ls
aaa.txt ddd.txt testfile test.txt

服务器端查看:

1
2
[root@debian /home/username/nfs]#  ls
aaa.txt ddd.txt testfile test.txt

服务器端写:

1
2
3
4
[root@debian /home/username/nfs]#  touch debian

[root@debian /home/username/nfs]# ls
aaa.txt ddd.txt debian testfile test.txt

客户端查看:

1
2
[root@debian /home/username/nfs]#  ls
aaa.txt ddd.txt debian testfile test.txt