I created a function in my bashrc file so that when i ssh into something it automatically opens a tmux session.

function ssht () {    
    /usr/bin/ssh $@ -t 'tmux a || tmux || /bin/bash'

This works great, but i don't want to use ssht, i want to just use ssh. When i change the function from ssht to ssh tmux doesn't open but it does successfully ssh into the machine.

I assume it isn't working because when i call ssh it ignores the function in the bashrc file and instead runs ssh.

Is there some way i can just use "ssh" instead of "ssht"

  • 1
    You really shouldn't do this to prevent any unwanted side effects if something else expects ssh to be just ssh. – Sven Jan 5 '15 at 23:41
  • Post output of type ssh when your function name is ssh. – Cyrus Jan 6 '15 at 2:37
  • @Cyrus the output of type ssh is ssh is /usr/bin/ssh – cwiggs Jan 6 '15 at 9:50

SSH Bash Function

Making an ssh function to run something on login like you did, you can do the following:

function ssht () {    
    \ssh $@ -t 'tmux a || tmux || /bin/bash'

alias ssh=ssht
  • Adding a \ in front of the ssh temporarily disables the alias.
  • The ssht function stays the same but we add an alias to ssh that point to it.
  • DOWNSIDE: other things may break, good example is tab-autocompleting hosts out of your ssh config

SSH Command (Alternative)

Alternatively you could use SSH's built-in way of doing this by appending Command="tmux" in front of your public key in the authorized_keys file. It can be used as follows:

command="echo howdy" ssh-rsa AAAAB3...
| improve this answer | |
  • Whilst this may theoretically answer the question, please provide some more context context around that code so others will have some idea what it is and why it makes the difference. – HBruijn Jan 6 '15 at 7:35
  • That was a really clever solution! – Jenny D Jan 6 '15 at 17:57
  • This function worked great until i tried to ssh into an OpenWRT router. The problem is that OpenWRT doesn't have /bin/bash, instead it has /bin/sh. In order to fix the problem you just add || /bin/sh to the end of the function. Therefore it'll look like: function ssht () { \ssh $@ -t 'tmux a || tmux || /bin/bash || /bin/sh' } alias ssh=ssht – cwiggs Feb 24 '15 at 22:10
  • Can you explain why exactly interactive ssh fails when calling it in a bash function, but succeeds when you alias that function? Seems strange. – Luke Davis Feb 28 '18 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.