- Authenticate with GCP and set project
gcloud auth login
gcloud config set project <YOUR_PROJECT_ID>
- Set the compute region and zone (Optional)
gcloud config set compute/region us-central1
gcloud config set compute/zone us-central1-a
gcloud compute instances create nfs-server \
--zone=us-central1-a \
--machine-type=e2-medium \
--image-project=ubuntu-os-cloud \
--image-family=ubuntu-2004-lts \
--tags=nfs-server
gcloud compute ssh nfs-server
- Install NFS server on the VM
sudo apt update
sudo apt install nfs-kernel-server -y
- Create a directory for NFS sharing
sudo mkdir -p /var/nfs/shared
sudo chown nobody:nogroup /var/nfs/shared
sudo chmod 777 /var/nfs/shared
- Configure NFS exports
sudo nano /etc/exports
Copy paste the following line
/var/nfs/shared *(rw,sync,no_subtree_check,no_root_squash)
- Restart NFS service
sudo systemctl restart nfs-kernel-server
- Create a firewall rule
gcloud compute firewall-rules create allow-nfs \
--action=ALLOW \
--direction=INGRESS \
--rules=tcp:2049,udp:2049 \
--source-ranges=0.0.0.0/0 \
--target-tags=nfs-server
- Create a client instance (optional if testing on GCE)
gcloud compute instances create nfs-client \
--zone=us-central1-a \
--machine-type=e2-medium \
--image-project=ubuntu-os-cloud \
--image-family=ubuntu-2004-lts
- SSH into the client machine
gcloud compute ssh nfs-client
- Install NFS client tools
sudo apt update
sudo apt install nfs-common -y
- Mount the NFS share
sudo mkdir -p /mnt/nfs/shared
- Replace nfs-server with the external IP of your NFS server (you can find it using gcloud compute instances list) (optional, works well with the instance name)
sudo mount -t nfs nfs-server:/var/nfs/shared /mnt/nfs/shared
- Create files and dirs
/mnt/nfs/shared
- To make the NFS mount persistent across reboots on the client, edit the /etc/fstab file on the client machine:
sudo nano /etc/fstab
Copy paste the following line
nfs-server:/var/nfs/shared /mnt/nfs/shared nfs defaults 0 0
- Publicly accessible NFS shares are not secure. If you want a secure configuration, consider restricting access to specific IP ranges in the firewall or using VPNs for private access.
gcloud compute firewall-rules update allow-nfs --source-ranges=YOUR_IP_RANGE