Set up NFS server and client on Debian 9 (Stretch)

Set up NFS server and client on Debian 9 (Stretch)

This guide explains how to set up an NFS server and NFS client on Debian 9. NFS stands for Network File System; through NFS, the client can access (read, write) the remote share on the NFS server as if it were on the local hard disk. In this tutorial, I will show you two different NFS exports, an export of client directories that store files as the user nobody/nogroup without retaining file system permissions, and /var/www that retains file permissions and ownership Export of the directory, if required to host server settings.

1 Preliminary explanation

I use two Debian Wheezy systems here:

  • NFS server: server.example.com, IP address: 192.168.1.100
  • NFS client: client.example.com, IP address: 192.168.1.101

2 Install NFS

Ensure that the servers are up to date by updating the package list and installing pending updates on both servers

apt-get update
apt-get upgrade

Then proceed to the NFS server and client installation.

server:

On the NFS server, we run:

apt-get install nfs-kernel-server nfs-common

Then we create a system boot link for the NFS server and start it:

customer:

On the client side, we can install NFS as follows (this is actually the same as on the server side):

apt-get install nfs-common

3 Export the directory on the server

server:

I want the client to be able to access the directories /home/client1 and /var/www to show two different access modes of the NFS server. The directory /home/client1 is shared in standard mode, so all files written into this directory are stored as user nobody and group nogroup. For the directory /var/www, I use the no_root_squash option to instruct the NFS server to retain file permissions and ownership.For example, this is necessary when you want to export the /var/www directory of a Web server managed in the following way ISP configuration 3

First, I will create the /home/client1 directory

mkdir /home/client1chown nobody:nogroup /home/client1chmod 755 /home/client1

The /var/www directory most likely exists on your server. If not, create it:

mkdir /var/wwwchown root:root /var/wwwchmod 755 /var/www

Now we have to modify /etc/exports and we “export” our NFS share.We designate /home/client1 and /var/www as NFS shares and tell NFS to access /home/client1 as the user nobody (to learn more about /etc/exports, its format and available options, please see

man 5 exports

)

nano /etc/exports
/home/client1           192.168.1.101(rw,sync,no_subtree_check)
/var/www        192.168.1.101(rw,sync,fsid=0,crossmnt,no_subtree_check,no_root_squash)

(The no_root_squash option causes /var/www to be accessed as root.)

In order to apply the changes in /etc/exports, we restart the kernel nfs server

service nfs-kernel-server restart

4 Mount the NFS share on the client

customer:

First, we create a directory to mount the NFS share, for example:

mkdir -p /mnt/nfs/home/client1mkdir -p /var/www

If the directory /var/www already exists on your server, stop apache, rename the directory and create a new empty directory as the mount point

service apache2 stopmv /var/www /var/www_bakmkdir -p /var/www

After that, we can mount them as follows:

mount 192.168.1.100:/home/client1 /mnt/nfs/home/client1mount 192.168.1.100:/var/www /var/www

You should now see two NFS shares in the output

df -h
[email protected]:/tmp# df -hFilesystem Size Used Avail Use% Mounted onudev 990M 0 990M 0% /devtmpfs 201M 6.0M 195M 3% /run/dev/sda1 28G 1.2G 25G 5% /tmpfs 1001M 0 1001M 0% /dev/shmtmpfs 5.0M 0 5.0M 0% /run/locktmpfs 1001M 0 1001M 0% /sys/fs/cgrouptmpfs 200M 0 200M 0% /run/user/1000192.168.1.100:/home/client1 28G 1.2G 25G 5% /mnt/nfs/home/client1192.168.1.100:/var/www 28G 1.2G 25G 5% /var/www[email protected]:/tmp#

with

