KVM Virsh Console Access to Linux VM – CentOS 6

virsh console grub ttyS0

This document will show you how to get virsh console access to guest virtual machines.

The console command within virsh is an excellent feature to have working for your virtual environments. For example, it can be really handy to watch a machine boot without launching a GUI tool ie. virt-manager. I personally find virsh console access a quicker way to configure networking settings which may not be able to be achieved via SSH, other than using virt-manager.

Just note that this document assumes CentOS 6 for all example code and references. Syntax, file locations and codes may vary based on your distribution.

Typically out of the box, when you install a new CentOS 6 virtual machine, the “virsh console” command will not work…

Step 1: Configure Serial Terminal

On your new CentOS 6 virtual machine, you’ll need to configure ttyS0. This serial interface is how “virsh console” gains access to your virtual machine.

Log into your virtual machine…

Create new ttyS0 config file

vi /etc/init/ttyS0.conf

Copy/Paste the following config

stop on runlevel[016]
start on runlevel[345]
respawn
instance /dev/ttyS0
exec /sbin/mingetty /dev/ttyS0

Step 2: Allow login into ttyS0

By default CentOS will not allow a user to login via ttyS0 unless we modify securetty.

echo ttys0 >> /etc/securetty

Step 3: Start ttyS0

Make ttyS0 available, from your terminal execute the following command

initctl start ttyS0
ttyS0 (/dev/ttyS0) start/running, process 1487

Step 4: Test Virsh Console

From your KVM server, connect to the console of your virtual machine

# virsh console centos_template

Connected to domain centos_template
Escape character is ^]
CentOS release 6.3 (Final)
Kernel 2.6.32-279.14.1.el6.x86_64 on an x86_64

localhost.localdomain login:

Step 5: Configure Access to Boot Output

To watch your virtual machines boot/shutdown messages we need to make a couple of changes to your boot process.
Edit your grub config

vi /etc/grub.conf

Your kernel entry may look something like this

kernel /vmlinuz-2.6.32-279.14.1.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root 
rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap 
SYSFONT=latarcyrheb-sun16 crashkernel=auto 
rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us 
rd_NO_DM rhgb quiet

You’ll want to remove the “rhgb” option, this is the boot splash screen. The “quiet” entry hides a lot of boot messages, I remove so more detail is outputted.
Finally you’ll want to add “console=ttyS0” to send the boot messages to your virsh console. Your kernel line may now look like this

kernel /vmlinuz-2.6.32-279.14.1.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root 
rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap 
SYSFONT=latarcyrheb-sun16 crashkernel=auto 
rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us 
rd_NO_DM console=ttyS0