"Failed to get shell PTY: Protocol error" for an nspawn container with systemd inside

Solution 1:

It looks like I was missing dbus inside the container.

# chroot /var/lib/machines/test apt install dbus

Or using from the beginning:

# debootstrap --variant=minbase --include=systemd,iproute2 buster "/var/lib/machines/test"

both solves the problem.

Solution 2:

Tutorial

Create Debian container

This step creates the minimal container including the two essential packages enabling effortless "machinectl login debiancontainer" later on, without any "Failed to get login PTY" errors. Notice the absense of minbase (this avoids errors when using passwd).

debootstrap --include=systemd,dbus unstable /var/lib/machines/debiancontainer

Set root password

This one-time step configures the root password inside the new container.

systemd-nspawn --machine debiancontainer

Example output that should follow

 systemd-nspawn --machine debiancontainer 
Spawning container debiancontainer on /var/lib/machines/debiancontainer.
Press ^] three times within 1s to kill container.
root@debiancontainer:~#

Now just execute and finish with exit or CTRL-D

root@debiancontainer:~# passwd
New password: 
Retype new password: 
passwd: password updated successfully
root@debiancontainer:~# 
logout

Start container using machinectl

Note: These commands can be used as normal user, so provide the applicable password in the requestor dialog.

machinectl start debiancontainer
machinectl list

Example output:

local@yourhost:~$ machinectl list
MACHINE         CLASS     SERVICE        OS     VERSION ADDRESSES
debiancontainer container systemd-nspawn debian -       -        

1 machines listed.

Login using machinectl

Example output

$ machinectl login debiancontainer
Connected to machine debiancontainer. Press ^] three times within 1s to exit session.

Debian GNU/Linux bookworm/sid yourhost pts/1

yourhost login: root
Password: 
Linux yourhost 5.13.19-2 #1 SMP 5.13.19-4 (Mon, 29 Nov 2021 12:10:09 +0100) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
root@yourhost:~#