3

At work, we run a portal server to access all of our "real" servers. Essentially, anyone who needs to SSH into one of the "real" (web) servers would first SSH into the portal, so it looks like this:

mike@localhost#    ssh mike@portal
#ssh login notice stuff blah blah
mike@portal#    ssh mike@webserver

I'm trying to write a bash alias or function to combine these two steps into one. I'm not the sysadmin by any stretch, so keeping this as simple as possible without installing extra software would be nice.

I've tried the following but it won't go through as I had hoped:

ssh mike@portal; ssh mike@webserver #terminates on terminal for portal
ssh mike@portal `ssh mike@webserver` #hangs
ssh mike@portal ssh mike@webserver #connects, but gives error "pseudo-terminal will not be allocated because stdin is not a terminal"

Any ideas or workarounds for this?

2
  • 2
    If your intermediate host is running a recent version of OpenSSH, or has NC installed, then use the ProxyCommand option to connect you directly to the target host. serverfault.com/a/66332/984 – Zoredache Sep 4 '13 at 1:49
  • is there any way to implement this in a bash alias/script without involving the ssh config file? – mike Sep 4 '13 at 1:55
2

ssh mike@portal exec ssh mike@webserver

Assuming you are connecting with keys, more flags required if using interactive passwords.

2

Put the following into your ~/.ssh/config:

Host webserver
    ProxyCommand ssh -A -e none portal nc %h 22

Then just ssh webserver will work. Requires netcat to be installed on portal.

1

This is a pretty common need.

One way ( http://www.eng.cam.ac.uk/help/jpmg/ssh/two-hop-tunnel.html ) is to use a tunnel:

ssh -f -L 51526:server.example.com:22 -1 gateway.example.com sleep 3600

then

ssh -p 51526 localhost
1

A variation on iii's answer ended up being the solution after some tinkering...

ssh -t -t mike@portal exec "ssh mike@webserver"

and for SFTP:

ssh -t -t mike@portal exec "sftp mike@webserver"
0

You're talking about several webservers, it seems to me. So the best way to do it is probably to put the following in your $HOME/.ssh/config

Host portal
    Hostname portal
    User mike
    StrickHostKeyChecking yes
    LocalForward 1024 webserver1:22
    LocalForward 1025 webserver2:22
    LocalForward 1026 webserver3:22
    ...

Host webserver1
    Hostname localhost
    Port 1024
    User mike

Host webserver2
    Hostname localhost
    Port 1025
    User mike

...

Then, you just have to instantiate your connection with

ssh portal

and then you can connect to any of the webservers using

ssh webserver<n>

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