N FS(网络文件系统)客户端允许您从Linux客户端访问共享目录。共享目录的计算机称为NFS服务器(也可以是NAS服务器),连接到该服务器的计算机或设备称为客户端。客户端需要使用mount命令来访问共享目录。
如何在 Ubuntu Linux 中挂载 NFS 文件系统
- 在 Ubuntu Linux 客户端中安装nfs-common软件包。
- 使用mkdir 命令创建一个名为 /nfs_client/ 的新挂载点:
$ sudo mkdir -vp /nfs_client/
- 使用如下mount命令从192.168.2.100 NFS服务器挂载名为/Data的共享目录:
$ sudo mount -o hard,rsize=1048576,wsize=1048576,timeo=600 192.168.2.100:/data /nfs_client/
- 使用 df 或 du/mount 命令验证它:
$ df -H
$ mount | grep -i nfs
让我们详细查看所有命令。
Ubuntu Linux NFS 客户端配置
在Ubuntu Linux中需要安装nfs-command包,如下所示,才能使用mount命令挂载NFS共享。首先,打开终端应用程序并键入以下apt-get 命令或apt 命令以在 Ubuntu Linux 上更新和安装待处理的安全更新。键入:如果使用“ reboot ”/“ shutdown ”或“systemctl command”安装了新的Linux内核,则重新启动Ubuntu Linux机器:
$ sudo apt update
$ sudo apt upgrade
$ sudo systemctl reboot
## OR ##
$ sudo reboot
在 Ubuntu 中为 NFS 客户端安装 nfs-common 软件包
键入以下两个命令:
示例输出:
$ sudo apt-get update
$ sudo apt-get install nfs-common
正在阅读包裹清单...完成 构建依赖树...完成 读取状态信息...完成 将安装以下附加软件包: keyutils libnfsidmap1 rpcbind 推荐套餐: 看门狗 将安装以下新软件包: keyutils libnfsidmap1 nfs-common rpcbind 0已升级,4已新安装,0已删除,0未升级。 需要获取381 kB 的档案。 此操作后,将使用1 , 447 kB 的额外磁盘空间。 你想继续吗?[是/否]是 Get:1 http://mirrors.linode.com/ubuntu jammy-updates/main amd64 libnfsidmap1 amd64 1:2.6.1-1ubuntu1.2 [42.9 kB] Get:2 http://mirrors.linode.com/ubuntu jammy/main amd64 rpcbind amd64 1.2.6-2build1 [46.6 kB] Get:3 http://mirrors.linode.com/ubuntu jammy/main amd64 keyutils amd64 1.6.1-2ubuntu3 [50.4 kB] Get:4 http://mirrors.linode.com/ubuntu jammy-updates/main amd64 nfs-common amd64 1:2.6.1-1ubuntu1.2 [241 kB] Fetched 381 kB in 0s (1,872 kB/s) Selecting previously unselected package libnfsidmap1:amd64. (Reading database ... 109654 files and directories currently installed.) Preparing to unpack .../libnfsidmap1_1%3a2.6.1-1ubuntu1.2_amd64.deb ... Unpacking libnfsidmap1:amd64 (1:2.6.1-1ubuntu1.2) ... Selecting previously unselected package rpcbind. Preparing to unpack .../rpcbind_1.2.6-2build1_amd64.deb ... Unpacking rpcbind (1.2.6-2build1) ... Selecting previously unselected package keyutils. Preparing to unpack .../keyutils_1.6.1-2ubuntu3_amd64.deb ... Unpacking keyutils (1.6.1-2ubuntu3) ... Selecting previously unselected package nfs-common. Preparing to unpack .../nfs-common_1%3a2.6.1-1ubuntu1.2_amd64.deb ... Unpacking nfs-common (1:2.6.1-1ubuntu1.2) ... Setting up libnfsidmap1:amd64 (1:2.6.1-1ubuntu1.2) ... Setting up rpcbind (1.2.6-2build1) ... Created symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service → /lib/systemd/system/rpcbind.service. Created symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket → /lib/systemd/system/rpcbind.socket. Setting up keyutils (1.6.1-2ubuntu3) ... Setting up nfs-common (1:2.6.1-1ubuntu1.2) ... Creating config file /etc/idmapd.conf with new version Creating config file /etc/nfs.conf with new version Adding system user `statd' (UID 115) ... Adding new user `statd' (UID 115) with group `nogroup' ... Not creating home directory `/var/lib/nfs'. Created symlink /etc/systemd/system/multi-user.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target. Created symlink /etc/systemd/system/remote-fs.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target. auth-rpcgss-module.service is a disabled or a static unit, not starting it. nfs-idmapd.service is a disabled or a static unit, not starting it. nfs-utils.service is a disabled or a static unit, not starting it. proc-fs-nfsd.mount is a disabled or a static unit, not starting it. rpc-gssd.service is a disabled or a static unit, not starting it. rpc-statd-notify.service is a disabled or a static unit, not starting it. rpc-statd.service is a disabled or a static unit, not starting it. rpc-svcgssd.service is a disabled or a static unit, not starting it. rpc_pipefs.target is a disabled or a static unit, not starting it. var-lib-nfs-rpc_pipefs.mount is a disabled or a static unit, not starting it. Processing triggers for man-db (2.10.2-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.1) ... Scanning processes... Scanning linux images...
See The List Of All Shared NFS Directories
Type the following command if you are using NFSv3 for earlier. The following command will not work with NFSv4:
$ showmount -e server-Ip-address
$ showmount -e 192.168.1.1
Sample outputs from my 192.168.1.1 NFS server:
Export list for 192.168.1.1: /viveks /Web /Usb /Recordings /Public /Network Recycle Bin 1 /Multimedia /Download
Mounting Shared NFS Directory in Ubuntu Linux
Now mount /viveks directory as follows using the mount command. First, make a new mount point using the mkdir command:
$ sudo mkdir /nfs
Now mount it:
$ sudo mount -o soft,timeo=150,intr,rsize=8192,wsize=8192 192.168.1.1:/viveks /nfs/
OR use hard mount option:
$ sudo mount -o hard,timeo=600,rsize=1048576,wsize=1048576 192.168.1.1:/viveks /nfs/
Verify nfs mounted file system on Ubuntu Linux using the df command or du command. For example:
$ df -h
Sample outputs:
/dev/sda5 477G 25G 428G 6% /
none 4.2G 398k 4.2G 1% /dev
none 4.2G 377k 4.2G 1% /dev/shm
none 4.2G 230k 4.2G 1% /var/run
none 4.2G 0 4.2G 0% /var/lock
none 4.2G 0 4.2G 0% /lib/init/rw
192.168.1.1:/viveks 491G 208M 491G 1% /nfs
Understanding mount command
Let us breakdown the following command is used to mount a remote NFSv3/NFSv4 filesystem:
$ sudo mount -o hard,timeo=600,rsize=1048576,wsize=1048576 192.168.1.1:/viveks /nfs/
- mount : Mount a filesystem including NFSv4/NFSv3.
- -o:-o选项指定 NFS 的安装选项。选项包括hard、timeo=600、rsize=1048576和wsize=1048576设置和配置 NFS 的挂载。硬选项告诉内核重试失败的挂载请求,直到挂载成功。它设置 NFS 请求超时后 NFS 客户端的恢复行为。timeo =600选项指定软选项和硬选项的安装请求的超时(以秒为单位)。rsize =1048576和wsize=1048576选项指定安装点的读取和写入大小。这是优化选项,它将加快文件的读写速度。
- 192.168.1.1:/viveks:这是远程 NFSv4 或 NFSv3 服务器的 IP 地址(您可以使用 DNS 名称,例如 server1.example.com)以及正在挂载的目录的路径,即/viveks/。
- /nfs/:这是 Ubuntu Linux 中的本地挂载点
Ubuntu Linux 中的软 NFS 挂载与硬 NFS 挂载
软或硬选项决定了 NFS 请求超时后 NFS 客户端的恢复行为。换句话说,这两个选项都设置了 NFS 请求超时后 NFS 客户端的恢复行为。我建议使用硬安装选项来保证数据完整性。您可以使用timeo=SECONDS选项设置超时值(以秒为单位)。如果使用软NFS 安装,请将timeo参数设置为至少 150 分秒(15 秒),以最大限度地降低软安装固有的数据损坏风险。对于硬挂载,NFS 挂载将超时值设置为 600 分秒(60 秒)。
如何使用 NFS 访问我的文件?
只需使用 cd 命令进入挂载点即 /nfs 目录,
$ cd /nfs/
然后使用 ls 命令列出文件:
$ ls
您可以使用文本编辑器(例如 vim/emacs/nano)或 GUI 文本编辑器(例如 gedit)创建新文件:
$ gedit file.c
如何重启后自动挂载NFS?
编辑/etc/fstab,输入:
$ sudo vi /etc/fstab
或者在使用 Ubuntu Linux 桌面时使用 gui 文本编辑器,例如 gedit:
$ sudo gedit /etc/fstab
在文件末尾添加以下行:
192.168.1.1:/viveks /nfs nfs 软,intr,rsize=8192,wsize=8192
保存并关闭文件。
如何删除挂载的NFS目录(unmount NFS)?
键入以下命令:
$ cd
$ sudo umount /nfs
$ df -H
加起来
这就是如何使用 mount 命令在 Ubuntu Linux 中挂载 NFS 共享目录或文件夹。有关详细信息,请使用 man 命令或 help 命令参阅以下手册页:
$ man 5 nfs
$ man 5 fstab
$ man 8 mount
$ man 8 umount
$ man 5 mount.nfs
$ man 5 umount.nfs
$ man 5 exports