How can I set my default shell to start up tmux

I would like my default bash shell to go straight into tmux instead of my always having to type tmux every time.

Asked By: Michael Durrant


There is command chsh which changes login shell. Consult man chsh.

Make tmux your login shell, but don’t forget to configure the default-shell setting for tmux first!

Example of ~/.tmux.conf:

set-option -g default-shell "/bin/bash"

You need to test this first, before setting tmux as your login shell.

Answered By: paul

Start tmux on every shell login, from Arch wiki, seems to work. Simply add the following line of bash code to your .bashrc before your aliases; the code for other shells is very similar:

[[ $TERM != "screen" ]] && exec tmux
Answered By: StarNamer

As Mikel already stated in his comment to have tmux as your login shell can have side effects. But you can make tmux your default shell, literally. A user’s default shell is defined in /etc/passwd. So you can become root and edit /etc/passwd, e.g. sudo vi /etc/passwd search for the line that begins with your username. It probably ends with :/bin/bash. Change /bin/bash to /usr/bin/tmux and now tmux is your default login shell.

However, no guarantee that this won’t cause problems!

What may work better is to NOT do anything that requries root privileges. I would try to create a file in my home directoy named .bash_login and start tmux from within that file: `echo “tmux” >~/.bash_login.

This should work, but you have to try and find our yourself, b/c the bash documentation is not very exact about what file is read and executed when.

Answered By: Bananguin

@StarNamer’s answer is generally accurate, though I typically include the following tests to make sure that

  1. tmux exists on the system
  2. we’re in an interactive shell, and
  3. tmux doesn’t try to run within itself

So, I would add this to the .bashrc:

if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ]; then
  exec tmux


Answered By: user7089

I’m successfully using

case $- in *i*)
    [ -z "$TMUX" ] && exec tmux

in my .zshrc. If you’re using bash, put it in your .bashrc instead.

I also just tried setting tmux as my default shell (chsh -s $(which tmux)) and it seems to break direct command execution via SSH, e.g. ssh $some_server echo foo will not produce any output.

Answered By:

Add this into your ~/.tmux.conf

set -g default-command /usr/local/bin/fish
Answered By: IanVaughan

None of the above responses worked for me – exec tmux prevents me closing tmux without quitting the shell (whether it’s opened with Ctrl + T or from the application menu).

I use Linux Mint, which lets you map certain hotkeys to commands, and (un-kosher as it may be to some..) I’ve got bash shells with commands starting up there, e.g. Win+Alt+B does some sort of convoluted bash -exec(vim) statement to edit my .bashrc, so it acts like a regular shell.

tmux loaded rather than vim under that situation after placing the above at the top of my .bashrc. I’ve wrapped the tmux executing line in another if statement, checking that it’s running in interactive mode.

if command -v tmux>/dev/null; then
        if [ ! -z "$PS1" ]; then # unless shell not loaded interactively, run tmux
                [[ ! $TERM =~ screen ]] && [ -z $TMUX ] && tmux

You could wrap all that onto one line but for readability I think that’s fine.

Answered By: Louis Maddox

Adding to @Louis Maddox ‘s answer, I would execute tmux part with;

(exec tmux attach || exec tmux new-session)
Answered By: devrimbaris

Go to terminal preferances.
enter image description here

And then click to “command” bar.
enter image description here

Check the “Run a custom command instead of my sell” and write whatever command you want to execute at the startup of your terminal.

Answered By: user174935

Adding a line like

[ -z "$TMUX"  ] && { tmux attach || exec tmux new-session && exit;}

in your bashrc file will probably do the job. Note this line will exit ssh and terminate the connection once you detach or exit tmux. I like this configuration as it saves key strokes to terminate the connection. But if you don’t love this(which I think is very unlikely) and would rather remain in the login shell after termination, just remove the exit part:

[ -z "$TMUX"  ] && { tmux attach || exec tmux new-session;}

Also note you shouldn’t wrap tmux attach with exec, as this would cause the connection to be closed when there are no tmux sessions to attach to.

Answered By: hzh

You could (and as of now probably should) use sudo chsh $(logname) command to set /usr/bin/tmux, or whatever is the path to it, as a default shell, then add the following to ~/.tmux.conf:

set-option -g default-shell </path/to/your/favourite/shell>
Answered By: Błażej Michalik

This is in my ~/.profile (I’m using ksh93):

if [[ -z "$TMUX" ]]; then
    if tmux has-session 2>/dev/null; then
        exec tmux attach
        exec tmux

If the TMUX environment variable is unset/empty, then we’re not already in a tmux session, so…

If tmux has-session returns with a zero exit status (true), there is an available session to attach to. Attach to it.

If not, create a new session.

It’s also possible to use tmux as your login shell. If you do this, however, make sure to set default-shell to an actual shell in your ~/.tmux.conf file (see the tmux manual for more info about this).

Answered By: Kusalananda on strike

I combined the extensive checks, with the conditional session checking, and put my own spin on it, to create a default session that is connected to or else created. Unless you are inside of another tmux session, or other protective conditions are met.

if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ]; then
  tmux a -t default || exec tmux new -s default && exit;

Based on these answers:

Answered By: yosefrow

this make tmux open by default on all new shell of user

cmd='[ $TERM == screen ] || exec tmux'
echo -e "$cmdn$(cat ~/.bashrc)" > ~/.bashrc
Answered By: user234701

I use the following for zsh.

if command -v tmux &> /dev/null && [[ -z "$TMUX" ]]
    tmux new-session -c $PWD

The difference with existing solutions being, If you are using a DE and try to use ‘Right Click > Open In Terminal’ then it will open in current location.

Another difference is, we do not use exec in this particular case. Please read Mark Hansen’s Blog titled An Argument for Auto-Starting Tmux.

Some people suggest you exec tmux, this replaces your entire shell’s
process with tmux, and can fail pretty badly if tmux has errors. At
least running tmux directly will fall back to your terminal if tmux
has an error. It also makes it easy to drop the tmux session if you
don’t want it – just detach. The extra process is a small cost to pay.

For Fish shell, I follow similar steps. I put the following in the conf.d:

if status is-interactive
    if not set -q TMUX
        tmux new-session -c $PWD

There is also the Tiny Frames problem.

tmux will auto-size its window frame to the size of the smallest
terminal attached to the session. Often this results in a tiny laptop
terminal window making the usable space on a huge desktop screen tiny.

Even in my machine, connecting to same session via two terminals does not end up well. Same command mirror in both terminals.

Terminal Mirroring

The point is using code like bellow does not work for me.

if tmux has-session 2>/dev/null; then
    tmux attach
    exec tmux

So I have a fish script which does the following:

  • if no session then create new session
  • if has unattached session then give option to connect to one of them (after attaching, cd to $PWD)
  • if do not want to attach to unattached session, then create new session

Here is the script (I put it in conf.d):

if status is-interactive
    if not set -q TMUX
        if tmux list-sessions &>/dev/null
            set -l array_unattached

            for i in (tmux list-sessions -F "#{session_attached} #{session_name} "#{session_path}"")
                if test (echo $i | cut -d' ' -f1) -eq 0
                    set -a  array_unattached (echo $i | cut --complement -d ' ' -f 1)

            if set -q array_unattached[1]
                set -l selection (printf '%sn' $array_unattached | rofi -dmenu -p "Select tmux session")
                if test $status -eq 0
                    set -l session_name (echo $selection | cut -d ' ' -f 1)
                    tmux send-keys -t $session_name:1.1 "^C"
                    tmux send-keys -t $session_name:1.1 "cd $PWD &> /dev/null" ENTER
                    tmux attach-session -t $session_name:1.1
                    tmux new-session -c $PWD
            tmux new-session -c $PWD
Answered By: Ahmad Ismail

I had an issue where I could not login (on Ubuntu 20.04). I added the following tweak to huangzonghao’s answer:

# autoload tmux - place at EOF (end-of-file) within ~/.bashrc
# if shell is interactive, and TMUX var is set...
[[ $- == *i* ]] && [[ -z "${TMUX}" ]] && { tmux attach || exec tmux new-session && exit; }
Answered By: Richard

Add the below line to ~/.config/fish/conf.d/

if not set -q TMUX
    set -g TMUX tmux new-session -d -s base
    eval $TMUX
    tmux attach-session -d -t base

The above worked on Fedora 34. However, on exit, only tmux is exited and the running session remains active.

Answered By: Alang'o Bryan

There seem to be two classes of answers here:

  1. Those that just "start tmux" (exec tmux) using various techniques and (preferably) wrappers for error handling. E.g. exec tmux

  2. Those that first attempt to attach to an existing tmux and then fallback to the first method if an existing session doesn’t exist. E.g. tmux attach || exec tmux.

This second method can be simplified and made more efficient with:

exec tmux new -A

The -A flag to new (short for new-session):

  • Checks to see if an existing tmux session exists
  • Attaches to it if if so
  • Starts a new session if not

In addition to being more concise than the longer form, it’s also theoretically slightly more efficient, since tmux only has to start once, rather than potentially twice (once to check, again to actually run if it wasn’t running).

I use a slightly longer form personally:

exec tmux new -AD -t main -s main


  • Makes sure that there is only one session-group and session running (named main)
  • Detaches any other running tmux client.

I do still recommend wrapping this in an if statement per user7089’s answer.

Answered By: NotTheDr01ds

if tmux already installed on your system , do this:
sudo usermod -s $(which tmux) USERNAME
now your user configured for use tmux as default shell .

now config tmux as your preferred shell :
set-option -g default-shell "/bin/bash"
or any other configuration you want ….
Note: tmux execute /bin/bash , so /etc/bashrc or $USER/.bashrc loaded immediately .

Answered By: mah454

Not quite what the original question asked, but a flexible alternative is to add

history -s "tmux attach"

to your ~/.profile. That way when logging in you can just use the up arrow and enter to start tmux back up.

Answered By: Daniel

If someone wants to start tmux session always BUT NOT within Visual Studio Code, i did the following.

Its basically the same like @user7089 answer, so for explanation look at his answer, i just added the exclude vscode part

do this in your vscode settings.json

    "terminal.integrated.profiles.linux": {
        "bash": {
            "path": "bash",
            "icon": "terminal-bash"
    "terminal.integrated.defaultProfile.linux": "bash",
    "terminal.integrated.env.linux": {
        "LAUNCHED": "vscode"

do this in your ~/.bashrc

if command -v tmux &> /dev/null && [ -n "$PS1" ] &&  [[ ! "$LAUNCHED" == "vscode" ]] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ]; then
  exec tmux new-session -A -s main
Answered By: Elytscha Smith
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.