Linux Administration Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

When you use the -L flag with SSH, you're specifying that any connection attempts made to the local machine, on the first port listed, are to be forwarded to the remote host and port.

Let's break down the command:

[vagrant@centos1 ~]$ ssh -f -L 9999:127.0.0.1:8888 192.168.33.11 sleep 120

First, the -f and sleep 120 at the end of the command are a quick way to create a session and background it while we perform our test:

-f ... sleep 120
In the real world, you're not limited to just one Terminal window, and generally, you'll find yourself opening a session to a remote host in one window while you work in another.

The second part is the interesting bit:

-L 9999:127.0.0.1:8888

Here, we're saying that local port 9999 should have any connection requests forwarded to the remote host on 127.0.0.1:8888.

Because of the way we created our web server, the following is also valid syntax:

-L 9999:192.168.33.11:8888

This is because our remote web server was listening on all addresses, so instead of sending our request to the remote localhost address, we're just using the eth1 address instead.

I've frequently seen setups where less secure programs are run on the localhost address only, meaning that if you want to access the program, you have to SSH to the remote host first.
You're also not limited to cURL and the command line—you could navigate to http://127.0.0.1:9999 in your web browser, and it would still work.