How to Upload a File From a Host to a Virtual Machine
Cerise Chapeau Training
A Red Lid training course is available for RHEL 8
Affiliate xiv. Sharing files betwixt the host and its virtual machines
You may oftentimes require to share data between your host system and the virtual machines (VMs) it runs. To practice so speedily and efficiently, you can set upwardly NFS or Samba file shares on your system.
fourteen.i. Sharing files between the host and Linux virtual machines
For efficient file sharing between your host system and the Linux VMs it is connected to, you tin export an NFS share that your VMs can mount and access.
Prerequisites
- The
nfs-utils
packet is installed on the host. -
A directory that y'all want to share with your VMs. If you exercise not want to share any of your existing directories, create a new one, for example named shared-files .
# mkdir shared-files
-
The host is visible and reachable over a network for the VM. This is mostly the case if the VM is connected using the NAT and span type of virtual networks. However, for the macvtap connectedness, you must offset set up the macvlan characteristic on the host. To do then:
-
Create a network device file in the host's
/etc/systemd/network/
directory, for example chosenvm-macvlan.netdev
.# vim /etc/systemd/network/ vm-macvlan.netdev
-
Edit the network device file to accept the post-obit content. You tin can supervene upon
vm-macvlan
with the name you chose for your network device.[NetDev] Name= vm-macvlan Kind=macvlan [MACVLAN] Manner=bridge
-
Create a network configuration file for your macvlan network device, for example
vm-macvlan.network
.# vim /etc/systemd/network/ vm-macvlan.network
-
Edit the network configuration file to have the following content. You tin can replace
vm-macvlan
with the name you chose for your network device.[Friction match] Name=_vm-macvlan_ [Network] IPForward=yep Accost=192.168.250.33/24 Gateway=192.168.250.1 DNS=192.168.250.one
-
Create a network configuration file for your physical network interface. For example, if your interface is
enp4s0
:# vim /etc/systemd/network/enp4s0.network
If you lot are unsure what interface name to use, you can utilise the
ifconfig
command on your host to obtain the list of active network interfaces. -
Edit the physical network configuration file to make the physical network a part of the macvlan interface, in this instance vm-macvlan :
[Friction match] Name=enp4s0 [Network] MACVLAN=vm-macvlan
- Reboot your host.
-
- Optional: For improved security, ensure your VMs are compatible with NFS version 4 or afterwards.
Process
-
On the host, export a directory with the files you want to share as a network file arrangement (NFS).
-
Obtain the IP address of each virtual machine you want to share files with. The following instance obtains the IPs of testguest1 and testguest2 .
# virsh domifaddr testguest1 Name MAC address Protocol Address ---------------------------------------------------------------- vnet0 52:53:00:84:57:90 ipv4 192.168.124.220 /24 # virsh domifaddr testguest2 Name MAC address Protocol Address ---------------------------------------------------------------- vnet1 52:53:00:65:29:21 ipv4 192.168.124.17 /24
-
Edit the
/etc/exports
file on the host and add a line that includes the directory you desire to share, IPs of VMs y'all desire to share with, and sharing options.Shared directory VM1-IP(options) VM2-IP(options) [...]
For instance, the following 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.168.124.220(rw,sync) 192.168.124.17(rw,sync)
-
Export the updated file system.
# exportfs -a
-
Ensure the NFS procedure is started:
# systemctl offset nfs-server
-
Obtain the IP address of the host system. This will be used for mounting the shared directory on the VMs later.
# ip addr [...] 5: virbr0: [Broadcast,MULTICAST,Upward,LOWER_UP] mtu 1500 qdisc noqueue state UP grouping default qlen one thousand link/ether 52:54:00:32:ff:a5 brd ff:ff:ff:ff:ff:ff inet 192.168.124.1/24 brd 192.168.124.255 scope global virbr0 valid_lft forever preferred_lft forever [...]
Notation that the relevant network is the one being used use for connection to the host by the VMs yous want to share files with. Unremarkably, this is
virbr0
.
-
-
On the guest OS of a VM specified in the
/etc/exports
file, mount the exported file system.-
Create a directory you lot want to utilize as a mount indicate for the shared file system, for example
/mnt/host-share
:# mkdir /mnt/host-share
-
Mountain the directory exported by the host on the mount point. This example mounts the
/usr/local/shared-files
directory exported by the192.168.124.ane
host on/mnt/host-share
in the guest:# mount 192.168.124.i:/usr/local/shared-files /mnt/host-share
-
Verification
-
To verify the mount has succeeded, access and explore the shared directory on the mount bespeak:
# cd /mnt/host-share # ls shared-file1 shared-file2 shared-file3
14.2. Sharing files between the host and Windows virtual machines
For efficient file sharing between your host organization and the Windows VMs it is continued to, yous tin prepare a Samba server that your VMs can access.
Prerequisites
-
The
samba
packages are installed on your host. If they are not:# yum install samba
-
The host is visible and reachable over a network for the VM. This is mostly the case if the VM is continued using the NAT and bridge type of virtual networks. However, for the macvtap connection, yous must offset ready the macvlan feature on the host. To practice then:
-
Create a network device file, for example chosen
vm-macvlan.netdev
in the host's/etc/systemd/network/
directory.# vim /etc/systemd/network/ vm-macvlan.netdev
-
Edit the network device file to accept the following content. Y'all can replace
vm-macvlan
with the name you lot chose for your network device.[NetDev] Name= vm-macvlan Kind=macvlan [MACVLAN] Mode=span
-
Create a network configuration file for your macvlan network device, for example
vm-macvlan.network
.# vim /etc/systemd/network/ vm-macvlan.network
-
Edit the network configuration file to accept the post-obit content. Yous tin can supersede
vm-macvlan
with the proper name yous chose for your network device.[Match] Name=_vm-macvlan_ [Network] IPForward=yes Accost=192.168.250.33/24 Gateway=192.168.250.1 DNS=192.168.250.ane
-
Create a network configuration file for your physical network interface. For example, if your interface is
enp4s0
:# vim /etc/systemd/network/enp4s0.network
If you are unsure what interface to apply, you can use the
ifconfig
command on your host to obtain the list of active network interfaces. -
Edit the physical network configuration file to make the physical network a function of the macvlan interface, in this example vm-macvlan :
[Friction match] Name=enp4s0 [Network] MACVLAN=vm-macvlan
- Reboot your host.
-
Procedure
-
On the host, create a Samba share and go far accessible for external systems.
-
Add firewall permissions for Samba.
# firewall-cmd --permanent --zone=public --add-service=samba success # firewall-cmd --reload success
-
Edit the
/etc/samba/smb.conf
file:-
Add together the following to the
[global]
section:map to invitee = Bad User
-
Add the following at the end of the file:
#=== Share Definitions === [VM-share] path = /samba/VM-share browsable = yeah guest ok = yes read only = no hosts allow = 192.168.122.0/24
Note that the
hosts let
line restricts the accessibility of the share only to hosts on the VM network. If you want the share to exist accessible by anyone, remove the line.
-
-
Create the
/samba/VM-share
directory.# mkdir -p /samba/VM-share
-
Enable the Samba service.
# systemctl enable smb.service Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.
-
Restart the Samba service.
# systemctl restart smb.service
-
Allow the
VM-share
directory to be attainable and modifiable for the VMs.# chmod -R 0755 /samba/VM-share/ # chown -R nobody:nobody /samba/VM-share/
-
Add the SELinux Samba sharing label to
/etc/samba/VM-share/
# chcon -t samba_share_t /samba/VM-share/
-
-
On the Windows guest operating organisation, attach the Samba share as a network location.
- Open the File Explorer and right-click "This PC".
-
In the context card, click
Add a network location
. - In the Add Network Location wizard that opens, select "Choose a custom network location" and click .
-
In the "Internet or network address" field, type host-IP /VM-share, where host-IP is the IP address of the host. Usually, the host IP is the default gateway of the VM. Later on, click .
- When the sorcerer asks if you lot want to rename the shared directory, keep the default name. This ensures the consistency of file sharing configuration across the VM and the guest. Click .
- If accessing the network location was successful, you can now click and open the shared directory.
Source: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_and_managing_virtualization/sharing-files-between-the-host-and-its-virtual-machines_configuring-and-managing-virtualization
0 Response to "How to Upload a File From a Host to a Virtual Machine"
Publicar un comentario