Setup NFS Server for Local Network File Sharing
My workflow involves a multi-workspace setup, where I usually works on a MacBook Pro as *THE TERMINAL*---enjoying its *Liquid Retina XDR* display and the aesthetic of macOS for coding, writing, and graphic design---while using a Linux machine as the server for heavy lifting tasks, such as long-run simulation, batch data processing, machine learning, etc.
This guide provides steps to set up a Network File System (NFS) server on a Ubuntu machine and how to mount it from a macOS client.
Configure Directories Sharing on the Server
Suppose you are logging to the Ubuntu server as a non-root user---with *uid* `1000` and *gid* `1000`, and with sudo privilege---and you want to share a directory located at `/mnt/workspace/share` with your local network.
1. Install the NFS server package on the Ubuntu machine:
sudo apt update sudo apt install nfs-kernel-server
2. Create the directory to share
sudo mkdir -p /mnt/workspace/share
3. Set the appropriate permissions for the shared directory:
sudo chown $USER:$USER /mnt/workspace/share
4. Configure the NFS exports by editing the `/etc/exports` file: # replace `192.168.1.0/24` with your actual network
echo "/mnt/workspace/share 192.168.1.0/24(rw,sync,all_squash,no_subtree_check,anonuid=1000,anongid=1000)" | sudo tee -a /etc/exports
5. Export the shared directory:
sudo exportfs -ra
6. Start the NFS server:
sudo systemctl enable nfs-server sudo systemctl start nfs-kernel-server ``` If you have a firewall enabled, you may need to allow NFS traffic: ``` cmd @server sudo ufw allow from 192.168.1.0/24 to any port nfs sudo ufw reload
NOTE, many tutorials may suggest set the permission of the shared directory as
sudo chown nobody:nogroup /mnt/workspace/share sudo chmod 777 /mnt/workspace/share
but I think it is more convenient to just use a *common* non-root user on the server to manage the shared directory. The use of `all_squash` option maps all user requests to the anonymous user, which would default to `nobody:nogroup`, but is set specifically to `anonuid:anongid`.
This is suitable for a one-man multi-workspace setup, or a small team that does not want tangle with user permission issues. However, if you need fine-grained access control, you may want to reconsider the permission policy or even you would better consider using a protocol other than NFS.
Connect to the NFS Shared Directory from Clients
Connect from macOS
You can mount an NFS share from macOS---macOS includes built-in support for NFS:
sudo mount -t nfs -o resvport 192.168.1.100:/mnt/workspace/share /mnt/nfs_share
To unmount:
sudo umount /mnt/nfs_share
NOTE, the command `mount -t nfs -o resvport ...` is equivalent to `mount_nfs -P ...`, where the `-P` option tells the NFS client to use of a privileged source port ($< 1024$), which many NFS servers (especially Linux-based ones) require for security. However, the latter one---`mount_nfs -P` is NOT documented in the manpage, for better code maintainability, you should prefer using the former command.
Since I usually use my MacBook Pro as a portable work terminal, I often do not configure persistence on-boot mount, rather I would like to create some wrapper utilities to help automate the mount/unmount task---for example, have a look at [ohmypwsh](https://github.com/madpang/ohmypwsh).
Connect from Ubuntu
To connect to a NFS server from a Ubuntu client, you first need to install NFS client software:
sudo apt install nfs-common
Then mount the shared directory:
sudo mount 192.168.1.100:/mnt/workspace/share /mnt/nfs_share
To unmount:
sudo umount /mnt/nfs_share
In a real world company project, your team may have a central NFS server, and you may want to mount the NFS share automatically at boot time. You can add an entry to the `/etc/fstab` file to ensure this:
echo "192.168.1.100:/mnt/workspace/share /mnt/nfs_share nfs defaults 0 0" | sudo tee -a /etc/fstab
A small tip: you can use the command `showmount -e <nfs-server-address>` from the client to check the available NFS shares on the server. This even works with `showmount -e localhost` on the server itself to verify the NFS is correctly configured.
Conclusion
Setting up an NFS server on a Ubuntu machine and connecting to it from macOS or Ubuntu clients is straightforward. This setup allows for efficient and consistent multi-workspace workflows.