feat(system_baseline): optional tmux session on SSH login #122

Merged
Defame1297 merged 5 commits from feat/system-baseline-tmux into main 2026-11-14 19:50:16 +00:00
Collaborator

What

An opt-in tmux session on SSH login for system_baseline, so a dropped connection loses nothing.

  • tmux is in the ubuntu_lxc and ubuntu_vm profiles (not pve).
  • system_baseline_tmux_autostart (default false), system_baseline_tmux_session (default main) and system_baseline_tmux_windows ([{name, command}], default []), with typed entries in meta/argument_specs.yml and a README section.
  • tasks/tmux.yml (tag tmux) and templates/tmux-autostart.sh.j2 install /etc/profile.d/tmux-autostart.sh when autostart is on and the resolved package list holds tmux; otherwise the file is removed.

Why profile.d

Every login shell reads /etc/profile.d, so the snippet itself acts only for an interactive shell (case $-) with a terminal and an SSH connection, outside tmux, and without ~/.no-tmux. scp, sftp, ssh host command, automation and ssh -t host 'bash -lc cmd' never trigger it. sshd's ForceCommand would apply to every session type and a broken tmux would lock all of them out, so it is not used. A failed attach leaves a plain shell instead of closing the connection; detaching ends the connection. zsh and fish do not read /etc/profile.d.

Security of the window commands

  • Name and commands are role inputs (inventory), never read from a user or a fact. The session and window names must match A-Za-z0-9_- (32 chars) and a command must be one line; both are asserted in validate.yml and anchored with \Z.
  • Every value is shell-quoted in the template. The script is root-owned and not writable by its users.
  • Commands run as the logging-in user, are world-readable and show in ps: no secrets. They start with a minimal environment (use absolute paths). A session outlives the SSH connection.

Verified

  • Hooks (yamllint, gitleaks, REUSE, changelog lint, ansible-lint production, Conventional Commit) pass.
  • The template rendered with the pinned ansible-core passes sh -n and shellcheck, with and without windows.
  • In a pty with TMUX unset: a failed attach (TERM without terminfo) prints the fallback line and leaves the shell; bash -c with a tty does not attach.
  • CI on the earlier head ran the system_baseline Molecule scenario (converge, idempotence, verify) green; the new verify steps (no-tty snippet run, trailing-newline refusals, removal path) are not yet run in CI.
  • The earlier version of the snippet runs on two real Ubuntu hosts of the author's own repository.

Not verified

  • A clean detach (Ctrl-b d) ending the login, and a real attach with a good TERM, were not confirmed in the pty harness.
  • system_baseline_tmux_windows on a real host; Ubuntu 22.04 and Debian 13; mosh and Emacs TRAMP.

Changelog fragment: changelogs/fragments/system-baseline-tmux.yml.

