NFS搭建與配置


 NFS應用場景是:A,B,C三台機器上需要保證被訪問到的文件是一樣的,A共享數據出來,B和C分別去掛載A共享的數據目錄,從而B和C訪問到的數據和A上的一致性

172.131.1.135  服務器端

172.131.1.137  客戶端端

1.服務器安裝

[root@localhost~]# yum install -y nfs-utils rpcbind

查看端口:

root@localhost]# netstat -lntp

[root@localhost ~]# 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:20048 0.0.0.0:* LISTEN 1783/rpc.mountd
tcp 0 0 0.0.0.0:34099 0.0.0.0:* LISTEN -
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2263/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1344/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 841/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2041/master
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:40037 0.0.0.0:* LISTEN 1216/rpc.statd
tcp6 0 0 :::60943 :::* LISTEN 1216/rpc.statd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::20048 :::* LISTEN 1783/rpc.mountd
tcp6 0 0 :::22 :::* LISTEN 1344/sshd
tcp6 0 0 ::1:631 :::* LISTEN 841/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 2041/master
tcp6 0 0 :::40826 :::* LISTEN -
tcp6 0 0 :::2049 :::* LISTEN -

啟動nfs服務

[root@localhost ]# systemctl start nfs

root@localhost ~]# systemctl status nfs

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
└─order-with-mounts.conf
Active: active (exited) since 四 2017-11-16 15:22:31 CST; 18min ago

[root@localhost ~]# ps -ef|grep nfs
root 1874 2 0 15:22 ? 00:00:00 [nfsd4_callbacks]
root 1890 2 0 15:22 ? 00:00:00 [nfsd]
root 1891 2 0 15:22 ? 00:00:00 [nfsd]
root 1892 2 0 15:22 ? 00:00:00 [nfsd]
root 1893 2 0 15:22 ? 00:00:00 [nfsd]
root 1894 2 0 15:22 ? 00:00:00 [nfsd]
root 1895 2 0 15:22 ? 00:00:00 [nfsd]
root 1896 2 0 15:22 ? 00:00:00 [nfsd]
root 1897 2 0 15:22 ? 00:00:00 [nfsd]

 [root@iZbp1a7wyzv7b3ujsfphj2Z nfsdir]# netstat -lntp

[root@localhost ~]# 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:20048 0.0.0.0:* LISTEN 1783/rpc.mountd
tcp 0 0 0.0.0.0:34099 0.0.0.0:* LISTEN -
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2263/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1344/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 841/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2041/master
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:40037 0.0.0.0:* LISTEN 1216/rpc.statd
tcp6 0 0 :::60943 :::* LISTEN 1216/rpc.statd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::20048 :::* LISTEN 1783/rpc.mountd
tcp6 0 0 :::22 :::* LISTEN 1344/sshd
tcp6 0 0 ::1:631 :::* LISTEN 841/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 2041/master
tcp6 0 0 :::40826 :::* LISTEN -
tcp6 0 0 :::2049 :::* LISTEN -

 rpc的服務也跟着帶起來:

[root@localhost ~]# ps -aux|grep rpc
root 397 0.0 0.0 0 0 ? S< 15:22 0:00 [rpciod]
root 540 0.0 0.0 17148 392 ? Ss 15:22 0:00 /usr/sbin/rpc.idmapd
rpcuser 1216 0.0 0.0 44448 1824 ? Ss 15:22 0:00 /usr/sbin/rpc.statd
rpc 1695 0.0 0.0 64948 1436 ? Ss 15:22 0:00 /sbin/rpcbind -w
root 1783 0.0 0.1 42992 2124 ? Ss 15:22 0:00 /usr/sbin/rpc.mountd
root 4011 0.0 0.0 112668 972 pts/0 R+ 15:42 0:00 grep --color=auto rpc

開機自啟動:

[root@localhost nfsdir]# systemctl enable rpcbind
[root@localhost  nfsdir]# 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@localhost nfsdir]# chmod 777 /nfs/nfsdir

最關鍵的就是修改nfs的配置文件:

[root@localhost ~]# cat /etc/exports
/nfs/nfsdir 172.31.1.0/24(rw,sync,no_root_squash)

相關參數:

rw 讀寫
ro 只讀
sync 同步模式,內存數據實時寫入磁盤  速度很快但是會降低磁盤效率
async 非同步模式 能保證磁盤效率  但是如果服務器斷電,會造成數據丟失
no_root_squash 客戶端掛載NFS共享目錄后,root用戶不受約束,權限很大
root_squash 與上面選項相對,客戶端上的root用戶收到約束,被限定成某個普通用戶
all_squash 客戶端上所有用戶在使用NFS共享目錄時都被限定為一個普通用戶
anonuid/anongid 和上面幾個選項搭配使用,定義被限定用戶的uid和gid

 

2.NFS服務器端的配置:

[root@localhost~]# yum install nfs-utils
[root@localhost~]# systemctl start rpcbind
[root@localhost~]# systemctl status rpcbind
rpcbind.service - RPC bind service
Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
Active: active (running) since 四 2017-11-16 13:24:06 CST; 5s ago

[root@localhost python_practice]# showmount -e 172.31.1.135

clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
這是因為開啟了防火牆
[root@localhost python_practice]# systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2017-09-26 21:42:06 CST; 1 months 20 days ago
root@localhost python_practice]# systemctl stop firewalld
需要關閉兩邊的防火牆和getenforce
[root@localhost python_practice]# getenforce
Enforcing
[root@localhost python_practice]# setenforce 0
[root@localhost python_practice]# getenforce
Permissive

[root@localhost python_practice]# showmount -e 172.131.1.135
clnt_create: RPC: Port mapper failure - Timed out
原因是2台機器都叫默認名localhost.localhost
修改2台服務器的名:
[root@localhost ~]# hostnamectl set-hostname "centos7-137"
另外一台是類似的命令

再進行查看:

oot@localhost nfsdir]# showmount -e 172.31.1.135
Export list for 172.31.1.135:
/nfs/nfsdir 172.31.1.0/24

最關鍵的是這一步:
[root@localhost nfsdir]# mount -t nfs 172.31.1.135:/nfs/nfsdir /pubdir/nfsdir

其中/nfs/nfsdir是135准備共享的目錄,/pubdir/nfsdir是137上對應查看的目錄
[root@localhost nfsdir]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/sda3 38G 5.9G 32G 16% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 9.0M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 197M 156M 41M 80% /boot
tmpfs 183M 24K 183M 1% /run/user/0
/dev/sr0 4.1G 4.1G 0 100% /run/media/root/CentOS 7 x86_64
172.31.1.135:/nfs/nfsdir 38G 13G 25G 35% /pubdir/nfsdir

測試:

在135上新建一個文件:

[root@localhost nfsdir]# touch kingleoric.txt
[root@localhost nfsdir]# pwd
/nfs/nfsdir

在137上查看該文件已生成:

[root@localhost nfsdir]# cd /pubdir/nfsdir
[root@localhost nfsdir]# ls
kingleoric.txt

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM