Setting up an NFS Server and Client on Debian 9 (Stretch)

setting up an nfs server and client on debian 9 stretch

This guide explains how to set up an NFS server and an NFS client on Debian 9. NFS stands for Network File System; through NFS, a client can access (read, write) a remote share on an NFS server as if it was on the local hard disk. In this Tutorial, I will show you two different NFS exports, the export of a client directory that stores files as user nobody/nogroup without preserving filesystem permissions and a export of the /var/www directory which preserves permissions and ownership of files, as required on a hosting server setup.

1 Preliminary Note

I’m using 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 Installing NFS

Ensure that the server is up to date by updating the package lists and install pending updates on both servers

apt-get update
apt-get upgrade

Then continue with the NFS server and client installation.

server:

On the NFS server we run:

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

Then we create the system startup links for the NFS server and start it:

client:

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

apt-get install nfs-common

3 Exporting Directories on the Server

server:

I’d like to make the directories /home/client1 and /var/www accessible to the client to show the two different access modes of the NFS server. The directory /home/client1 is shared in standard mode, so all files written to this directory are stored as user nobody and group nogroup. For the directory /var/www I use the no_root_squash option which instructs the NFS server to preserve permissions and ownership of the files. This is e.g. required when you like to export the /var/www directory of a web server managed with ISPConfig 3

First, I’ll create the /home/client1 directory

mkdir /home/client1
chown nobody:nogroup /home/client1
chmod 755 /home/client1

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

mkdir /var/www
chown root:root /var/www
chmod 755 /var/www

Now we must modify /etc/exports where we “export” our NFS shares. We specify /home/client1 and /var/www as NFS shares and tell NFS to make accesses to /home/client1 as user nobody (to learn more about /etc/exports, its format and available options, take a look at

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 makes that /var/www will be accessed as root.)

To apply the changes in /etc/exports, we restart the kernel nfs server

service nfs-kernel-server restart

4 Mounting the NFS shares on the Client

client:

First, we create the directories where we want to mount the NFS shares, e.g.:

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

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

service apache2 stop
mv /var/www /var/www_bak
mkdir -p /var/www

Afterward, we can mount them as follows:

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

You should now see the two NFS shares in the outputs of

df -h
[email protected]:/tmp# df -h
Filesystem Size Used Avail Use% Mounted on
udev 990M 0 990M 0% /dev
tmpfs 201M 6.0M 195M 3% /run
/dev/sda1 28G 1.2G 25G 5% /
tmpfs 1001M 0 1001M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1001M 0 1001M 0% /sys/fs/cgroup
tmpfs 200M 0 200M 0% /run/user/1000
192.168.1.100:/home/client1 28G 1.2G 25G 5% /mnt/nfs/home/client1
192.168.1.100:/var/www 28G 1.2G 25G 5% /var/www
[email protected]:/tmp#

and

mount
[email protected]:/tmp# mount
sysfs 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 Testing

On the client, you can now try to create test files on the NFS shares:

client:

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

Now go to the server and check if you can see both 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/www
total 0
-rw-r--r-- 1 root root 0 Nov 16 10:52 test.txt
[email protected]:/tmp#

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

6 Mounting NFS Shares At Boot Time

Instead of mounting the NFS shares manually on the client, you could modify /etc/fstab so that the NFS shares get mounted automatically when the client boots.

client:

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

Instead of rw,sync,hard,intr you can use different mount options. To learn more about available options, take a look at

man nfs

To test if your modified /etc/fstab is working, unmount the shares and run mount -a:

umount /mnt/nfs/home/client1
umount /var/www
mount -a

You should now see the two NFS shares in the outputs of

df -h
[email protected]:/# df -h
Filesystem Size Used Avail Use% Mounted on
udev 990M 0 990M 0% /dev
tmpfs 201M 6.0M 195M 3% /run
/dev/sda1 28G 1.2G 25G 5% /
tmpfs 1001M 0 1001M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1001M 0 1001M 0% /sys/fs/cgroup
tmpfs 200M 0 200M 0% /run/user/1000
192.168.1.100:/home/client1 28G 1.2G 25G 5% /mnt/nfs/home/client1
192.168.1.100:/var/www 28G 1.2G 25G 5% /var/www
[email protected]:/#

and

mount
[email protected]:/# mount
sysfs 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 in the Centos NFS Server Tutorial from Falko Timme.

نوشته های مشابه