How To Mount NFS Share On Ubuntu
Network File System (NFS)
“NFS allows a system to share directories and files with others over the network. By using NFS, users and programs can access files on remote systems almost as if they were local files.”
In this quick guide we will illustrate how to install the needed nfs client component and mount a NFS share on a Ubuntu client. In this guide I am mounting a NFS share that i have setup on my NAS.
- NFS server
- Shared directory on the NFS server
- Ubuntu client
Step 1: Prepare the Ubuntu client
Lets start by updating the Ubuntu client.
sudo apt update -y
Step 2: Configure the Firewall to allow NFS traffic
Enable the firewall to allow NFS traffic, the default port for NFS is 2049. Make sure to substitute the IP address to you NFS server IP address. My NFS server have IP address 192.168.200.222.
sudo ufw allow from [NFS_Server_IP or NFS_subnet_address] to any port nfs
sudo ufw allow from 192.168.200.222 to any port nfs
Verify the firewall change
sudo ufw status
org@per:~$ sudo ufw status Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 80/tcp ALLOW Anywhere 443/tcp ALLOW Anywhere 2049 ALLOW 192.168.200.222 22/tcp (v6) ALLOW Anywhere (v6) 80/tcp (v6) ALLOW Anywhere (v6) 443/tcp (v6) ALLOW Anywhere (v6) org@per:~$
Step 3: Install NFS common client
nfs-common provides NFS functionality without including the server component, It will allow us to mount a NFS share.
Run the command bellow to install nfs-common.
sudo apt install nfs-common -y
Step 4: Configure the client and mount the NFS share
Lets make sure that we can reach the NFS share before we configure the client. Enter the command bellow the list the NFS shares on the NFS server. Change the IP address to match your NFS server.
sudo showmount --exports 192.168.200.222
org@per:~$ sudo showmount --exports 192.168.200.222 Export list for 192.168.200.222: /i-data/19ddc0ea/nfs * /i-data/19ddc0ea/nfs/cloud 192.168.100.16,192.168.100.99 /i-data/19ddc0ea/nfs/NAS 192.168.100.99,192.168.100.100,192.168.100.110 org@per:~$
Create a mount point for the NFS shared folder
Next we need to create a mount point for the shared directory. The new directory is where we will mount and access our NFS share.
I am creating a new directory called cloud under /nfs
sudo mkdir -p /Your_directory/Your_sharedfolder
sudo mkdir -p /nfs/cloud
Mount the NFS share to the new directory
Mount the shared directory on your NFS server to the new directory on the client.
sudo mount NFS_ServerIP:/Folder_on_NFS_server /Your_directory/Your_sharedfolder
sudo mount 192.168.200.222:/cloud /nfs/cloud
Confirm that the mount is successful with the command bellow.
sudo df -h
org@per:/$ sudo df -h Filesystem Size Used Avail Use% Mounted on udev 953M 0 953M 0% /dev tmpfs 199M 1.1M 198M 1% /run /dev/sda2 98G 4.9G 89G 6% / tmpfs 994M 0 994M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/loop0 90M 90M 0 100% /snap/core/7917 /dev/loop1 55M 55M 0 100% /snap/lxd/12211 /dev/loop2 218M 218M 0 100% /snap/nextcloud/19299 tmpfs 199M 0 199M 0% /run/user/1000 /dev/loop3 92M 92M 0 100% /snap/core/8689 /dev/loop4 67M 67M 0 100% /snap/lxd/13522 192.168.200.222:/cloud 3.6T 41G 3.6T 2% /nfs/cloud org@per:/$
Step 5: Mount the remote NFS share at boot
Configure the fstab configuration file to auto mount the NFS share a boot. Edit the fstab configuration file and add the following line at the bottom of the file.
NFS_Server_IP:/cloud /Folder_on_NFS_server /Your_directory nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
Edit the line to match your share.
sudo nano /etc/fstab
192.168.200.222:/cloud /nfs/cloud nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
Reboot the system and confirm that the share have auto mounted, after reboot type in the command below and confirm that you can see the NFS share.
sudo df -h
Step 6: Write a file to the NFS share
Lets try out the share by creating a test file with some text in it
echo "File from NFS Client" | sudo tee /nfs/cloud/testfile1
Catalog the new test file
org@per:~$ cat /nfs/cloud/testfile1 File from NFS Client
Step 7: Optional un-mounting an NFS Share
You can unmount a NFS share by moving it out of the share’s directory structure, use the command below to unmount a share.
sudo umount /nfs/cloud
If you also want to prevent the share from being remounted on the next reboot, edit /etc/fstab and either delete the line or comment it out.
In this quick guide we configured the NFS client and mounted the NFS share on a Ubuntu client.
For more Linux quick guides please check out the Linux guide section.