NFS 服务器
部署服务
这些命令是在 NFS 共享的基础上,解决客户端权限问题、配置开机自动挂载,并验证整个流程的稳定性,就像 “给共享文件夹设置权限、让电脑开机自动加载共享盘,最后确认文件能正常访问”
###server端,搭建 NFS 共享服务器,让服务器具备 “共享文件” 的功能[root@server ~ 09:56:21]# yum install -y nfs-utils###client端,准备网页服务器并访问 NFS 共享,客户端装 NFS 工具,否则无法识别服务器的共享[root@client ~ 10:10:36]# yum install -y nfs-utils-作用:客户端安装 NFS 工具(虽然是客户端,但需要 NFS 协议相关工具才能挂载服务器的共享目录)#安装 Apache 网页服务器[root@client ~ 10:10:36]# yum install -y httpd#查看apache用户的 ID 信息[root@client ~ 10:13:45]# id apacheuid=48(apache) gid=48(apache) 组=48(apache)-实际场景:Apache 服务默认用apache用户运行,后续挂载的 NFS 共享目录里的网页文件,需要让apache用户有权限访问(否则网页会打不开)。这里先确认apache的 ID,方便后续在 Server 端设置权限匹配。###server端,配置 NFS 共享目录及权限[root@server ~ 10:10:46]# useradd -u 1000 lyk[root@server ~ 10:14:45]# echo 123 | passwd --stdin lyk[root@server ~ 10:15:28]# mkdir -p /shares/nfs[root@server ~ 10:21:49]# chown lyk /shares/nfs#改成给uid=48的用户(对应客户端apache用户的 ID)[root@server ~ 10:21:54]# chown 48:48 /shares/nfs-实际场景:关键操作!因为客户端的 Apache 服务用apache用户(ID=48)运行,而 NFS 共享默认会检查 “客户端访问用户的 ID” 是否和服务器端目录的权限匹配。这里把服务器共享目录的所有者设为 48,客户端apache用户(ID=48)访问时就有权限了,避免出现 “权限不足” 的错误。#查看共享文件夹的权限[root@server ~ 10:22:17]# ll -d /shares/nfsdrwxr-xr-x 2 48 48 6 8月 7 10:15 /shares/nfs###client端,查看服务器共享了哪些文件夹(输出/shares/nfs 10.1.8.0/24),确认服务器的共享路径[root@client ~ 10:13:50]# showmount -e serverExport list for server:/shares/nfs 10.1.8.0/24###server端[root@server ~ 10:23:01]# vim /etc/exports#服务器配置共享规则(相当于设置 “谁能访问共享文件夹”)[root@server ~ 10:25:27]# cat /etc/exports/shares/nfs 10.1.8.0/24(rw) *(ro)-设置共享规则:10.1.8.0/24网段的电脑可以 “读写”(rw),其他电脑只能 “只读”(ro)[root@server ~ 10:25:34]# systemctl enable nfs-server.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.#确保防火墙不阻止 NFS 共享[root@server ~ 10:26:13]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:firewalld(1)#简化规则,只允许10.1.8.0/24网段读写,去掉其他电脑的访问权限(更安全)[root@server ~ 10:26:29]# vim /etc/exports[root@server ~ 10:26:53]# cat /etc/exports/shares/nfs 10.1.8.0/24(rw)[root@server ~ 10:26:56]# systemctl restart nfs-server[root@server ~ 10:27:12]# echo Welcome to lyk website. > /shares/nfs/index.html###client端,客户端挂载共享并启动网站[root@client ~ 10:28:15]# mount server:/shares/nfs /var/www/html[root@client ~ 10:28:54]# ls /var/www/html/index.html[root@client ~ 10:29:13]# systemctl enable httpd --now#打开浏览器输入10.1.8.11显示Welcome to lyk website.
[root@client ~ 10:29:22]# cd /var/www/html/[root@client html 10:48:54]# lsindex.html#客户端root用户的 ID 是0,和服务器共享文件夹的所有者 ID(48)不匹配,所以没权限写(就像 “老板想改文件,但共享文件夹只允许管理员改”)[root@client html 10:48:55]# echo hhhh > web1.html-bash: web1.html: 权限不够#切换到apache用户(ID=48),成功创建web1.html(因为 ID 匹配服务器权限)[root@client html 10:49:20]# su -l -s /bin/bash apache-bash-4.2$ echo aaaa > /var/www/html/web1.html-bash-4.2$ 登出#确认apache用户的 ID 是48,且默认不能直接登录(/sbin/nologin),增强安全性(相当于 “确认管理员账号的信息正确”)[root@client html 10:51:14]# grep apache /etc/passwdapache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin#server:/shares/nfs挂载到了/var/www/html[root@client html 10:53:39]# df文件系统 1K-块 已用 可用 已用% 挂载点devtmpfs 2001016 0 2001016 0% /devtmpfs 2013056 0 2013056 0% /dev/shmtmpfs 2013056 11808 2001248 1% /runtmpfs 2013056 0 2013056 0% /sys/fs/cgroup/dev/mapper/centos-root 52403200 2218340 50184860 5% //dev/sda1 1038336 142216 896120 14% /boottmpfs 402612 0 402612 0% /run/user/0server:/shares/nfs 52403200 2220544 50182656 5% /var/www/html#设置开机自动挂载,重启后不用手动连接共享文件夹[root@client html 10:53:52]# vim /etc/fstabserver:/shares/nfs /var/www/html nfs defaults 0 0#退出当前目录才能取消挂载[root@client html 10:54:59]# umount /var/www/html umount.nfs4: /var/www/html: device is busy[root@client html 10:55:21]# cd#测试自动挂载配置是否正确,重启后验证共享是否自动连接[root@client ~ 10:55:24]# umount /var/www/html [root@client ~ 10:55:26]# mount /var/www/html[root@client ~ 10:55:53]# reboot#reboot后再次连接#浏览器网站输入10.1.8.11/web1.html显示aaaa