How to Install and Use tmux on Amazon Linux (EC2 Guide for Beginners)

If you’re working on an AWS EC2 instance running Amazon Linux, tmux is one of the best tools to prevent losing progress when your SSH connection drops.

tmux lets you run long tasks (training models, builds, downloads…) in detachable sessions. Disconnect, come back later — everything is still there.

Step 1: Install tmux on Amazon Linux

Amazon Linux 2 (most EC2 instances)

Bash

sudo yum update -y
sudo yum install tmux -y

Amazon Linux 2023

Bash

sudo dnf update -y
sudo dnf install tmux -y

Verify:

Bash

tmux -V
# Example: tmux 3.2a or newer

Step 2: 5 Essential tmux Commands

ActionCommandNotes
Start new named sessiontmux new -s myjobUse names like “train”, “deploy”
List sessionstmux lsShows running sessions
Re-attach to a sessiontmux a -t myjobShort: tmux attach -t myjob
Re-attach (if only one session)tmux aQuick when you have just one
Detach (keep running)Ctrl+b then dSafe to close SSH now
Exit & close sessionexit (inside tmux)Or Ctrl+d

Step-by-Step Example on EC2

  1. SSH in ssh -i key.pem ec2-user@your-ec2-ip
  2. Install tmux (one time) sudo yum install tmux -y # or dnf on AL2023
  3. Start session tmux new -s training
  4. Run your long task python3 train.py –epochs 1000
  5. Detach safely Press Ctrl+b, then d → [detached]
  6. Later: SSH back and list tmux ls
  7. Re-enter tmux a -t training
  8. Finish: type exit

Quick Copy-Paste Cheat Sheet

Bash

# Start
tmux new -s work

# List
tmux ls

# Attach
tmux a -t work

# Detach
Ctrl + b → d

# Kill (if needed)
tmux kill-session -t work

tmux is lightweight, reliable on AWS, and once you use detach/attach a few times, you’ll wonder how you lived without it.

If you work with EC2 a lot, this one tool can save hours of frustration.

Happy coding (and no more lost jobs)! 🚀

Auto-Log All Pane Output to Files

tmux scrollback is limited, so old output gets lost on long jobs. To automatically save every pane’s output to files in ~/tmux-logs/, add these lines to ~/.tmux.conf:

Bash

# Auto-log output for new sessions, windows, and panes

set-hook -g session-created    'pipe-pane -o "cat >> ~/tmux-logs/tmux-#S-#I-#P-$(date +%Y%m%d).log"'
set-hook -g after-new-window   'pipe-pane -o "cat >> ~/tmux-logs/tmux-#S-#I-#P-$(date +%Y%m%d).log"'
set-hook -g after-split-window 'pipe-pane -o "cat >> ~/tmux-logs/tmux-#S-#I-#P-$(date +%Y%m%d).log"'

create directory:

mkdir -p ~/tmux-logs  # run once if the folder doesn't exist

To make it take effect: Close all existing tmux sessions completely (or kill them with tmux kill-server if needed), then start new sessions as usual. From then on, every new pane will auto-save its output to files like tmux-mySession-0-1-20260225.log.

You can view them anytime with cat, less, or grep — even after sessions end. Great for EC2 long-running tasks!

(If you want it ultra-short, just keep the code block + the bold sentence about closing sessions.) Let me know if this fits!

Leave a Reply