Resolving “can’t create socket” Error After Killing Byobu/Tmux Session: Socket Cleanup and Restoration


14 views

When you forcibly killed your Byobu session (which uses Tmux underneath), the Unix domain socket file wasn't properly cleaned up. Tmux uses this socket for inter-process communication, and only one instance can use a specific socket path at a time.

$ ls -la /tmp/tmux-*
srwx------ 1 user user 0 Jan 1 12:34 /tmp/tmux-1000/default

First, identify and remove the stale socket file:

$ tmux ls
can't connect to server: Connection refused
$ lsof /tmp/tmux-*/default
$ rm -f /tmp/tmux-$(id -u)/default

If you need to preserve existing sessions, use a different socket path:

$ tmux -L temp new-session

When basic cleanup fails, completely reset the environment:

$ pkill -9 tmux
$ pkill -9 byobu
$ rm -f /tmp/tmux-*/default

To avoid this in future sessions:

$ byobu-disable
$ byobu-enable

Also consider adding this to your .bash_logout:

# Cleanup tmux sockets on logout
[ -n "$TMUX" ] && tmux detach

In some containerized environments, you might need additional steps:

$ ip netns identify
$ sudo ip netns delete troubled_namespace

When you forcefully terminate a Byobu (which uses Tmux as backend) session using kill -9, it doesn't get the chance to properly clean up its Unix domain socket. This socket typically resides in /tmp and follows the pattern tmux-{userid}/default.

The error occurs because:

$ tmux new-session
can't create socket: Permission denied
failed to connect to server

First, verify the status of existing Tmux sessions:

$ ps aux | grep tmux
$ lsof -U | grep tmux

Then check the socket directory permissions:

$ ls -la /tmp/tmux-*
drwx------  2 user user  4096 Jan 31 10:00 tmux-1000

Follow these steps to completely resolve the issue:

1. Remove stale socket files:

$ rm -rf /tmp/tmux-*

2. Kill any remaining tmux processes:

$ pkill -9 tmux
$ pkill -9 byobu

3. Verify no tmux-related processes remain:

$ pgrep -l tmux

4. For system-wide installations, you might need to clean up systemd or upstart sessions:

$ sudo systemctl restart tmux
$ sudo service tmux restart

To avoid this issue in the future:

  • Always detach from sessions properly using Ctrl+b d
  • When you must kill a session, first try tmux kill-session -t session_name
  • Consider adding this alias to your .bashrc:
alias fix-tmux='rm -rf /tmp/tmux-* && pkill -9 tmux'

For power users who frequently encounter this issue, you can configure a custom socket path in ~/.tmux.conf:

set -g base-index 1
set -g default-command "${SHELL}"
set -g default-terminal "screen-256color"
set -g socket-path "~/.tmux_socket"

Then always specify the socket when starting tmux:

tmux -S ~/.tmux_socket new -s mysession