## What An opt-in tmux session on SSH login for system_baseline, so a dropped connection loses nothing. - `tmux` is in the `ubuntu_lxc` and `ubuntu_vm` profiles (not `pve`). - `system_baseline_tmux_autostart` (default `false`), `system_baseline_tmux_session` (default `main`) and `system_baseline_tmux_windows` (`[{name, command}]`, default `[]`), with typed entries in `meta/argument_specs.yml` and a README section. - `tasks/tmux.yml` (tag `tmux`) and `templates/tmux-autostart.sh.j2` install `/etc/profile.d/tmux-autostart.sh` when autostart is on and the resolved package list holds `tmux`; otherwise the file is removed. ## Why profile.d Every login shell reads `/etc/profile.d`, so the snippet itself acts only for an interactive shell (`case $-`) with a terminal and an SSH connection, outside tmux, and without `~/.no-tmux`. `scp`, `sftp`, `ssh host command`, automation and `ssh -t host 'bash -lc cmd'` never trigger it. sshd's `ForceCommand` would apply to every session type and a broken tmux would lock all of them out, so it is not used. A failed attach leaves a plain shell instead of closing the connection; detaching ends the connection. zsh and fish do not read `/etc/profile.d`. ## Security of the window commands - Name and commands are role inputs (inventory), never read from a user or a fact. The session and window names must match `A-Za-z0-9_-` (32 chars) and a command must be one line; both are asserted in `validate.yml` and anchored with `\Z`. - Every value is shell-quoted in the template. The script is root-owned and not writable by its users. - Commands run as the logging-in user, are world-readable and show in `ps`: no secrets. They start with a minimal environment (use absolute paths). A session outlives the SSH connection. ## Verified - Hooks (yamllint, gitleaks, REUSE, changelog lint, ansible-lint production, Conventional Commit) pass. - The template rendered with the pinned ansible-core passes `sh -n` and shellcheck, with and without windows. - In a pty with `TMUX` unset: a failed attach (`TERM` without terminfo) prints the fallback line and leaves the shell; `bash -c` with a tty does not attach. - CI on the earlier head ran the system_baseline Molecule scenario (converge, idempotence, verify) green; the new verify steps (no-tty snippet run, trailing-newline refusals, removal path) are not yet run in CI. - The earlier version of the snippet runs on two real Ubuntu hosts of the author's own repository. ## Not verified - A clean detach (`Ctrl-b d`) ending the login, and a real attach with a good `TERM`, were not confirmed in the pty harness. - `system_baseline_tmux_windows` on a real host; Ubuntu 22.04 and Debian 13; mosh and Emacs TRAMP. Changelog fragment: `changelogs/fragments/system-baseline-tmux.yml`.
Dragonish added 1 commit 2026-11-14 14:28:33 +00:00
feat(system_baseline): optional tmux session on SSH login
Build / build (pull_request) Successful in 12s
Lint / lint (pull_request) Successful in 20s
Molecule / molecule (pull_request) Successful in 1m40s
dcdc9133f6
Adds tmux to the ubuntu_lxc and ubuntu_vm profiles and an opt-in
/etc/profile.d snippet that attaches an interactive SSH login to a tmux
session, created with configurable windows when it does not exist.

- system_baseline_tmux_autostart (default false), _session (default main)
  and _windows ([{name, command}]); only ubuntu_lxc and ubuntu_vm install
  the snippet, pve never does, and false removes it.
- profile.d is read only by an interactive login shell, so scp, sftp,
  ssh host command and automation are unaffected. ~/.no-tmux opts a user
  out. ForceCommand is not used: it would affect every session type.
- Names and commands are validated and shell-quoted; the script is
  root-owned. Commands run as the logging-in user and are world-readable,
  so they must hold no secret.
- Molecule converge enables it, verify checks ownership, mode, the attach
  line, the quoting and sh -n.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q72rsMEKREGMfB9DH3T9mR
Defame1297 added 1 commit 2026-11-14 14:45:30 +00:00
Merge branch 'main' into feat/system-baseline-tmux
Build / build (pull_request) Successful in 11s
Lint / lint (pull_request) Successful in 41s
Molecule / molecule (pull_request) Successful in 1m24s
de0d636002
Defame1297 added 1 commit 2026-11-14 16:12:32 +00:00
Merge branch 'main' into feat/system-baseline-tmux
Build / build (pull_request) Successful in 11s
Lint / lint (pull_request) Successful in 20s
Molecule / molecule (pull_request) Successful in 1m26s
d3d39097b0
Claude left a comment
Owner

Nice feature. The quoting is solid: hostile commands ($(...), backticks, ;, embedded quotes) rendered through the template each reach tmux as one argv element with nothing executed. The snippet passes sh -n and shellcheck, panes don't recurse ($TMUX), and the install/remove when: logic is right. CI is green on the current head, including Molecule converge, idempotence and verify. Two shell behaviours should change before merge; the rest is minor.

Must fix

1. A failed attach ends the SSH login (templates/tmux-autostart.sh.j2:13)

  • exec tmux attach-session replaces the login shell, so if attach fails the connection closes, and it does so again on every interactive login.
  • Realistic trigger: a client TERM with no terminfo on the host (xterm-kitty, xterm-ghostty, wezterm). tmux exits 1 with "open terminal failed: missing or unsuitable terminal".
  • Recovery works (ssh host touch ~/.no-tmux), but this is the failure mode the README gives for rejecting ForceCommand.
  • Detaching also closes the connection, which the README doesn't say.
  • Suggested fix:
    tmux attach-session -t '=NAME' && exit
    echo 'tmux-autostart: attach failed, using a plain shell' >&2
    

