Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I regularly ssh into boxes with varying IPs. Something like "ssh -i <key> <user>@<ip>". Every time I want to scp a file, I quit the ssh session, press UP, modify the ssh command into an scp one, execute then restore the ssh session. It's slow and annoying.

Does anyone know of a way to use scp without hassle, once ssh session is established?



You can setup an `~/.ssh/config` file to make it easier:

  Host *
   ControlMaster auto
   ControlPath ~/.ssh/ssh_mux_%h_%p_%r
  Host host1
   HostName 1.2.3.4
   LocalForward 45432 db.internal.net:5432
   User user1
  Host host2
   HostName host2.whatever.com
   LocalForward 46432 db2.internal.net:5432
   User user2
This way you can now scp/ssh to user1@1.2.3.4 with just this:

  scp file host1:
  ssh host1
It also setups up port forwarding. In that case I can connect a postgresql client to localhost 45432 and it'll forward (assuming the ssh host has network access to it) the tcp connection to the host db.internal.net . I do this to use GUI SQL clients to our back-end DB.

The Control options setup ssh multiplexing. If you ssh into a host, subsequent connections piggy-back off of the initial connection. This is useful if you have 2fa and don't want to do 2 factor for every connection.

There's much more you can do: https://linux.die.net/man/5/ssh_config


Use session sharing (ControlMaster setting). Then, don't quit your session - in another terminal on the same computer, run your scp command, and it will use the existing session (and its authentication), without need for -i.

Alternatively, ssh-add is your friend.

(But for security, make sure you do not forward ssh-agent unless you understand the risks)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: