Tag: tmux

  • Reorder windows in tmux

    To reorder windows in tmux, you can use following command

    CTRL + B + .
    

    Press CTRL + B or whatever prefix you use, then press . (dot). This will ask you number/position where you need current window moved.

    Another method is

    CTRL + B + :
    

    This will give you tmux command promt. Now enter

    move-window -t 0
    

    This will move current window to position 0. This only work if position 0 is empty. If another window already present in position 0, use swap-window command.

    swap-window -t 0
    

    This with swap current window with window in position 0.

    Another useful command is

    CTRL + B + ,
    

    This will allow you to rename current tmux window.

    Using shortcuts

    You can edit .tmux.conf file and add following

    bind-key -n C-S-Left swap-window -t -1
    bind-key -n C-S-Right swap-window -t +1
    

    Now you can use CTRL + SHIFT + LEFT OR RIGHT arrow to move windows.

    See tmux

  • run same command on multiple tmux panes

    If you want to run same command on multiple servers, you can use tmux synchronize-panes feature. This will be useful when you need to setup multiple servers, say install nginx/php/mysql on multiple servers.

    You can create multiple panes in tmux. SSH into each of the servers from these panes. Now run

    CTRL + B :
    

    Now type

    setw synchronize-panes on
    

    Whatever command you run on one of the pane will be executed on all panes. Be careful when you run server specific commands.

  • tmux create new window on current directory

    When you start a new window or split pane on tmux, it always start with the directory from where you run tmux command.

    I prefer it start with current working directory of active tmux window. To do this, edit file

    vi ~/.tmux.conf
    

    Add

    bind c new-window -c "#{pane_current_path}"
    bind '"' split-window -c "#{pane_current_path}"
    bind % split-window -h -c "#{pane_current_path}"
    
  • Auto Attach to tmux session on SSH

    To start tmux and attach to existing connection on SSH in, edit file

    vi ~/.bashrc
    

    Add

    if [[ "$TMUX" == "" ]] && [[ "$SSH_CONNECTION" != "" ]] && [ "$SSH_TTY" != "" ]; then
        WHOAMI=$(whoami)
        if tmux has-session -t $WHOAMI 2>/dev/null; then
            tmux -2 attach-session -t $WHOAMI
        else
            tmux -2 new-session -s $WHOAMI
        fi
    fi
    

    Method 2

    You can use following command when connecting to remote sevrer.

    ssh  USER@SERVER_IP -t tmux a
    
  • SSH Agent forwarding with tmux

    Most of the time i connect to remote virtial machine to SSH to other servers. This way i will always have a fixed IP, so i can white list my IP in firewall. I use tmux on this server, so even if i get disconnected, my connection to these servers won’t get disconnected. This is useful when you running some commands that take long to finish.

    On this VPS, i don’t keep my SSH keys for security reason, instead i use SSH Agent forwarding with ssh -A option. From my PC, i connect to sshbox with command

    ssh -A root@IP_OF_VM
    

    If i start a new tmux session, i will be able to login to other servers using my SSH key. If i attach to pre extsing tmux session, my SSH key won’t work. This is because SSH Agent use an environment variable SSH_AUTH_SOCK, this point to a sock file. When you get disconnected, it get deleted.

    To fix this problem, edit ~/.tmux.conf file

    vi ~/.tmux.conf
    

    Add

    set-environment -g 'SSH_AUTH_SOCK' ~/.ssh/ssh_auth_sock
    

    Create file

    vi ~/.ssh/rc
    

    with following content

    if [ ! -S ~/.ssh/ssh_auth_sock ] && [ -S "$SSH_AUTH_SOCK" ]; then
        ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
    fi
    

    ~/.ssh/rc file get executed every time a user connect using SSH. It will set symlink to SSH_AUTH_SOCK location if SSH agent forwarding is enabled.

  • Install tmux on CentOS 6

    tmux is a terminal emulator. It work like screen, but have more features.

    CentOS 6 do not come with tmux, you need to enable rpmforge repository.

    http://repoforge.org/use/

    On CentOS 6 64 bit, run

    rpm -ivh http://repository.it4i.cz/mirrors/repoforge/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
    yum install tmux
    
  • tmux

    To attach to a specific session, run

    tmux a -t SESSION_ID
    
    CTRL+B % Split window vertically
    CTRL+B “ Split window horizontally
    CTRL+B c Create new window
    CTRL+B d Detach tmux session
    CTRL+B $ Rename session
    CTRL+B n Next window.
    CTRL+B p Previous window.
    CTRL+B arrow keys switch between panes.

    .tmux.conf

    .tmux.conf file is used to configure tmux

    Here is the .tmux.conf i use

    https://gist.github.com/serverok/f8f729e2c22a5ab1fa6b0d82765bcddc

    If you change this file, you need to restart the terminal or run

    tmux source-file .tmux.conf
    

    Scroll Mode in tmux

    To scroll, you need to enter scroll mode by pressing

    CTRL + B, then press PageUp key.
    CTRL + B, then [
    

    To exit scroll mode, press q