[Linux] Multi-hop ssh/scp configuration

Tadashi Shigeoka ·  Mon, December 21, 2015

Running ssh and scp commands through multiple servers was tedious, so I added a configuration to the .ssh/config file to enable direct ssh/scp.

Linux

Multi-hop ssh/scp configuration method

Before the configuration, I had to ssh twice from the local environment: first ssh to server1, then ssh to server2 from server1.

# .ssh/config file
Host server2.example.com
  ProxyCommand ssh server1.example.com nc %h %p
  IdentityFile    ~/.ssh/server1_example_com.pem
  User            ec2-user

After the configuration, you can directly ssh to server2 without going through server1.

File transfers with scp command can also be done directly as shown below, which is super convenient!

scp ~/Downloads/spam.csv server2:

If you have opportunities to ssh or scp through multiple servers, please try this configuration.

Reference Information

That’s all from the Gemba.