Chapter 15. Sharing files between the host and its virtual machines
You may frequently require to share data between your host system and the virtual machines (VMs) it runs. To do so quickly and efficiently, you can set up NFS file shares on your system.
15.1. Sharing files between the host and its virtual machines by using NFS
For efficient file sharing between the RHEL 8 host system and the virtual machines (VMs), you can export an NFS share that VMs can mount and access.
Prerequisites
The
nfs-utils
package is installed on the host.# yum install nfs-utils -y
-
Virtual network of
NAT
orbridge
type is configured to connect a host to VMs. - Optional: For improved security, ensure your VMs are compatible with NFS version 4 or later.
Procedure
On the host, export a directory with the files you want to share as a network file system (NFS):
Share an existing directory with VMs. If you do not want to share any of the existing directories, create a new one:
# mkdir shared-files
Obtain the IP address of each VM to share files from the host, for example, testguest1 and testguest2 :
# virsh domifaddr testguest1 Name MAC address Protocol Address ---------------------------------------------------------------- vnet0 52:53:00:84:57:90 ipv4 192.0.2.2/24 # virsh domifaddr testguest2 Name MAC address Protocol Address ---------------------------------------------------------------- vnet1 52:53:00:65:29:21 ipv4 192.0.2.3/24
Edit the
/etc/exports
file on the host and add a line that includes the directory you want to share, IPs of VMs to share, and additional options:/home/<username>/Downloads/<shared_directory>/ <VM1-IP(options)> <VM2-IP(options)> ...
The following example shares the
/usr/local/shared-files
directory on the host with testguest1 and testguest2, and enables the VMs to edit the content of the directory:/usr/local/shared-files/ 192.0.2.2(rw,sync) 192.0.2.3(rw,sync)
NoteTo share a directory with a Windows VM, you need to ensure the Windows NFS client has write permissions in the shared directory. You can use the
all_squash
,anonuid
, andanongid
options in the/etc/exports
file./usr/local/shared-files/ 192.0.2.2(rw,sync,all_squash,anonuid=<directory-owner-UID>,anongid=<directory-owner-GID>)
The <directory-owner-UID> and <directory-owner-GID> are the UID and GID of the local user that owns the shared directory on the host.
For other options to manage NFS client permissions, follow the Securing the NFS service guide.
Export the updated file system:
# exportfs -a
Start the
nfs-server
service:# systemctl start nfs-server
Obtain the IP address of the host system to mount the shared directory on the VMs:
# ip addr ... 5: virbr0: [BROADCAST,MULTICAST,UP,LOWER_UP] mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 52:54:00:32:ff:a5 brd ff:ff:ff:ff:ff:ff inet 192.0.2.1/24 brd 192.0.2.255 scope global virbr0 valid_lft forever preferred_lft forever ...
Note that the relevant network connects the host with VMs to share files. Usually, this is
virbr0
.
Mount the shared directory on a Linux VM that is specified in the
/etc/exports
file:# mount 192.0.2.1:/usr/local/shared-files /mnt/host-share
-
192.0.2.1
: The IP address of the host. -
/usr/local/shared-files
: A file-system path to the exported directory on the host. /mnt/host-share
: A mount point on the VMNoteThe mount point must be an empty directory.
-
To mount the shared directory on a Windows VM as mentioned in the
/etc/exports
file:- Open a PowerShell shell prompt as an Administrator.
Install the
NFS-Client
package on the Windows.To install on a server version, enter:
# Install-WindowsFeature NFS-Client
To install on a desktop version, enter:
# Enable-WindowsOptionalFeature -FeatureName ServicesForNFS-ClientOnly, ClientForNFS-Infrastructure -Online -NoRestart
Mount the directory exported by the host on a Windows VM:
# C:\Windows\system32\mount.exe -o anon \\192.0.2.1\usr\local\shared-files Z:
In this example:
-
192.0.2.1
: The IP address of the host. -
/usr/local/shared-files
: A file system path to the exported directory on the host. Z:
: The drive letter for a mount point.NoteYou must choose a drive letter that is not in use on the system.
-
Verification
List the contents of the shared directory on the VM so that you can share files between the host and the VM:
$ ls <mount_point> shared-file1 shared-file2 shared-file3
In this example, replace <mount_point> with a file system path to the mounted shared directory.
Additional resources