NFS服务搭建和配置
服务端
[root@hanlin ~]# yum -y install nfs-utils rpcbind
[root@hanlin ~]# vim /etc/exports
/home/nfstestdir
192.168.0.102/32(rw,sync,all_squash,anonuid=1000,anongid=1000) (可以分享数据的主机和相对应的权限,anonuid=1000是指创建的文件的所有者为机器上uid=1000的账号,gid同理)
rw 读写
ro 只读
sync 同步模式,内存数据实时写入磁盘
async 非同步模式
no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid
[root@hanlin ~]# mkdir /home/nfstestdir
[root@hanlin ~]# chmod 777 !$
chmod 777 /home/nfstestdir
[root@hanlin ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1203/nginx: master
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1685/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1173/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1174/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1564/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1203/nginx: master
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1173/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1174/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 1564/master
tcp6 0 0 :::3306 :::* LISTEN 20467/mysqld
[root@hanlin ~]# systemctl start nfs
[root@hanlin ~]# systemctl start rpcbind
[root@hanlin ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@hanlin ~]# systemctl enable rpcbind
Created symlink from /etc/systemd/system/multi-user.target.wants/rpcbind.service to /usr/lib/systemd/system/rpcbind.service.
[root@HANLIN mnt]# touch 111
[root@HANLIN mnt]# touch 12,txt
[root@HANLIN mnt]# ll
总用量 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 12月 6 21:58 111
-rw-r--r--. 1 nfsnobody nfsnobody 0 12月 6 22:00 12,txt
-rw-r--r--. 1 root root 0 12月 6 22:01 xxxxxx
客户端
[root@HANLIN ~]# yum install -y epel-release
[root@HANLIN ~]# yum install -y nfs-utils
[root@HANLIN ~]# showmount -e 192.168.0.12 (出现这个挂在测试提醒之后就可以挂载了)
Export list for 192.168.0.12:
192.168.0.102/32(rw,sync,all_squash,anonuid=1000,anongid=1000) *
/home/nfstestdir
[root@HANLIN ~]# mount -t nfs 192.168.0.12:/home/nfstestdir /mnt (挂载,指定挂载格式nfs)
[root@HANLIN ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 10G 7.0G 3.1G 70% /
devtmpfs 978M 0 978M 0% /dev
tmpfs 993M 0 993M 0% /dev/shm
tmpfs 993M 9.0M 984M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
192.168.0.12:/home/nfstestdir 10G 8.0G 2.1G 80% /mnt
/dev/sda2 2.0G 33M 2.0G 2% /swap
/dev/sda1 197M 136M 61M 70% /boot
tmpfs 199M 12K 199M 1% /run/user/42
tmpfs 199M 0 199M 0% /run/user/0
错误
[root@HANLIN mnt]# !touch
touch 1,jpg
touch: 无法创建"1,jpg": 只读文件系统 (服务器端exports里面改成如下字样)
/home/nfstestdir *(rw,sync)
192.168.0.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
卸载遇忙碌
umount.nfs4: /mnt: device is busy
umount.nfs4: /mnt: device is busy
[root@HANLIN mnt]# fuser -m -v /mnt
用户 进程号 权限 命令
/mnt: root kernel mount /mnt
root 1925 ..c.. bash
[root@HANLIN mnt]# kill -9 1925
Connection closing...Socket close.
[root@HANLIN ~]# umount /mnt
或者umount -l /mnt (-l lazy)
[root@HANLIN mnt]# touch 111
[root@HANLIN mnt]# touch 12,txt
[root@HANLIN mnt]# ll
总用量 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 12月 6 21:58 111
-rw-r--r--. 1 nfsnobody nfsnobody 0 12月 6 22:00 12,txt
-rw-r--r--. 1 root root 0 12月 6 22:01 xxxxxx