virsh console is a jarring experience. If you're running a Linux VM with libvirt, consider using virsh lxc-enter-namespace instead. I'll show you why and how.
The left screen shows two shells connecting to the same VM and you can already see that:
The escape method departs from shell norms (exit or ctrl+D)
It forces you to explicitly log in each time without telling you
The second connection doesn't display anything and writes to the first
What you can't see is that:
You need to explicitly log in but it doesn't tell you
Going over 80 characters messes up the display
I assume that these things are done for compatibility reasons as libvirt is popular and broad in scope. However, when it's not being annoying it's brazen and dangerous in how it runs against the grain of your expectations.
However, virsh can also run your shell of choice within the VM with lxc-enter-namespace. It will your expectations by leveraging your shell's familiar environment. The tool I built on the right is using that on the back end. The images above paint a stark contrast, I think.
To do this, I use this line:
sudo virsh -c lxc:/// lxc-enter-namespace <vm-id> --noseclabel -- /usr/bin/sudo -i -H -u <user> -- <command>
If you leave <command> blank then it will just load up your user's shell.
I would love to know why this is so difficult. Why can't I just run virsh shell <vm-id> --as-user <user><command>? I suggest that you toss that command into a script and use it instead of virsh console.
This trick has saved our team a lot of pain and opened up new possibilities for automation. Hopefully it helps someone else too.













