A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://unix.stackexchange.com/questions/25055/ssh-via-multiple-hosts/25056 below:

ssh via multiple hosts - Unix & Linux Stack Exchange

To get to my machine in my office, at the moment I am doing this:

me@home:~$ ssh unix.university.com
me@unix:~$ ssh unix.department.univeristy.com
[email protected]:~$ ssh office-machine.department.university.com
me@office-machine:~$ echo "This is very annoying"

Is there an easy way of automating this process, perhaps a single command that I can use at my end?

amphetamachine

5,61722 gold badges3737 silver badges4343 bronze badges

asked Nov 21, 2011 at 2:46

LucasLucas

1,44722 gold badges1212 silver badges1313 bronze badges

1

Yes, there is a great way to do that using ssh ProxyCommand and netcat

Put something like this in your .ssh/config

Host *.department.university.com
User me
ForwardAgent yes
ProxyCommand ssh unix.university.com nc %h %p

This will log directly into any .department.university.com server using the jump/bastion host unix.university.com. You may also need a stanza for unix.university.com directly.

Here is a link explaining how it works: http://backdrift.org/transparent-proxy-with-ssh

With this technique, you can now just write

ssh unix.department.university.com

and it will all appear direct. Tools like rsync, scp, etc (anything in the ssh stack) will work transparently, as well.

answered Nov 21, 2011 at 13:48

Aaron BrownAaron Brown

1,24588 silver badges77 bronze badges

8

You can use the ssh client to execute ssh on the remote machine upon login.

ssh -t unix.university.com \
    ssh -t unix.department.univeristy.com \
    ssh -t office-machine.department.university.com

(The reason I include -t in the invocations is because ssh was giving me errors re: stdin not being a terminal when I tried it on my own machine; your machine may be different.)

When you exit from the last shell, the process will chain-exit, saving you typing Ctrl-D over and over again.

answered Nov 21, 2011 at 2:50

amphetamachineamphetamachine

5,61722 gold badges3737 silver badges4343 bronze badges

4

In OpenSSH 7.3, ssh added the -J command line flag and the corresponding ProxyJump configuration option to solve exactly this problem.

Give the hosts you wish to ssh through as a comma-separated list to -J. For example:

ssh -J unix.university.com,unix.department.university.com  \
  office-machine.department.university.com

answered Oct 13, 2017 at 20:33

MiikkaMiikka

52544 silver badges1313 bronze badges

To ssh into server B from server A with the same username

Host target
  HostName <serverB_hostname>
  ForwardX11Trusted yes
  LogLevel verbose
  User <username>
  ProxyCommand ssh <username>@<serverA_hostname> -W %h:%p

answered Jun 7, 2023 at 18:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4