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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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 -
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
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:
sshto background after authentication succeeds
-Menables master mode
-Ndo not request any remote command, or else it would execute a (pointless in this case) remote shell as per default behavior
-Spath to a name for the UNIX socket that will be created on file-system
-o ControlPersist=yesmake the backgrounded
sshprocess 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.
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.
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
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,