{"id":2265,"date":"2020-12-04T13:23:35","date_gmt":"2020-12-04T13:23:35","guid":{"rendered":"https:\/\/linuxways.net\/?p=2265"},"modified":"2021-03-05T05:04:08","modified_gmt":"2021-03-05T05:04:08","slug":"how-to-configure-nfs-client-and-server-on-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/debian\/how-to-configure-nfs-client-and-server-on-linux\/","title":{"rendered":"How to Configure NFS Client and Server on Linux"},"content":{"rendered":"<p>NFS was developed by Sun Microsystems for sharing files and directories between their systems. NFS allows the systems on a network to share files through the TCP\/IP network. It is operated in the client-server fashion where the system having the shared directory is known as a server while the system accessing the shared directory is known as a client. The client can mount the share directory by NFS server to its own system which appears as a local disk partition. A client can then read\/write files on the remote NFS server based on the permissions assigned by the NFS server. Some of the features of NFS are as follows:<\/p>\n<ul>\n<li>User can access remote files locally<\/li>\n<li>It is not necessary for both machines to have the same operating system<\/li>\n<li>It offers a centralized storage solution<\/li>\n<li>User can store their data in a central location hence it allows saving local storage space.<\/li>\n<\/ul>\n<p>In this post, we will be showing you how to install the NFS server and client, export shares, and mount\/unmout the NFS share on client.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>For the demonstration, we will be using the two machines with the following details:<\/p>\n<p><strong>NFS host (Debian 10)<\/strong><\/p>\n<ul>\n<li>Hostname: nfs<\/li>\n<li>IP: 192.168.72.158<\/li>\n<\/ul>\n<p><strong>NFS client (Debian 10)<\/strong><\/p>\n<ul>\n<li>Hostname: client<\/li>\n<li>IP: 192.168.72.159<\/li>\n<\/ul>\n<p><strong>Note: The steps mentioned here have been tested on Debian 10 (Buster system). <\/strong><\/p>\n<h2>Installing and Configuring NFS server<\/h2>\n<p>In the following section, we will first install the NFS server on our host machine. Then we will setup two share directories that are<strong> \/var\/nfs-public <\/strong>and<strong> \/var\/nfs-docs<\/strong> for sharing to client machines. Let\u2019s get started.<\/p>\n<h3>Step 1: Install the NFS server on the host machine<\/h3>\n<p>For a system to setup as an NFS server, you will need to install <strong>nfs-kernel-server<\/strong> package on it. On the NFS host machine, first, update the local repository index using the below command in Terminal:<\/p>\n<pre>$ sudo apt update<\/pre>\n<p>Then install <strong>nfs-kernel-server<\/strong> using the below command:<\/p>\n<pre>$ sudo apt install nfs-kernel-server<\/pre>\n<p>Now you may be provided with the <strong>y\/n<\/strong> option to continue the installation. Hit <strong>y<\/strong> to continue, after which the system will begin the installation of the <strong>nfs-kernel-server<\/strong>.<\/p>\n<p>Once the <strong>nfs-kernel-server<\/strong> is installed, you can verify the installation using the below command in Terminal:<\/p>\n<pre>$ dpkg -l | grep nfs-kernel-server<\/pre>\n<h3>Step 2: Create a shared directory on the host<\/h3>\n<p>Now, create a directory on the host that you want to set for sharing with the clients. We have created two directories <strong>\/var\/nfs<\/strong>&#8211;<strong>docs<\/strong> and <strong>\/var\/nfs-public <\/strong>with different configuration settings<strong>.<\/strong><\/p>\n<pre>$ sudo mkdir -p \/var\/nfs-docs<\/pre>\n<pre>$ sudo mkdir -p \/var\/nfs-public<\/pre>\n<p>For the <strong>\/var\/nfs-public<\/strong> directory, we will modify its ownership to <strong>nobody:nogroup<\/strong> using the below command:<\/p>\n<pre>$ sudo chown nobody:nogroup \/var\/nfs-public<\/pre>\n<p>To verify if the ownership has changed, use the below command:<\/p>\n<pre>$ ls -la \/var\/nfs-public<\/pre>\n<p>For the <strong>\/var\/nfs-docs<\/strong>, we will not change the ownership.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2266\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-13.png\" alt=\"how to configure nfs client and server linux\" width=\"669\" height=\"327\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-13.png 669w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-13-300x147.png 300w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/p>\n<h3>Step 3: Export the shares<\/h3>\n<p>Now we will configure the NFS server export file for sharing of the resources. The NFS export file is <strong>\/etc\/exports<\/strong> which can be edited as follows:<\/p>\n<pre>$ sudo nano \/etc\/exports<\/pre>\n<p>Now in this file, we will specify the directories that we want to share and the client that can access it. You can add entries in the NFS export file using the below syntax:<\/p>\n<pre>directory host1(options) host2(options)...hostN(options)...<\/pre>\n<p>Where<\/p>\n<ul>\n<li><strong>directory<\/strong> is the directory you want to share with the clients.<\/li>\n<li><strong>host<\/strong> is the IP address\/subnet address of the client\/subnet you want to share with. For finding the IP address of your client machine, you can visit our guide on <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-find-your-ip-address-in-ubuntu-20-04-lts\/\">How to Find Your IP address<\/a> .<\/li>\n<li><strong>options<\/strong> are some of the configuration options that specify how the resources should be shared.<\/li>\n<\/ul>\n<p>For both our directories <strong>\/var\/nfs-docs<\/strong> and <strong>\/var\/nfs-public<\/strong>, we have added the following entries in the <strong>\/etc\/exports<\/strong> file:<\/p>\n<pre>\/var\/nfs-public 192.168.72.159(rw,sync,no_subtree_check)\n\n\/var\/nfs-docs 192.168.72.159(rw,sync, no_root_squash,no_subtree_check)<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2267\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-14.png\" alt=\"how to configure nfs client and server linux\" width=\"754\" height=\"164\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-14.png 754w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-14-300x65.png 300w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/><\/strong><br \/>\nIn the above configuration file, both entries tell the NFS server to share the <strong>\/var\/nfs-public <\/strong>and<strong> \/var\/nfs-docs <\/strong>directories with the NFS client<strong> 192.168.72.159<\/strong> with <strong>rw<\/strong> (read and write) permission. In the second entry, we have added the <strong>no_root_squash<\/strong> that allows the client to have root permissions on the NFS server as well. Once you are done with the export configurations, save and close the <strong>\/etc\/exports<\/strong> file.<\/p>\n<p>Now in order to export the shares, you will have to restart the NFS server. Here is the command to do so:<\/p>\n<pre>$ sudo systemctl restart nfs-kernel-server<\/pre>\n<p>Whenever you make any change in the <strong>\/etc\/exports<\/strong> file, make sure to restart the service to apply the configuration changes.<\/p>\n<p>Alternatively, you can use this command to export the shares:<\/p>\n<pre>$ sudo exportfs -a<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2268\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-15.png\" alt=\"how to configure nfs client and server linux\" width=\"453\" height=\"85\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-15.png 453w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-15-300x56.png 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/p>\n<p>To verify if the NFS server is running without any issues check its status using the below command in Terminal:<\/p>\n<pre>$ sudo systemctl status nfs-kernel-server<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"935\" height=\"129\" class=\"wp-image-2269\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-16.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-16.png 935w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-16-300x41.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-16-768x106.png 768w\" sizes=\"auto, (max-width: 935px) 100vw, 935px\" \/><\/p>\n<h3>Step 4: Set up Firewall on the NFS server<\/h3>\n<p>If a firewall is enabled on the NFS server, you will need to configure it to allow clients to connect to NFS port <strong>2049<\/strong>. To verify if the firewall is enabled on the NFS server, issue the below command in Terminal:<\/p>\n<pre>$ sudo ufw status<\/pre>\n<p>In the following output, you can see the <strong>active<\/strong> status which shows the firewall is enabled.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"567\" height=\"172\" class=\"wp-image-2270\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-17.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-17.png 567w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-17-300x91.png 300w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><\/p>\n<p>Now to allow clients to NFS port <strong>2049<\/strong>, you will have to add a rule in the firewall. Issue the below command in Terminal to do so:<\/p>\n<pre>$ sudo ufw allow from client_ip\/subnet_ID to any port nfs<\/pre>\n<p>For instance, to allow whole subnet <strong>192.168.72.0\/24<\/strong> access to NF port <strong>2049<\/strong>, the command would be:<\/p>\n<pre>$ sudo ufw allow from 192.168.72.0\/24 to any port nfs<\/pre>\n<p>Then to verify if the rule has been added successfully, check the status of firewall:<\/p>\n<pre>$ sudo ufw status<\/pre>\n<p>The highlighted entry in the following output shows the connection from <strong>192.168.72.0\/24<\/strong> is <strong>allowed<\/strong> to port <strong>2049<\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"596\" height=\"201\" class=\"wp-image-2271\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-18.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-18.png 596w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-18-300x101.png 300w\" sizes=\"auto, (max-width: 596px) 100vw, 596px\" \/><\/p>\n<p>Now we have completed the configurations on the NFS Server. Let\u2019s move towards NFS client installation and configuration.<\/p>\n<h2>Installing and Configuring NFS Client<\/h2>\n<p>In the following section, we will first install the NFS client on our client machine. Then we will setup two mount points that are<strong> \/media\/public <\/strong>and<strong> \/media\/docs<\/strong> for mounting the remote NFS shares <strong>\/var\/nfs-public <\/strong>and<strong> \/var\/nfs-docs<\/strong>. Let\u2019s get started.<\/p>\n<h3>Step 1: Install NFS Client Package on the Client Machine<\/h3>\n<p>For a system to setup as an NFS client, you will need to install <strong>the nfs-common<\/strong> package on it. On the NFS client machine, first, update the local repository index using the below command in Terminal:<\/p>\n<pre>$ sudo apt update<\/pre>\n<p>Then install <strong>nfs-common<\/strong> using the below command:<\/p>\n<pre>$ sudo apt install nfs-common<\/pre>\n<p>Now you may be provided with the <strong>y\/n<\/strong> option to continue the installation. Hit <strong>y<\/strong> to continue, after which the system will begin the installation of the <strong>nfs-common<\/strong> package.<\/p>\n<h3>Step 2: Create Mount Points<\/h3>\n<p>On the client system, you will need to create a mount point to mount the shares located on the NFS server. On our client, we have created the two directories at <strong>\/media<\/strong> for our mount points:<\/p>\n<pre>$ sudo mkdir -p \/media\/docs\n\n$ sudo mkdir -p \/media\/public<\/pre>\n<h3>Step 3: Mount NFS shares on the client manually<\/h3>\n<p>Now we will mount the NFS shares on the mount points we created in the previous step. Here is the syntax to do so:<\/p>\n<pre>$ sudo mount NFS_server_IP:NFS_share client_mountpoint<\/pre>\n<p>Where :<\/p>\n<p><strong>NFS_server_IP<\/strong> is the NFS server\u2019s IP address<\/p>\n<p><strong>NFS_share<\/strong> is the share located on the NFS server<\/p>\n<p><strong>client_mountpoint<\/strong> is the mount point where you want to mount the share.<\/p>\n<p>From our NFS server, we have exported two shared directories that were <strong>:\/var\/nfs-docs<\/strong> and <strong>\/var\/nfs-public<\/strong>. On our client machine, we will mount one share <strong>\/var\/nfs-docs<\/strong> to one mount point \/<strong>media\/docs<\/strong> while the other share <strong>\/var\/nfs-public<\/strong> on the second mount point <strong>\/media\/public<\/strong>.<\/p>\n<p>We have used the following commands to mount the NFS shares to the NFS client.<\/p>\n<pre>$ sudo mount 192.168.72.158:\/var\/nfs-docs \/media\/docs\n\n$ sudo mount 192.168.72.158:\/var\/nfs-public \/media\/public<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"64\" class=\"wp-image-2272\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-19.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-19.png 834w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-19-300x23.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-19-768x59.png 768w\" sizes=\"auto, (max-width: 834px) 100vw, 834px\" \/><\/p>\n<p>Now to verify if the NFS shared directories have been mounted successfully, issue the \u201cdf \u2013h\u201d command in Terminal:<\/p>\n<pre>$ df -h<\/pre>\n<p>Here is the output of \u201cdf -h\u201d command which shows two NFS shares at the bottom.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"781\" height=\"293\" class=\"wp-image-2273\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-20.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-20.png 781w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-20-300x113.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-20-768x288.png 768w\" sizes=\"auto, (max-width: 781px) 100vw, 781px\" \/><\/p>\n<p>As we have mounted the shares on the <strong>\/media<\/strong> directory, you can also access the mounted directories from File Manager.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2274\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-21.png\" alt=\"how to configure nfs client and server linux\" width=\"847\" height=\"442\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-21.png 847w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-21-300x157.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-21-768x401.png 768w\" sizes=\"auto, (max-width: 847px) 100vw, 847px\" \/><\/p>\n<h3>Step 4: Mounting the NFS shares on client automatically at Boot<\/h3>\n<p>Each time you restart the client system, you will need to mount the NFS share manually using the mount command. This is okay for one time or occasional use. However, if you want the NFS shares access all the time, you will need to make the mount permanent using the <strong>\/etc\/fstab<\/strong> file.<\/p>\n<p>Edit the <strong>\/etc\/fstab<\/strong> file:<\/p>\n<pre>$ sudo nano \/etc\/fstab<\/pre>\n<p>Now add entries for your NFS share that you want to mount automatically:<\/p>\n<pre>192.168.72.158:\/var\/nfs-docs \/media\/docs nfs rw,sync,hard,intr 0 0\n\n192.168.72.158:\/nfs-public \/media\/public nfs rw,sync,hard,intr 0 0<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2275\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-22.png\" alt=\"how to configure nfs client and server linux\" width=\"916\" height=\"264\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-22.png 916w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-22-300x86.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-22-768x221.png 768w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/p>\n<p>Then save and close the <strong>\/etc\/fstab<\/strong> file.<\/p>\n<p>Next time, when you reboot the system, the client will automatically mount the NFS shares.<\/p>\n<h2>Test NFS Access<\/h2>\n<p>Now we will test the access to NFS shared directories by creating a new file in each directory.<\/p>\n<p>In the client machine, first, create a <strong>test1<\/strong> file as sudo in the local mount point <strong>\/media\/public<\/strong>:<\/p>\n<pre>$ sudo touch \/media\/public\/test1<\/pre>\n<p>Now check the ownership of the new file:<\/p>\n<pre>$ ls -l \/media\/public\/test1<\/pre>\n<p>You will see the ownership of the file is <strong>nobody<\/strong> user and <strong>nogroup<\/strong> group. Although, we created the file as sudo on the client machine, but the NFS server has translated it to the ownership of the host <strong>nobody:nogroup<\/strong>. This means a root user on the client cannot perform the administrative task on the server\u2019s shared directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2276\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-23.png\" alt=\"how to configure nfs client and server linux\" width=\"714\" height=\"66\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-23.png 714w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-23-300x28.png 300w\" sizes=\"auto, (max-width: 714px) 100vw, 714px\" \/><\/p>\n<p>Now create a <strong>test1<\/strong> file as sudo in the second local mount point <strong>\/media\/docs<\/strong>:<\/p>\n<pre>$ sudo touch \/media\/docs\/test1<\/pre>\n<p>If we check the ownership of this file, you will see it is owned by the root user and root group. This is because we have set the <strong>no_root_squash<\/strong> option which allows the root user on the client machine to act as a root on the server&#8217;s directory and can perform administrative tasks.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"664\" height=\"64\" class=\"wp-image-2277\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-24.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-24.png 664w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-24-300x29.png 300w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/p>\n<p>Now on the NFS server machine, check if you can view both files.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2278\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-25.png\" alt=\"how to configure nfs client and server linux\" width=\"535\" height=\"176\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-25.png 535w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/12\/word-image-25-300x99.png 300w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><\/p>\n<h2>Unmounting the NFS Share from the Client machine<\/h2>\n<p>If you no longer need the NFS share, you can unmount it from the client system. To unmount the NFS shares, type umount followed by the name of the directory where the NFS share is mounted.<\/p>\n<pre>$ umount \/media\/docs\n\n$ umount \/media\/public<\/pre>\n<p>This is how you can setup NFS server and client in Debian 10 system. In this article, we have covered how to install the NFS server and client, configure NFS shared directories, and mount\/unmount the NFS shares on the client system. You may want to visit our post on <a href=\"https:\/\/linuxways.net\/de\/mint\/how-to-configure-nfs-server-and-client-on-linux-mint-20\/\">How to Configure NFS Server and Client on Linux Mint 20<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>NFS was developed by Sun Microsystems for sharing files and directories between their systems. NFS allows the systems on a network to share files through the TCP\/IP network.&hellip;<\/p>","protected":false},"author":4,"featured_media":2281,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17,35,91,90],"class_list":["post-2265","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-debian","tag-debian-10","tag-linux","tag-nfs-client","tag-nfs-server"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/2265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=2265"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/2265\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/2281"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=2265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=2265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=2265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}