Skip to content

Instantly share code, notes, and snippets.

@Lolozendev
Created July 8, 2025 22:43
Show Gist options
  • Select an option

  • Save Lolozendev/a9c7a40304a6c4d56bdaca07cb407cb0 to your computer and use it in GitHub Desktop.

Select an option

Save Lolozendev/a9c7a40304a6c4d56bdaca07cb407cb0 to your computer and use it in GitHub Desktop.
a bash script to be able to create shared tmux sessions.
#!/usr/bin/env bash
SOCKDIR=/tmp/sharedtmux
SOCK=$SOCKDIR/shared.sock
SESSION=shared
GROUP=sharedtmux
# Check if user is in the sharedtmux group
if ! groups "$USER" | grep -q "\b$GROUP\b"; then
echo "User $USER is not in group $GROUP"
exit 1
fi
# Setup shared socket dir if needed
if [ ! -d "$SOCKDIR" ]; then
mkdir "$SOCKDIR"
chgrp "$GROUP" "$SOCKDIR"
chmod 770 "$SOCKDIR"
fi
# Check if socket exists
if [ -S "$SOCK" ]; then
echo "Attaching to existing session as $USER"
else
echo "Creating session as $USER"
# Set umask for group access
umask 007
tmux -S "$SOCK" new -d -s "$SESSION"
# Fix permissions immediately
chmod 770 "$SOCK"
chgrp "$GROUP" "$SOCK"
# Grant access to all users in the group
for user in $(getent group "$GROUP" | cut -d: -f4 | tr ',' ' '); do
if [ "$user" != "$USER" ] && [ -n "$user" ]; then
tmux -S "$SOCK" server-access -a "$user"
fi
done
fi
# Attach
tmux -S "$SOCK" attach -t "$SESSION"
# Cleanup
if ! tmux -S "$SOCK" ls &>/dev/null; then
echo "Session ended. Cleaning up socket."
[ -S "$SOCK" ] && rm -f "$SOCK"
fi
@Lolozendev
Copy link
Author

  • You'll need tmux installed
  • You'll need a group named 'sharedtmux' on all user that you want to be able to join your shared sessions
  • ❗Be careful there may be weird behavior if you use this script inside an already existing tmux session (tmuxception)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment