KVM 通过virsh console连入虚拟机

新安装一台虚拟机后,是无法通过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

即可

posted @ 2016-12-23 16:16 xie仗剑天涯 阅读(...) 评论(...) 编辑 收藏