2. Fires for non-interactive login shells that have a tty, and swallows the command (templates/tmux-autostart.sh.j2:4)

  • /etc/profile is read by every login shell, not only interactive ones.
  • ssh -t host 'bash -lc "sudo apt upgrade"' attaches to tmux and the command never runs. Reproduced under script with SSH_CONNECTION set.
  • The README, argument_specs and PR body say "read only by an interactive login shell". What really keeps scp, sftp and Ansible out is the [ -t 0 ] / SSH_CONNECTION guard.
  • Fix: wrap the block in case $- in *i*) ... ;; esac and correct the docs.

Minor

  1. The validation regexes accept one trailing newline (tasks/tmux.yml:11-15). Ansible's match test and regex_search filter use Python re, where $ matches before a final \n. A YAML command: | or a session name written as a block scalar passes; confirmed with a real Ansible run.
    • Harmless for commands, because they're quoted.
    • For the session name, tmux 3.4 appears to store main\n escaped, so attach -t '=main\n' would never match and every login would hit #1. That comes from tmux source, not a test.
    • Fix: anchor with \Z instead of $. Don't put \Z inside a YAML double-quoted scalar, where it's an invalid escape.
  2. Window commands start with a partial environment. They run as sh -c (no profile) from a server started before ~/.profile, so they miss ~/.local/bin and anything else set there. A README note or absolute paths fixes it.
  3. Keying on 'tmux' in system_baseline_essential_packages instead of the hard-coded ['ubuntu_lxc', 'ubuntu_vm'] would:
    • follow the role's existing pattern (the unattended-upgrades check in tasks/main.yml:16);
    • remove the list written out twice at lines 34 and 41;
    • stop the case where a consumer overrides the package list without tmux, enables autostart, and the snippet silently does nothing.
  4. Validation placement.
    • The tmux assert runs only under the tmux tag, while the role validates under always elsewhere.
    • is sequence and is not string and default='' duplicate what argument_specs already enforces.
    • The main entry description (argument_specs.yml:7) doesn't mention the new validation.
  5. Tests.
    • The removal path is never exercised (autostart false with the file present). It belongs in verify.yml, since a converge that installs then removes breaks idempotence.
    • The new pve assert not system_baseline_tmux_autostart only re-checks the default.
    • Nothing runs the snippet. A cheap check: source it with SSH_CONNECTION set and no tty, and assert it does nothing.
  6. Docs.
    • zsh and fish login shells don't read /etc/profile.d, so for those users the feature does nothing; worth a line in the README.
    • The PR body's "Molecule not run" is out of date. CI ran the system_baseline scenario and it passed.

Not an issue: no galaxy.yml bump is needed. Versions only change in chore(release) commits (CONTRIBUTING.md).

Not verified: tmux wasn't available on the review host, so the tmux-specific behaviour (session-name escaping, =name: targets) comes from tmux source and the man page, not from running it. mosh and Emacs TRAMP weren't tested.

🤖 Generated with Claude Code

