当前位置 : 主页 > 操作系统 > centos >

KVM 通过virsh console连入虚拟机

来源:互联网 收集:自由互联 发布时间:2022-06-20
新安装一台虚拟机后,是无法通过virsh console 命令连入虚拟机中的,这时我们需要开启虚拟机的console功能。 RHEL/CentOS6 一、添加ttyS0的许可,允许root登陆 [root@Inf-c6-n0 ~]# echo "ttyS0" /etc/s

新安装一台虚拟机后,是无法通过virsh console 命令连入虚拟机中的,这时我们需要开启虚拟机的console功能。

RHEL/CentOS6

一、添加ttyS0的许可,允许root登陆

[root@Inf-c6-n0 ~]# echo "ttyS0" >> /etc/securetty

 

二、编辑/etc/grub.conf

加入console=ttyS0

[root@Inf-c6-n0 ~]# 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/c01-root # initrd /initrd-[generic-]version.img #boot=/dev/vda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS 6 (2.6.32-754.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/mapper/c01-root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=c01/root rd_LVM_LV=c01/swap KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet console=ttyS0 initrd /initramfs-2.6.32-754.el6.x86_64.img

三、编辑/etc/inittab

在最后一行加入内容 S0:12345:respawn:/sbin/agetty ttyS0 115200

[root@Inf-c6-n0 ~]# cat /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:3:initdefault: S0:12345:respawn:/sbin/agetty ttyS0 115200

四、重启服务器

[root@Inf-c6-n0 ~]# reboot

Broadcast message from root@Inf-c6-n0
(/dev/pts/0) at 15:06 ...

The system is going down for reboot NOW!

五、在宿主机上测试连接

[root@SIT-KVM01 ~]# virsh console centos6-128.132 Connected to domain centos6-128.132 Escape character is ^] Welcome to CentOS Starting udev: [ OK ] Setting hostname Inf-c6-n0: [ OK ] Setting up Logical Volume Management: 3 logical volume(s) in volume group "c01" now active [ OK ] Checking filesystems Checking all file systems. [/sbin/fsck.ext4 (1) -- /] fsck.ext4 -a /dev/mapper/c01-root /dev/mapper/c01-root: clean, 48174/3276800 files, 545673/13107200 blocks [/sbin/fsck.ext4 (1) -- /boot] fsck.ext4 -a /dev/vda1 /dev/vda1: clean, 39/65536 files, 22134/262144 blocks [/sbin/fsck.ext4 (1) -- /var] fsck.ext4 -a /dev/mapper/c01-data /dev/mapper/c01-data: clean, 1382/1966080 files, 264494/7864320 blocks [ OK ] Remounting root filesystem in read-write mode: [ OK ] Mounting local filesystems: [ OK ] Enabling local filesystem quotas: [ OK ] Enabling /etc/fstab swaps: [ OK ] Entering non-interactive startup Calling the system activity data collector (sadc)... Starting monitoring for VG c01: 3 logical volume(s) in volume group "c01" monitored [ OK ] Loading OpenIB kernel modules:[ OK ] ip6tables: Applying firewall rules: [ OK ] iptables: Applying firewall rules: [ OK ] Bringing up loopback interface: [ OK ]

注:按 ctrl+] 组合键退出virsh console

RHEL/CentOS7

在虚拟机里面执行

[root@inf-c7-n1 ~]# grubby --update-kernel=ALL --args="console=ttyS0" [root@inf-c7-n1 ~]# [root@inf-c7-n1 ~]# [root@inf-c7-n1 ~]# [root@inf-c7-n1 ~]# reboot

在宿主机上测试

[root@SIT-KVM01 ~]# virsh console centos7-128.131 Connected to domain centos7-128.131 Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-862.el7.x86_64 on an x86_64 inf-c7-n1 login: root Password: Last login: Mon Aug 30 14:15:57 from 10.199.5.128 [root@inf-c7-n1 ~]# [root@inf-c7-n1 ~]# [root@inf-c7-n1 ~]# [root@inf-c7-n1 ~]# exit logout CentOS Linux 7 (Core) Kernel 3.10.0-862.el7.x86_64 on an x86_64 inf-c7-n1 login: CentOS Linux 7 (Core) Kernel 3.10.0-862.el7.x86_64 on an x86_64 inf-c7-n1 login: [root@SIT-KVM01 ~]# RHEL/CentOS8

在虚拟机里执行

[root@inf-r8-n0 ~]# grubby --update-kernel=ALL --args="console=ttyS0" [root@inf-r8-n0 ~]# [root@inf-r8-n0 ~]# reboot

在宿主机上测试

[root@localhost vmfs]# virsh start Rocky8 Domain Rocky8 started [root@localhost vmfs]# virsh console Rocky8 Connected to domain Rocky8 Escape character is ^] [ OK ] Started Show Plymouth Boot Screen. [ OK ] Started Forward Password Requests to Plymouth Directory Watch. [ OK ] Reached target Paths. [ OK ] Found device /dev/mapper/r01-root. [ OK ] Reached target Initrd Root Device. [ OK ] Found device /dev/mapper/r01-swap. Starting Resume from hibernation using device /dev/mapper/r01-swap... [ OK ] Started Resume from hibernation using device /dev/mapper/r01-swap. [ OK ] Reached target Local File Systems (Pre). [ OK ] Reached target Local File Systems. Starting Create Volatile Files and Directories... [ OK ] Started dracut initqueue hook. Starting File System Check on /dev/mapper/r01-root... [ OK ] Reached target Remote File Systems (Pre). [ OK ] Reached target Remote File Systems.

 

网友评论