MacOS - How to fix Mounting of NFS shared folders on Vagrant?
To give you a little background on the Vagrant NFS shared folder issue which I have struggled with this issue for almost a week before I found the root cause of the problem.
Here is the vagrant's mount.nfs:connection timed out error
1default: Mounting NFS shared folders... 2 3The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! 4 5mount -o vers=3,udp 192.168.0.1:/System/Volumes/Data/Users/rwagh/Git/jhooq.com/src /var/www/jhooq 6 7Stdout from the command 8Stderr from the command: 9mount.nfs: Connection timed out
If you are also having the same issue mounting the shared volume then I would suggest going through the following key points because your MacBook and Development setup might be a little different than mine, so validate your Vagrant and Development setup based on the key troubleshooting steps which I mentioned in the post.
My Vagrant File -
1Vagrant.configure("2") do |config| 2 config.vm.define "amaster" do |amaster| 3 amaster.vm.box_download_insecure = true 4 amaster.vm.box = "hashicorp/bionic64" 5 amaster.vm.network "private_network", ip: "126.96.36.199" 6 amaster.vm.hostname = "amaster" 7 amaster.vm.synced_folder "/Users/rahul/Documents", "/home/vagrant", SharedFoldersEnableSymlinksCreate: true, type: 8 "nfs" 9 amaster.vm.provider "virtualbox" do |v| 10 v.name = "amaster" 11 v.memory = 2048 12 v.cpus = 2 13 end 14 end 15end
If you look at my Vagrantfile then I am trying to mount my "/Users/rahul/Documents" directory under vagrant's home directory /home/vagrant
1amaster.vm.synced_folder "/Users/rahul/Documents", "/home/vagrant", SharedFoldersEnableSymlinksCreate: true, type: "nfs"
Here are my Key Troubleshooting steps -
- Assign Full Disk access to /sbin/nfsd
- Check /etc/hosts file the localhost entry
- Check invalid entries in /etc/exports
- Install vagrant-vbguest plugin
- Check for any active VPN connection
1. Assign Full Disk access to /sbin/nfsd
The first and most probable root cause for this issue is you do not have Full Disk Access on /sbin/nfsd. Use the following steps to give Full Disk Access -
Goto -> System Preferences
Then go for -> Security & Privacy
- Look for -> Full Disk Access (In the left navigation)
- Click on the Unlock button
- Enter your **Administrator password**
- Click on + sign
- Press Command + Shift + G
- Type in the /sbin
- Look of nfsd and then click on Open
- This will add nfsd to Full Disk Access
- Restart the nfsd service -
sudo nfsd update
If nfsd service is the culprit behind adding the shared volume to your vagrant box then above troubleshooting steps should fix your issue and you should be able to mount the share drive onto your vagrant box.
2. Check /etc/hosts file the localhost entry
The second troubleshooting step would be to verify the /etc/hosts file and look for the localhost entry inside it.
Ideally your /etc/hosts file should have only one entry for localhost 127.0.0.1 localhost and you should either comment or remove other entries from the /etc/hosts file.
3. Check invalid entries in /etc/exports
The next troubleshooting step would be to verify the /etc/exports file and so whenever we start or reload the vagrant box updates the /etc/exports so pay close attention to the file and look for any malicious and old entry into that file that you think is not relevant anymore. Also, you need to add nfs_export: false in your vagrant file -
Here are steps which you can perform -
- Add nfs_export: false inside your vagrant file
1config.vm.synced_folder "/Users/rahul/Documents", "/home/vagrant", SharedFoldersEnableSymlinksCreate: true, type: 2"nfs", nfs_export: false
Here is my complete Vagrantfile -
1 Vagrant.configure("2") do |config| 2 config.vm.define "amaster" do |amaster| 3 amaster.vm.box_download_insecure = true 4 amaster.vm.box = "hashicorp/bionic64" 5 amaster.vm.network "private_network", ip: "188.8.131.52" 6 amaster.vm.hostname = "amaster" 7 amaster.vm.synced_folder "/Users/rahul/Documents", "/home/vagrant", SharedFoldersEnableSymlinksCreate: true, type: 8 "nfs", nfs_export: false 9 amaster.vm.provider "virtualbox" do |v| 10 v.name = "amaster" 11 v.memory = 2048 12 v.cpus = 2 13 end 14 end 15end
- After that you should issue vagrant reload command and the /etc/exports should have the full path to each shared folder -
1/System/Volumes/Data/Users/myname/Git/jhooq.com -alldirs -mapall=501:20 192.168.33.3 2/System/Volumes/Data/Users/myname/Git/jhooq.com/src -alldirs -mapall=501:20 192.168.33.3
4. Install vagrant-vbguest plugin
Before installing the vagrant-vbguest make sure you have installed the latest version of Virtualbox.
This step is common for Linux as well as MacOS and vagrant-vbguest can be installed using following command -
1 vagrant plugin install vagrant-vbguest
After that, you can simply reload your vagrant box -
1 $ vagrant reload 2==> default: Attempting graceful shutdown of VM... 3... 4==> default: Machine booted and ready! 5GuestAdditions 4.3.12 running --- OK. 6==> default: Checking for guest additions in VM... 7==> default: Configuring and enabling network interfaces... 8==> default: Exporting NFS shared folders... 9==> default: Preparing to edit /etc/exports. Administrator privileges will be required... 10==> default: Mounting NFS shared folders...
5. Check for any active VPN connection
This is also one of the most probable causes because of remote working a lot of developers are working remotely with a corporate VPN connection. Somehow mounting of the NFS share drive is interrupted by the VPN connections so in such cases use the following steps to start your vagrant box -
- Disconnect from VPN
- Start or Reload the Vagrant Box (vagrant up or vagrant reload)
- After the successful start of Vagrant Box connect to VPN
Hopefully, it should fix your NFS share drive mount problem
Here are some more GitHub References - Vagrant up results in mount.nfs: Connection timed out