lxc-console
Access the console of a container
TLDR
Start a console in a container
Connect to an lxc console
Exit lxc-console
Display help
SYNOPSIS
lxc-console -n NAME [-t TTY_ID] [-L LOGLEVEL] [-o LOGFILE]
PARAMETERS
-n NAME, --name NAME
Specifies the name of the container to which to connect the console. This is a mandatory option.
-t TTY_ID, --tty TTY_ID
Specifies the TTY ID to connect to. The default is 0, which typically corresponds to the container's main console. Other IDs (e.g., 1, 2, etc.) may connect to additional virtual terminals if configured within the container.
-s, --session ID
Attach to an existing console session by its ID. This is typically used internally or for specific advanced use cases.
-L LOGLEVEL, --loglevel LOGLEVEL
Sets the log level for `lxc-console` itself. Common levels include `TRACE`, `DEBUG`, `INFO`, `NOTICE`, `WARN`, `ERROR`, and `CRIT`.
-o LOGFILE, --logfile LOGFILE
Specifies a file to write log messages to. If not specified, logs are typically written to stderr or syslog depending on configuration.
--help
Displays a help message and exits.
--version
Shows version information and exits.
DESCRIPTION
The `lxc-console` command provides a direct virtual console connection to a running Linux Container (LXC). It emulates connecting a physical terminal to the container's standard TTYs, allowing low-level interaction, similar to accessing a server via serial console or directly from a hypervisor's console.
Unlike `lxc-exec` or `lxc-attach`, which are designed for executing commands or attaching to namespaces, `lxc-console` focuses on persistent, interactive console sessions. This is particularly useful for debugging container startup issues, interacting with init systems (like Systemd or OpenRC within the container) that manage TTYs, or when network access to the container's SSH server isn't available or configured.
By default, it connects to TTY 0, but can be directed to other TTYs if the container's configuration and init system support them. It provides a robust method for deep interaction with the container's operating system environment.
CAVEATS
Connecting to a container's console via `lxc-console` requires the target container to be in a running state. You generally need root privileges or appropriate capabilities (`CAP_SYS_ADMIN`) to use this command, as it interacts directly with container resources.
Exiting the `lxc-console` session often requires a specific escape sequence, commonly Ctrl+a followed by q (i.e., `Ctrl+a`, then `q`). Simply typing `exit` or `Ctrl+d` inside the console might only exit the shell within the container, not detach the `lxc-console` session, potentially leaving the TTY in an unexpected state if the shell restarts.
The availability and behavior of specific TTYs within the container depend heavily on the container's configuration and its init system (e.g., `systemd`, `sysvinit`). If the container's init system doesn't manage or provide TTYs as expected, `lxc-console` might not function as intended or might only connect to a non-interactive TTY.
EXITING THE CONSOLE
Unlike standard SSH or terminal sessions, exiting `lxc-console` typically requires an escape sequence. The most common sequence is pressing Ctrl+a, releasing it, and then pressing q. This sequence sends a signal to the `lxc-console` client to detach from the container's TTY, returning control to the host shell. Failure to use the correct escape sequence may leave the `lxc-console` process running in the background or require manual termination.
COMPARISON WITH `LXC-ATTACH` AND `LXC-EXEC`
While all three commands allow interaction with a running container, their methods differ significantly:
- `lxc-console`: Connects directly to a specific virtual TTY (e.g., `/dev/tty0`) within the container, behaving like a physical console. It's best for interacting with the container's init system or low-level services.
- `lxc-attach`: Executes a command or provides a shell by attaching to the container's existing namespaces (PID, network, mount, etc.). It runs processes as though they were part of the container's existing process tree.
- `lxc-exec`: Executes a command inside the container's environment, often creating a new process that runs within the container's specific Cgroups and namespaces, but typically does not attach to existing TTYs. It's often preferred for one-off commands or script execution within the container.
HISTORY
The `lxc-console` command is a core component of the LXC (Linux Containers) project, which predates more widely known container runtimes like Docker. LXC began development in the mid-2000s, leveraging Linux kernel features like control groups (cgroups) and namespaces to provide OS-level virtualization. `lxc-console` has been a fundamental utility since the early days of LXC, offering a direct, low-level access mechanism to running containers, mirroring the experience of connecting to a physical server's console. Its utility remains significant for debugging and system administration in environments heavily reliant on LXC as a lightweight virtualization solution.
SEE ALSO
lxc(1), lxc-attach(1), lxc-exec(1), lxc-start(1), lxc-stop(1), lxc.conf(5)