This question already has an answer here:

I've looked at a lot of similar questions about this but haven't found anything to solve my issue.

I would like to be able to open an ssh connection to my university's server and keep the connection open in the background. What I'm doing is I ssh into the uni server and run a script, then scp a file from there to my remote, like:

$ssh remote@remote.edu "./bin/script"
remote@remote.edu's password:
$scp remote@remote.edu:~/some_file.txt .
remote@remote.edu's password:

I do this sequence of commands a ton, and eventually my connection gets refused because the university mistakes it for malicious software. So what I'd like to be able to do is open the ssh connection once, then keep it open so I can continuously run the script on the remote server. I don't want to open the ssh connection in another terminal as its very annoying having two terminals open and constantly alt+tab'ing to the wrong one. Surely there must be a way to do this, I would really appreciate some help.

marked as duplicate by Eric Carvalho, muru command-line Mar 18 '16 at 18:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


ssh may reuse existing connections.

Here is a quote of an answer by chepner to this similar question on Stack Overflow:

If you open the first connection with -M:


subsequent connections to $REMOTEHOST will "piggyback" on the connection established by the master ssh. Most noticeably, further authentication is not required. See man ssh_config under "ControlMaster" for more details. Use -S to specify the path to the shared socket; I'm not sure what the default is, because I configure connection sharing using the configuration file instead.

In my .ssh/config file, I have the following lines:

host *
  ControlMaster auto
  ControlPath ~/.ssh/ssh_mux_%h_%p_%r

This way, I don't have to remember to use -M or -S; ssh figures out if a sharable connection already exists for the host/port/username combination and uses that if possible.

  • Link-only answers are discouraged because they become useless if the linked content gets (re)moved. Also it requires the readers to leave the site. Please put a bit more effort into your answers in the future and summarize the content of the linked sites here. You can still add the link as source and for further reading and detail. – Byte Commander Mar 19 '16 at 12:36
  • No problem, here it is.... It is always helpful to post a comment than just downvote – cmks Mar 19 '16 at 12:42
  • 1
    I did not vote on this post at all yet. But I think now I can upvote it. – Byte Commander Mar 19 '16 at 12:53

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