SSH multihop from one server to another

At my work i login to server via an intermediary server.

So to go to machine i have to first 

Laptop > Inter > WorkMachine

Lets abbrevate the as

L > I > W

So to directly login via ssh from my L to W i setup multi hop ssh

First we need to generate some key on L

Laptop changes

ssh-keygen -t rsa -C "your_email@example.com"

Then add your new key to the ssh-agent:

# start the ssh-agent in the background
eval "$(ssh-agent -s)"
# Agent pid 59566
ssh-add ~/.ssh/id_rsa

Run the following code to copy the key to your clipboard.

pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

Inter changes

Now login to machine Inter (I)

Add the details of newly created ssh key

Run the following code to copy the key to your clipboard.

vi ~/.ssh/authorized_keys
# Paste the contents of clipboard

Save it and exit

Test

Just try to ssh from Laptop to Inter

ssh inter

This should allow you to directly ssh from laptop to inter without password.

Work changes

Now login to WorkMachine and add the key to its authorized keys also following above steps

Laptop changes

In your laptop local ssh config add the following

vi ~/.ssh/config


Paste the content below

 

Host inter

HostName hostname.inter.com

Host workm

Hostname hostname.work.com

ProxyCommand ssh -q inter nc %h 22

 

Final run

To login from laptop directly to WorkMachine

ssh workm

No comments:

Post a Comment

Please share your views and comments below.

Thank You.