[HowTo] Share data between two computers using NFS

Goal

Sharing data between 2 computer in both directions. We will apply the sharing of the ~/Music folder and we will ensure there is no duplicated data.

This guide is an application of the Arch wiki guide to NFS

Preparation

On Manjaro the package providing NFS is nfs-utils and is installed as part of the system. As the NFS service works using either IP address or hostnames it is a good idea to test if you can ping a computer using its hostname.

We will use the client/server topology so select a system with a lot of available storage as server - as it makes sense to designate the available storage to as a network share.

A server needs to be powered up and visible on the network and have a predictable address on the network.

In this guide we assume you are using Network Manager and you set a static IP for your network card. You can use WiFi but it is not as good as a wired connection.

For this guide we assume

  • network: 192.168.1.0
  • subnet: 255.255.255.0
  • server IP: 192.168.1.20
  • computername: server01

The share

As we want to avoid duplicating data we will move the relevant folder from the home folder to a designated structure and symlink the data into the home folder.

Create the data structure

On the server create the folder structure and make the sharepoint writeable by world - in this example the a Music folder.

sudo mkdir -p /data/Music
sudo chmod ugo+rwx /data/Music

Move the content of the ~/Music folder to the new folder - remove the empty ~/Music folder and symlink it to the new location

mv ~/Music/.* /data/Music/ -R
rm -r ~/Music
ln -s /data/Music ~/Music

Verify it is done right by listing the content of ~/Music

ls ~/Music

Create export folders

To share the folders using nfs we need an export point and the /srv folder is entrypoint. Create a folder to designate this is the nfs service point.

sudo mkdir -p /srv/nfs/Music

Server bind mount

To avoid sharing a location which could expose the system we create a bind mount in the file system table binding the data folder with the nfs export point

/etc/fstab

/data/Music /srv/nfs/Music none bind 0 0

Mount without restart

Remount the mountpoints using mount

sudo mount -a

Export the shares

Edit the file /etc/exports share the root of the nfs and the share itself and access control for the shares.

NFS works only with IP and/or hostname restrictions so it is possible to restrict further down using only IP addresses or hostnames. The example below adapted from the Arch wiki - tested and tried - it works.

Additionally you can export the same share multiple times thus limiting access to a specific share to e.g. two devices - if the device uses a dynamic ip you can specifiy hostname and get the same result as if it uses static ip. In the example below we have allowed all computers on the network to connect to the Music share

/etc/exports

/srv/nfs            192.168.1.0/24(rw,sync,cossmnt,fsid=0)
/srv/nfs/Music      192.168.1.0/24(rw,sync)

Service

Enable and start the service.

systemctl enable --now nfs-server.service

Client

Enable and start the service

systemctl enable --now nfs-client.target

Mounting

Create a folder in your home for it - name it anything you like e.g. nfs-mount and mount the share. Use the hostname of the server computer or it's IP address

sudo mount -t nfs server01:/Music ~/nfs-mount

Verify the share is up by listing the content of the mount

ls ~/nfs-mount

If you the shared music folder to appear directly as Music on client you can symlink directly to the clients Music folder - just ensure it is empty.

sudo mount -t nfs 192.168.1.20:/Music ~/Music

Conclusion

You have now shared your collection of music on your local network. Any change to the ~/Music folder will immediately be reflected on the network.


Background on the chosen folder tree

On the *nix filesystem we have a location named /mnt. This location is mostly used to mount filesystems to chroot into, make changes and exit. The folder is described as a place for temporary mounts Filesystem Hierachy Standard

/mnt Temporarily mounted filesystems.

The standard also suggests a folder for shared data

/srv Site-specific data served by this system, such as data and scripts for web servers, data offered by FTP servers ...

The SysAdmin recommendation

What follows here is my personal experience as a sysadmin and believe me - make it simple - easily memorable - you can thank me later.

Your data

As a safeguard of your system, the actual data should be located in a separate structure. You will later be adding bind mounts for them.

The data structure can - as starting point - be anything but sticking to the recognizable - use a distinctive pattern and simple rules

  • Partitions mount in folders right below /data/
  • Shares from other systems in service/folder structure e.g. /data/nfs/data
  • Move local data to designated folders and use local symlinks to users home e.g. move the ~/Music to /data/local/Music and symlink the folder back ln -s /data/local/Music ~/Music

Example

Starting in /data/ your structure could look like this

/data >>> tree -L 2
.
β”œβ”€β”€ build
β”œβ”€β”€ nfs
β”‚   └── data
β”œβ”€β”€ smb
β”‚   └── data
β”œβ”€β”€ local
β”‚   β”œβ”€β”€ Music
β”‚   └── Video
β”œβ”€β”€ projects
└── virtualbox

Mount your partitions using fstab

Your shares

For the data you plan to share, create a similar structure using /srv/ as base.

/srv >>> tree -L 2
.
└── nfs
     β”œβ”€β”€ Music
     └── Video

Add bind mounts to fstab - as bind mounts adds a layer of security to your shares - as you should avoid sharing anything which eventually could be used to traverse the root of your system.

/data/local/Music /srv/nfs/Music none bind 0 0    
/data/local/Video /srv/nfs/Video none bind 0 0

To avoid duplication of data you could create a symlink to your home folder - or - in case of indexing utilities like Albert which do not follow symlinks you could create an additional bind mount for the user(s) on the system.

As example - replace username with the actual username for which you would like to make the folder indexable.

/data/local/Music /home/username/Music none bind 0 0

Further reading

https://wiki.archlinux.org/index.php/NFS
https://wiki.archlinux.org/index.php/samba


Windows NFS Client

Based on the comments below - there are options to use Windows as NFS client. The forum do not recommend one solition for another and do not support such client.

8 Likes

This is wonderful. Thank you @linux-aarhus for this.
I've been struggling with NFS for a while now.

I have a question. Is there a way to have a windows machine see this on the network? I'd much rather use this than Samba.

Thanks again!

I think there is an option in Windows - Add/Remove Windows features - it is well hidden but if I recall correct there is a Unix services option - but I have never tried.

The actual service availability could also depend on the version of Windows used. The Home edition is known for lacking specialized network support e.g. AD.

1 Like

Ok, I'll check later today then. I know the same is for SMB services. They are tucked away very well on Windows.

I'll have a look. Thank you.

Looks like don't have it, being on the neutered Home edition. Dang.

Did a quick search using DDG and it seems possible - though from your image it seems to be an advanced feature available only in Win 10 Pro and Enterprise.

The entry would be named Services for NFS if available and the mounting could be done using different commands.

net use <DriveLetter>  \\<hostname>\path\to\share 

Or

mount  \\<hostname or IP >\path\to\share  [<DriveLetter>]

Or

mklink /D [LinkLocation] [share location]

ADDED
On further reading - it seems the app makes the win box an NFS server - not client

Narrowing down the search to Win 10 Home I found this topic on the Kodi.tv forum. It appears that using a thirdparty software can make NFS client connections available on Windows - for free - they claim. The topic is from March 2018 - not that old.

https://forum.kodi.tv/showthread.php?tid=329452


Windows NFS Client

There is a github repo for a Windows NFS client

1 Like

I've been reading over this...

I'm not sure how not having the NFS server portion would affect offering files back and forth from Linux though.

I'll take another look at my features in Windows again.

Thank you for the link. I'll go check that out right now.

Very much appreciated.


Thanks for github link.

I'll try these out and see how it goes.

:+1:

IIRC it's part of "Windows Subsystem for Linux"

You could be right - I didn't think of that.

Isn't that only available for developers?

Or can any user install it from the store?

I just enabled WSL and looking at the store distros.
Kind of creeping me out a little. :rofl:


Ubuntu, openSUSE leap, Kali, Debian, Alpine, and Suse Linux Enterprise are the free ones.
Fedorka remix and Pengwin are charging.

Before I do all this with WSL, I'm going to give the programs @linux-aarhus linked to me and see how that goes.
Then if all goes sideways I'll start looking into this WSL thingy.

I'd just like to add for the record four little words... in over my head. :smirk:

Thank you for this awesome guide Frede. NFS is totally superior to Samba IMO. I have simply given up on offering support for Samba on the forum as it is a lost cause as far as I'm concerned. If you are operating a Linux only network ditch Samba and convert to the Linux native Network File System.

@Sinister, it may be possible to hack in NFS support into editions MS disabled it on. I'm not recommending that, but I know it is sometimes possible to enable components MS disabled on home versions. Again, I'm not recommending this, just saying it may be possible through a registry hack.

2 Likes

I have WSL version 1 installed on my work Win10 laptop. AIUI, you will get version 1 unless you are part of the Windows Insider Program. (source: https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux)

1 Like

I think the Store-based WSL is different to

image

which has been around for years.

Yep, I enabled that and rebooted.
It didn't unlock any other entries or have nested selections after reboot. :woman_shrugging:
But it was a good excuse to play with Linux on Windows (eventually). :grin:

@tbg I've searched but it appears it's completely ripped out as far as I can gather.
When M$ enabled it in Win10 Pro apparently there was quite the download, among that was the NFS systems. Or that's what I understand from the posts I've read on some Window blogs.

Going to try these Apps Frede found. This will be awesome if I can get my terabytes of crap all shared. Samba can bite me.

I think it just adds some command-line utilities which is why most Windows users don't know about them. I can't really remember what they were though...

1 Like

Little disappointing there's no Arch-based WSL systems.
Apparently there was for a brief few days but was yanked? I guess rolling releases probably don't mix well with Windows Rolling release model. :wink:

This may possibly work to add NFS Server support to Windows 10 (I have not tested as I do not use Windows):

https://sourceforge.net/projects/freenfs/

Also:

https://forum.kodi.tv/showthread.php?tid=329452

2 Likes

This feels so weird

Apologies, @linux-aarhus. I'll not clutter the thread with anymore WSL silliness. :wink:

Thank you all for your advice/assistance/knowledge.

I'll be tinkering with this now for a while.

Could you mark them as NFS servers as they are not clients.

1 Like

the name of β€œsharedfolder” have to in our system’s mnt(mount) directory or can be else where? and one point of directory or it can be multiple point of directory?
example: /mnt/music or /home/music ?

best regards

As long as it is bind mounted into /srv/nfs/sharename you can use any folder structure.

For a beginner I would avoid using folders located in your personal home - I have always used dedicated folders with dedicated permission to avoid pitfalls arising from the stricter permissions on personal folders.

You may succeed and you may not - frankly I have not tested such scenario. From a sysadmin perspective - always use dedicated folders where you can control the access and you can make sure you are not messing with users personal files.

Another point is the /mnt folder which is usually used for temporary mounts directly on /mnt which makes it a bad choice for persistent mount structure.

/media is already the choice for removable media so /data is reasonable choice as root for a persistent mount structure. And if you want to have more shared folders then you need to grow the /data tree and thus you will get issues later on if you have locked yourself by mount to the /data instead of /data/share.

@antman
EDIT
I you want to avoid duplicated data you could move e.g. your ~/Music into /data and then symlink that folder into your home. That way any change you make in your ~/Music folder will immediately reflect on the network.

mv ~/Music /data -R
ln -s /data/Music ~/Music
1 Like

Forum kindly sponsored by Bytemark