How can I achieve the result of many of

scp ~/here/is/one me@remote:~/where/it/goes
scp ~/yet/another me@remote:~/the/second/place
scp ~/the/number/three/one me@remote:~/foo/bar/qux

with only entering the password for that remote user once?

  • 2
    Please consider setting up SSH keys. Allowing passwords for SSH authentication is bad for security, and attackers are actively probing systems for it.
    – marcelm
    May 4 at 13:58

2 Answers 2


With OpenSSH you can use its "master mode", whereby you first run an ssh which will establish the connection, authenticate, and then place itself in the background while keeping its connection alive waiting for further instructions to perform. Such background connection will be represented by a named UNIX socket which is purposefully created by the self-backgrounded ssh process. You therefore direct your scp commands to use such background connection instead of establishing their own new ones, until you finally close it explicitly.

For instance, in the example below the background connection is used through a UNIX socket on file arbitrarily named bgconn in the current directory:

ssh -fMNS bgconn -o ControlPersist=yes me@remote
scp -o ControlPath=bgconn ~/here/is/one me@remote:~/where/it/goes
scp -o ControlPath=bgconn ~/yet/another me@remote:~/the/second/place
scp -o ControlPath=bgconn ~/the/number/three/one me@remote:~/foo/bar/qux
ssh -S bgconn -O exit -

The first ssh establishes the connection while the last one simply instructs the first one to terminate it and exit, which will also remove the bgconn socket file. The commands in between are directed to use the named UNIX socket, hence the already established connection, to copy their files. Note that you can run as many scp (and ssh and sftp too) commands as you wish, all utilizing the same background connection, provided that you direct them to use the named socket like in the example above.

Options used in the first ssh in particular:

  • -f places ssh to background after authentication succeeds
  • -M enables master mode
  • -N do not request any remote command, or else it would execute a (pointless in this case) remote shell as per default behavior
  • -S path to a name for the UNIX socket that will be created on file-system
  • -o ControlPersist=yes make the backgrounded ssh process persist until explicitly closed, or else it would exit on its own accord at the end of the first operation

How OpenSSH "master mode" works under the hood:

The SSH protocol natively allows several so-called "channels" to share one single SSH connection. Each such channel represents one service among those supported by SSH, e.g. a terminal session, a remote command, a port-forwarding, and so on. This is essentially the basis on top of which it is possible to request for instance a port-forwarding together with the typical terminal session.

The ssh client command by OpenSSH courteously exports this connection-sharing facility to external applications as well, by means of its "master mode" as described above. See an official description here.

  • A few words of technical explanation (or pointers to such) would be nice.
    – U. Windl
    May 4 at 8:22
  • @U.Windl Sure, I can try to, if it's not too deep down the SSH protocol's details. Which aspects would you like insights about?
    – LL3
    May 4 at 18:26
  • Specifically what the first command does in order to be used by the subsequent commands. And how the last command will undo what the first command did...
    – U. Windl
    May 9 at 7:37
  • @U.Windl see edit
    – LL3
    May 9 at 13:46
  • @LL3 Thanks, I edited it, hoping I did not mess it up. Possibly the part at the end could be trimmed a bit now.
    – U. Windl
    May 10 at 10:59

With the SSH suite, if you want to avoid typing password multiple times, you should use ssh keys.

They are generated by ssh-keygen, can be sent to the other host with ssh-copy-id (only the public key is sent).

If the key is not cyphered, no password is asked.

If you don’t trust the private status of your file (attack…), you can have the key cyphered ssh-keygen -p.

With cyphered keys, the password is asked, but you can start eval $(ssh-agent). This agent can store in memory the key when you type ssh-add. Then the password is only asked once by ssh-add and the key is available for ssh/scp.

When you don’t need the agent anymore, ssh-agent -k.

Not the answer you're looking for? Browse other questions tagged .