1

Aloha !

I have a headless server connected to a monitor. The server has no graphical environment installed, nor has any keyboard or mice attached to it. I am able to login via SSH.

I would like to have it display a tmux session on the console (tty) displayed on the monitor, without having to plug in a keyboard, log into the tty, and attach the tmux session there. This way, I could simply attach to that tmux session over SSH and control what gets displayed on that monitor remotely (logs, system usage, etc).

How may I know which console is attached to that monitor (login-shell, no user logged in), and how to automatically run a command on it at startup (and/or remotely/from another console) ?

1 Answer 1

0

Solution

This is for Linux that uses systemd.

As root, create a file /etc/systemd/system/tty8tmux.service with the following content:

[Unit]
Description=Tmux session in tty8

[Service]
Type=idle
User=herrcrazi
KillMode=process
ExecStart=/usr/bin/tmux new-session -s tty8 -A -f read-only
ExecStartPost=+/usr/bin/chvt 8
ExecStop=/usr/bin/tmux kill-session -t tty8
ExecStopPost=+/usr/bin/chvt 1
TTYPath=/dev/tty8
StandardInput=tty
StandardOutput=tty
StandardError=tty

[Install]
WantedBy=multi-user.target

Inform systemd something has changed:

sudo systemctl daemon-reload

Enable the service:

sudo systemctl enable tty8tmux.service

Start the service:

sudo systemctl start tty8tmux.service

Starting the service will create a tmux session named tty8 for the user named herrcrazi and attach a tmux client to it. The client will run in the virtual terminal tty8. If the tmux session already exists, the client will attach to it without creating anew. In any case tty8 will become the foreground virtual terminal.

Stopping the service will destroy the tmux session named tty8 and switch display to tty1. If you prefer killing just the tmux client (not the session) then just omit (remove or comment out) the line that specifies ExecStop=. Thanks to KillMode=process only the main process (i.e. the tmux client) will be killed.

The service will start automatically with the system.

After connecting via SSH as herrcrazi, attach to the tmux session by invoking:

tmux attach-session -t tty8 -f ignore-size

Hints, notes

  • The service will start with the system and there will probably be no tmux server for the user herrcrazi yet. A tmux server will start automatically. As herrcrazi you may want to create other tmux sessions later, they will use the same server. The environment of the server will come from its actual parent (here from systemd) and won't be automatically updated when a client connects, therefore it may or may not be what you expect. See e.g. this answer and links therein.

    The tmux server will formally belong to the service (in terms of control groups), although KillMode=process will save it from being killed when the service stops.

    For the above reasons consider User=somebodyelse or tell tmux to use a non-default socket (e.g. with -L tty8). When attaching, you will need to match the user (sudo -u somebodyelse tmux a …) or pick the right socket. This way your regular user herrcrazi will be able to use tmux for other purposes as if the service wasn't there.

    somebodyelse may be root.

  • Instead of ExecStart=/usr/bin/tmux … you can ExecStart=/path/to/shellscript, so shellscript creates a tmux session and runs pre-defined set of tools (e.g. htop, watch sensors, watch df -h) in a pre-defined layout of panes and ultimately execs to tmux attach-session -t tty8. Specific notes:

    • You need to set everything up without attaching and attach last (example).
    • Before you attach, tmux may assume wrong dimensions (see this question). Use like tmux new-session -d -s tty8 -A -f read-only -x - -y - in the script, then set the rest up.
    • Finally you need to exec tmux a …, so the tmux client replaces the script and systemd does not see the process (script) has died.
    • Read this answer to learn what to do and what not to do.
    • There is a way to pre-create an exact layout of panes and use it on demand, see this answer.
  • Kernel messages may appear in tty8, especially when shutting down. During normal operations they will interfere with what our tmux client displays; and because the client running inside tty8 configures the terminal for its own needs, such messages will look out of place. Consider telling the kernel to use tty1 only. You do this by adding console=tty1 to kernel parameters. If this is the only parameter then the relevant line in /etc/default/grub should look like this:

    GRUB_CMDLINE_LINUX="console=tty1"
    

    (or GRUB_CMDLINE_LINUX_DEFAULT).

    After saving the file invoke sudo update-grub and reboot. Note you can still see the messages on demand with dmesg or keep watching with dmesg -wH.

  • If you ever want to manually change the foreground tty to ttyN, use chvt N. If not in a virtual terminal (e.g. if logged in via SSH), you need sudo:

    sudo chvt 2
    
  • Tested on Debian GNU/Linux 12.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .