DEV Community

loading...

How to create New VM in CLI

K Dhanesh
Infra Coder Interested in Public & Private Cloud, DC experience in Sublime IP, Equinix | AWS, Python, Ruby on Rails | Automation Using Ansible Docker Capistrano
・2 min read

KVM to Create New VM using CLI Commands, Tested in ubuntu 16.04 its working fine

sudo virt-install \
--virt-type=kvm \
--name centos7 \
--ram 6144 \
--vcpus=8 \
--os-variant=rhel7 \
--hvm \
--cdrom=/RootPartitions/CentOS-7-x86_64-DVD-1810.iso \
--network network=default,model=virtio \
--graphics vnc \
--disk path=/RootPartitions/centos7-0.1.img,size=30,bus=virtio


# Spin a New VM with Bridge Network

sudo virt-install \
--virt-type=kvm \
--name centos7 \
--ram 6144 \
--vcpus=8 \
--os-variant=rhel7 \
--hvm \
--cdrom=/RootPartitions/CentOS-7-x86_64-DVD-1810.iso \
--network bridge=br0,model=virtio \
--graphics vnc \
--disk path=/RootPartitions/centos7.img,size=30,bus=virtio
Enter fullscreen mode Exit fullscreen mode

Basic Virsh Commands

sudo virsh list --all. # to all the VM in the Host Machine 
sudo virsh destroy <vm-nam> # Stopping or Shutting down the VM
sudo virsh dumpxml vm-name > vm-name.xml # Export the VM setting in XML Formate
sudo cat vm-name.xml | grep source # to list the attached Partition
sudo lvremove /partition/name # remove the LVM Partition 
sudo rm -rf /RootParition/Centos.img # To remove the .img and .qcow2 partitions
sudo virsh undefine <vm-name> # Removing the VM from the Host Machine, before execute
sudo virsh vncdisplay # to display the VNC port number
Enter fullscreen mode Exit fullscreen mode

How to Connect the Private VM from the Local machine

Host Machine should have the Public and Private Network and VM should configured via bridge network from Privat network.

Using SSH tunnel we can connect the VM, before pls install VNC viewer in your local - https://www.realvnc.com/en/connect/download/viewer/

From Local Machine

ssh -L localhost:5900:VM-IP:VNC-Port-Number username@hostmachine.com
Enter fullscreen mode Exit fullscreen mode

For Example

HostMachine: dhanesh.vm-test.com (Public IP)
HostMachine: UserName: dhanesh
VM PrivateIP: 192.168.100.100
VM VNC Port: 12

ssh -L localhost:5900:192.168.100.100:5912 dhanesh@dhanesh.vm-test.com
Enter fullscreen mode Exit fullscreen mode

Finally open the VNC viewer and type localhost:5900 you can able to see the VM from your local machine.

Ensure that all the Network settings are working fine.

Discussion (0)