【NFS】Linux安裝NFS
阿新 • • 發佈:2022-03-06
Linux上NFS 服務端 客戶端的安裝及配置
1. 系統環境
$ cat /proc/version
Linux version 3.10.0-327.el7.x86_64 ([email protected]) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Thu Oct 29 17:29:29 EDT 2015
2. NFS服務端搭建
2.1 安裝並檢查NFS和RPC服務
# 安裝 $ yum install nfs-utils rpcbind -y # 檢查 $ rpm -qa nfs-utils rpcbind rpcbind-0.2.0-32.el7.x86_64 nfs-utils-1.3.0-0.21.el7.x86_64
2.2 啟動RPC服務和NFS服務 (必須先啟動RPC服務,然後再啟動NFS服務)
$ systemctl start rpcbind $ systemctl start nfs $ ps -ef|grep rpc rpc 960 1 0 2020 ? 00:01:15 /sbin/rpcbind -w rpcuser 11716 1 0 13:57 ? 00:00:00 /usr/sbin/rpc.statd --no-notify root 11724 2 0 13:57 ? 00:00:00 [rpciod] root 11728 1 0 13:57 ? 00:00:00 /usr/sbin/rpc.idmapd root 11743 1 0 13:57 ? 00:00:00 /usr/sbin/rpc.mountd root 12483 20145 0 13:57 pts/1 00:00:00 grep --color=auto rpc $ ps -ef|grep nfs root 11755 2 0 13:57 ? 00:00:00 [nfsd4] root 11756 2 0 13:57 ? 00:00:00 [nfsd4_callbacks] root 11760 2 0 13:57 ? 00:00:00 [nfsd] root 11761 2 0 13:57 ? 00:00:00 [nfsd] root 11762 2 0 13:57 ? 00:00:00 [nfsd] root 11763 2 0 13:57 ? 00:00:00 [nfsd] root 11764 2 0 13:57 ? 00:00:00 [nfsd] root 11765 2 0 13:57 ? 00:00:00 [nfsd] root 11766 2 0 13:57 ? 00:00:00 [nfsd] root 11767 2 0 13:57 ? 00:00:00 [nfsd] root 12644 20145 0 13:57 pts/1 00:00:00 grep --color=auto nfs $ lsof -i:111 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 960 rpc 6u IPv4 90650864 0t0 UDP *:sunrpc rpcbind 960 rpc 8u IPv4 90650866 0t0 TCP *:sunrpc (LISTEN) rpcbind 960 rpc 9u IPv6 90650867 0t0 UDP *:sunrpc rpcbind 960 rpc 11u IPv6 90650869 0t0 TCP *:sunrpc (LISTEN) $ rpcinfo -p localhost program vers proto port service 100000 4 tcp 111 portmapper 100000 3 tcp 111 portmapper 100000 2 tcp 111 portmapper 100000 4 udp 111 portmapper 100000 3 udp 111 portmapper 100000 2 udp 111 portmapper 100024 1 udp 51177 status 100024 1 tcp 55076 status 100005 1 udp 20048 mountd 100005 1 tcp 20048 mountd 100005 2 udp 20048 mountd 100005 2 tcp 20048 mountd 100005 3 udp 20048 mountd 100005 3 tcp 20048 mountd 100003 3 tcp 2049 nfs 100003 4 tcp 2049 nfs 100227 3 tcp 2049 nfs_acl 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100227 3 udp 2049 nfs_acl 100021 1 udp 60484 nlockmgr 100021 3 udp 60484 nlockmgr 100021 4 udp 60484 nlockmgr 100021 1 tcp 38648 nlockmgr 100021 3 tcp 38648 nlockmgr 100021 4 tcp 38648 nlockmgr
2.3 設定開機自啟動並檢查
$ systemctl enable rpcbind $ systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. $ systemctl list-unit-files --type=service|grep "enabled"|egrep "rpcbind|nfs" nfs-server.service enabled nfs.service enabled
2.4 建立共享目錄
$ mkdir /data/nfs-server-images
$ chown -R nfsnobody.nfsnobody /data/nfs-server-images
$ ls -ld /data/nfs-server-images
drwxr-x--- 2 nfsnobody nfsnobody 4096 1月 6 14:18 /data/nfs-server-images
2.5 設定允許接入的網址和目錄
$ cat>>/etc/exports<<EOF
#NFS server share directories
/data/nfs-server-images 10.1.236.0/24(ro,sync,no_root_squash)
EOF
$ cat /etc/exports
#NFS server share directories
/data/nfs-server-images 10.1.236.0/24(ro,sync,no_root_squash)
2.6 平滑重啟NFS服務並檢查服務
$ systemctl reload nfs
$ cat /var/lib/nfs/etab
/data/nfs-server-images 10.1.236.0/24(ro,sync,wdelay,hide,nocrossmnt,secure,no_root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,ro,secure,root_squash,no_all_squash)
2.7 本地檢視掛載目錄
$ showmount -e 10.1.236.58
Export list for 10.1.236.58:
/data/nfs-server-images 10.1.236.0/24
3. NFS客戶端搭建
3.1 安裝NFS和RPC服務並檢查
$ yum install nfs-utils rpcbind -y
$ rpm -qa nfs-utils rpcbind
nfs-utils-1.3.0-0.21.el7.x86_64
rpcbind-0.2.0-32.el7.x86_64
3.2 啟動RPC服務(不需要啟動NFS服務,原因是不安裝NFS服務的話沒有showmount這個命令)
$ systemctl start rpcbind
$ ps -ef|grep rpc
rpc 9119 1 0 14:38 ? 00:00:00 /sbin/rpcbind -w
root 9201 6816 0 14:39 pts/0 00:00:00 grep --color=auto rpc
[root@ocdp-42-59 data]# lsof -i:111
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 9119 rpc 6u IPv4 2910094566 0t0 UDP *:sunrpc
rpcbind 9119 rpc 8u IPv4 2910094568 0t0 TCP *:sunrpc (LISTEN)
rpcbind 9119 rpc 9u IPv6 2910094569 0t0 UDP *:sunrpc
rpcbind 9119 rpc 11u IPv6 2910094571 0t0 TCP *:sunrpc (LISTEN)
3.3 設定開機自啟動並檢查
$ systemctl enable rpcbind
$ systemctl list-unit-files --type=service|grep "enabled"|grep "rpcbind"
3.4 檢查服務端的NFS掛載目錄是否OK
$ showmount -e 10.1.236.58
Export list for 10.1.236.58:
/data/nfs-server-images 10.1.236.0/24
3.5 掛載設定開機自動掛載
$ mkdir /data/nfs-client-images
$ mount -t nfs 10.1.236.58:/data/nfs-server-images /data/nfs-client-images
$ echo "mount -t nfs 10.1.236.58:/data/nfs-server-images /data/nfs-client-images">>/etc/rc.local
#提示:也可以放入/etc/fstab,如果要放入/etc/fstab,需啟用netfs服務。這是因為fstab會優先於網路被Linux系統載入。網路沒啟動時執行fstab會導致連不上NFS伺服器端,無法實現開機自動掛載。
3.6 檢視是否掛載成功
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-lv_root 36G 25G 12G 69% /
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 16G 1.6G 15G 10% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda1 509M 110M 399M 22% /boot
tmpfs 3.2G 0 3.2G 0% /run/user/995
tmpfs 3.2G 0 3.2G 0% /run/user/0
tmpfs 3.2G 0 3.2G 0% /run/user/1039
/dev/vdb 493G 185G 283G 40% /data
10.1.236.58:/data/nfs-server-images 493G 129G 339G 28% /data/nfs-client-images #我們剛掛載的
4. 驗證是否可以共享
在NFS Server共享目錄新增一個檔案
$ ll nfs-server-images/
總用量 4
-rw-r----- 1 root root 12 1月 6 14:35 test.txt
在NFS Client共享目錄檢視檔案已經同步
$ ll /data/nfs-client-images/
total 4
-rw-r----- 1 root root 12 Jan 6 14:35 test.txt
在NFS Client嘗試刪除檔案
# 在2.5步我們設定共享目錄許可權為(ro,sync),表示只讀許可權,並且請求或寫入資料時,資料同步寫入到NFS Server的硬碟後才返回
$ rm test.txt
rm: remove regular file ‘test.txt’? y
rm: cannot remove ‘test.txt’: Read-only file system
5. NFS許可權說明
許可權 | 說明 |
---|---|
rw | 表示可讀寫許可權 |
ro | 表示只讀許可權 |
sync | 請求或寫入資料時,資料同步寫入到NFSServer的硬碟才返回(優點,資料安全不會丟,缺點,效能比不啟用該引數要差) |
async | 寫入時資料會先寫到記憶體緩衝區,只到硬碟有空檔才會再寫入磁碟,這樣可以提高寫入效率!;風險為若伺服器宕機或不正常關機,會損失緩衝區為寫入磁碟的資料(解決辦法:伺服器主機板加電池或加UPS不間斷電源)! |
no_root_squash | 訪問NFS Server共享的使用者如果是root的話,它對該共享目錄具有root許可權。這個配置原本是為無盤客戶端準備的,使用者避免應用 |
root_squash | 如果訪問NFS Server共享目錄的使用者是root,則它的許可權將被壓縮或匿名使用者,同時它的UID和GID通常會變成nfsnobody帳號身份 |
all_squash | 不管訪問NFS Server共享目錄的使用者身份如何,它的許可權都將被壓縮成匿名使用者,同時它的UID和GID通常會變成nfsnobody帳號身份。在早期多個NFS客戶端同時寫入NFS Server資料時,這個引數很有用*在生產中配置NFS的重要技巧1)、確保所有客戶端伺服器對NFS共享目錄都具備相同的使用者訪問許可權 a、all_squash把所有客戶端都壓縮成固定的匿名使用者(UID相同) b、就是aninuid,anongid指定的UID和GID的使用者2)、所有的客戶端和服務端都需要有一個相同的UID和GID使用者,即nfsnobody(UID必須相同) |
anonuid | 引數以anon*開頭即指anonymous匿名使用者,這個使用者的UID設定值通常為nfsnobody的UID值,當然也可以自行設定這個UID值。但是,UID必須存在於/etc/passwd中。在多NFS Clients時,如多臺Web Server共享一個NFS目錄,通過這個引數可以使得不同的NFS Clients寫入的資料對所有NFS Clients保持同樣的使用者許可權,即為配置的匿名UID對應使用者許可權,這個引數很有用,一般預設即可 |
anongid | 同anonuid,區別就是把uid(使用者id)換成gid(組id) |
noatime | 在檔案系統上不更新inode訪問時間 |
nosuid | 不允許設定使用者標識或設定組標識為才能生效 |
intr | 當伺服器宕機或者無法到達時,允許中斷NFS請求 |
noexec | 當伺服器宕機或者無法到達時,允許中斷NFS請求 |
rsize=8192和wsize=8192 | 通過設定大的同時傳輸的資料塊大小(bytes),以提高NFS讀寫的速度 |
nodiratime | 不允許更新檔案系統上的目錄 inode 訪問記錄 |
問題解決
設定共享目錄只讀是防止客戶端刪除image
發現把目錄設定為只讀ro之後,NFS端無法解壓tar包,或者拷貝tar包到yum repo目錄
解決辦法:
# 修改許可權,需要no_root_squash
$ cat >/etc/exports<<EOF
#NFS server share directories
/data/nfs-server-images 10.1.236.0/24(ro,sync,no_root_squash)
EOF
$ systemctl reload nfs
Snow nothing, reap nothing.