Nice feature. The quoting is solid: hostile commands (`$(...)`, backticks, `;`, embedded quotes) rendered through the template each reach tmux as one argv element with nothing executed. The snippet passes `sh -n` and shellcheck, panes don't recurse (`$TMUX`), and the install/remove `when:` logic is right. CI is green on the current head, including Molecule converge, idempotence and verify. Two shell behaviours should change before merge; the rest is minor. ## Must fix **1. A failed attach ends the SSH login** (`templates/tmux-autostart.sh.j2:13`) - `exec tmux attach-session` replaces the login shell, so if attach fails the connection closes, and it does so again on every interactive login. - Realistic trigger: a client `TERM` with no terminfo on the host (`xterm-kitty`, `xterm-ghostty`, `wezterm`). tmux exits 1 with "open terminal failed: missing or unsuitable terminal". - Recovery works (`ssh host touch ~/.no-tmux`), but this is the failure mode the README gives for rejecting `ForceCommand`. - Detaching also closes the connection, which the README doesn't say. - Suggested fix: ```sh tmux attach-session -t '=NAME' && exit echo 'tmux-autostart: attach failed, using a plain shell' >&2 ``` **2. Fires for non-interactive login shells that have a tty, and swallows the command** (`templates/tmux-autostart.sh.j2:4`) - `/etc/profile` is read by every login shell, not only interactive ones. - `ssh -t host 'bash -lc "sudo apt upgrade"'` attaches to tmux and the command never runs. Reproduced under `script` with `SSH_CONNECTION` set. - The README, argument_specs and PR body say "read only by an interactive login shell". What really keeps scp, sftp and Ansible out is the `[ -t 0 ]` / `SSH_CONNECTION` guard. - Fix: wrap the block in `case $- in *i*) ... ;; esac` and correct the docs. ## Minor 3. **The validation regexes accept one trailing newline** (`tasks/tmux.yml:11-15`). Ansible's `match` test and `regex_search` filter use Python `re`, where `$` matches before a final `\n`. A YAML `command: |` or a session name written as a block scalar passes; confirmed with a real Ansible run. - Harmless for commands, because they're quoted. - For the session name, tmux 3.4 appears to store `main\n` escaped, so `attach -t '=main\n'` would never match and every login would hit #1. That comes from tmux source, not a test. - Fix: anchor with `\Z` instead of `$`. Don't put `\Z` inside a YAML double-quoted scalar, where it's an invalid escape. 4. **Window commands start with a partial environment.** They run as `sh -c` (no profile) from a server started before `~/.profile`, so they miss `~/.local/bin` and anything else set there. A README note or absolute paths fixes it. 5. **Keying on `'tmux' in system_baseline_essential_packages`** instead of the hard-coded `['ubuntu_lxc', 'ubuntu_vm']` would: - follow the role's existing pattern (the unattended-upgrades check in `tasks/main.yml:16`); - remove the list written out twice at lines 34 and 41; - stop the case where a consumer overrides the package list without tmux, enables autostart, and the snippet silently does nothing. 6. **Validation placement.** - The tmux assert runs only under the `tmux` tag, while the role validates under `always` elsewhere. - `is sequence and is not string` and `default=''` duplicate what argument_specs already enforces. - The main entry description (`argument_specs.yml:7`) doesn't mention the new validation. 7. **Tests.** - The removal path is never exercised (autostart `false` with the file present). It belongs in `verify.yml`, since a converge that installs then removes breaks idempotence. - The new pve assert `not system_baseline_tmux_autostart` only re-checks the default. - Nothing runs the snippet. A cheap check: source it with `SSH_CONNECTION` set and no tty, and assert it does nothing. 8. **Docs.** - zsh and fish login shells don't read `/etc/profile.d`, so for those users the feature does nothing; worth a line in the README. - The PR body's "Molecule not run" is out of date. CI ran the system_baseline scenario and it passed. **Not an issue:** no `galaxy.yml` bump is needed. Versions only change in `chore(release)` commits (CONTRIBUTING.md). **Not verified:** tmux wasn't available on the review host, so the tmux-specific behaviour (session-name escaping, `=name:` targets) comes from tmux source and the man page, not from running it. mosh and Emacs TRAMP weren't tested. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
@@ -0,0 +8,4 @@
# world-readable and the commands show in `ps`, so a command must hold no secret. Commands run as the user who logs in.
- name: Check the tmux inputs
ansible.builtin.assert:
that:
Owner

$ in Python re matches before a trailing \n, so "main\n" (a YAML block scalar) passes. Anchor with \Z; the same applies to lines 13 and 15. See the review body, #3.

`$` in Python `re` matches before a trailing `\n`, so `"main\n"` (a YAML block scalar) passes. Anchor with `\Z`; the same applies to lines 13 and 15. See the review body, #3.
@@ -0,0 +1,14 @@
# Managed by Ansible (mortis.kamino.system_baseline, tasks/tmux.yml). Do not edit: the next run overwrites it.
# Interactive SSH logins only (profile.d is not read by scp, sftp, `ssh host command` or Ansible).
# Off for one user: touch ~/.no-tmux
if [ -n "$SSH_CONNECTION" ] && [ -z "$TMUX" ] && [ -t 0 ] && [ ! -e "$HOME/.no-tmux" ] && command -v tmux >/dev/null 2>&1; then
Owner

No interactivity check: ssh -t host 'bash -lc "cmd"' is a non-interactive login shell with a tty, so this attaches to tmux and cmd never runs. Wrap the block in case $- in *i*) ... ;; esac. See the review body, #2.

