RHEL7: Access a virtual machine’s console.

Share this link

Note: This is an RHCSA 7 exam objective.

Standard procedure

With KVM, to access the virtual machine’s console under X Window, type:

# virt-manager

If you aren’t under X Window, there is another way to access a virtual machine’s console: you can go through a serial console.

On the virtual machine, add ‘console=ttyS0‘ at the end of the kernel lines in the /boot/grub2/grub.cfg file:

# grubby --update-kernel=ALL --args="console=ttyS0"

Note: Alternatively, you can edit the /etc/default/grub file, add ‘console=ttyS0‘ to the GRUB_CMDLINE_LINUX variable and execute ‘# grub2-mkconfig -o /boot/grub2/grub.cfg‘.

Now, reboot the virtual machine:

# reboot

With KVM, connect to the virtual machine’s console (here vm.example.com):

# virsh console vm.example.com
Connected to domain vm.example.com
Escape character is ^]

Red Hat Enterprise Linux Server 7.0 (Maipo)
Kernel 3.10.0-121.el7.x86_64 on an x86_64

vm login:

Emergency procedure

Sometimes you have lost all links to your virtual machine (error in the /etc/fstab file, ssh configuration, etc) and, as you didn’t set up any virtual console, you are in real trouble. There is still a solution!
Connect to the physical host and shut down your virtual machine (here called vm.example.com):

# virsh destroy vm.example.com

Define where the virtual machine image file is located (by default in the /var/lib/libvirt/images directory with a name like vm.example.com.img):

# virsh dumpxml | grep "source file="
      <source file='/var/lib/libvirt/images/vm.example.com.img'/>      

Map your virtual machine image file into the host environment (-a for add and -v for verbose):

# kpartx -av /var/lib/libvirt/images/vm.example.com.img
add map loop0p1 (253:2): 0 1024000 linear /dev/loop0 2048
add map loop0p2 (253:3): 0 10240000 linear /dev/loop0 1026048

From the previous display, you know that you’ve got two partitions (in fact /boot and /, distinguishable by their respective size).
You need to mount the /boot partition to be able to change the grub configuration:

# mount /dev/mapper/loop0p1 /mnt

Then, edit the /mnt/grub2/grub.cfg file and add ‘console=ttyS0‘ at the end of every line containing /vmlinuz (the linux kernel).
Unmount the partition:

# umount /mnt

Unmap the virtual machine image file (-d for delete and -v for verbose):

# kpartx -dv /var/lib/libvirt/images/vm.example.com.img
del devmap : loop0p2
del devmap : loop0p1
loop deleted : /dev/loop0

Restart your virtual machine:

# virsh start vm.example.com
Domain vm.example.com started

Connect to your virtual machine console:

# virsh console vm.example.com
Connected to domain vm.example.com
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-123.el7.x86_64 on an x86_64

vm login: 

This procedure works for RHEL 6/CentOS 6 and RHEL 7/CentOS 7.

