From dcdc9133f6313688d0d9c0c652549f77b004c72c Mon Sep 17 00:00:00 2001 From: Dragonish Date: Sat, 14 Nov 2026 14:26:19 +0000 Subject: [PATCH] feat(system_baseline): optional tmux session on SSH login 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 Claude-Session: https://claude.ai/code/session_01Q72rsMEKREGMfB9DH3T9mR --- changelogs/fragments/system-baseline-tmux.yml | 6 +++ roles/system_baseline/README.md | 23 +++++++++- roles/system_baseline/defaults/main.yml | 9 ++++ roles/system_baseline/meta/argument_specs.yml | 35 +++++++++++++++- .../molecule/default/converge.yml | 7 ++++ .../molecule/default/verify.yml | 24 +++++++++++ roles/system_baseline/tasks/main.yml | 4 ++ roles/system_baseline/tasks/tmux.yml | 42 +++++++++++++++++++ .../templates/tmux-autostart.sh.j2 | 14 +++++++ 9 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/system-baseline-tmux.yml create mode 100644 roles/system_baseline/tasks/tmux.yml create mode 100644 roles/system_baseline/templates/tmux-autostart.sh.j2 diff --git a/changelogs/fragments/system-baseline-tmux.yml b/changelogs/fragments/system-baseline-tmux.yml new file mode 100644 index 0000000..3cbd640 --- /dev/null +++ b/changelogs/fragments/system-baseline-tmux.yml @@ -0,0 +1,6 @@ +--- +minor_changes: + - >- + system_baseline - add an optional tmux session on SSH login for the ``ubuntu_lxc`` and ``ubuntu_vm`` profiles + (``system_baseline_tmux_autostart``, ``system_baseline_tmux_session``, ``system_baseline_tmux_windows``). + It is off by default. The two profiles now install ``tmux``. diff --git a/roles/system_baseline/README.md b/roles/system_baseline/README.md index 3370d07..d012d1e 100644 --- a/roles/system_baseline/README.md +++ b/roles/system_baseline/README.md @@ -1,6 +1,6 @@ # system_baseline -Applies an OS baseline chosen by `system_baseline_system_type`: apt upgrade, package install and removal, unattended-upgrades, locale, timezone, a journal size cap, sysctl hardening, tmpfs mounts and an optional reboot. Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml), which is the source of truth. +Applies an OS baseline chosen by `system_baseline_system_type`: apt upgrade, package install and removal, unattended-upgrades, locale, timezone, a journal size cap, sysctl hardening, tmpfs mounts, an optional tmux session on SSH login and an optional reboot. Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml), which is the source of truth. ## Supported platforms @@ -36,6 +36,9 @@ templating error instead of the role's own message. | `system_baseline_sysctl_hardening` | from profile | no | List of `name` and `value` sysctl pairs | | `system_baseline_secure_tmp_mounts` | from profile | no | Paths remounted as tmpfs with `noexec,nosuid,nodev` | | `system_baseline_journal_max_use` | `200M` | no | Upper bound of the persistent journal (journald `SystemMaxUse`). | +| `system_baseline_tmux_autostart` | `false` | no | Attach interactive SSH logins to a tmux session (`ubuntu_lxc` and `ubuntu_vm` only) | +| `system_baseline_tmux_session` | `main` | no | Session name, `A-Za-z0-9_-`, up to 32 characters | +| `system_baseline_tmux_windows` | `[]` | no | `{name, command}` windows made when the session is created | | `system_baseline_timezone` | `Etc/UTC` | no | Timezone | | `system_baseline_locale` | `en_US.UTF-8` | no | Locale | | `system_baseline_reboot_if_required` | `true` | no | Reboot when `/var/run/reboot-required` exists | @@ -78,7 +81,7 @@ Besides `main`, two task files are a supported interface, for a caller that driv ## Tags -`always` (validation), `update` (apt upgrade and the reboot), `packages`, `unattended-upgrades`, `locale`, `timezone`, `journald`, `sysctl`, `secure_tmp`. +`always` (validation), `update` (apt upgrade and the reboot), `packages`, `unattended-upgrades`, `locale`, `timezone`, `journald`, `sysctl`, `secure_tmp`, `tmux`. ## Example play @@ -92,6 +95,22 @@ Besides `main`, two task files are a supported interface, for a caller that driv - role: mortis.kamino.system_baseline ``` +## tmux on SSH login + +With `system_baseline_tmux_autostart: true` an interactive SSH login attaches to the session `system_baseline_tmux_session` (created when missing), so a dropped connection loses nothing. The `ubuntu_lxc` and `ubuntu_vm` profiles install `tmux`; `pve` never gets the snippet. + +```yaml +system_baseline_tmux_autostart: true +system_baseline_tmux_session: ops +system_baseline_tmux_windows: + - { name: logs, command: "journalctl -f" } +``` + +- It is `/etc/profile.d/tmux-autostart.sh`, read only by an interactive login shell, so `scp`, `sftp`, `ssh host command` and Ansible never trigger it. sshd's `ForceCommand` is not used: it applies to every session type and a broken tmux would lock all of them out. It only acts for an SSH login with a terminal, outside tmux, and without `~/.no-tmux`. +- Turn it off for one login with `ssh -t host bash` (a non-login shell does not read `/etc/profile.d`), for one user with `touch ~/.no-tmux`, and for the host by setting the variable to `false` and running the role (it removes the snippet). +- The windows are made when the session is created; a login that re-attaches adds none, so a change applies from the next new session. A window closes when its command exits. +- Security: the name and commands are validated (`A-Za-z0-9_-`, one line) and shell-quoted, and come from the inventory, never from a user. The file is root-owned and not writable by its users. The commands run as the user who logs in, are readable by every user and show in `ps`: no secrets in a command. A session outlives the SSH connection, so a root session stays open until it is closed. + ## Notes - The `pve` profile has no `ufw`; the `ubuntu_*` profiles install it and `fail2ban`. diff --git a/roles/system_baseline/defaults/main.yml b/roles/system_baseline/defaults/main.yml index 0f08499..64a213b 100644 --- a/roles/system_baseline/defaults/main.yml +++ b/roles/system_baseline/defaults/main.yml @@ -6,6 +6,13 @@ system_baseline_locale: "en_US.UTF-8" system_baseline_reboot_if_required: true # Upper bound of the persistent journal (journald SystemMaxUse); older archived files are removed above it. system_baseline_journal_max_use: 200M +# Attach an interactive SSH login to a tmux session (see tasks/tmux.yml). Off by default: it changes what every login does. +# Only the ubuntu_lxc and ubuntu_vm profiles honour it; pve never installs the snippet. +system_baseline_tmux_autostart: false +# Name of the session an SSH login attaches to. +system_baseline_tmux_session: main +# Windows created, as extra entries next to a plain shell, when the session is created: a list of {name, command}. +system_baseline_tmux_windows: [] # Per-system_type policy. Everything below is a role default, so group_vars and host_vars # override it with normal Ansible precedence. @@ -83,6 +90,7 @@ system_baseline_profiles: essential_packages: - logrotate - vim + - tmux - curl - wget - git @@ -137,6 +145,7 @@ system_baseline_profiles: - qemu-guest-agent - logrotate - vim + - tmux - curl - wget - git diff --git a/roles/system_baseline/meta/argument_specs.yml b/roles/system_baseline/meta/argument_specs.yml index 7e0346e..5dcd22c 100644 --- a/roles/system_baseline/meta/argument_specs.yml +++ b/roles/system_baseline/meta/argument_specs.yml @@ -5,7 +5,7 @@ argument_specs: description: - >- Validates C(system_baseline_system_type), then runs the update, packages, unattended-upgrades, locale, timezone, journald, - sysctl, tmpfs and reboot task files, in that order. + sysctl, tmpfs, tmux and reboot task files, in that order. - Needs gathered facts, because the reboot check reads C(ansible_os_family). - >- The resolved inputs C(system_baseline_profile), C(system_baseline_essential_packages), @@ -42,6 +42,39 @@ argument_specs: description: - Upper bound of the persistent systemd journal (journald C(SystemMaxUse), for example C(200M)). - Written to a drop-in under C(/etc/systemd/journald.conf.d). + system_baseline_tmux_autostart: + type: bool + default: false + description: + - >- + Attach an interactive SSH login to a tmux session, creating it when it does not exist. Installs C(/etc/profile.d/tmux-autostart.sh), + read only by an interactive login shell, so scp, sftp, C(ssh host command) and automation are unaffected. A user opts out with + C(~/.no-tmux). Only the C(ubuntu_lxc) and C(ubuntu_vm) profiles install it; with false, or on C(pve), the file is removed. + system_baseline_tmux_session: + type: str + default: main + description: + - Name of the session, 1 to 32 characters of C(A-Za-z0-9_-). Checked only when C(system_baseline_tmux_autostart) is true. + system_baseline_tmux_windows: + type: list + elements: dict + default: [] + description: + - >- + Windows made when the session is created (a login that only re-attaches adds none), next to a plain shell. A window closes when its + command exits. The commands come from the inventory and are shell-quoted, but they run as the user who logs in, are readable by + every user on the host and show in C(ps): put no secret in one. + options: + name: + type: str + required: true + description: + - Window name, 1 to 32 characters of C(A-Za-z0-9_-). + command: + type: str + required: true + description: + - One-line shell command (no line break) run in the window. system_baseline_timezone: type: str default: Etc/UTC diff --git a/roles/system_baseline/molecule/default/converge.yml b/roles/system_baseline/molecule/default/converge.yml index 2cbb555..4caf575 100644 --- a/roles/system_baseline/molecule/default/converge.yml +++ b/roles/system_baseline/molecule/default/converge.yml @@ -24,6 +24,10 @@ gather_facts: true vars: system_baseline_system_type: ubuntu_lxc + system_baseline_tmux_autostart: true + system_baseline_tmux_session: molecule + system_baseline_tmux_windows: + - { name: logs, command: "echo 'a b'; true" } roles: - role: mortis.kamino.system_baseline @@ -44,6 +48,8 @@ ansible.builtin.assert: that: - "'ufw' not in system_baseline_essential_packages" + - "'tmux' not in system_baseline_essential_packages" + - not system_baseline_tmux_autostart - "'unattended-upgrades' in system_baseline_essential_packages" - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '03:00' @@ -65,6 +71,7 @@ ansible.builtin.assert: that: - "'qemu-guest-agent' in system_baseline_essential_packages" + - "'tmux' in system_baseline_essential_packages" - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false - system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '04:00' - system_baseline_sysctl_hardening | selectattr('name', 'equalto', 'net.ipv4.conf.all.log_martians') | list | length == 1 diff --git a/roles/system_baseline/molecule/default/verify.yml b/roles/system_baseline/molecule/default/verify.yml index 8849164..0b2c280 100644 --- a/roles/system_baseline/molecule/default/verify.yml +++ b/roles/system_baseline/molecule/default/verify.yml @@ -13,6 +13,7 @@ - "'ufw' in ansible_facts.packages" - "'fail2ban' in ansible_facts.packages" - "'unattended-upgrades' in ansible_facts.packages" + - "'tmux' in ansible_facts.packages" - "'telnet' not in ansible_facts.packages" quiet: true @@ -27,6 +28,29 @@ - system_baseline_verify_journald.content | b64decode is search('(?m)^SystemMaxUse=200M$') quiet: true + - name: Read the tmux auto-start snippet + ansible.builtin.slurp: + src: /etc/profile.d/tmux-autostart.sh + register: system_baseline_verify_tmux + + - name: Stat the tmux auto-start snippet + ansible.builtin.stat: + path: /etc/profile.d/tmux-autostart.sh + register: system_baseline_verify_tmux_stat + + - name: The snippet is root-owned, attaches to the session, and quotes the window command as one argument + ansible.builtin.assert: + that: + - system_baseline_verify_tmux_stat.stat.pw_name == 'root' + - system_baseline_verify_tmux_stat.stat.mode == '0644' + - system_baseline_verify_tmux.content | b64decode is search('tmux attach-session -t =molecule') + - system_baseline_verify_tmux.content | b64decode is search("-n logs 'echo '\"'\"'a b'\"'\"'; true'") + quiet: true + + - name: The snippet is valid shell + ansible.builtin.command: /bin/sh -n /etc/profile.d/tmux-autostart.sh + changed_when: false + - name: Read the apt configuration the role writes ansible.builtin.slurp: src: "{{ item }}" diff --git a/roles/system_baseline/tasks/main.yml b/roles/system_baseline/tasks/main.yml index aa70e51..28b8707 100644 --- a/roles/system_baseline/tasks/main.yml +++ b/roles/system_baseline/tasks/main.yml @@ -36,6 +36,10 @@ ansible.builtin.import_tasks: secure_tmp.yml tags: secure_tmp +- name: Start tmux on SSH login + ansible.builtin.import_tasks: tmux.yml + tags: tmux + - name: Reboot if required ansible.builtin.import_tasks: reboot.yml tags: update diff --git a/roles/system_baseline/tasks/tmux.yml b/roles/system_baseline/tasks/tmux.yml new file mode 100644 index 0000000..887dc4a --- /dev/null +++ b/roles/system_baseline/tasks/tmux.yml @@ -0,0 +1,42 @@ +--- +# Attach an interactive SSH login to a tmux session, created with the configured windows when it does not exist. +# /etc/profile.d is read only by an interactive login shell, so scp, sftp, `ssh host command` and automation never trigger it. +# sshd's ForceCommand is deliberately not used: it applies to every session type and a broken tmux would lock out all of them. +# +# Security: the session name, window names and commands are role inputs (inventory), never read from a user or a fact. They are +# validated here and shell-quoted in the template. The script is root-owned and not writable by the users it runs for. It is +# 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: + - system_baseline_tmux_session is match('^[A-Za-z0-9_-]{1,32}$') + - system_baseline_tmux_windows is sequence and system_baseline_tmux_windows is not string + - system_baseline_tmux_windows | map(attribute='name', default='') | map('regex_search', '^[A-Za-z0-9_-]{1,32}$') | select | list | length + == system_baseline_tmux_windows | length + - system_baseline_tmux_windows | map(attribute='command', default='') | select('match', '^[^\n\r]+$') | list | length + == system_baseline_tmux_windows | length + fail_msg: >- + system_baseline_tmux_session must be 1-32 characters of A-Z a-z 0-9 _ - ; every system_baseline_tmux_windows entry needs + a name (same characters) and a one-line command. + quiet: true + when: system_baseline_tmux_autostart | bool + +- name: Install the tmux auto-start snippet + ansible.builtin.template: + src: tmux-autostart.sh.j2 + dest: /etc/profile.d/tmux-autostart.sh + owner: root + group: root + mode: "0644" + validate: /bin/sh -n %s + when: + - system_baseline_tmux_autostart | bool + - system_baseline_system_type in ['ubuntu_lxc', 'ubuntu_vm'] + become: true + +- name: Remove the tmux auto-start snippet + ansible.builtin.file: + path: /etc/profile.d/tmux-autostart.sh + state: absent + when: not (system_baseline_tmux_autostart | bool and system_baseline_system_type in ['ubuntu_lxc', 'ubuntu_vm']) + become: true diff --git a/roles/system_baseline/templates/tmux-autostart.sh.j2 b/roles/system_baseline/templates/tmux-autostart.sh.j2 new file mode 100644 index 0000000..66dd9c0 --- /dev/null +++ b/roles/system_baseline/templates/tmux-autostart.sh.j2 @@ -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 + # new-session -d fails when the session exists, so the windows below are made only when this login creates it (once, even + # for two logins at the same moment). Windows close when their command exits; window 0 is always a plain shell. + if tmux new-session -d -s {{ system_baseline_tmux_session | quote }} 2>/dev/null; then + : +{% for window in system_baseline_tmux_windows %} + 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 }} +fi