No interactivity check: `ssh -t host 'bash -lc "cmd"'` is a non-interactive login shell with a tty, so this attaches to tmux and `cmd` never runs. Wrap the block in `case $- in *i*) ... ;; esac`. See the review body, #2.
@@ -0,0 +10,4 @@
tmux new-window -d -t {{ ('=' ~ system_baseline_tmux_session ~ ':') | quote }} -n {{ window.name | quote }} {{ window.command | quote }}
{% endfor %}
fi
exec tmux attach-session -t {{ ('=' ~ system_baseline_tmux_session) | quote }}
Owner

exec makes any attach failure (e.g. a client TERM with no terminfo on the host) close the SSH login, on every interactive login. Suggest tmux attach-session -t ... && exit followed by a stderr warning, so a failure falls back to a plain shell. See the review body, #1.

`exec` makes any attach failure (e.g. a client TERM with no terminfo on the host) close the SSH login, on every interactive login. Suggest `tmux attach-session -t ... && exit` followed by a stderr warning, so a failure falls back to a plain shell. See the review body, #1.
Dragonish added 1 commit 2026-11-14 16:36:15 +00:00
fix(system_baseline): keep tmux autostart from ending or swallowing logins
Build / build (pull_request) Successful in 11s
Lint / lint (pull_request) Successful in 21s
Molecule / molecule (pull_request) Successful in 1m30s
da3389eac1
Review of #122.

- A failed attach no longer closes the connection: the snippet does not exec tmux, it
  prints a line and leaves the plain shell (a client TERM without terminfo made every
  login fail). A clean detach still ends the connection.
- The snippet acts only for an interactive shell (case $-): a login shell that is not
  interactive, such as `ssh -t host 'bash -lc cmd'`, is left alone. Docs corrected.
- Validation anchors with \Z, so a trailing newline from a YAML block scalar is refused.
  It moves to validate.yml (tag always); argument_specs already covers types.
- Install keys on tmux being in the resolved package list, not a second profile list.
- verify.yml: the snippet does nothing without a terminal, the trailing-newline refusals,
  and the removal path. The vacuous pve assert is gone.
- README and argument_specs: window commands start with a minimal environment (use absolute
  paths), zsh and fish do not read /etc/profile.d, detach ends the connection.
Author
Collaborator

Addressed in da3389e. 1: no exec, a failed attach prints a line and leaves a plain shell (tested with TERM=nosuchterm); detach still ends the connection and the README says so. 2: the snippet is wrapped in case $- in i, docs corrected. 3: \Z anchors, moved to validate.yml under tag always, with verify.yml cases for a trailing newline in the session and a command. 4: README and argument_specs note the minimal environment. 5: install now keys on 'tmux' in system_baseline_essential_packages. 6: validation is under always and no longer repeats argument_specs; the main description mentions it. 7: verify.yml covers the removal path and runs the snippet without a tty; the vacuous pve assert is removed. 8: zsh and fish noted, PR body updated. Not run: the new verify steps in CI, and a clean Ctrl-b d in my pty harness.

Addressed in da3389e. 1: no exec, a failed attach prints a line and leaves a plain shell (tested with TERM=nosuchterm); detach still ends the connection and the README says so. 2: the snippet is wrapped in case $- in *i*, docs corrected. 3: \Z anchors, moved to validate.yml under tag always, with verify.yml cases for a trailing newline in the session and a command. 4: README and argument_specs note the minimal environment. 5: install now keys on 'tmux' in system_baseline_essential_packages. 6: validation is under always and no longer repeats argument_specs; the main description mentions it. 7: verify.yml covers the removal path and runs the snippet without a tty; the vacuous pve assert is removed. 8: zsh and fish noted, PR body updated. Not run: the new verify steps in CI, and a clean Ctrl-b d in my pty harness.
Defame1297 added 1 commit 2026-11-14 19:48:08 +00:00
Merge branch 'main' into feat/system-baseline-tmux
Build / build (pull_request) Successful in 12s
Lint / lint (pull_request) Successful in 28s
Molecule / molecule (pull_request) Successful in 1m28s
873a40c48d
Defame1297 scheduled this pull request to auto merge when all checks succeed 2026-11-14 19:48:12 +00:00
Defame1297 merged commit 653669ff66 into main 2026-11-14 19:50:16 +00:00
Defame1297 deleted branch feat/system-baseline-tmux 2026-11-14 19:50:17 +00:00
Sign in to join this conversation.
No Reviewers
3 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#122