KVM 通過virsh console連入虛擬機器

xie仗劍天涯發表於2016-12-23

新安裝一臺虛擬機器後,是無法通過virsh console 命令連入虛擬機器中的,這時我們需要開啟虛擬機器的console功能。

一、新增ttyS0的許可,允許root登陸

[root@localhost ~]# echo "ttyS0" >> /etc/securetty

二、編輯/etc/grub.conf中加入console=ttyS0

[root@localhost ~]# less /etc/grub.conf 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.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=128M rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet console=ttyS0
        initrd /initramfs-2.6.32-431.el6.x86_64.img
[root@localhost ~]#

三、編輯/etc/inittab,在最後一行加入內容 S0:12345:respawn:/sbin/agetty ttyS0 115200

[root@localhost ~]# less /etc/inittab 
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
id:5:initdefault:

S0:12345:respawn:/sbin/agetty ttyS0 115200
[root@localhost ~]#

四、重啟伺服器

[root@localhost ~]# reboot

五、在宿主機上測試連線

[root@study ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 4     centos                         running

[root@study ~]# virsh console 4
Connected to domain centos
Escape character is ^]

CentOS release 6.5 (Final)
Kernel 2.6.32-431.el6.x86_64 on an x86_64

localhost.localdomain login: root
Password: 
Last login: Thu Oct 13 02:51:30 on ttyS0
[root@localhost ~]#

注:按 ctrl+] 組合鍵退出virsh console

 

對於centos 7

在虛機裡執行

[root@localhost ~]# grubby --update-kernel=ALL --args="console=ttyS0"

then

[root@localhost ~]# reboot

即可

相關文章