使用virsh console连接虚拟机

在严格的网络环境中,virsh console提供了一种替代远程桌面的安全连接虚拟机方式。当kernel启动时不支持ttyS0时,需在GRUB启动选项中添加参数来启用console通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在网络控制的比较严格的情况下,远程桌面等连接虚拟机的方式可能会受到限制。virsh有一个现有的命令,可以在需要的时候应应急

[root@pc ~]# virsh help console
  NAME
    console - connect to the guest console

  SYNOPSIS
    console <domain> [--devname <string>] [--force] [--safe]

  DESCRIPTION
    Connect the virtual serial console for the guest

  OPTIONS
    [--domain] <string>  domain name, id or uuid
    --devname <string>  character device name
    --force          force console connection (disconnect already connected sessions)
    --safe           only connect if safe console handling is supported

但是默认的情况下,执行virsh console dom_name的时候,会发现hang住,没有任何反应。

原因是kernel在启动的时候并没有打开对ttyS0的支持,console其实是通过这个字符设备进行通信的。只需要简单的在grub对应的选项里面加上启动参数即可:

[root@cent65 ~]# cat /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/vg_cent65-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/vda
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/vg_cent65-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_LVM_LV=vg_cent65/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_LVM_LV=vg_cent65/lv_root rd_NO_DM rhgb quiet <span style="font-size:18px;color:#ff0000;"><strong>console=ttyS0</strong></span>

不管是grub还是grub2,只要在对应处进行设置就可以正常连接了。

[root@pc ~]# virsh console cent65
Connected to domain cent65
Escape character is ^]
root
Password: 
Last login: Mon May 19 15:40:28 on ttyS0


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值