Here's a situation:

I do all my work on a Mac.

  1. There's a certain linux server 'server01' that provides access to another linux server 'server02' via a pseudo terminal

    So, to ssh into 'server02', I do this from my mac:

    ssh -t server01 'inline server02'
  2. Then it asks me for a password to 'server01'; I enter it and that logs me into 'server02'. Now, I am on the 'server02' terminal.

  3. From 'server02' I can access the mysql database that is sitting on 'server03' using the following mysql command:

    mysql --host=server03 --port=4201 --user=myuser --password=mypass mydb
  4. Neither 'server01' nor 'server02' nor my mac have SSH access to 'server03'

Now, I want to access the mysql on 'server03' from my mac directly through some kind of port forwarding. So, if I type the below command on my mac terminal, it should connect me to the mysql database on 'server03':

mysql --host=localhost --port=4201 --user=myuser --password=mypass mydb

Is there a way to do that? Any help is greatly appreciated.

  • What does your inline command on server01 do to get to server02? – roaima Nov 4 '15 at 13:06

If you can ssh to server02 from the server01 you could try

ssh -t -L server02:4201 server01 'ssh -N -L server03:4201 server02' 

it will chain connection

localhost:4201 -> server02:4201 -> server03:4201

just connects locally to port 4201 on the mac

Better use key authentication and ssh-agent to avoid typing passwords.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.