使用介绍:
[root@kvm1 ~]# virsh console --help NAME 控制台 - 连接到客户机控制台 SYNOPSIS console <domain> [--devname <string>] [--force] [--safe] DESCRIPTION 为虚拟机连接虚拟串行控制台 OPTIONS [--domain] <string> domain name, id or uuid --devname <string> 字符设备名称 --force 强制连接控制台(断开已连接的会话) --safe 只有在具备安全操作控制台的相关支持的情况下,才可进行连接。
使用一台新安装的虚拟机测试
无法进入到虚拟机的控制台;原因是:虚拟机没有正确配置串口设备
[root@kvm1 ~]# virsh list Id Name State --------------------------- 3 centos7.9 running 。/ [root@kvm1 ~]# virsh console centos7.9 Connected to domain centos7.9 Escape character is ^]
为已经安装好的虚拟机配置串口
我的虚拟机是
centos7.9
,也测试过ubuntu22.04
步骤也是一样的;这里必须能够进入到虚拟机,使用ip连接到虚拟机或者使用virt-manger在内核启动参数中添加
console=ttyS0
参数,添加完重启系统
grubby --update-kernel=ALL --args="console=ttyS0"
启动getty 程序
# systemd 会在 ttyS0 端口上启动 getty 程序。getty 程序会监听该端口,等待用户输入用户名和密码。 [root@localhost ~]# systemctl start serial-getty@ttyS0 [root@localhost ~]# systemctl enbale serial-getty@ttyS0 [root@localhost ~]# systemctl status serial-getty@ttyS0 ● serial-getty@ttyS0.service - Serial Getty on ttyS0 Loaded: loaded (/usr/lib/systemd/system/serial-getty@.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2025-06-24 05:03:42 EDT; 1min 12s ago Docs: man:agetty(8) man:systemd-getty-generator(8) http://0pointer.de/blog/projects/serial-console.html Main PID: 8627 (agetty) CGroup: /system.slice/system-serial\x2dgetty.slice/serial-getty@ttyS0.service └─8627 /sbin/agetty --keep-baud 115200,38400,9600 ttyS0 vt220 Jun 24 05:03:42 localhost.localdomain systemd[1]: serial-getty@ttyS0.service holdoff time over, scheduling restart. Jun 24 05:03:42 localhost.localdomain systemd[1]: Stopped Serial Getty on ttyS0. Jun 24 05:03:42 localhost.localdomain systemd[1]: Started Serial Getty on ttyS0.
使用宿主机来连接虚拟机
virsh console centos7.9
敲一下回车进入输入账号密码界面,使用ctrl + ]
退出虚拟机[root@kvm1 ~]# virsh list Id Name State --------------------------- 3 centos7.9 running [root@kvm1 ~]# virsh console centos7.9 Connected to domain centos7.9 Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-1160.el7.x86_64 on an x86_64 localhost login: root Password: Last login: Tue Jun 24 05:03:30 on ttyS0 [root@localhost ~]# [root@kvm1 ~]#
在创建虚拟机时添加
console=ttyS0
内核参数virt-install \ --name centos7.9 \ --os-type linux \ --os-variant centos7.0 \ --memory 2048 \ --vcpus 1 \ --disk size=20 \ --location /var/lib/libvirt/images/CentOS-7-x86_64-DVD-2009.iso \ --network bridge=br0,model=virtio \ --initrd-inject /root/centos79.cfs \ --extra-args="ks=file:/centos79.cfs console=ttyS0" \ --graphics vnc \ --noautoconsole
--extra-args="ks=file:/centos79.cfs console=ttyS0"
: 添加内核参数,不但指定了ks文件,而且添加了串口ks文件内容如下:
#version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom # Use graphical install text # Run the Setup Agent on first boot firstboot --enable ignoredisk --only-use=vda # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 # Network information network --bootproto=dhcp --device=eth0 --ipv6=auto --no-activate network --hostname=localhost.localdomain # Root password rootpw --iscrypted $6$6RNXJjFR0En6Sxwj$21fidwn6Mt1BPl9rMzJCg34DxtrEUHbjeWrirBAkyt45BC8HJsR6Cw0BJ5roT0U2QBAvucc23MEjA.NvTsues. # System services services --enabled="chronyd" # System timezone timezone America/New_York --isUtc # System bootloader configuration bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=vda # Partition clearing information clearpart --all --initlabel # Disk partitioning information part /boot --fstype="xfs" --ondisk=vda --size=300 part / --fstype="xfs" --ondisk=vda --grow %packages @^minimal @core chrony kexec-tools %end %addon com_redhat_kdump --enable --reserve-mb='auto' %end %anaconda pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty %end reboot
在宿主机上使用
virsh consle
来连接虚拟机
可以看出是正常连接的.[root@kvm1 ~]# virsh console centos7.9 Connected to domain centos7.9 Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-1160.el7.x86_64 on an x86_64 192 login: root Password: Last login: Tue Jun 24 09:57:02 from 192.168.25.1
查看
getty 程序
是否启动,ttyS0
端口是否被监听如下可以看出,
在使用virsh-install 安装 虚拟机时在内核 console=ttyS0,会自动启动getty 程序
[root@192 ~]# systemctl status serial-getty@ttyS0 ● serial-getty@ttyS0.service - Serial Getty on ttyS0 Loaded: loaded (/usr/lib/systemd/system/serial-getty@.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2025-06-24 11:42:34 EDT; 1min 38s ago Docs: man:agetty(8) man:systemd-getty-generator(8) http://0pointer.de/blog/projects/serial-console.html Main PID: 565 (agetty) CGroup: /system.slice/system-serial\x2dgetty.slice/serial-getty@ttyS0.service └─565 /sbin/agetty --keep-baud 115200,38400,9600 ttyS0 vt220 Jun 24 11:42:34 localhost.localdomain systemd[1]: Started Serial Getty on ttyS0.
查看内核参数
总结: 启动串口需要 在内核启动参数种添加
console=ttyS0
,需要启动serial-getty@ttyS0
ttyS0
串口编号,补充:串口端口号必须在/etc/securetty
里面。[root@192 ~]# cat /etc/grub2.cfg | grep "console=ttyS0" linux16 /vmlinuz-3.10.0-1160.el7.x86_64 root=UUID=237e423d-356b-4352-9bc0-19e1321b4d79 ro crashkernel=auto spectre_v2=retpoline console=ttyS0 LANG=en_US.UTF-8 linux16 /vmlinuz-0-rescue-1d166deb1edb4402b9b9267d1f2fee03 root=UUID=237e423d-356b-4352-9bc0-19e1321b4d79 ro crashkernel=auto spectre_v2=retpoline console=ttyS0