Strovent SystemsSign in
Reference

Unix shell and tmux

Working in a remote Unix shell: basics, keeping sessions alive with tmux, and detecting SSH or tmux.

Shell and tmux basicsA short list of Unix shell commands and tmux, which keeps your work running when a connection drops.Check if a shell is running over SSH, inside tmux, or inside screenTest SSH_CONNECTION or SSH_TTY for an SSH login, TMUX for tmux and STY for screen, and know when these variables are stale or missing after sudo or su.Keep your work after a disconnect with tmux, and attach automaticallyHow tmux keeps a session alive when SSH or the app drops, one command that creates or reattaches, and how to run it over SSH, over Mosh and at login.tmux killed on SSH logout: systemd KillUserProcesses and lingertmux or screen gone after logout? systemd-logind ended your session scope. Set KillUserProcesses=no, or enable linger and start tmux with systemd-run.Scroll back and search history in tmuxHow to scroll back in tmux copy mode, the emacs and vi keys, how to search, how many lines tmux keeps, and what the mouse option does.Unknown terminal type or "Error opening terminal" when you use tmux, screen or SSHWhy a host rejects TERM names like tmux-256color, how to copy the terminfo entry to your home folder with infocmp and tic, and how to change TERM.screen -r "There is no screen to be resumed": reattach an Attached or lost sessionscreen -r says There is no screen to be resumed when the session is still marked Attached or is gone. Use screen -ls, then screen -d -r or -x to get it back.Colors wrong in tmux: get 256 colors and true color (default-terminal, RGB)Colors look washed out in tmux when it thinks the outer terminal has only 8 colors or no RGB. Fix TERM outside, default-terminal inside, and add RGB.tmux "sessions should be nested with care, unset $TMUX to force"tmux refuses to attach or start a session from a shell already inside tmux. Use switch-client or prefix then s to change session, or nest on purpose.Bash prompt wraps onto the same line: mark color codes in PS1Long commands wrap back over the bash prompt when PS1 has color codes outside \[ and \]. Wrap each escape sequence in \[ \] so readline counts the prompt right.Terminal size wrong after resize: stty size, resize and COLUMNSWhen vi, less or top use only part of the window, the host has the wrong size. Check stty size, set it with stty rows and cols or resize, drop fixed COLUMNS.SSH agent lost after reattaching tmux or screen: fix SSH_AUTH_SOCKAfter you reconnect and reattach tmux, old panes still point SSH_AUTH_SOCK at the last login's agent socket, now gone. Refresh it, or use a fixed link.xterm-ghostty or xterm-kitty not found after SSH: fix TERM and terminfoGhostty and kitty set TERM to their own name, and SSH sends it to hosts that lack that terminfo entry. Copy the entry to the host or send TERM=xterm-256color.Accented or Unicode characters show as ? or boxes over SSH: LANG, LC_ALL and UTF-8Why é, ü or emoji show as ?, as é or as boxes in an SSH session, how to see which side is wrong with locale, and how to set a UTF-8 locale or convert a file.Print escape sequences with echo -e or printf: why it differs between shellsecho -e works in bash but prints "-e" in dash, the default /bin/sh on Debian. Why echo is not portable, and how to print colors and escape codes with printf.Can't select or copy text in tmux over SSH: mouse mode, Shift and the clipboardWith tmux mouse mode on, the terminal sends the mouse to tmux instead of selecting. In xterm, hold Shift to select, or copy in tmux to the clipboard.Ctrl+Left and Ctrl+Right don't move by word over SSH: key codes and ~/.inputrcCtrl+Left and Ctrl+Right print ;5D or 5C, or do nothing, in bash over SSH. What the keys send, why readline ignores them, and the ~/.inputrc fix.Script fails with "/bin/bash^M: bad interpreter" or "$'\r': command not found": Windows line endingsA script saved with Windows CRLF line endings fails on Linux with bad interpreter, required file not found or $'\r' errors. Find the CR characters, remove them.Ctrl+Z and "Stopped": get the program back with fg, bg and jobsCtrl+Z suspends the running program and prints [1]+ Stopped. It is still there: fg brings it back, bg lets it run in the background, jobs lists them.Ctrl+D logs you out of the shell: IGNOREEOF and ignoreeofCtrl+D at an empty prompt is end of input, so the shell exits and the SSH session or tmux pane closes. Set IGNOREEOF to make Bash ask for exit instead.nohup, disown or tmux: keep a command running after SSH disconnectsWhen SSH drops, the shell sends SIGHUP to its jobs. nohup and disown keep a command alive but you cannot get back to it; tmux keeps it and its screen.Command not found with "ssh host command" but it works after logging in: PATH and .bashrcssh host command runs a non-interactive shell that skips .profile and most of .bashrc, so PATH is shorter. Use a full path, move the PATH line or run bash -lc.Fix "mesg: ttyname failed: Inappropriate ioctl for device" over SSH, sudo -i or su -The message comes from a mesg n line in root's .profile running without a terminal. It is harmless; make the line check for a terminal first.Change the tmux prefix key from Ctrl+B to Ctrl+A or another keySet a new tmux prefix in ~/.tmux.conf with set -g prefix, unbind C-b and bind send-prefix, then reload with source-file. Includes keys that clash.tmux "open terminal failed: not a terminal" and bash "no job control in this shell" over SSHBoth mean the command got no terminal: ssh host command does not ask for one. Add -t, or -tt from a script, or start tmux detached with new -d.Bash history lost or missing commands after SSH disconnect, tmux or several terminalsBash keeps history in memory and writes ~/.bash_history only at exit, overwriting it. Use histappend and history -a so each command is saved at once.tmux fills part of the screen with dots: another client is attached at a different sizeDots in tmux mark screen space outside the window: another client of a different size is attached, or window-size is set. Use attach -d or change window-size.

Other categories