Network File Sharing

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,525
Reaction score
3,285
Credits
31,524
What if you have two Linux computers at your house. What if you need to transfer files back and forth between those two computers frequently.

Well, you have some options. You could use scp or sftp. But typing in passwords and paths every time you want to copy a file in a little bit of a hassle. What do we do? What if I could see the drive on computer1 on computer2 just like it was a mounted file system. Wouldn't that be handy?

For the sake of this tutorial, I will call the computer doing the sharing the NFS server, and the remote computer the NFS client.
You could actually make both of them servers, and both of them clients, but I rarely find the need to do that. The gotcha here is that
both computers have to be on the same local network. (There are ways around this with VPNs but that's another subject).

First, we need to install some packages. (if they aren't already installed). Both computers do not have to be the same distro.
You can mount Debian shares on Fedora, and you can mount Rocky shares on Ubuntu.

Deb/Apt based systems.
Server:
Code:
sudo apt update
sudo apt install nfs-kernel-server

Client:
Code:
sudo apt update
sudo apt install nfs-common

Rpm/Dnf based systems.
Server:
Code:
sudo dnf install nfs-utils

Client:
Code:
sudo dnf install nfs-utils

Pacman based systems.
Server:
Code:
sudo pacman -S nfs-utils

Client:
Code:
sudo pacman -S nfs-utils

Now we need to create a directory to export. This could really be almost any directory, but I do not recommend sharing your home
directory. I know people who do... but... anyway. It's best if "nobody" owns the shared filesystem.

Server:
Code:
sudo mkdir -p /srv/nfs/share
sudo chown nobody:nogroup /srv/nfs/share

We also need to create an exports file. The file may already be there, but you need to edit it if it is.
For the shared directory above it would look like this. Change the IP address to the address of the client you to share to.

Server:
Code:
/srv/nfs/share    192.168.3.14(rw,sync,no_subtree_check)

Once that is done, we need to initialize the service.

Server:
Code:
sudo exportfs -a
sudo systemctl restart nfs-server

Now on the client, we need to create a mount point to mount the shared drive to. This is really just a directory.
It could be anywhere, but the Linux convention is to put mounted drives under "/mnt".

Client:
Code:
sudo mkdir -p /mnt/nfs/share

Now we simply mount the shared drive. Change the IP address to the IP address of your NFS server.

Client:
Code:
sudo mount 192.168.3.44:/srv/nfs/share /mnt/nfs/share

Assuming all of that works, we can make this permanent by adding the share to our fstab. That way it gets automatically mounted
if I reboot the computer. Add this line to your /etc/fstab file.

Client:
Code:
192.168.3.44:/srv/nfs/share    /mnt/nfs/share    nfs    defaults    0    0

There you go. Let me know if there any questions.
 
Last edited:


What if want to share this directory to multiple computers? hat if I have more than one client computer?
There are a few ways to do this.

You could add a second line to your /etc/exports file on your NFS server.
Code:
/srv/nfs/share    192.168.3.14(rw,sync,no_subtree_check)
/srv/nfs/share    192.168.3.36(rw,sync,no_subtree_check)

Just add as many lines as you need, but once you get past 5 or 6 clients, this is a pain to manage.
So, now what do we do?

Server: /etc/exports
Code:
/srv/nfs/share    *(rw,sync,no_subtree_check)

This will allow any computer on the local subnet to connect.
Another way of doing this is...

Code:
/srv/nfs/share    192.168.1.0/24(rw,sync,no_subtree_check)

If you're using local DNS, you can use hostnames and domain names.
Code:
/srv/nfs/share    *.example.com(rw,sync,no_subtree_check)

Code:
/srv/nfs/share    client*.example.com(rw,sync,no_subtree_check)
 
Now, I used these mounting options on the server.
Code:
(rw,sync,no_subtree_check)

But there are other options you should be aware of.

rw: Mount the NFS share with read-write access.
ro: Mount the NFS share with read-only access.

sync: Ensures that changes are written to the server before the command completes. This is safer but slower.
async: Allows the server to cache changes and write them later. This is faster but less safe.

no_subtree_check: Disables subtree checking, which can improve performance.
subtree_check: Enables subtree checking, which verifies that the requested file is in the exported tree.

no_root_squash: Allows root on the client to have root privileges on the NFS server.
root_squash: Maps root on the client to a non-privileged user on the server for security.

secure: Requires that requests originate from a port less than 1024.
insecure: Allows requests from any port.

wsize=n: Sets the write buffer size to n bytes.
rsize=n: Sets the read buffer size to n bytes.

noexec: Prevents execution of binaries on the mounted file system. Useful for security.
nosuid: Disables set-user-identifier or set-group-identifier bits. This prevents remote users from gaining higher privileges by

nolock: Disables file locking. This is sometimes required when connecting to very old NFS servers.
nfsvers=version: Specifies the NFS protocol version to use (e.g., 3 or 4).
vers=version: Another way to specify the NFS protocol version.

sec: Specifies the security flavor to use for accessing files. Common values include sys (default), krb5 (Kerberos V5), krb5i (Kerberos V5 with integrity checking), and krb5p (Kerberos V5 with privacy).

Here's an example of how you might use some of these options in the /etc/fstab file:
Code:
server_ip:/srv/nfs/share    /mnt/nfs/share    nfs    rw,sync,no_subtree_check    0    0

There are ways to mount Linux nfs shares in Windows. Perhaps someone else can give instructions on how to do this?
 
I found the need to do this type of thing yesterday.

Both pc's running Linux Mint, and both on the same network

Warpinator. It is already installed in Linux Mint 22.1 and 21.3 etc

Also works on windows PC's etc


 
Last edited:


Members online


Top