If i already have a ssh connection established from system-source to system-destination, can i copy a file from system-source to system-destination within that ssh connection or do i need to establish a separate scp connection from system-source to system-destination? Also, since i am connected to system-destination over ssh, is there way for me to list files on system-source?
When i execute scp command such as user@system-destination:/folder/file.txt, the parameters are exposed in clear text. Is there a way to pass folder information, AFTER ssh handshake has been done?
-
1About 2: are you saying the network traffic actually shows the parameters? I never analyzed the traffic, but I always that it indeed was after the SSH handshake.– HalfgaarJul 10, 2013 at 15:00
-
As to 1 see: blogs.perl.org/users/smylers/2011/08/ssh-productivity-tips.html particularly the controlmaster option.– ZoredacheJul 10, 2013 at 15:39
-
@Halfgaar I think his concern is it's exposed on ps output. Every information exchange on the network happens after the handshake.– GnPJul 10, 2013 at 16:22
2 Answers
- If i already have a ssh connection established from system-source to system-destination, can i copy a file from system-source to system-destination within that ssh connection
Yes, man ssh_config
and take a look at ControlMaster
and ControlPath
:
ControlMaster
Enables the sharing of multiple sessions over a single network connection. When set to “yes”, ssh(1) will listen
for connections on a control socket specified using the ControlPath argument. Additional sessions can connect to
this socket using the same ControlPath with ControlMaster set to “no” (the default). These sessions will try to
reuse the master instance's network connection rather than initiating new ones, but will fall back to connecting
normally if the control socket does not exist, or is not listening.
Setting this to “ask” will cause ssh to listen for control connections, but require confirmation using the
SSH_ASKPASS program before they are accepted (see ssh-add(1) for details). If the ControlPath cannot be opened,
ssh will continue without connecting to a master instance.
X11 and ssh-agent(1) forwarding is supported over these multiplexed connections, however the display and agent
forwarded will be the one belonging to the master connection i.e. it is not possible to forward multiple displays
or agents.
Two additional options allow for opportunistic multiplexing: try to use a master connection but fall back to cre‐
ating a new one if one does not already exist. These options are: “auto” and “autoask”. The latter requires
confirmation like the “ask” option.
ControlPath
Specify the path to the control socket used for connection sharing as described in the ControlMaster section
above or the string “none” to disable connection sharing. In the path, ‘%L’ will be substituted by the first
component of the local host name, ‘%l’ will be substituted by the local host name (including any domain name),
‘%h’ will be substituted by the target host name, ‘%n’ will be substituted by the original target host name spec‐
ified on the command line, ‘%p’ the port, ‘%r’ by the remote login username, and ‘%u’ by the username of the user
running ssh(1). It is recommended that any ControlPath used for opportunistic connection sharing include at
least %h, %p, and %r. This ensures that shared connections are uniquely identified.
Add 2 these lines to your ~/.ssh/config
:
ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r
then exit any existing SSH connections, and make a new connection to the server. Now in a second window, the scp
session will be tunneled over the first.
Also, since i am connected to system-destination over ssh, is there way for me to list files on system-source?
Remote port forwarding is what you are looking for.
man ssh
:
-R [bind_address:]port:host:hostport
Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the
local side. This works by allocating a socket to listen to port on the remote side, and whenever a connection is
made to this port, the connection is forwarded over the secure channel, and a connection is made to host port
hostport from the local machine.
Port forwardings can also be specified in the configuration file. Privileged ports can be forwarded only when
logging in as root on the remote machine. IPv6 addresses can be specified by enclosing the address in square
braces.
By default, the listening socket on the server will be bound to the loopback interface only. This may be over‐
ridden by specifying a bind_address. An empty bind_address, or the address ‘*’, indicates that the remote socket
should listen on all interfaces. Specifying a remote bind_address will only succeed if the server's GatewayPorts
option is enabled (see sshd_config(5)).
If the port argument is ‘0’, the listen port will be dynamically allocated on the server and reported to the
client at run time. When used together with -O forward the allocated port will be printed to the standard out‐
put.
On the server, from the ssh command line, create a remote port forwarding by typing:
~C
Enter-R 2302:localhost:22
Enter
you will see something like this:
[user@server ~] $
ssh> -R 2302:localhost:22
Forwarding port.
then you can list the files on the client by running:
ssh localhost -p 2302 "ls"
man ssh_config reveals:
ControlMaster Enables the sharing of multiple sessions over a single network connection. When set to ''yes'', ssh(1) will listen for connections on a control socket specified using the ControlPath argument. Additional sessions can connect to this socket using the same ControlPath with ControlMaster set to ''no'' (the default). These sessions will try to reuse the master instance's network connection rather than initiating new ones, but will fall back to connecting normally if the control socket does not exist, or is not listening.