mount
[email protected]:/tmp# mountsysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)udev on /dev type devtmpfs (rw,nosuid,relatime,size=1012912k,nr_inodes=253228,mode=755)devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=204804k,mode=755)/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd)pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=33,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=9848)mqueue on /dev/mqueue type mqueue (rw,relatime)debugfs on /sys/kernel/debug type debugfs (rw,relatime)hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=204800k,mode=700,uid=1000,gid=1000)fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)192.168.1.100:/home/client1 on /mnt/nfs/home/client1 type nfs (rw,relatime,vers=3,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.100,mountvers=3,mountport=57821,mountproto=udp,local_lock=none,addr=192.168.1.100)192.168.1.100:/var/www on /var/www type nfs (rw,relatime,vers=3,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.100,mountvers=3,mountport=57821,mountproto=udp,local_lock=none,addr=192.168.1.100)[email protected]:/tmp#

5 test

On the client, you can now try to create a test file on the NFS share:

Client: Advertising

touch /mnt/nfs/home/client1/test.txttouch /var/www/test.txt

Now go to the server and check if you can see the two test files:

server:

ls -l /home/client1/
[email protected]:/tmp# ls -l /home/client1/total 0-rw-r--r-- 1 nobody nogroup 0 Nov 16 10:52 test.txt[email protected]:/tmp#
ls -l /var/www
[email protected]:/tmp# ls -l /var/wwwtotal 0-rw-r--r-- 1 root root 0 Nov 16 10:52 test.txt[email protected]:/tmp#

(Please note the different ownership of the test files: /home/client1 NFS share is accessed as nobody / nogroup and owned by nobody / nogroup; /var/www share is accessed as root, so /var/www/test.txt belongs to the user And group root.)

6 Mount the NFS share at boot time

You can modify /etc/fstab to automatically mount the NFS share when the client starts, instead of manually mounting the NFS share on the client.

customer:

Open /etc/fstab and append the following lines:

nano /etc/fstab
[...]
192.168.1.100:/home/client1  /mnt/nfs/home/client1   nfs      rw,sync,hard,intr  0     0
192.168.1.100:/var/www  /var/www   nfs      rw,sync,hard,intr  0     0

You can use different mount options instead of rw, sync, hard, intr.To learn more about the available options, check out

man nfs

To test whether the modified /etc/fstab is working properly, unmount the share and run mount -a:

umount /mnt/nfs/home/client1umount /var/wwwmount -a

You should now see two NFS shares in the output

df -h
[email protected]:/# df -hFilesystem Size Used Avail Use% Mounted onudev 990M 0 990M 0% /devtmpfs 201M 6.0M 195M 3% /run/dev/sda1 28G 1.2G 25G 5% /tmpfs 1001M 0 1001M 0% /dev/shmtmpfs 5.0M 0 5.0M 0% /run/locktmpfs 1001M 0 1001M 0% /sys/fs/cgrouptmpfs 200M 0 200M 0% /run/user/1000192.168.1.100:/home/client1 28G 1.2G 25G 5% /mnt/nfs/home/client1192.168.1.100:/var/www 28G 1.2G 25G 5% /var/www[email protected]:/#

with

mount
[email protected]:/# mountsysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)udev on /dev type devtmpfs (rw,nosuid,relatime,size=1012912k,nr_inodes=253228,mode=755)devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=204804k,mode=755)/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd)pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=33,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=9848)mqueue on /dev/mqueue type mqueue (rw,relatime)debugfs on /sys/kernel/debug type debugfs (rw,relatime)hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=204800k,mode=700,uid=1000,gid=1000)fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)192.168.1.100:/home/client1 on /mnt/nfs/home/client1 type nfs (rw,relatime,vers=3,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.100,mountvers=3,mountport=57821,mountproto=udp,local_lock=none,addr=192.168.1.100)192.168.1.100:/var/www on /var/www type nfs (rw,relatime,vers=3,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.100,mountvers=3,mountport=57821,mountproto=udp,local_lock=none,addr=192.168.1.100)[email protected]:/#

7 credits

This tutorial is based on the Centos NFS server tutorial by Falko Timme.

Source