Source: Deepal Jayasinghe’s blog.

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
61 comments on “RHEL7: Access a virtual machine’s console.
  1. abcdef says:

    Changing the values of /boot/grub2/grub.cfg doesn’t/ wouldn’t work. Any tips on getting (virsh console domain) command working?

    [root@station52 ~]# cat /boot/grub2/grub.cfg
    # It is automatically generated by grub2-mkconfig using templates
    # from /etc/grub.d and settings from /etc/default/grub

  2. fernandezcuesta says:

    You may also need to set GRUB_TERMINAL=”console serial” in order to be able to see/control grub from the serial line too.

  3. Oxygen says:

    Could you please advise if all of that needs to be done via console or GUI is allowed? Because if it is, all kickstarts and vms stuff is quite easy as you don’t need to do blind installations and wait just to see how it fails again and again.

  4. ak340 says:

    I CertDepot, I have currently a Centos VM via Virtualbox however I cannot use KVM as I don’t have any vmx/svm CPU flags. Do you know any alternatives where I can practice this objective?
    What I mean is I have a Centos VM and cannot use KVM inside that VM.

    • CertDepot says:

      You can buy a small second-hand PC like a HP N54L that you will be able to reuse as a NAS later on for $250/250Euros. Alternatively, you can rent a host with virtualization capabilities from an hosting company the time you practice your exam objectives. Finally, there are perhaps companies providing this kind of services for little money but I’m simply not aware of it.

    • tom says:

      I am not sure about VirtualBox, but VMware Player supports nested KVM as hypervisor. At least it works for me very well. I have VMware Player running on Windows 8. CentOS7 as guest OS in Player and one more CentOS 7 as guest in host CentOS. You need to set CPU Virtualization Engine to Intel VT-x/EPT. I think VirtualBox also supports this mode.

  5. ak340 says:

    thank you cerdepot! appreciate your concern for us!!!!

  6. ak340 says:

    Hi CertDepot, is there a way to recover the lost root password if I were in serial console?

  7. ak340 says:

    Thank you CertDepot!

  8. ak340 says:

    Hi not getting anything from kpartx, do you have an advice?

    # kpartx -av /var/lib/libvirt/images/centos7.0.qcow2

  9. langtu1511 says:

    Thank CertDepot,

    This is a great blog.

    I am wondering in the exam, can we use GUI for all questions? do I have to use command lines for all questions in the exam?

    With these kind of question (in this blog post), I think it doesn’t matter if we have GUI virt-manager.


  10. Gjorgi says:

    While trying a console connection to a VM, it was very slow, so I don’t know if the following helps:
    * you know username and password of a user on the VM. I’d assume you already have those credentials since you are the one that created the VM.
    * you need to know the IP of that VM so you can ssh into it to fix stuff: arp -n and you’ll see the IP of the VM.
    ssh into it with credentials you have and iron out what needs to be fixed.

  11. Gjorgi says:

    Also, if the VM’s image is in qcow2 format (which it is on CentOS 7 systems) this emergency procedure won’t work. Which is a strong hint that the author of the blog from which this procedure was taken simply copy/pasted someone else’s instructions without ever bothering to test.

    • CertDepot says:

      If there is something true here is I personally test every procedure that I display on my website. And if this procedure comes from somebody else, I write the source of it. After, how can you say that a procedure doesn’t work in any situation? There are 3 different versions (RHEL 7.0, RHEL 7.1, RHEL 7.2), did you test all of them to say that? Are you a KVM expert?
      If you’ve got useful information, offer your knowledge in a positive manner in a comment.

    • redhat0329 says:

      Gjorgi, if you don’t trust all of the information here then don’t visit this site. Simple as that. peace

  12. aaqrabaw says:

    is all stuff mentioned here needed for the RHCSA exam ?

  13. mehboob says:

    HI Certdepot,

    Is this Emergency procedure related to exam objectives..???


  14. misterX says:

    Maybe a workaround for the kpartx / qcow2 problem… It seems that kpartx can not always handle the qcow2 format correctly. Try to convert qcow2 to raw: #qemu-img convert file.qcow2 file.raw -> then edit the VM xml file. Lines “source file=/path/file.raw” and “driver name=qemu type=raw”. Thank you for your http://www.certdepot.net site it’s a huge help!

  15. bashtheshell says:

    Have you tried virt-rescue command? Might be easier than the kpartx command.

  16. smrbukhari says:

    Hello CertDepot,

    Would there be a VM provided with virtual console setup? I just realized that virt-viewer and virt-manager would not work in multi-user target. If this is not the case how do you recommend we login into VM to make the changes in grub2 config as you mentioned above.

    Also how do you exit from virsh console, I’m in non-GUI mode and none of the escape characters are working. Using “logout” or “exit” takes me back to the login prompt again.

    Appreciate your help on this!

    • CertDepot says:

      Don’t worry about the virtual console setup, during the exam you will have access to the console.
      Concerning virt-manager (I’m not used to virt-viewer), I don’t remember having any problem to access the console even in multi-user target.
      Exiting from virsh console is effectively a great pain! Have a look at this SuperUser question. It worked for me, I hope it works for you!

  17. dudniksergei says:

    Here you should provide vm name

    # virsh dumpxml “VM_name”| grep “source file=”

  18. ylemouel says:


    I’m having trouble to run a CentOS iso.
    Here my setup:
    VirtualBox (CentOS 7)running from Mac OSX.
    Running Virtual Machine Manager from CentOS 7 (Virtualbox)
    When I boot CentOS via virt-manager I’m getting MP-bios bug: 8254 timer not connected to IO-APIC

    I disabled I/O APIC from Virtualbox settings.

    What do you think looks like a hardware incompatibility, assuming running 2 VM could be an issue.

    Thanks for your help

    • CertDepot says:

      I have never used Mac OSX, so I’m definitively not an expert in this domain.
      However, I think it is a hardware configuration problem.

    • Lisenet says:

      Try adding “noapic” to the kernel line and see if it boots up.

    • Sam says:

      Er, no that is not a good idea, or can’t be done with any in a good manor.

      What do I mean, running a virt-manager inside virtualbox, causes issues. I would be surprised if it ran at all with most os’s. This is a hardware limitation issue. That’s my guess.

      • ylemouel says:

        So I’m running virtualbox on my Mac then virt-manager from the centOS (virtualbox). SO yes 2 virtualization.

        • Lisenet says:

          Been there, done that. VirtualBox does not have the ability to passthrough the VT-x features to a guest.

        • Sam says:

          I am pretty sure this will not work, unless you do something very odd.

          The way I see it you have 2 options assuming using a mac, and ither way you will need to work on this from other sources. I have no experteese on this and it depends on hardware limits(ram and num CPUs)

          * use virtualbox, using the inbuilt vlan between the various VM

          * Create a Dual boot enviorment. you will have to research this. this is more efficent, a few years ago mac had a simple method of doing this. WARRNING using this method may damage your software BACKUP(s) and check the BACKUPS

          If you can afford it look at getting 2 or 3 raspberry pie,(case, microSD(fast) pi3, and micro usb). Centos now support ARM as of ver 7.1. NOT 7.0. I would not recomend this method yet.

          • Lisenet says:

            OK, just to clarify this one more time, you cannot use VirtualBox to deploy KVM on top of it. However, you can use VirtualBox to set up you home lab and practice at least 95% of the exam tasks.

          • ylemouel says:

            Thanks all, I just wanted to discuss some bit about these VM skills. I’m also learning with Linux Academy Lab.

  19. blahblah says:

    so what does the exam actually look like ? based on sander van vugt’s videos, and the comments around the website, i was under the impression it would be like:
    a desktop, with a GNOME desktop installed, with a virtual machine pre-installed, and the virtual machine may or may not include GNOME.
    is this correct, or wrong ?

  20. Zorbeg says:

    Ok, this answer confuses me a lot. I think console=ttyS0 configuration is beyond scope of exam.

    1. As far as I know Server with GUI is provided on RHCSA right? So virt-manager should be available.

    2. If you are given CLI only access to your test system, it has VM1 installed on it and you can’t ssh to it, how are you supposed to ” add ‘console=ttyS0‘ ” to its grub.cfg?
    If you can SSH to VM1, then you are already on the console, so why bother configuring serial port?

    • CertDepot says:

      You can’t expect to get a GUI during the exam.
      For the rest, in order to access the console during the exam, the only way I see is to reboot the VM.

Leave a Reply

Upcoming Events (Local Time)

There are no events.

RHCSA7: Task of the day

Allowed time: 5 minutes.
Configure a cron task to write the uptime at 2PM every day.

RHCE7: Task of the day

Allowed time: 15 minutes.
Configure a httpd server with a password protected directory under the /var/www/html/private directory.

Follow me on Twitter

Poll for favorite RHEL 7 book

What is your favorite RHEL 7 book to prepare RHCSA & RHCE exams?

View Results

Loading ... Loading ...

Poll for most difficult RHCSA 7 topic

What do you think is the most difficult RHCSA 7 topic?

View Results

Loading ... Loading ...

Poll for most difficult RHCE 7 topic

What do you think is the most difficult RHCE 7 topic?

View Results

Loading ... Loading ...