Opening a shell inside non-systemd nspawn containers

If you try to open shell inside a container that runs e.g. Alpine Linux using machinectl, the following error will appear:

# machinectl shell vpn
Failed to get shell PTY: Protocol error

The reason for this is that the container is not running systemd.

Because systemd-nspawn just uses Linux namespaces 1, nsenter can alternatively be used to access the container. For this, we'll need the PID of the init process inside the container:

# systemctl status systemd-nspawn@vpn
● systemd-nspawn@vpn.service - Container vpn
   Loaded: loaded (/lib/systemd/system/systemd-nspawn@.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-08-11 19:49:19 UTC; 6 months 3 days ago
 Main PID: 795 (systemd-nspawn)
   Status: "Container running."
   CGroup: /machine.slice/systemd-nspawn@vpn.service
           ├─payload
           │ ├─ 797 /sbin/init
           │ ├─1028 /sbin/syslogd -t
           [...]
In this case the PID of init is 797, you can then spawn a login shell inside the container:
nsenter -t 797 -a /bin/sh -l

All in all, this can be turned into a nice alias for your .bashrc:

function center ()
{
      [ -z "$1" ] && { echo "Usage: center <name>" >&2; return 1; }
      pid=$(sed -n 2p "/sys/fs/cgroup/pids/machine.slice/systemd-nspawn@$1.service/tasks")
      [ -z "$pid" ] && { echo "Container not running" >&2; return 1; }
      nsenter -t $pid -a /bin/sh -l
}
1

http://man7.org/linux/man-pages/man7/namespaces.7.html